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

What's wrong with this simple code?

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



Joined: 02 Feb 2012
Posts: 56

View user's profile Send private message

What's wrong with this simple code?
PostPosted: Sat Feb 18, 2012 7:35 am     Reply with quote

I'm using Mplab, CCS and a pickit 2 with a PIC16F887

My code compiles and uploads with no problems. I'm just not getting the functionality that I expect from the PIC.

Here is my code

#include <16F887.h>
#fuses INTRC_IO //use internal oscillator
#fuses NOWDT //No watch dog timer
#fuses PUT//
#fuses BROWNOUT//reset chip if voltage falls
#fuses MCLR//master clear enabled
#fuses NOLVP
#use delay(clock=8000000)


void main()
{

while(1)
{

if (input(pin_D0)) { //read pin D0
Output_high(pin_D4); //enable pin D4 when D1 is hi
}
else {
Output_low(pin_D4); //else keep D4 low
}

if (input(pin_D1)) { //read pin D1
Output_low(pin_D4); //if pin D1 hi then turn D4 off
}

}

}

I am experimenting with pins as i/p's. I have pin D4 driving an LED. I expect that when I apply VCC to pin D0, that D4 will be enabled and otherwise non enabled. On top of that, I expect that if D4 is enabled, then applying VCC to D1 will disable D4 and the LED.

The problem is that when I run everything, the LED seems to have a mind of its own. It will light up even when pin_D0 doesn't connect to VCC. I can just touch the pin with a wire attached to nothing and activate it. Also, applying VCC to D0 does not turn it off right away.
Or sometimes applying VCC to D1 (which should disable D4) actually enables it. I've tried to separate the 2 i/p pins (Do & D1) and it works a little better but not really. If I can get pin D1 to disable D4, it does not happen right away.

I eventually want the pic to accept a combination of i/p's before the led activates. I am just starting small.
I have a feeling the problem lies with the fact that the pins aren't continuously being monitored by my program. Maybe that's why there is a long delay between pin D1 disabling D4 when I apply VCC.

I know this is long winded and apologize for that.

Is there any advice?
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 8:28 am     Reply with quote

1) do you have curent limiting resistors for the LEDs?

2) do you have a pullup( 10k) on MCLR ?

3) have you got a '1 Hz blinking LED' program to work ?
rems



Joined: 02 Feb 2012
Posts: 56

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 8:44 am     Reply with quote

I do have current limit resistors. I have gotten various blinking programs to work. I did one where four o/p's blinked some leds in a binary sequence.

Now I'm just trying to experiment by having more control. Such as turning them on and off with a switch. Only I haven't put the switch in yet as I am just touching vcc to the appropriate pin. which can be replaced by a switch when working properly.

I do not have a 10k pullup on MCLR. I am operating on a breadboard and have lots of various parts. I will put one in and see if that helps.

Thank you
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 8:45 am     Reply with quote

4) Do you have a pull down resistor on D1 and D0?
Otherwise the input will be floating and give random read results.
rems



Joined: 02 Feb 2012
Posts: 56

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 8:50 am     Reply with quote

No I don't have pulldown resisters either. What value should I use? I assume that I just connect each pin to ground through the pulldown resister?

Also, with the 10k pullup; do I just connect MCLR to VCC through the 10k?
rems



Joined: 02 Feb 2012
Posts: 56

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 8:53 am     Reply with quote

I should clarify that I understand that I would connect D0 and D1 to ground through different pulldown resisters. Not sharing a single pulldown to ground.
rems



Joined: 02 Feb 2012
Posts: 56

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 9:03 am     Reply with quote

I used 10 k pulldown resisters and a 10k pullup resister. Everything works beautifully.

Thank you to the both of you that helped.

I understand what the pulldown resisters are doing. I understand that floating pins is very confusing to the PIC and completely get why I use them. The pullup on MCLR though. Is that to keep the PIC from floating low and resetting itself?
rems



Joined: 02 Feb 2012
Posts: 56

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 9:10 am     Reply with quote

Well not everything works as expected actually.

The led is 100% stable now. Applying VCC to pinD1 turns led on and removing VCC from D1 turns led off. That's perfect.

I put a delay in so the led would stay on and works perfect.

But. I try to turn led off by applying VCC to pin D1 and nothing. The led continues to stay lit.

while(1)
{
Output_low(pin_D4);
if (input(pin_E0)) { //read pin D1
Output_high(pin_D4); //enable pin D4 when D1 is hi
delay_ms(5000);
}


if (input(pin_D1)) { //read pin D1
Output_low(pin_D4); //if pin D1 hi then turn D4 off
}

Is my delay that keeps the led lit over-riding my call to turn it off ? The delay comes before the line of code that directs pinD1 to turn it off. So I think that me be the problem.

Can anyone confirm?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 9:21 am     Reply with quote

During that delay_ms(5000) the PIC will do NOTHING for 5 seconds. It will not look at input pins during those 5 seconds. The only way it would do something during those 5 seconds would be an interrupt.

I am not sure what behavior you want, but maybe you want to delay 5ms and do it 1000 times, checking the inputs between 5ms delays?
_________________
The search for better is endless. Instead simply find very good and get the job done.
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 9:23 am     Reply with quote

delay_ms(5000);

Means that the PIC will do NOTHING but loop around wasting time until 5 seconds has passed...THEN it executes the next line of your code( checking the pin, in your case).
rems



Joined: 02 Feb 2012
Posts: 56

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 9:27 am     Reply with quote

I get it now. Okay thank you.

I just wanted the led to stay on to confirm that applying VCC to pin D1 would then turn it off. That's why I used a delay. It was to keep the led on.
rems



Joined: 02 Feb 2012
Posts: 56

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 9:52 am     Reply with quote

I got it. I had an extra piece of code before my if statement. It set my led pin to low. So when I enabled it, it would turn off as soon as no more VCC reached the enabling pin. That's why I was using a delay to keep it on.

I removed the code and now the led stays on when D0 just gets touched by VCC. It then stays on until VCC goes to D1. It then turns off. That's exactly what I wanted.

Everything is great.

Thanks to everyone for your help.
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