View previous topic :: View next topic |
Author |
Message |
ctempel2 Guest
|
Starting state problems |
Posted: Mon Apr 14, 2008 10:57 pm |
|
|
So I have a PIC16F877A, and I have code that has worked for me before but the problem is that it works very sporadically. I'm guessing it is a problem with the starting state since once i place it on the protoboard and I get it to work, it keeps on working. However, if I take it off and put it back on, it stops working and it takes me a lot of playing around with the inputs to get it to work again.
I have omitted the calculations in the code as they arent relevant to the problem. Basically all the codes does is output timed pulses (this works perfectly when I can get it to work).
Here is my partial code:
Code: |
#include <16F877a.h>
#USE DELAY( CLOCK=20000000 ) //20MHz crystal
#FUSES HS,NOWDT,NOPROTECT, NOLVP
#byte port_a=5
#byte port_b=6
#byte port_c=7
#byte port_d=8
int8 counter;
void main()
{
set_tris_b(0b01111111);
set_tris_c(0b00000000);
set_tris_d(0b00000000);
port_c = 0;
port_d = 0;
for(;;)
{
if (INPUT(PIN_B0) == 0)
{
counter = 0;
}
else if ( INPUT(PIN_B0) == 1 )
{
// does some calculations and outputs signal, not really
// relevant
if (counter < 1)
{
outputcycle();
counter = counter+1;
}
}
}
}
|
Essentially, I have a switch going to pin B0. If the switch is turned on, I want the output cycle to run and I need the program to know that the cycle has been run already (hence the counter incrementing after the cycle). So, the only way to run the cycle again is to switch B0 back to 0 and then back to 1.
I'm not sure what the problem with this would be, does anyone have any suggestions on how I can implement this? |
|
|
Ttelmah Guest
|
|
Posted: Tue Apr 15, 2008 2:57 am |
|
|
How is the switch wired?.
At present it appears that it pulls the pin to +5v. What pulls the pin to '0', when the switch is not operated?....
Best Wishes |
|
|
temp2290
Joined: 07 Apr 2008 Posts: 9
|
|
Posted: Tue Apr 15, 2008 3:50 pm |
|
|
What do you mean? The input is just wired to a normal switch that chooses between a 5V source and ground.
Oh, and I am ctempel2. I forgot to log in when I posted. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 15, 2008 4:12 pm |
|
|
That's not the normal way to connect a switch. I suggest that you
enable the Port B pull-up resistors. Then connect the switch so that
it connects the PIC pin to ground when the switch is "On", and that
it connects to nothing when the switch is "Off". So when the switch is
off, the internal pull-up will keep the PIC pin at a logic high level.
Example of code to enable the internal pull-ups on Port B:
Code: | port_b_pullups(TRUE); |
Other things to do:
1. Make sure you have an external pull-up resistor on the MCLR pin
of the PIC. Use 10K if you have a Microchip ICD2. Use 47K if you
have a ICD-U40.
2. Make sure you have a 100 nF (0.1 uF) ceramic capacitor between
each Vdd pin on the PIC and ground. Place the caps close to the chip. |
|
|
temp2290
Joined: 07 Apr 2008 Posts: 9
|
|
Posted: Wed Apr 16, 2008 9:18 am |
|
|
Ok, point taken about the switches, but will my method of switching work consistently too? |
|
|
temp2290
Joined: 07 Apr 2008 Posts: 9
|
|
Posted: Wed Apr 16, 2008 10:08 am |
|
|
To me, it seems like the program isnt even starting for some reason. I have the command port_d=0 right at the beginning of main, and the outputs are 5V always, not matter what I do. |
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 16, 2008 10:27 am |
|
|
Change-over switches, tend to be more expensive than simple 'make' switches. Hence the main reason for preferring the latter. Normally, the former will be 'break before make', which means that at some point, the pin will be floating. this is never a 'good thing'.
Even worse, if the switch is make before break (rarer), then it'll short out the power supply...
In general you should add some form of de-bounce to the switch test (make sure it gives the same reading for a few mSec, before deciding on what to do.
Have you got the pull-up on MCLR, mentioned by PCM programmer?. This is _vital_.
Add 'PUT' to the fuses. Improves the start-up reliability.
How is power supplied to the PIC?. What smoothing have you got close to the PIC?. Commonest cause of problems is a 5v rail that is rippling, or rising very slowly.
Best W |
|
|
temp2290
Joined: 07 Apr 2008 Posts: 9
|
|
Posted: Wed Apr 16, 2008 4:30 pm |
|
|
Thanks for the reply. I am using a protoboard with a 5V DC power supply (one of the big hulking bench supplies that I have at my university). I have two 0.1uF capacitors connected from Vdd to ground right at the input to the PIC pins. I also have a 47k resistor from the MCLR to Vdd (although I'm not sure if that's the one I should be using... how can I tell?).
My inputs are DIP 3 way switches connected to Vdd and ground. For example:
Vdd------>
------->PIC input
Grd------>
So I guess it could be a problem that the input is floating at times (debounce problem). I will just add a delay_ms(10) right before it reads any inputs, hopefully that will fix it.
Honestly it's got to be just some reliability problem, since it sporadically works (and when it does, it is perfect). Right now I'm kind of worried because the output is *always* 5V, which tells me the code never even gets to where i initialize port_d to zero. |
|
|
temp2290
Joined: 07 Apr 2008 Posts: 9
|
|
Posted: Sat Apr 19, 2008 10:50 am |
|
|
Still not getting any luck... maybe it has to do with my external oscillator? It's peak to peak voltage is only 2V... |
|
|
foodwatch
Joined: 18 Apr 2006 Posts: 66
|
|
Posted: Sat Apr 19, 2008 8:52 pm |
|
|
Virtually all switches "bounce" when activated. This results is a series of semi random make/breaks as the switch first closes. The easiest way is to use the rb0 as an interrupt and create a 250ms loop to assure that the pic sees only 1 closure and not the bouncing. I'm not sure if this will help in your application. |
|
|
temp2290
Joined: 07 Apr 2008 Posts: 9
|
|
Posted: Sun Apr 20, 2008 10:39 am |
|
|
Ok, I got it working. I just pulled everything out of my protoboard and re-attached it with my same code. Musta been some sort of messed up connection.
Thanks for all the help. |
|
|
|