I have two files
blink1.cpp
typedef unsigned char int8_t;
typedef volatile int8_t * volatile port_type;
port_type portB = (port_type) 0x25;
port_type ddrB = (port_type) 0x24;
void delay_500ms()
{
asm (
// 8000000 cycles
"ldi r19, 150 \n\t"
"ldi r20, 128 \n\t"
"ldi r23, 41 \n\t"
"L1: \n\t"
"dec r20 \n\t"
"brne L1 \n\t"
"dec r19 \n\t"
"brne L1 \n\t"
"dec r23 \n\t"
"brne L1 \n\t"
);
}
int main()
{
ddrB = (port_type) 0x20;
while(true)
{
*portB = (int8_t) 0x20;
delay_500ms();
*portB = (int8_t) 0x00;
delay_500ms();
}
}
blink2.cpp
typedef unsigned char int8_t;
typedef volatile int8_t * volatile port_type;
port_type portB = (port_type) 0x25;
port_type ddrB = (port_type) 0x24;
port_type portC = (port_type) 0x28;
port_type ddrC = (port_type) 0x27;
port_type portD = (port_type) 0x2B;
port_type ddrD = (port_type) 0x2A;
void delay_500ms()
{
asm (
// 8000000 cycles
"ldi r19, 150 \n\t"
"ldi r20, 128 \n\t"
"ldi r23, 41 \n\t"
"L1: \n\t"
"dec r20 \n\t"
"brne L1 \n\t"
"dec r19 \n\t"
"brne L1 \n\t"
"dec r23 \n\t"
"brne L1 \n\t"
);
}
int main()
{
ddrB = (port_type) 0x20;
while(true)
{
*portB = (int8_t) 0x20;
delay_500ms();
*portB = (int8_t) 0x00;
delay_500ms();
}
}
The difference between these two files are the definition of only a few unused ports in blink2.
I compile and upload them to the board by
avr-g++ blink1.cpp -o blink1
avr-objcopy -O ihex -R .eeprom blink1 blink1.hex
avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyUSB0 -b 115200 -U flash:w:blink1.hex
avr-g++ blink2.cpp -o blink2
avr-objcopy -O ihex -R .eeprom blink2 blink2.hex
avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyUSB0 -b 115200 -U flash:w:blink2.hex
The first one does not blink properly but the second one does. What is the problem of the first code?
avr-g++ -Os blink1/2.cpp -o blink1/2
for compilation?-Os
both act as before.ddrB = (port_type) 0x20;
should be*ddrB = (int8_t) 0x20;
no?