PORT, PIN, DDR question

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

Good morning,

If set the DDR on a port as o/p and place a value on the pins, can I read back that value from the port? If so, is this good practice? I will test this in the simulator but lately I have had trouble with several peripherals and would like some feedback.

A

AVR Studio 4 Ver. 4.18 684
avr-gcc Ver. 4.3.0
ISIS 7
ELECTRA

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

When a port is configured for output then the PORT register simply latches the last thing you wrote to it so if you read it back you'll get that value - but what's the point? Surely you already know what the value you wrote was?

Cliff

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

The best practice is to write to the PORT and read from the PIN. Reading from the PORT gives you what you last wrote to it. Reading the PIN gives you fresh data.

Special case: set the pin as an input and then write a 1 to the PORT in order to enable the pullup resistor on that pin.

If you think education is expensive, try ignorance.

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

Good morning Cliff,
I need to change several ports 1 bit at a time. Rather than save the initial port value, I thought it made sense to read/modify/write. Any suggestions?

A

AVR Studio 4 Ver. 4.18 684
avr-gcc Ver. 4.3.0
ISIS 7
ELECTRA

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

You have read the 101 thread in Tutorial haven't you? Setting, resetting or toggling bits in PORTs should be as simple as:

PORTx |= (1<<bitpos); //set
PORTx &= ~(1<<bitpos); // clear
PORTx ^= (1<<bitpo); // toggle

though no doubt someone will be along in a second to point out that the modern AVRs allow the toggle thing by actually using:

PINx |= (1<<bitpos);

which could make for a single SBI rather than a r/m/w assuming the PIN register is in the right place.

Cliff

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

emuler,

My op was in error. Thanks for the correction. It sounds like this is a standard way to do things. ie. Read the pins to find a previous value placed on an o/p port.

A

AVR Studio 4 Ver. 4.18 684
avr-gcc Ver. 4.3.0
ISIS 7
ELECTRA

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

Cliff,
I am a little past the tutorial. The app is asm so the C stuff doesn't apply. I was interested in best practice.

Thanks for the help,

A

AVR Studio 4 Ver. 4.18 684
avr-gcc Ver. 4.3.0
ISIS 7
ELECTRA

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

Quote:

Read the pins to find a previous value placed on an o/p port.

Nope. Read the PORT to find a previous value placed on an o/p port. Read the PIN to actually get the true status of the pins (whether they are HIGH or LOW).

In the vast majority of cases, you will read the PIN. Reading the PORT is not recommended.

If you think education is expensive, try ignorance.

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

Well the best practice is to position your IO on pins that are reachable via registers in the 0x00..0x1F IO space and then use the hell out of the SBI and CBI opcodes. Once you pass 0x1F you will be into using IN/(AND/OR)/OUT and beyond 0x3F it'll have to be LDS/(AND/OR)/STS

Beyond 0x1F you have no option but to use full byte AND or OR masks, on the other hand, it can be easier/more efficient to change several bits at a time once you are in IN or LDS land.

Cliff

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

To set or clear individual bits in a port, use the SBI and CBI instructions respectively.

To act on all the pins of that port, use the OUT instruction.

To do a read/modify/write (useful when you want to modify several of the bits without messing with the others), use the IN instruction to read the PIN, modify as desired, then use OUT to write it back to the port.

If you haven't got it already, get the AVR Instruction set document from the Atmel web site. The summary in the data sheet is just that - a summary. Not good for learning purposes.

If you think education is expensive, try ignorance.

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

Thanks,

A

AVR Studio 4 Ver. 4.18 684
avr-gcc Ver. 4.3.0
ISIS 7
ELECTRA