• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Reading logic level of an output pin

I've checked out a ton of resources but I can't seem to get a simple answer on this. Maybe this could go in HT but its pretty basic so I was hoping someone familar with PICs could tell me.

I need to flash some LEDs hooked to a PICs pins. I set the desired pins to output with set_tris_c(..).

Of course to flash them I want something like:

if ( logic(pin) )
{
// LED is ON, turn it OFF
....
}
else
{
// LED is OFF, turn it ON ...
}

Of course logic isn't a standard function but there's probably something like it.

From what I've read it seems to say I need to switch the pins to input to read the logic level. But I just want to "peek" at what the current logic level is. I was reading a bunch of stuff about what you might read differs from the actual logic level.

Right now I have a byte variable that keeps track of whether the pins in question are high or low. But it'd sure simplify things if I could just peek at the logic level then act accordingly.
 
I don't know for sure about the PIC microcontrollers, but with the other ones I've used, (Freescale and Atmel), you should just be able to read the port register that the LED's are on and mask off whatever bits you dont care about.

So if you want to know if the LED on PORTA Bit 3 (assuming it goes 7:0),

#define LED_Mask 0x08 (put in the header)

if(PORTA & LED_Mask){
// LED is on
} else {
// LED is off
}
 
Originally posted by: jmcoreymv
I don't know for sure about the PIC microcontrollers, but with the other ones I've used, (Freescale and Atmel), you should just be able to read the port register that the LED's are on and mask off whatever bits you dont care about.

So if you want to know if the LED on PORTA Bit 3 (assuming it goes 7:0),

#define LED_Mask 0x08 (put in the header)

if(PORTA & LED_Mask){
// LED is on
} else {
// LED is off
}
:thumbsup:

Sounds right to me, but I only played with a PIC once like 7 months ago. But the concept is right: reference the port the LED is on and only look at the bits of that port which refer to the LED pins, hence the mask. Bingo bango
 
So since I'd need to check 3 LEDs, I'd do something like this?


#define GREEN_MASK 0x01
#define YELLOW_MASK 0x02
#define RED_MASK 0x04
SwitchLED(GREEN_MASK);
SwitchLED(YELLOW_MASK);
SwitchLED(RED_MASK);

void SwitchLED(U8 led_mask);
{
if (PORTC & led_mask)
{
// turn desired LED off
}
else
{
// turn desired LED on
}
}



THe green is on PORTC bit 0, yellow is PORTC bit 1, and red is PORTC bit 2. So I figured I'd need to send a defined mask as an argument to the function with each call. Then I'd have to figure out how to use the mask number to activate the desired LED. I was hoping I'd not need to check each one individually. Like PORTC pin 0 is 56, pin 1 is 57, pin 2 is 58. So if wanted to turn one off, I'd use output_low(55+led_mask) but the compiler doesn't like that sinec it needs to evaluate to a constant at compile time. I'm not too familar with C... I only know Java which I think that'd be okay in. Plus since the RED_MASK requires 0x04 (bit 2), it would equal 59.

So I'm kinda back to where I started. Just now I'm directly comparing a mask with the port register, when before I was passing those 3 masks and comparing it with my own byte variable that essentially had the same value as the direct PORTC register.

edit: THe reason I'd want to pass a separate mask for each led is that later I will need to blink each LED at a different rate. So its not like they would all necessarily be on or off at any given point.
 
So are PORTA, PORTB, PORTC, etc all somehow defined? They aren't in my devices .h file nor any of my project .h files. I didn't find an address to it in the datasheet to define it myself. I assume if I change PORTC then it directly affects the output? Like the built-in function output_low(pin) and output_high(pin) is probably just a way to indirectly set the PORTC register?
 
PORTC should be mapped to some actual address. In the IDE's I've used, you can specify the target device and it loads the memory map for you. If your's doesnt do that, you have to specify the addresses yourself. They should definitely be in the datasheet.
 
Yeah I definitely got my device (PIC16F684) selected in MPLAB.

And yeah my bad, I was looking in the wrong section in the datasheet for the register addresses. PORTC is definitely 07h.

So should I do this?

#define PORTC (U8*) (0x07)

And then to use it would I do?

*PORTC |= led_mask

I had gotten an LVALUE error so I figured a pointer was needed for the left-hand side of the operator in an expression. I'm not too sure about ptrs tho.. this is my first ptr by myself lol.

So I think I'm pretty set. I gotta solder up some SOIC/DIP or whatever the hell it is adapter so I can use the ICE 2000 to see if this thing even works!

Thanks for all the help guys!
 
Hey the PIC 16F684, that's what we used! Too bad for you I don't remember a whole lot about it 😉

But jmcoreymv sounds right on
 
Back
Top