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] Atmega64 USART Problem

Status
Not open for further replies.

rongo024

Advanced Member level 4
Full Member level 1
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:
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);
   }
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.
 
Hi,
What are you using in hardware, HyperTerminal? If so, then check your hyperterminal settings, eg baud rate, stop bits, etc.

Hope this helps.
Tahmid.
 

Actually there is a mistake. Atmega64 is shipped with mega103 compatibility mode where you are allowed to use only USART1. USART0 cannot be used. Here I am using USART0. Naturally I am not getting any USART data from USART0. After removing mega103 compatibility it works fine.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top