Just few days ago I moved from Arduiono IDE to Atmel Studio 7. I am trying to create my own 1602a LCD screen driver for ATmega 328P MCU. In course of writing an initialization procedure I have ran over a strange behavior of the compiled code that I can't understand nor overcome.
E input of the LCD is connected to the PINC2 of the MCU, and PINC2 configured as output. The following code:
PINC = 0B00000100; _delay_us(1000); PINC = 0B00000000; _delay_us(1000); PINC = 0B00000100; _delay_us(1000); PINC = 0B00000000; _delay_us(1000); PINC = 0B00000100; //<-- missed _delay_us(1000); PINC = 0B00000000; _delay_us(1000); PINC = 0B00000100; //<-- missed
results in signals as shown on the screenshot: two last repetitions of 1 are missed.
In 4-bit mode to send a byte I require to set E high and low twice. If write 1 to PINC2 twice, the second 1 never appears on the MCU output.
I suspect this is somehow connected with the code optimisation that compiler does, but can't find any useful hints on how to overcome it. Please, help.
P.S. This is an Arduino VaNILLIN board. Fuses are configured as follows:
avrdude.exe: safemode: lfuse reads as 0
avrdude.exe: safemode: hfuse reads as 0
avrdude.exe: safemode: efuse reads as 0
avrdude.exe: safemode: Fuses OK (E:00, H:00, L:00)
MCU timed from external 16 MHz oscillator