10
\$\begingroup\$

What is the difference between reading PORTA and PINA? What is the relation between a port and its pins?I'm really confused!!! sample of reading PORT:(reading pin is not useful here)

Assembly f=1MHZ ATmega8 I need to complement PORTB every 1ms.(Dutycycle=50%)

    LDI R16,$FF
    OUT DDRB,R16
    MAIN: LDI R16,00
    DELAY:CPI R16,200
    BREQ LOOP
    INC R16
    RJMP DELAY
    loop:IN R17,PORTB
    COM R17
    OUT PORTB,R17
    OUT R17,PORTB
    RJMP MAIN
\$\endgroup\$
4
  • \$\begingroup\$ Are we supposed to use our crystal balls to find out which microcontroller (and for the code: which programming language) you are using?? \$\endgroup\$ Apr 26, 2013 at 15:44
  • \$\begingroup\$ Sorry it's edited. \$\endgroup\$
    – Hanna
    Apr 26, 2013 at 18:56
  • \$\begingroup\$ Is this your own code or is this from an example? If it is an example, where does it come from? Why do you say "reading pin is not useful here"? \$\endgroup\$ Apr 26, 2013 at 19:45
  • \$\begingroup\$ I've tested it before in AVR studio.Do you think IN R17,PINB is correct? \$\endgroup\$
    – Hanna
    Apr 26, 2013 at 19:49

3 Answers 3

15
\$\begingroup\$

PORT is the OUTPUT buffer, PIN is the INPUT buffer.

When you want to set the pin to a "high" or "low" voltage, write to the PORT register.

When you want to know what voltage is currently presented to a pin, read the PIN register.

The bits of these registers represent the corresponding pins of the general-purpose input/output port.

Here is a simplified schematic of the electronics inside the AVR connected to a single pin (go here for complete datasheets).

This circuit block is repeated for each pin. Eight of these form a port (port A, for example).

enter image description here

Starting at the left-most square (which represents the physical connection to the outside world), you can see three paths:

  1. The upper-most path is the software selectable pull-up resistor
  2. The middle path is used when the pin is configured as an output
  3. The lowest path is used when the pin is configured as an input

It should be noted that some or all of this circuitry can be bypassed when the pin is shared with an internal peripheral. For example, the Analog-to-Digital Converter (ADC).

\$\endgroup\$
7
  • \$\begingroup\$ +1 It's also worth noting that when the ports are configured as inputs, writing 1 to any bit of PORT enables the internal pull up resistor on that pin. \$\endgroup\$
    – Matt Young
    Apr 26, 2013 at 14:32
  • \$\begingroup\$ When should I read PORT? \$\endgroup\$
    – Hanna
    Apr 26, 2013 at 14:34
  • \$\begingroup\$ You read from PIN. Reading from PORT will not give you anything useful. \$\endgroup\$
    – Matt Young
    Apr 26, 2013 at 14:36
  • \$\begingroup\$ @user -- Reading from PORT will give you the state of the pull-up resistors (whether the pull-up is enabled for that pin or not), but as Matt Young mentions, that isn't all that useful. \$\endgroup\$ Apr 26, 2013 at 14:43
  • \$\begingroup\$ I added a sample of reading PORT \$\endgroup\$
    – Hanna
    Apr 26, 2013 at 15:31
4
\$\begingroup\$

I agree with DrFriedParts answer completely, however, I do want to add that, you can indeed read PORT to find out whether the port/pin is configured as OUTPUT high or low (when DDR = 1, port/pin configured as output) or whether the internal pullup resistor is activated (when DDR = 0, port/pin configured as input). In this way a port port can be used (in a rather silly way) as general storage or to pass information about the state of the output/input pullups, as in the following scenario

Perhaps one part of your program writes something to ports or activates/deactivates pullups, and the other part of the program reads PORT to find out how it is configured. Reading PORT immediately after writing PORT does not make sense: having written to PORT, you know what the value of PORT is.

\$\endgroup\$
3
  • \$\begingroup\$ Can you give me a better code?And why IN R17,PINB doesn't answer there? \$\endgroup\$
    – Hanna
    Apr 26, 2013 at 20:05
  • \$\begingroup\$ It isn't clear from your question what are you trying to do... can you clarify that in your question: what is the goal of this code snippet? Are you just tring to toggle a port at a particular frequency and duty cycle? \$\endgroup\$ Apr 26, 2013 at 20:07
  • \$\begingroup\$ yes!!!just this \$\endgroup\$
    – Hanna
    Apr 27, 2013 at 16:35
1
\$\begingroup\$

For your purposes, reading from PORT actually makes sense: The pins in question are output pins, and you simply want to read the last value you wrote, toggle it, and write it back. So you’re using the port register as a byte of externally observable RAM.

On the other hand, for this particular app, you could simply just keep the value in a register and wouldn’t even have to read back the PORT register.

\$\endgroup\$
3
  • \$\begingroup\$ So why reading PIN is incorrect? \$\endgroup\$
    – Hanna
    Apr 27, 2013 at 16:36
  • \$\begingroup\$ The value you get back from PIN depends, in principle, on what is attached to the pin externally, and in your particular application, you’re only interested in the value you previously wrote yourself (This is, of course, rather atypical of I/O pins). In practice, I would, however, expect that the values of PORT and PIN are identical here (did you try it?). \$\endgroup\$ Apr 28, 2013 at 14:23
  • \$\begingroup\$ Yes, I've tested it before in AVR studio.I think what you said is true.We just need to complement port value... \$\endgroup\$
    – Hanna
    Apr 28, 2013 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.