0
votes

I have a ATMega16 and have looped the Rx Tx (just connected the Rx to the Tx), to send and receive one char in a loop. But i only seems to be receiving 0x00 instead of the char i send. I have the CPU configured to 1MHz. But my thought is that since the Rx and Tx are just looped, it shouldn't matter what speed i set, since both are the same?

So basically, I'm trying to get a LED to flash at PORTC when receiving the correct char.

Here is the code:

#ifndef F_CPU
#define F_CPU 10000000
#endif

#define BAUD 9600
#define BAUDRATE ((F_CPU)/(BAUD*16)-1)
#include <avr/io.h>
#include <util/delay.h>



void uart_init(void){
    UBRRH = (BAUDRATE>>8);
    UBRRL = BAUDRATE;
    UCSRB = (1<<TXEN) | (1<<RXEN);
    UCSRC = (1<<URSEL) | (1<<UCSZ0) | (1<<UCSZ1);
}

void uart_transmit (unsigned char data){
    while (!(UCSRA & (1<<UDRE)));
    UDR = data;
}

unsigned char uart_recive(void){
    while(!(UCSRA) & (1<<RXC));
    return UDR;
}

int main(void)
{
    uart_init();
    unsigned char c;
    PORTC = 0xff;
    DDRC = 0xff;
    while(1)
    {
        _delay_ms(200);
        uart_transmit(0x2B);
        c = uart_recive();
        if(c==0x2B){
        PORTC = PORTC ^ 0xff;
        }
    }
}

Any thoughts of what i am doing wrong?

4

4 Answers

0
votes

The code seems right.

Thing you may have to check:

  • if your baudrate is the one you should have
  • if you try to send a char like 'p'; now you are sending a '+'
  • check your port configuration and see if it matches to your configuration

I think the last one is the problem.

You can try this code from ATMega manual:

/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);

After building your program, go to your port configuration and make sure it it set on 8 bits data format and 2 stop bits. Then test it on you microcontroller and see what happens. Please come back with the result.

0
votes

Consider real baudrate accuracy. See e.g. http://www.wormfood.net/avrbaudcalc.php?postbitrate=9600&postclock=1, AVR provides 7.5% error for 9600baud @ 1MHz clock, which is rather high error. Depend what you are sending and receiving. "Normally" you can see a garbage, if you receive permanently 0x00s it looks like another problem.

0
votes

your F_CPU is set to 10MHz. you sad that it is configured to 1Mhz.

Also check your Fuses if you really activated the crystal. If you just use the internal oscillator: it has a relatively large error so that your UART timings may be broken (i never got problems using internal oscillator for debugging).

Another source of error may be your F_CPU definition. Mostly this Preprocessor constant is defined already (propably also wrong) in Makefile (or in IDE project settings) so your #define in Code has not affect since the #ifndef

0
votes

PORTC pins(TDI,TMS,TCK,SDA) always high because these pins for JTAG and JTAG is enable by default. if you want to use PORTC in your application you have to Disable the JTAG by setting fuse bit. for atmega16 JTAGEN is fuse bit set it to 1(means unprogrammed). in case of fuse bit 0(means programmed) and 1(means unprogrammed) one more thing if you use more than 8MHz you have to set fuse bit otherwise your program will give unexpected or wrong result thanks.