USART Programming in Atmega328

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

/* Program for transmitting a string serially at 9600 baud rate continously, 
using 8 data bit, 1 stop bit , XTAL = 8MHz */

 

#define F_CPU 8000000UL 
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1

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

void USART_Init( unsigned int ubrr);
void USART_Transmit( unsigned char data );
unsigned char USART_receive(void);
void USART_putstring(char* StringPtr);

int main( void )
{
    USART_Init(MYUBRR);

    while(1)
    {
        // Chose an arbitrary character to test.     
        //char String[]="Hello world!!";
        //USART_putstring(String); 
        USART_Transmit('1') ;
        _delay_ms(1000);
    }

}

void USART_Init( unsigned int ubrr)
{
    /*Set baud rate */
    /* UBRR0H contains the 4 most significant bits of the
    baud rate. UBRR0L contains the 8 least significant
    bits.*/  
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)ubrr;
    

    /*Enable transmitter */
    UCSR0B = (1<<RXEN0) | (1<<TXEN0) ;
    
    /* Set frame format: 8data */
    UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
}

void USART_Transmit( unsigned char data )
{
    /* Wait for empty transmit buffer */
    while ( !( UCSR0A & (1<<UDRE0)) );
    
    /* Put data into buffer, sends the data */
    UDR0 = data;
}

unsigned char USART_receive(void){
    
    while(!(UCSR0A & (1<<RXC0)));
    return UDR0;
    
}

void USART_putstring(char* StringPtr){
    
    while(*StringPtr != 0x00){
        USART_Transmit(*StringPtr);
    StringPtr++;}
    
}

 

when I try to run this code & see the received string using Teraterm terminal , the terminal shows different kind of characters ? 

First Fail & then succeed, this is what Engineering teach me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is you CPU really running at 8MHz?

 

How have you proved that?

 

Are you using the internal RC oscillator?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see you include my setbaud.h but then you define MYUBRR yourself?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using calibrated internal RC oscillator i.e 8Mhz .

First Fail & then succeed, this is what Engineering teach me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is no change after removing setbaud.h  .

First Fail & then succeed, this is what Engineering teach me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
I am using calibrated internal RC oscillator i.e 8Mhz .

How is it "calibrated"? I see nothing in the code that represents a calibration process.

 

The internal RC oscillator is really inadequate for timing a UART unless you do a genuine calibration. This is often done with an external 32.768KHz crystal. Even better is an 8MHz crystal.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

I am using calibrated internal RC oscillator i.e 8Mhz .

How is it "calibrated"? I see nothing in the code that represents a calibration process.

 

The internal RC oscillator is really inadequate for timing a UART unless you do a genuine calibration. This is often done with an external 32.768KHz crystal. Even better is an 8MHz crystal.

 

Jim

Thank you for the information.

I made the fuse bits to its default settings of CKSEL3..0 which results in internal RC oscillator @ 8 Mhz .

As a result of which I get the proper result on terminal.

First Fail & then succeed, this is what Engineering teach me.