0
\$\begingroup\$

I have ATMEGA328P-PU micro-controller connected to POLOLU - PMG03A programmer. I'm using the following code to send data:

#define F_CPU     1600000UL
#define BAUD      19200
#define BRC       ((F_CPU/16/BAUD) - 1)

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <util/setbaud.h> 

int main( void ) 
{
    UBRR0H = (BRC >> 8);    
    UBRR0L = BRC;

    UCSR0B = ( 1 << TXEN0 );
    UCSR0C = ( 1 << UCSZ01 ) | ( 1 << UCSZ00 );

    while( 1 )
    {
        UDR0 = 0B11110000;
        _delay_ms(100);
    }


    return 0;
}

but instead of receiving '11110000' in /dev/ttyACM0, I receive '11111000'.

0000037e: 11111000 11111000 11111000 11111000 11111000 11111000  ......
00000384: 11111000 11111000 11111000 11111000 11111000 11111000  ......
0000038a: 11111000 11111000 11111000 11111000 11111000 11111000  ......
00000390: 11111000 11111000 11111000 11111000 11111000 11111000  ......
00000396: 11111000 11111000 11111000 11111000 11111000 11111000  ......
0000039c: 11111000 11111000 11111000 11111000 11111000 11111000  ......

In addition the byte values change sometimes. Why is this happening?

\$\endgroup\$
  • 1
    \$\begingroup\$ What does the waveform look like on the scope? \$\endgroup\$ – Dave Tweed Jul 20 '16 at 22:24
2
\$\begingroup\$

By experimenting I found out that I had to change BAUD rate to 14400 and it worked:

  #define BAUD    14400
\$\endgroup\$
  • 1
    \$\begingroup\$ At 133% the speed of 14400, that explains a consistent single bit difference. Every 5th bit would be wrong. \$\endgroup\$ – Passerby Jul 21 '16 at 1:11
  • 1
    \$\begingroup\$ That said, your microcontoller AND your terminal program must be at the same baud to work (Auto-detect doesn't always work either). So it's likely that hyperterminal/putty/whatever you were using was not set to 19200 baud either. Double check it's current setting. \$\endgroup\$ – Passerby Jul 21 '16 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.