CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Simple LED blink problem (ignores conditional statement)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
JiggyPepper



Joined: 02 Feb 2012
Posts: 7

View user's profile Send private message

Simple LED blink problem (ignores conditional statement)
PostPosted: Thu Feb 02, 2012 2:42 am     Reply with quote

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? :(
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Feb 02, 2012 3:14 am     Reply with quote

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
gip_mad



Joined: 23 Aug 2008
Posts: 24
Location: Italy

View user's profile Send private message Visit poster's website

PostPosted: Thu Feb 02, 2012 3:24 am     Reply with quote

Also, your code infinitely prints "hello."
JiggyPepper



Joined: 02 Feb 2012
Posts: 7

View user's profile Send private message

PostPosted: Thu Feb 02, 2012 3:43 am     Reply with quote

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]
gip_mad



Joined: 23 Aug 2008
Posts: 24
Location: Italy

View user's profile Send private message Visit poster's website

PostPosted: Thu Feb 02, 2012 4:22 am     Reply with quote

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.
JiggyPepper



Joined: 02 Feb 2012
Posts: 7

View user's profile Send private message

PostPosted: Thu Feb 02, 2012 5:07 am     Reply with quote

thanks for the tip gip_mad . At least I can see its functioning logically.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Feb 02, 2012 5:35 am     Reply with quote

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
JiggyPepper



Joined: 02 Feb 2012
Posts: 7

View user's profile Send private message

PostPosted: Mon Feb 06, 2012 3:23 am     Reply with quote

Thank you so much Ttelmah. The pull down resistor did the trick. Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group