NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » OR gate?
March 27, 2011 by SpaceGhost |
I'm need a program snippet that would act as a four-input type of "OR" gate (hope I've described that right). What I need is - Programming that sinks one output (lets say, PB5) IF one of four inputs is activated (grounded). Something like: If input PB1, or PB2, or PB3, or PB4 is low - Activate (sink) PB5. I would really like to learn a proper way to do this. Any suggestions that anyone could offer of course is most appreciated. Dave |
---|---|
March 27, 2011 by esoderberg |
Perhaps:
|
March 27, 2011 by SpaceGhost |
For some reason, I'm not getting this to work. I tried it this way too -
but it didn't work either. Both ways loaded with no errors. |
March 27, 2011 by esoderberg |
SG, I assume you set the pins up as output/input as required? If not read pg. 62 of NK guide. Eric |
March 27, 2011 by SpaceGhost |
I have an LED cathode on PB5 (pin 19), anode to (+) rail, and I have a wire on the (-) power rail that I'm using to connect to the inputs PB1-PB4 (pins 15-18). Pretty simple set up, and here's the simple code -
Trying to get this part working before I incorporate it into something else. |
March 27, 2011 by esoderberg |
Maybe it needs a logical not, vice a bitwise not, in the if statement:
Also above will work like a latch, if you want PB5 to go back high if none of other PB inputs low than:
|
March 27, 2011 by SpaceGhost |
Tried it like this -
still didn't work. I'm wanting the LED to turn on if any of the inputs are closed to the negative supply. I would like for the LED to turn back off if all input switches are open. |
March 27, 2011 by SpaceGhost |
Just tried it with
in the while(1) loop.. No luck either. |
March 27, 2011 by esoderberg |
Crap, my logic was bad. How about:
So if PINB = 11100001 then ~PINB=00011110 and
(~PINB)| 0x1E) = 00011110 | 0x1E = 00011110 | 00011110 = 00011110 which should give logical one. |
March 27, 2011 by SpaceGhost |
Okay, I will try that too... Lot more compact than what I have just came up with. This works -
but I will try your code too, and report back. Thank you very much for your help! Dave |
March 27, 2011 by SpaceGhost |
With
in the while(1) loop, the program would not load. but with
in the while(1) loop, the program loaded but did not work... Dave |
March 27, 2011 by esoderberg |
After running in circles on this one I had to run it myself. This works:
My original input had the ~ on PINB&0x1E vice just on PINB as it needed to be |
March 27, 2011 by SpaceGhost |
Yes sir, this one works! Much simpler and compact code than what I had working - I thank you very much. Would you, um, mind explaining it a little, if you have time? (we were on this a while, lol) Thanks, Dave |
March 27, 2011 by esoderberg |
Dave, It sure took me longer than it should have to figure it out. Here's the logic: Eric |
Please log in to post a reply.
Did you know that Pulse Width Modulation (PWM) can be used to control the speed of a motor digitally? Learn more...
|