1. 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.
    Dismiss Notice

Serial Communication of Atmega 162

Discussion in 'AVR' started by Ameya1, Jan 26, 2015.

  1. Ameya1

    Ameya1 New Member

    Joined:
    Jan 26, 2015
    Messages:
    7
    Likes:
    0
    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);
    }
    }
     
  2. Ameya1

    Ameya1 New Member

    Joined:
    Jan 26, 2015
    Messages:
    7
    Likes:
    0
    plzz guys help...
     
  3. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,510
    Likes:
    189
    Location:
    Prague, Czechia (not Chechnya)
    You should be a bit more patient than that.
    IsnĀ“t it required to set the TX pin to output?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ameya1

    Ameya1 New Member

    Joined:
    Jan 26, 2015
    Messages:
    7
    Likes:
    0

    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.
     
  6. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,510
    Likes:
    189
    Location:
    Prague, Czechia (not Chechnya)
    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 (text):

    UBRR0H = 0;
    UBRR0L = 64;
    UCSR0B=0b00001000;
    UCSR0C=0b10000011
     
  7. Ameya1

    Ameya1 New Member

    Joined:
    Jan 26, 2015
    Messages:
    7
    Likes:
    0
    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
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    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
     
  9. Ameya1

    Ameya1 New Member

    Joined:
    Jan 26, 2015
    Messages:
    7
    Likes:
    0
    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.
     
  10. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,510
    Likes:
    189
    Location:
    Prague, Czechia (not Chechnya)
    Simulate it in Atmel studio instead.
     
  11. Ameya1

    Ameya1 New Member

    Joined:
    Jan 26, 2015
    Messages:
    7
    Likes:
    0
    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.
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    I don't use AVR.... So any suggestion I make will be untested....

    If you post the hex file I can simulate it in Oshonsoft!!
     
  13. Ameya1

    Ameya1 New Member

    Joined:
    Jan 26, 2015
    Messages:
    7
    Likes:
    0
    I have attached the hex file of my code. This is the code for baud rate 4800, 8 data bits and 1 stop bit.
     

    Attached Files:

  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    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...
    Code (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);
     
     
  15. Embedded.Ing

    Embedded.Ing New Member

    Joined:
    Feb 28, 2015
    Messages:
    5
    Likes:
    0
    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
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    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.
     
  17. Embedded.Ing

    Embedded.Ing New Member

    Joined:
    Feb 28, 2015
    Messages:
    5
    Likes:
    0
    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>>>
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    When embedded programming... All compilers are the same in this respect... You need to know low level register manipulation with any compiler ( save mikroC )...
     
  19. Embedded.Ing

    Embedded.Ing New Member

    Joined:
    Feb 28, 2015
    Messages:
    5
    Likes:
    0
    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
     
  20. johnsmith123

    johnsmith123 Member

    Joined:
    Sep 24, 2005
    Messages:
    141
    Likes:
    2
    did anyone ask the op if he programmed the CLSEL fuses correctly?

    I'll be that is the problem if the code is not an issue.
     

Share This Page