+ Reply to Thread
Results 1 to 5 of 5

Thread: For ATmega16, how to copy data of one port to other port. C code required

  1. #1
    rushi53 Newbie
    Join Date
    May 2009
    Location
    Pune, India
    Posts
    78

    Default For ATmega16, how to copy data of one port to other port. C code required

    Hi Electro-Tech users,

    I am using ATmega16 AVR,
    I want to copy data of one port to other port.
    I have written following code in embedded C.
    Code:
    DDRA = 0x00;             //PORTA is a input port
    DDRB = 0xFF;             //PORTB is a output port
    while(1)
    {
         PORTB = PORTA;
    }
    
    but the above code does not copy the data to PORTB

    Please let me know the mistake in the above code.
    Thanks,
    Rushikesh
    www.projectsof8051.com


  2. #2
    Papabravo Excellent Papabravo Excellent Papabravo Excellent Papabravo Excellent Papabravo Excellent Papabravo Excellent Papabravo Excellent
    Join Date
    Mar 2006
    Location
    Michigan, USA (GMT-5)
    Posts
    2,892

    Default

    If the datasheet is read carefully it will be discovered that to define the function of a pin it is necessary to write BOTH the data direction register and the PORT dara register. Then you need to understand the difference between the register PINA and the register PORTA. RTFDS.
    We never have time to do it right; but we always have time to do it over.

  3. #3
    Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent
    Join Date
    Oct 2006
    Location
    Rochester, NY U.S.
    Posts
    9,826
    Blog Entries
    1

    Default

    while(1)
    {
    PORTB = PINA;
    }

    Every IO port has three registers associated with it.

    DDRx == If a bit is set 1 that pin is an OUTPUT, if it is set 0, it is an INPUT.

    PORTx == What the data port is set to; On an OUTPUT this will set it's drive state, on an INPUT it will read/set the internal pull-up enable/disable feature of that I/O line.

    PINx == The actual current logic state of that port as ready directly from the I/O port comparator latch. Please note the PINx and PORTx can be different on an output port. If the PORT register is set to 1 on an output pin, and the corresponding PINx bit is 0 then that means the external circuit on that pin is holding that I/O line LOW. If the PORT register is set to 0 on an output pin and the corresponding PINx bit is 1, then that means the external circuit is forcing that I/O line HIGH. Mind you both of these states usually mean a fault of some kind, but can be very useful if the external circuitry is well defined. It can be used to determine if the I/O line effectively dead shorted, or it it's being driven by a harmfully high external voltage. Switching an output to an input during these two states can save the micro controller in fault conditions from being friend.

    What your code is doing is reading the PORTA register of an input, which will remain whatever it was at power on (or it's default state I'm not sure what it is) unless you change it, it has nothing to do with the what is being applied to that I/O port externally. It's a very common mistake.
    Last edited by Sceadwian; 6th October 2009 at 12:38 AM.
    "Because I be what I be. I would tell you what you want to know if I
    could, mum, but I be a cat, and no cat anywhere ever gave anyone a
    straight answer, har har."

  4. #4
    rushi53 Newbie
    Join Date
    May 2009
    Location
    Pune, India
    Posts
    78

    Default

    @Papabravo
    @Sceadwian

    Thanks to both of you....
    Actually I haven't read the datasheet thoroughly..

    I have used PINA instead of PORTA
    and now program is working... and giving desired output

    actually I am comparing the input dtmf code and then taking decision as which motor to rotate...

    so now my program is something like

    Code:
    DDRA = 0x00;             //PORTA is a input port
    DDRB = 0xFF;             //PORTB is a output port
    PORTA = 0X00;
    while(1)
    {
          if(PINA==0x05)      //if input dtmf code is 5
          {
                PORTB=0X00;   //then stop all motors
          }
    
          if(PINA==0x02)
          {
                PORTB=0X0A;
          }
    }
    
    Thank you very much....
    Thanks,
    Rushikesh
    www.projectsof8051.com

  5. #5
    Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent Sceadwian Excellent
    Join Date
    Oct 2006
    Location
    Rochester, NY U.S.
    Posts
    9,826
    Blog Entries
    1

    Default

    I know it's a long boring read, but take the time to read the datasheet from cover to cover at least once for the main AVR model that you use. You can glance over a lot of the register details, but try to pay attention to the important stuff like that =) The more you know about the chip you're using the easier it will be for you to program it. It may have features you don't even know about that could help you.
    "Because I be what I be. I would tell you what you want to know if I
    could, mum, but I be a cat, and no cat anywhere ever gave anyone a
    straight answer, har har."

+ Reply to Thread

Similar Threads

  1. Many data lines into Com Port
    By dion in forum Electronic Projects Design/Ideas/Reviews
    Replies: 4
    Latest: 25th May 2008, 11:23 AM
  2. Is optical isolation required for parallel port applications ??
    By arunb in forum General Electronics Chat
    Replies: 11
    Latest: 1st June 2007, 02:25 AM
  3. Different Data on Serieal Port.
    By Ayne in forum Micro Controllers
    Replies: 22
    Latest: 24th January 2007, 07:29 AM
  4. [ann] COM Port Toolkit - serial port monitoring on Win32 PCs
    By MGolikov in forum Electronic Projects Design/Ideas/Reviews
    Replies: 0
    Latest: 6th April 2004, 06:17 AM
  5. data to serial port
    By crchisholm in forum Electronic Projects Design/Ideas/Reviews
    Replies: 0
    Latest: 17th August 2003, 04:56 PM

Tags for this Thread