Reading individual port pins

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

Hello. I am looking for how I can read individual port pins without the PINA.x syntax. For example, if I want to read PORTA bits 0 and 1, how do I code this? I tried: var1 = PORTA & 0x00 (thinking this would read the logical level of bit 0, and var2 = PORTA & 0x01 (for bit 1). Obviously, this does not work as planned. Can anyone give any advice? I want to monitor 2 pins and see which goes high first. I am writing code for reading a rotary encoder.

Thanks much.

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

If you want to use polling, then use:

var1 = PINA & ((1<<PA0) | (1<<PA1));

Or you may use interrupt. Connect 1 pin to either INT0 or INT1. After entering the corresponding ISR, check the other pin to see whether it is high or low

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

Fitst, it would be VERY helpful if you would say which language and compiler you are using if asking about syntax. There have been many threads here about the PORTx.y syntax. One of the reasons there has been a lot of discussion is that this is not a part of standard C. [OT and the purists would rather write a series of heiroglyphics ericericeric than to use perfectly readable PINA.1].

Next, it would certainly help to know a little more about the application and espcially the encoder speeds needed. If you are indeed a "cnewbie" I wouldn't try to get more than 1kHz or so polling. For faster speeds you would need clean edges and be interrupt driven.

Note the "clean edges" part. You must ensure that your edges are VERY clean, or have some kind of debounce mechanism.

Which brings me to your encoder-decoding. Which signal "goes high first" is only part of the equation. You need to keep track of your current state, note change(s) from that current start, and then process accordingly. I use CdoeVison "bit" variables for this, which are also non-standard.

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

IAR compiler has I/O macros defined in iomacro.h, which allow you to write like this:
PORTA_Bit1 = PINC_Bit5;

Cats never lie. At least, they do this rarely.