Pins, Ports and Data Direction Registers

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

Hi there, I'm just starting to get into avr's and the usefulness of assembly language, I'm currently simulating in AVR studio using cerebot II boards for a course I'm doing, The question I wanted to ask is if anyone could clarify me the workings of pins, ports and ddr's? I kind of know a little bit but im seriously confused about them. This might seem a trivial question but any answer would be greatly appreciated! :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What part(s) of the datasheet chapter on I/O Ports are confusing to you?

Quote:
13. I/O-Ports
13.1 Overview
...
(or whatever chapter it is in the datasheet for your AVR model.)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
DDR PORT
 1    1      Output driven high
 1    0      Output driven low
 0    1      Input with pull-up (about 50K)
 0    0      floating (tristte/high-Z) input

Read PIN to get input state when DDR is 0. On modern AVRs write to PIN when DDR is 1 to toggle output.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Read PIN to get input state when DDR is 0. On modern AVRs write to PIN when DDR is 1 to toggle output.
More precisely, the PORT value is toggled regardless of the DDR value.
Also modern AVRs differ in how they handle SBI PINx, bit .
An atmega168 will just toggle a bit.
Some others will treat it as a byte-size read-modify-write.
The result is that the specified bit will be toggled and the rest will be cleared.
I think that the atmega128 is that way.

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The result is that the specified bit will be toggled and the rest will be cleared.
SBI will never do that on any AVR. On the older AVRs such as the atmega128, writing to the PINx register will do nothing. And I do not believe that there is any AVR that will have the toggle function on PINx and not have the atomic access version of SBI.

I believe that you are thinking of the SBI behavior with the interrupt flag registers. Those with the rmw version of SBI will clear any interrupt flags that are set in the register.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Also modern AVRs differ in how they handle SBI PINx, bit .
An atmega168 will just toggle a bit.
Some others will treat it as a byte-size read-modify-write.
The result is that the specified bit will be toggled and the rest will be cleared.
I think that the atmega128 is that way.

??? You've way lost me on this one.

"Classic" AVR models, including the Mega128, had no way to simply toggle an I/O pin. Newer models such as a Mega88 do have that feature. IIRC those models without the feature will not have the word "toggle" in the datasheet, and vice versa.

Those models without the toggle feature--PINx is read-only. Nothing will be changed; nothing will be cleared.

Are you thinking of interrupt flags?

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
Quote:
The result is that the specified bit will be toggled and the rest will be cleared.
SBI will never do that on any AVR. On the older AVRs such as the atmega128, writing to the PINx register will do nothing. And I do not believe that there is any AVR that will have the toggle function on PINx and not have the atomic access version of SBI.

I believe that you are thinking of the SBI behavior with the interrupt flag registers. Those with the rmw version of SBI will clear any interrupt flags that are set in the register.

Quite probably.
I seem to have extrapolated into an empty set.

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Maybe a bit "higher level" look is warranted, here....

I/O pins are collected together in groups of 8, each called a "port". This grouping is somewhat arbitrary and not all AVR ports have a full set of 8 pins in every port. In the AVRs, ports are lettered, as in PortA. We often speak of Portn which refers to ANY of the ports in the device. Just substitute the appropriate letter in place of the "n".

Pins in a port are numbered 0-7.

Each port has several controls. One control is the data direction register. It is named DDRn (that is, DDRA, DDRB, etc). Bit 0 of DDRn controls the direction of pin 0 in Portn. If the bit is 1, the pin is set as an output and if it is 0, the pin is set as an input.

Each port has a PORT register. That is, PORTA, PORTB, etc. There is a bit in the PORTn register for every pin. How each bit his register behaves depends on whether the bit is set as an input or output. If the pin is an output, a 1 makes the corresponding pin high (near Vcc) and a 0 makes it low (near ground). If it is an input, a 1 turns on an internal pullup resistor on that pin. But if it is a zero, the internal pullup is turned off and the pin "floats".

Each port also has a PIN register. PINA, PINB, etc. This register is used to read the logic level on the pin. It makes no difference whether the pin is an input or output. Reading a PINn register will show you the actual logic level of the pin.

Some newer AVRs have a "toggle" function if you write to a PINn register.

There are also "hardware over-rides" for many of the I/O pins. For example, if you turn on a UART transmitter, it will take over one specific pin for its use. No matter what the DDR might be set to, or the PORT register might be set to, the UART transmitter output will dominate on that one pin. You need to look at "alternate functions" in the I/O chapter of your spec sheet to see what functions over-ride what pins.

Hope this helps
Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First of all thank you to all of you for trying to help my understanding Ka7ehk I now understand the usefulness of the DDR and pin register I'm just now stuck on the usefulness of the PORTn register, is it a place you write output ?? Only confused with the port register now . Thanks heaps to all of you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the port / pin registers are I think the worst in all the mcu land. in avr, port is for output and pin is for input. and port also does pull-up as well.

on most other mcus, input and output are typically done over the same register (usually port alone). this makes some applications, like led light meter or capacitive sensing a little bit tougher on AVRs than on other mcus.

I would like to know what made atmel think this is a superior approach.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Read PIN to get input state when DDR is 0.
Reading PINx will give you the logic state at the pin irrespective of the state of the corresponding DDR bits. In the absence an overriding external signal (not a good thing, generally) for those bits that are outputs the PINx bit value will match the PORTx bit value.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With DDRx and PORTx there are 4 combinations.
Set to one only DDRx and PORTx to zero acts as open collector.
Set to one only PORTx and DDRx to zero acts as pull up.
Set to one PORTx and DDRx the avr pin outputs 5V/20-40mA.
Set PORTx and DDRx to zero the avr pin is hi tristated-floating.
The PINx register reads the state of avr pins regardless of PORTx DDRx setting,if a pin is pulled up
DDRB=0x00 PORTB=0x01 reading PINB register will have value 1.The same pin if drived low by another circuit connected to this pin reading PINB will read as zero.