Trouble reading input port

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

I'm trying to read an active low sensor on port B, pin 6 of an ATMega2561. Pins 5 and 7 are set to output, and 0 and 1, respectively. So I figured I should just sit in a loop waiting for PINB & 0xC0 to go from 1 to 0. You can see (hopefully) in the attached composited screen grab, however, that, when AVR Studio breaks right after the loop, PINB is still == 0xC0. So, I assume PINB & 0xC0 == 1 and the loop shouldn't have broken. The sensor goes active for about .5 second every 5 seconds, and its signal is very clean, so I don't think this is a noise or glitch issue. Probably a newbie programming one. Any thoughts? Thanks.

Attachment(s): 

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

I suck at 'C' but are you sampling the pin or waiting for an interrupt? I think you are sampling the pin, but I may be wrong. If I am right, then use a pin change interrupt if your selected AVR has that ability. Think yours does, a quick look at the datasheet will let you know yes or no

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
while ((PINB & 0XC0) == 1);

This will never be true. PINB & 0xC0 will only ever be one of 4 values: 0, 64, 128 or 192.

Regards,
Steve A.

The Board helps those that help themselves.

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

PINB & 0xC0 can never equal 1. It can equal 0, 0x40, 0x80, or 0xc0.
Does this help?
Dave Raymond

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

Oh, yeah. I goofed. Thanks.