こんなコードを書いていたら動かなかった。
#define F_CPU 1000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { //unsigned char data = 0; char data; //DDRD = 0b11111100; //portD : 0,1pin???? //PORTD = 0b00000000; UBRRH = 0x00; UBRRL = 0x19; //2400 @1MHz UCSRA = 0b00000000; UCSRB = 0b00011000; //Interrupt enable UCSRC = 0b00000110; //8bit //Stop 1bit while(1){ //while( !(UCSRA & 0b10000000) ); //Wait until RXComplete is set. loop_until_bit_is_set(UCSRA, UDRE); UDR = 'b'; data = UDR; while(!(UCSRA & _BV(RXC))) ; //while( !(UCSRA & 0b00100000) ); //Wait until UDREmpty is set. UDR = data; } return 0; }
こう直したら動いた。
#define F_CPU 1000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { //unsigned char data = 0; char data; //DDRD = 0b11111100; //portD : 0,1pin???? //PORTD = 0b00000000; UCSRA = 0b00000000; UCSRB = 0b00011000; //Interrupt enable UCSRC = 0b00000110; //8bit //THIS SHOULD COME HERE. WHY?? UBRRH = 0x00; UBRRL = 0x19; //2400 @1MHz //Stop 1bit while(1){ //while( !(UCSRA & 0b10000000) ); //Wait until RXComplete is set. loop_until_bit_is_set(UCSRA, UDRE); UDR = 'b'; data = UDR; while(!(UCSRA & _BV(RXC))) ; //while( !(UCSRA & 0b00100000) ); //Wait until UDREmpty is set. UDR = data; } return 0; }
原因はUBRRHレジスタとUCSRCが同じメモリを参照していることらしい?よくわからない。
http://winavr.scienceprog.com/avr-gcc-tutorial/programing-avr-usart-module.html
もしかしたらwhileの後のセミコロンのつけ忘れかも。