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.

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

Status
Not open for further replies.

rushi53

Member
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;             [COLOR="Green"]//PORTA is a input port[/COLOR]
DDRB = 0xFF;             [COLOR="Green"]//PORTB is a output port[/COLOR]
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.
 
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.
 
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:
@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;             [COLOR="Green"]//PORTA is a input port[/COLOR]
DDRB = 0xFF;           [COLOR="Green"]  //PORTB is a output port[/COLOR]
PORTA = 0X00;
while(1)
{
      if(PINA==0x05)      [COLOR="Green"]//if input dtmf code is 5[/COLOR]
      {
            PORTB=0X00;   [COLOR="Green"]//then stop all motors[/COLOR]
      }

      if(PINA==0x02)
      {
            PORTB=0X0A;
      }
}

Thank you very much....
:)
 
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top