rongo024
Advanced Member level 4
- Joined
- Dec 29, 2008
- Messages
- 111
- Helped
- 21
- Reputation
- 42
- Reaction score
- 19
- Trophy points
- 1,298
- Location
- Bangladesh
- Activity points
- 1,806
I have written a code for avr atmega64 USART communication but it is not working in hardware. But if I test this code(after making hex) in proteus it works.
my code:
To check whether the RX and TX pin is not damaged I wrote a simple i/o operation code
and check voltage at these pins using voltmeter and this value alternates periodically as the code says.
So, now what is the problem with my code for usart communication? I am using WINAVR2010.
my code:
Code:
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include <string.h>
#define baudrate_1 9600UL
#define baudrate_0 4800UL
#define ubrrval_1 (((F_CPU)/(baudrate_1*16UL))-1)
#define ubrrval_0 (((F_CPU)/(baudrate_0*16UL))-1)
volatile unsigned char m1[]="baud_4800";
void com_init_1(void)//communication initialization
{
UBRR1H=ubrrval_1>>8;
UBRR1L=ubrrval_1;
UCSR1C|=(1<<USBS1)|(1<<UCSZ11)|(1<<UCSZ10);
UCSR1B|=(1<<TXEN1)|(1<<RXEN1);
}
void com_init_0(void)
{
UBRR0H=ubrrval_0>>8;
UBRR0L=ubrrval_0;
UCSR0C|=(1<<USBS0)|(1<<UCSZ01)|(1<<UCSZ00);
UCSR0B|=(1<<TXEN0)|(1<<RXEN0)|(1<<RXCIE0);
}
unsigned char uartgetc0(void)
{
while(!(UCSR0A & (1<<RXC0))) ;
return UDR0;
}
unsigned char uartgetc1(void)
{
while(!(UCSR1A & (1<<RXC1))) ;
return UDR1;
}
void uartgets1(void)
{
unsigned char i=0;
unsigned char c;
for(i=0;;i++)
{
c=uartgetc1();
}
}
void uartgets0(void)
{
unsigned char i=0;
unsigned char ch;
for(i=0;;i++)
{
ch=uartgetc0();
}
}
void uartputc0(unsigned char data)
{
while ( !( UCSR0A & (1<<UDRE0)) ) ;
UDR0 = data;
_delay_ms(100);
}
void uartputc1(unsigned char data)
{
while ( !( UCSR1A & (1<<UDRE1)) ) ;
UDR1 = data;
_delay_ms(100);
}
void uartputs0(unsigned char m[], unsigned char len)
{
unsigned char i=0;
for(i=0; i<len; i++)
{
uartputc0(m[i]);
}
}
void uartputs1(unsigned char m[], unsigned char len)
{
unsigned char i=0;
for(i=0; i<len; i++)
{
uartputc1(m[i]);
}
}
int main(void)
{
DDRE &= ~(1<<PE0);
DDRE |= (1<<PE1);
DDRD &= ~(1<<PD2);
DDRD |= (1<<PD3);
com_init_0();
com_init_1();
sei();
while(1)
{
uartputs0("4800",4);
uartputc0('m');
_delay_ms(1000);
uartputs1("9600",4);
_delay_ms(1000);
uartputc1('m');
_delay_ms(1000);
}
return 0;
}
To check whether the RX and TX pin is not damaged I wrote a simple i/o operation code
Code:
DDRE = 0xFF;
DDRD = 0xFF;
while(1)
{
PORTE = 0x00;
PORTD = 0x00;
_delay_ms(1000);
PORTE = 0xFF;
PORTD = 0xFF;
_delay_ms(1000);
}
So, now what is the problem with my code for usart communication? I am using WINAVR2010.