I expect from the following program serial.c
to light the led on if I send key 1
from terminal, and light the led off when I send key 0
from terminal:
#define F_CPU 16000000UL
#define BAUD 9600
#include <avr/io.h>
char data;
void main(void) {
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
UCSR0B |= (1<<RXEN0);
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
DDRB |= (1<<PB1);
while(1) {
while(!(UCSR0A&(1<<RXC0)));
data = UDR0;
if (data=='0') PORTB &= ~(1<<PB1);
if (data=='1') PORTB |= (1<<PB1);
}
}
I use the following commands to compile and burn all the programs:
avr-gcc -Os -mmcu=atmega328p -c -o serial.o serial.c
avr-ld -o serial.elf serial.o
avr-objcopy -O ihex serial.elf serial.hex
avrdude -c usbasp -p atmega328p -U flash:w:serial.hex
The output of avrdude is the following:
$ avrdude -c usbasp -p atmega328p -U flash:w:serial.hex
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e950f
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "serial.hex"
avrdude: input file serial.hex auto detected as Intel Hex
avrdude: writing flash (12 bytes):
Writing | ################################################## | 100% 0.03s
avrdude: 12 bytes of flash written
avrdude: verifying flash memory against serial.hex:
avrdude: load data flash data from input file serial.hex:
avrdude: input file serial.hex auto detected as Intel Hex
avrdude: input file serial.hex contains 12 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.02s
avrdude: verifying ...
avrdude: 12 bytes of flash verified
avrdude: safemode: Fuses OK (E:07, H:DE, L:FF)
avrdude done. Thank you.
For serial connection I tried PL2302 and FT232RL usb-com adapters - the effect is always the same.
Terminal settings on the computer are correct (9600,8N1).
avr-gcc version 4.8.1 avrdude version 6.1
Why the very first program in this question does not work as needed and what should be changed in it to achieve the desired behavior?