Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Serial Communication of Atmega 162

Status
Not open for further replies.

Ameya1

New Member
I had a doubt regarding serial communication of Atmega 162
Can any one help please??
I have initialized the USART by setting bits in UCSRB and UCSRC and passed a value in UDR register..but not able to see anything on Serial port of my PC
I have written a simple code shown below.
Please help

#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 10000000
#define BAUD 9600
#define UBRR ((F_CPU/16UL/BAUD)-1)

int main(void)
{

UBRR0H = (UBRR >> 8);
UBRR0L = UBRR;
UCSR0B = (0<<UCSZ02) | (1<<TXEN0);
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00) | (1<<URSEL0) | (0<<UMSEL0) | (0<<USBS0);
while(1)
{
UDR0 = 0X55;
_delay_ms(100);
}
}
 
You should be a bit more patient than that.
Isn´t it required to set the TX pin to output?
 
I will surely be more patient.
kubeek, I tried setting the TX as output pin. I had read somewhere, when one enables TXEN in UCSRB it automatically configures TX as output pin.
 
Are you sure the code actually compiles? The example from the datasheet shows TXEN, not TXEN0, etc. What compiler are you using? Are you sure you are probing the correct pin?
You could alternatively try it like this:
Code:
UBRR0H = 0;
UBRR0L = 64;
UCSR0B=0b00001000;
UCSR0C=0b10000011
 
kubeek, I am using AVR Studio 6. The code compiles without any errors. I also checked the datasheet. The examples in it are shown with TXEN, but in actual code one needs to write TXEN0
 
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00) | (1<<URSEL0) | (0<<UMSEL0) | (0<<USBS0);
I have had ( with microchip ) problems with this form of bit manipulation... Just check that the UCSR0C reg is being set as you would expect... Try running your startup in a sim and checking it works
 
Ian Rogers, I tried simulating in proteus. I am not getting anything on virtual terminal. How do I check UCSR0C register? I tried playing with baud rates, I am getting garbage values on Serial terminal.
 
kubeek, not able to select AVR simulator for Atmega 162. I checked the supported devices on Atmel site. Atmega 162 is listed in supported devices.
 
I have attached the hex file of my code. This is the code for baud rate 4800, 8 data bits and 1 stop bit.
 

Attachments

  • usart_prac.hex
    631 bytes · Views: 290
Ok..

The datasheet tells me that the ATMEGA162 has a shared SFR location.... To access the UCSR0C you need to write 0x02 to the UBRRH register first page184 of the datasheet...
C:
/*
Set UBRRH to 2
*/
UBRRH = 0x02;
...
/*
Set the USBS and the UCSZ1 bit to one, and
*/
/*
the remaining bits to zero.
*/
UCSRC = (1<<URSEL)|(1<<USBS)|(1<<UCSZ1);
 
Brother don't waste your time with this compilers
use BascomAVR compiler the best and have all libraries for AVR so you can programe any serial communication easlly , you don't need to adjust every register by it self
 
Brother don't waste your time with this compilers
use BascomAVR compiler the best and have all libraries for AVR so you can programe any serial communication easlly , you don't need to adjust every register by it self
Beg pardon!!!!

"Don't waste time with a C compiler because Bascom basic compiler is better!!! " Yeah if you don't want to learn anything.
 
No Mr.Ian Rogers the idea not like that because using c dose not mean learning .... you should understand the datasheet first for all MCUs ... in order to learn.
but programming with BascomAVR you can earn time this is the idea>>>
 
No Mr.Ian Rogers the idea not like that because using c dose not mean learning .... you should understand the datasheet first for all MCUs ... in order to learn.
but programming with BascomAVR you can earn time this is the idea>>>

When embedded programming... All compilers are the same in this respect... You need to know low level register manipulation with any compiler ( save mikroC )...
 
ya that's true and i worked with mikroC pro for AVR great program and has good support ( i built control system connected to network through ethernet and i could control it through my mobile ) ya I like mikroC
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top