Solved: ATmega 162 - USART using AVRStudio 4

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

Hi all

 

I tried to use USART (0 or 1) in ATmega 162 but I can't do it right sad

 

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

UCSR0A = 0x00;
UCSR0C |= (1<<URSEL0)|(1<<UCSZ00)|(1<<UCSZ01); // 8 N 1
UBRR0L |= (uint8_t)BAUD_PRESCALE;	       // baud rate lo_byte
UBRR0H |= (uint8_t)(BAUD_PRESCALE>>8);         // baud rate hi_byte
UCSR0B |= (1<<TXEN0);       	               // Enable usart TX

int main(void)
{
 unsigned char data=0;

 while(1)
 {
  while(!(UCSR0A & (1 << UDRE0)));

  UDR0 = data++;
 }
}

With the code above I can get correct value only if I set the Rx device / Terminal Baudrate to 1200 not 9600 as expected.

 

I changed the MCU with ATmega16 or ATmega32 with above setting and I had no problem.

 

Can someone help me ?

What did I miss ?

 

Thank you

I don't know why I'm still doing this hobby

Last Edited: Tue. Apr 4, 2017 - 11:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MicroGyro wrote:
only if I set the Rx device / Terminal Baudrate to 1200 not 9600
Which tells you that the AVR is running 8 times slower than you think.

 

Does the 162 have a "CKDIV8" fuse by any chance?

 

EDIT: well yes it does....

 

That would be the first culprit I would be looking at!

 

AVRs that have CKDIV8 fuses have them activated by default so whatever clock you select it will run at 1/8th the speed.

 

You can either change the fuse or do the double write to CLKPR in your code to change the divisor back to divide by 1.

Last Edited: Tue. Apr 4, 2017 - 09:31 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Clawson

Thanks a lot for your quick answer yes

 

That's my stupid mistake ! sadno

 

CKDIV8 and CLKPR solved the problem! yes

 

Thank you very much sir smiley

 

 

I don't know why I'm still doing this hobby