-1

I developed this program which is supposed to display all odd numbers between 1-99 on the top line of the LCD with a 0.5 second delay between each iteration.

However, when I run the below code, my output is only '13' then '133' and I am so confused as to why this is.

My code:

#define _XTAL_FREQ 3276800
#include <xc.h>
#include "LCDdrive.h"

void main (void) {
    unsigned int oddNumber = 1;
    unsigned int nextNumber = 0;
    unsigned short i;

    LCD_initialise();
    LCD_cursor(0,0);
    LCD_display_value(oddNumber);

    while (1) {
        for (i = 0; i < 100; i++) {
            nextNumber = oddNumber + 2;

            LCD_cursor(1,0);
            LCD_display_value(nextNumber);
            __delay_ms(500);
            LCD_display_value(nextNumber);
            __delay_ms(500);
        }
    }
}
Mike
  • 4,041
  • 6
  • 20
  • 37
T07
  • 43
  • 7

1 Answers1

0

The program did exactly what it should:

Try something like this:

#define _XTAL_FREQ 3276800
#include <xc.h>
#include "LCDdrive.h"

void main (void) {
    unsigned int nextNumber = 0;
    unsigned short i;

    LCD_initialise();
    LCD_cursor(0,0);
    LCD_display_value(oddNumber);

    while (1) {

        for (i = 0; i < 50; i++) {
            nextNumber = nextNumber + 2;
            LCD_cursor(1,0);
            LCD_display_value(nextNumber);
            __delay_ms(500);
        }
    }
}
Mike
  • 4,041
  • 6
  • 20
  • 37
  • Thank you for your answer but I tried using your code and the output remains the same - jumping from '13' to '133'. Any idea as to why this is? – T07 Nov 15 '18 at 12:10
  • Try to debug with printf(). netxtNumber could only count 2, 4, 6, ...and so on – Mike Nov 15 '18 at 13:42