I'm using ATMEGA328P-PU and Pololu PGM03A (USB hardware programmer). I had a very simple code that was sending an 8-bit value back to my computer:
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 8000000UL
#define BAUD 9600
int main( void )
{
// BAUD RATE (bits per second)
UBRR0L = ( ( F_CPU / 8 / BAUD ) - 1 ); // 01100111
// CONFIGURATION REGISTERS
UCSR0B = 0b00001000; // enable transmitter (TXEN0)
UCSR0C = 0b00000110; // set data frame size (8-bit)
while( 1 )
{
UDR0 = 0b11110000;
_delay_ms(20);
}
return 0;
}
I can read the data using the following command:
xxd -c 2 -b /dev/ttyACM2
Every now and then a very strange thing happens - without any changes in the code, suddenly I'm getting zeros:
00019d40: 00000000 00000000 ..
00019d42: 00000000 00000000 ..
00019d44: 00000000 00000000 ..
00019d46: 00000000 00000000 ..
00019d48: 00000000 00000000 ..
00019d4a: 00000000 00000000 ..
00019d4c: 00000000 00000000 ..
00019d4e: 00000000 00000000 ..
00019d50: 00000000 00000000 ..
00019d52: 00000000 00000000 ..
00019d54: 00000000 00000000 ..
00019d56: 00000000 00000000 ..
00019d58: 00000000 00000000 ..
00019d5a: 00000000 00000000 ..
00019d5c: 00000000 00000000 ..
00019d5e: 00000000 00000000 ..
00019d60: 00000000 00000000 ..
00019d62: 00000000 00000000 ..
00019d64: 00000000 00000000 ..
00019d66: 00000000 00000000 ..
00019d68: 00000000 00000000 ..
00019d6a: 00000000 00000000 ..
00019d6c: 00000000 00000000 ..
00019d6e: 00000000 00000000 ..
00019d70: 00000000 00000000 ..
00019d72: 00000000 00000000 ..
00019d74: 00000000 00000000 ..
But there should be 1111000. I noticed that:
- This happens after reboot
- Switching USB ports take no effect
- Switching micro-controller produces the same problem. Even when I tried to write a simple code that simply blinks the light to a blank micro-controller, I can see the LED blink, but I still get 00000000's on serial port, but there should be no communication what so ever.
- Normally the data is sent at fairly slow rate (when it's sending the proper data), but now the output is very fast (when it sends the 00000000's )
Any tips on what's going on? Thanks!
UBRR0L = ...
doesn't look good; should beUBRR0 = ...
. \$\endgroup\$UCSR0A
, specificallyU2X0
, so you're not using double-speed mode; hence, you need to divide UBRR by 16 instead of 8:UBRR0 = ( ( F_CPU / (16 * BAUD )) - 1 );
As it is, you would be outputting at 19200bps which may confuse the receiver (framing errors...). \$\endgroup\$UCSR0C = 0b00000110
with more meaningful (and less error-prone) statements, likeUCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
. \$\endgroup\$UCSZ01
looks "meaningful", but at least one can refer to the datasheet to see what the intention is :)) \$\endgroup\$