NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Compiler issue?

May 11, 2012
by RogerFL
RogerFL's Avatar

Mysteriously my LCD quit working - just writes to top row instead of using both lines of my two line display. After many hours of debugging, this "fix" makes me think this is a compiler bug or I'm not using the right compiler options. Here's the code (from the NerdKits LCD driver):

void lcd_goto_position(uint8_t row, uint8_t col) {
// 20x4 LCD: offsets 0, 0x40, 20, 0x40+20
uint8_t row_offset; // works here, do not initialize to zero!
lcd_set_type_command();
//uint8_t row_offset = 0; // doesn't work here
switch(row) {
    case 0: row_offset = 0; break;
    case 1: row_offset = 0x40; break;
    case 2: row_offset = 20; break;
    case 3: row_offset = 0x40+20; break;
}

printf_P(PSTR("[POS]%x\r\n"), row_offset);
lcd_write_byte(0x80 | (row_offset + col));
}

It only works if row_offset is defined at the top of the function and it is not initialized to zero.

Causes a warning, but it works. Leaving it where it was (uncomment two lines down), causes row_offset to always be 0xff in that printf_P statement, when for second line it should be 0x40.

I'm using the latest CrossPack. Here's the compile command and output:

avr-gcc -g -Os -Wall -mmcu=atmega328p -Wa,-adhlns=lcd.lst -c -o lcd.o lcd.c
lcd.c: In function 'lcd_goto_position':
lcd.c:139:10: warning: 'row_offset' may be used uninitialized in this function

Anyone else seen something like this?

May 11, 2012
by pcbolt
pcbolt's Avatar

Roger -

I knew that problem rang a bell somewhere, so I tracked down This Thread. Humberto suggested changing the compiler optimizations. I know there was even another thread about it but I couldn't find it. I think Rick S. had a fix that involved just downloading and re-installing the original lcd.c and lcd.h files without the lcd.o file, then re-compiling. You mentioned using a 2-line display...was that from an older Nerdkit, before they shipped with the 4-line model? I know the 4-line model can be thought of as a 2-line model just displayed on 4 lines (if that makes any sense).

May 17, 2012
by RogerFL
RogerFL's Avatar

pcbolt, thanks for the info!

No, I got this 2-line LCD from sparkfun. But I did connect the 4-line display from NK and it had the same exact problem.

Some optimizer bug related to switch statements seems to be the root cause.

Thanks again.

Post a Reply

Please log in to post a reply.

Did you know that a motor's no-load current at a given voltage is much less than it's resistance would suggest? Learn more...