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.

need help to correct USART coding using ATMEGA32...transmit data

Status
Not open for further replies.

hanukaran

Junior Member level 2
Joined
Oct 15, 2010
Messages
24
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,283
Activity points
1,542
can anyone help me to correct this coding so that i can send the data to PC through rs232...pls..Im doing project traffic light.


#include <mega32.h>
#include <delay.h>
#include <stdlib.h>


#define red_1 PORTA.0
#define yellow_1 PORTA.1
#define green_1 PORTA.2

#define USBS 3
#define RXEN 4
#define TXEN 3
#define UCSZ1 2
#define UDRE 5
#define UCSZ0 1
#define RXC 7


void USART_Init(unsigned short int F_CPU_MHz, unsigned short int Baud_Rate)
{
F_CPU_MHz= 8 ; ---> Remove this line
Baud_Rate= 9600 ;--> Remove this line
UCSRB = 0x00; // Disable while setting baud rate
UCSRA = 0x00; // Disable USART flags
UCSRC = (1<<USBS)|(1<<UCSZ1)|(1<<UCSZ0); // 8-Bit data, 1-Bit Stop
UBRRL = ((F_CPU_MHz*1000000)/Baud_Rate/16)-1; // Set Baud Rate Lo - Asynch Normal Mode
UBRRH = 0x00; // Set Baud Rate Hi
UCSRB = (1<<RXEN)|(1<<TXEN); // Enable USART TX and RX
}


void Send_Byte(char data)
{

while ((UCSRA & (1<<UDRE)) == 0);
UDR = data;
}


char Receive_Byte(void)
{

while((UCSRA&(1<<RXC)) == 0);
return UDR;
}








void main(void){
USART_Init(8,9600);
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0x03;
PORTC=0x00;
DDRC=0xFF;
PORTD=0x00;
DDRD=0xFF;

while (1)
{
red_1=0;
green_1=1;
delay_ms(1500);
green_1=0;
yellow_1=1;
delay_ms(1500);
yellow_1=0;
red_1=1;
delay_ms(1500);
};

while(1)
{
if (red_1==0)
{
green_1=1;
Send_Byte('G');
}
if(green_1==0)
{
yellow_1=1;
Send_Byte('Y');
}
if (yellow_1==0)
{
red_1=1;
Send_Byte('R');
}}
}
 

The flow of program would never reach your second while(1) loop as it will be locked in the first one.

Move the contents of second while(1) loop to first one
 
thanks for your relpy sir..i really waiting for this answer.... is this will be like this?


#include <mega32.h>
#include <delay.h>
#include <stdlib.h>
#include <stdio.h>


#define red_1 PORTA.0
#define yellow_1 PORTA.1
#define green_1 PORTA.2

#define USBS 3
#define RXEN 4
#define TXEN 3
#define UCSZ1 2
#define UDRE 5
#define UCSZ0 1
#define RXC 7


void USART_Init(unsigned short int F_CPU_MHz, unsigned short int Baud_Rate)
{

UCSRB = 0x00; // Disable while setting baud rate
UCSRA = 0x00; // Disable USART flags
UCSRC = (1<<USBS)|(1<<UCSZ1)|(1<<UCSZ0); // 8-Bit data, 1-Bit Stop
UBRRL = ((F_CPU_MHz*1000000)/Baud_Rate/16)-1; // Set Baud Rate Lo - Asynch Normal Mode
UBRRH = 0x00; // Set Baud Rate Hi
UCSRB = (1<<RXEN)|(1<<TXEN); // Enable USART TX and RX
}


void Send_Byte(char data)
{

while ((UCSRA & (1<<UDRE)) == 0);
UDR = data;
}


char Receive_Byte(void)
{

while((UCSRA&(1<<RXC)) == 0);
return UDR;
}








void main(void){
USART_Init(8,9600);
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0x03;
PORTC=0x00;
DDRC=0xFF;
PORTD=0x00;
DDRD=0xFF;

while (1)
{
red_1=0;
green_1=1;
{
if (red_1==0)
{
green_1=1;
Send_Byte('G');
}
delay_ms(1500);
green_1=0;
yellow_1=1;
if(green_1==0)
{
yellow_1=1;
Send_Byte('Y');
}
delay_ms(1500);
yellow_1=0;
red_1=1;
if(green_1==0)
{
yellow_1=1;
Send_Byte('Y');
}
delay_ms(1500);
}}
}
 

Please check the value of the UBRRL are correct from the table of the datasheet of atmega32.
Because i think you must put also a value at the UBRRH.
if you using emulator stop and see the value..
 
Could you explain me more about it..pls
 

What is the problem? where did you stuck?

Could you explain me more about it..pls

Look at the datasheet of atmega32 in usart section
 
What is the problem? where did you stuck?



Look at the datasheet of atmega32 in usart section[/QUOTE

The problem is...there is alot value in this datasheet ..i dnt have idea to choose which is proper value......




why cant get the character i send to virtual terminal
 
the program is not working in ISIS or in real board?
Beacause sometimes ISIS has bugs!!!!

What is your frequancy and set the baud register from the table.
 
this is my problem nw....i'm not sure what freq and baud register to set..can you help with some more explanation
 
what software are you using? By seeing your code i guess codevision..

If so Codevision will do this job for you.

You can simply select the baud rate and it will show the error %. It should be below 0.2 to get proper results.
 
BUt why I still cant get the out put in simulation

#include <mega32.h>
#include <delay.h>
#include <stdlib.h>
#include <stdio.h>


#define red_1 PORTA.0
#define yellow_1 PORTA.1
#define green_1 PORTA.2

#define USBS 3
#define RXEN 4
#define TXEN 3
#define UCSZ1 2
#define UDRE 5
#define UCSZ0 1
#define RXC 7


void USART_Init(unsigned short int F_CPU_MHz, unsigned short int Baud_Rate)
{

UCSRB = 0x00; // Disable while setting baud rate
UCSRA = 0x00; // Disable USART flags
UCSRC = (1<<USBS)|(1<<UCSZ1)|(1<<UCSZ0); // 8-Bit data, 1-Bit Stop
UBRRL = ((F_CPU_MHz*1000000)/Baud_Rate/16)-1; // Set Baud Rate Lo - Asynch Normal Mode
UBRRH = 0x00; // Set Baud Rate Hi
UCSRB = (1<<RXEN)|(1<<TXEN); // Enable USART TX and RX
}


void Send_Byte(char data)
{

while ((UCSRA & (1<<UDRE)) == 0);
UDR = data;
}


char Receive_Byte(void)
{

while((UCSRA&(1<<RXC)) == 0);
return UDR;
}








void main(void){
USART_Init(8,9600);
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0x03;
PORTC=0x00;
DDRC=0xFF;
PORTD=0x00;
DDRD=0xFF;

while (1)
{
red_1=0;
green_1=1;
{
if (red_1==0)
{
green_1=1;
Send_Byte('G');
}
delay_ms(1500);
green_1=0;
yellow_1=1;
if(green_1==0)
{
yellow_1=1;
Send_Byte('Y');
}
delay_ms(1500);
yellow_1=0;
red_1=1;
if(green_1==0)
{
yellow_1=1;
Send_Byte('Y');
}
delay_ms(1500);
}}
}
 
Try using 7.3728 MHz Crystal.

ya tanks..but i'm telling the truth...i dnt know wer to put this...i'm really dnt know..i try to learn using the datasheet and other sources but still blur..if you dnt mind can you help me....
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top