Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Problem in setting the Baud Rate in AVR...

Status
Not open for further replies.

ismbn

Full Member level 3
Full Member level 3
Joined
Feb 11, 2012
Messages
160
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,308
Location
Mumbai. india
Activity points
2,444
Hello...

I am using the AT MEGA 32 and AVR studio 4.
In that i have given the F_CPU = 16000000 i.e 16MHz.
The Problem is I giving the UBRR = 51 (or any other value given in the data sheet) as given in the data sheet to set the Baud rate 19200.
But in the Hardware as well as in Simulation it is not working on 19200. but it is working on Baud Rate 2400(and if i am giving 103 for 9600 it is working on 1200). What can be the problem please guide me.
I am using Crystal of 16MHz, Is using the crystal of 16MHz is right or i have to use low value of crystal ?

here is the code:

Code:
#include <avr/io.h> 
#include <avr/interrupt.h> 

int i=0,j=0;
int main (void) 
{
int a; 
DDRA=0xFF;
DDRB=0xFF;
    
    a=51;           // to set the boud rate 19200

    UBRRL = a;
    a=a>>8;
    UBRRH = a;

    UCSRB |= (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitry 
    UCSRC |= (1<<U2X)| (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes 

//   UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 
//   BAUD_PRESCALE=(BAUD_PRESCALE>>8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 
//   UBRRH = (BAUD_PRESCALE>>8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 



   UCSRB |= (1 << RXCIE);// Enable the USART Recieve Complete interrupt (USART_RXC) 
   sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed 

   for (;;) // Loop forever 
   { 
    PORTA=j;     // Do nothing - echoing is handled by the ISR instead of in the main loop 
    _delay_ms(10);
    j++;
   }    
} 

ISR(USART_RXC_vect) 
{ 
   char ReceivedByte;    
   ReceivedByte = UDR; // Fetch the received byte value into the variable "ByteReceived" 
   UDR= ReceivedByte ; // Echo back the received byte back to the computer 
   PORTB=i++;
}

Ismail
 

I'm not sure what you have done wrong but here is a code that works fine in mega8 or mega32

Code:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#define F_CPU 16000000
#define USART_BAUDRATE 19200
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

 char ReceivedByte;
ISR(USART_RXC_vect)
{
  
   ReceivedByte = UDR; // Fetch the received byte value into the variable "ByteReceived"
   UDR = ReceivedByte; // Echo back the received byte back to the computer
}

int main(void){
   UBRRL = BAUD_PRESCALE;
   UBRRH = (BAUD_PRESCALE >> 8);
   UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
   UCSRB |= (1 << RXEN) | (1 << TXEN);
   UCSRB |= (1 << RXCIE);
   sei();
    while(1)
    {

    }
}
 

Have you changed your uC from ATMEGA 2560 to ATMEGA32? Where is the new proteus schematic?
 

@alexan_e

Sir I checked the code you have give.. on hardware its working, but on Baud rate 1200, not on 19200

Ismail
 

How can it work with 1200 when the settings in my code set uart to 19200?
 

that is the funnest this happening whit me right now....

is there any role of prescaler in this??

Ismail
 

Have you used a 16MHz crystal?
Have you set in AVR studio project properties the frequency to 16000000?
How did you figure that the output is 1200baud and not 19200?
 

i am simulating it on Protues as well as on hardware...
i have a kit of at mega 32 which is made for student. i have burned the coding in it.
1st i have checked it on 19200 by hyper terminal... it was not communicating at all.
Then i start decreasing the Baud at 1200 i got the output correctly...

Ismail

- - - Updated - - -

@madhusuthanan
done the setting of properties by 16MHz...
at starting only...

- - - Updated - - -
" return 0"
Negative (No change)
 

hi

UBRRL & UBRRH set as 0x33 in the program. do these changes check it out.
 

i am simulating it on Protues as well as on hardware...
i have a kit of at mega 32 which is made for student. i have burned the coding in it.
1st i have checked it on 19200 by hyper terminal... it was not communicating at all.
Then i start decreasing the Baud at 1200 i got the output correctly...

Ismail

Then you are doing something wrong.
Did you set the AVR clock to 16MHz?
Did you change the virtual terminal baud rate to 19200?

Upload your schematic
 

i have up loaded it check it Sir
Ismail
 

Attachments

  • as.rar
    16.6 KB · Views: 137

here is the hex file also
 

Attachments

  • test7.rar
    296 bytes · Views: 145

Your current setting is using internal RC 1MHZ

Snap1.gif

Change it to crystal so that the 16MHz apply and it will work fine

Snap2.gif

Alex
 
  • Like
Reactions: ismbn

    ismbn

    Points: 2
    Helpful Answer Positive Rating
@ Alex
yippeeeeeeeeee its working nice in simulation..........

But............. what to do with hardware Sir...........
Ismail
 

The code is correct in proteus or in real hardware, if you have problem then it is a wrong setting.

You are using a 16MHz crystal, have you changes the AVR fuses from the default 1MHz internal RC to External crystal, if not the crystal is ignored
 

it is a wrong setting.
You are using a 16MHz crystal, have you changes the AVR fuses from the default 1MHz internal RC to External crystal, if not the crystal is ignored

how to do this Sir..
i mean setting of fuse from 1MHz to 16MHz
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top