1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

USART Issues

Discussion in 'AVR' started by Billt321, Jul 19, 2015.

  1. Billt321

    Billt321 New Member

    Joined:
    Jul 19, 2015
    Messages:
    1
    Likes:
    0
    Hi,

    Recently i have been trying to get interrupt driven serial working for a project of mine, however i have been struggling to get it to work. I am using an Atmega328p (16Mhz external xtal), and an FTDI231x to communicate to a PC . I believe all the hardware works fine, as using Arduino IDE i can get the serial to work, but i need to use it in Atmel Studio 6. The transmit sort of works, i can see the Tx LED flashing, however i only see zeros or symbols with Realterm. The data sent by the AVR looks to be wrong on a scope aswell. Also, when sending data to the AVR, the Rx interrupt doesn't fire either. I have also tried a lot of example code of the web, checking to ensure that the Fcpu values and such were correct, and none have worked. I have also disabled ckdiv8 in the fuses. Any suggestions?

    Thanks in advance, Bill.

    Code (text):
    #include <avr/io.h>
    #include <util/delay.h>

    #define F_CPU 16000000UL
    #define FOSC 16000000
    #define BAUD 9600
    #define MYUBRR (FOSC/16/BAUD)-1

    volatile char usartReceive;
    volatile int rxF;
    unsigned char buffer;

    void USART_init();

    sei();

    int main(){  
        //Setup
        USART_init();      
       
        while ( !( UCSR0A & (1<<UDRE0)) ) // Wait until buffer is empty
            buffer = "Hello";
            UDR0 = buffer;      
       
        //Main Loop
        while(1){
            while ( !( UCSR0A & (1<<UDRE0)) )
                UDR0 = 0b01010101
                _delay_ms(1000);
            if(rxF == 1){
                while ( !( UCSR0A & (1<<UDRE0)) )
                UDR0 = usartReceive;
                rxF = 0;
            }
        }
    }

    void USART_init(){

        UBRR0H = (unsigned long)(MYUBRR>>8);
        UBRR0L = (unsigned long)(MYUBRR);
        UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
        UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);    // 8 Data bits, one stop bit

    }
    ISR (USART_RX_vect){
       
        usartReceive = UDR0;
        rxF = 1;
       
    }
     

Share This Page