View previous topic :: View next topic |
Author |
Message |
JiggyPepper
Joined: 02 Feb 2012 Posts: 7
|
Simple LED blink problem (ignores conditional statement) |
Posted: Thu Feb 02, 2012 2:42 am |
|
|
Hey guys, I'm stuck at a weird error I encountered.
Let me start off with my setup:
PIC16F877A
ICD2 Programmer
MPLAB (using CCS C compiler)
20 Mhz external clock
It's a code to start a blinking led once PIN_A1 is high (driven by a prox sensor).
Code is as follows:
Code: |
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay (clock = 20000000)
#use rs232 (baud=9600, xmit= PIN_C6, rcv= PIN_C7)
void prox1()
{
if (input(PIN_A1)) {
output_high(PIN_D0);
delay_ms(2000);
output_low(PIN_D0);
delay_ms(2000);
}
}
void main()
{
while(true){
printf("hello.");
prox1();
}
}
|
The problem is that the LED starts blinking, even without the prox going off. I tried removing the sensor completely and it still starts blinking. Lastly, I tried (!input(pin_A1)) to start blinking and this functions as planned. It looks like the PIC is completely ignoring the 'if' function. What am I doing wrong? :( |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Thu Feb 02, 2012 3:14 am |
|
|
First, without the sensor, the pin is _floating_, so it can trigger or not trigger on a completely 'luck' basis.....
Are you sure your sensor actually pulls the input 'low' when not triggered and 'high' when triggered?. That would be quite unusual behaviour (90% of sensors like this pull their outputs _low_ when triggered. The opposite of what your code expects. The other common type, are 'switch' outputs, and then you will need to add a pull down resistor to pull the pin low when the switch is open.
This might be your problem.
Generally though, turn off other things that are on the pin you are using. ADC jumps to mind. Look at examples using this chip, and you will see the most start by disabling this.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
gip_mad
Joined: 23 Aug 2008 Posts: 24 Location: Italy
|
|
Posted: Thu Feb 02, 2012 3:24 am |
|
|
Also, your code infinitely prints "hello." |
|
![](templates/subSilver/images/spacer.gif) |
JiggyPepper
Joined: 02 Feb 2012 Posts: 7
|
|
Posted: Thu Feb 02, 2012 3:43 am |
|
|
thanks for the comments. however, as I said I did check the code for both (input(PIN_A1)) and (!input(PIN_A1)) both to no avail. Blinker still starts. :(
nevermind the printf function as well, I just used that to check my LCD. :(
where should I exactly put the pull down resistor? will 10k do?
If it helps, I tried reprogramming it to output to pin b1 instead to no avail. Any more help will be greatly appreciated. [/quote] |
|
![](templates/subSilver/images/spacer.gif) |
gip_mad
Joined: 23 Aug 2008 Posts: 24 Location: Italy
|
|
Posted: Thu Feb 02, 2012 4:22 am |
|
|
You can try to remove the sensor and manually put the pin to Vdd or Vss and see if that works, so you will know if it's a hardware or a software problem. |
|
![](templates/subSilver/images/spacer.gif) |
JiggyPepper
Joined: 02 Feb 2012 Posts: 7
|
|
Posted: Thu Feb 02, 2012 5:07 am |
|
|
thanks for the tip gip_mad . At least I can see its functioning logically. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Thu Feb 02, 2012 5:35 am |
|
|
We are rather guessing, since you haven't given a part number for the sensor.
However quite a few sensors of this type have 'switch' outputs. Closing when something is 'seen'. If so, then the switch will be 'open' when nothing is seen, and the input pin won't be connected to anything, except a length of wire running to the sensor. Such a 'floating' line, _will_ pick up signals (both RF, and induced from any mains sources nearby). As such an undriven input, may well see signals going high and low possibly hundreds of times a second. If this is happening, then the code _will_ respond whether you test for the signal being high or low.....
If this is the case, then the wiring needs:
Ground connection to resistor.
Other end of resistor to the input, and the sensor line.
Other sensor output to 5v.
This way when the sensor is 'open', the line is pulled to 0v, when the sensor closes, the signal is pulled up to 5v.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
JiggyPepper
Joined: 02 Feb 2012 Posts: 7
|
|
Posted: Mon Feb 06, 2012 3:23 am |
|
|
Thank you so much Ttelmah. The pull down resistor did the trick. ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|