PDA

View Full Version : LED timing



elen-group
- 26th June 2008, 23:40
Hi,
We are trying to program our PIC 16F88 so that we can time our LED when triggered by a switch. We want the LED to stay on for five seconds everytime the switch is turned on.
This is the code we have so far:
'---------------------
DEFINE OSC 20

TRISB = %00000000 'setup PORTB; 0 for output, 1 for input
TRISA.0=1

loop:

IF PORTA.0 = 1 Then ledon
PORTB.0=0 'turn off LED

GOTO loop

ledon:

PORTB.0=1 'light LED
PAUSE 5000 'pause for 2 seconds
PORTB.0=0 'turn off LED
GOTO loop

END
'------------------------------------------


We have connected the switch to port A0 and the led to port B0. We would appreciate any help.

Archangel
- 27th June 2008, 01:40
Hello elen-group,
this should get you started:
ADCON0 = 0
ADCON1=0
CMCON = 7
ANSEL = 0
edit: pause 5000 = 5 seconds

skimask
- 27th June 2008, 02:58
We have connected the switch to port A0 and the led to port B0. We would appreciate any help.
So, what's the problem?

elen-group
- 27th June 2008, 20:48
Our circuit is not conditioned to the switch. Everytime we turn on the circuit our led comes on regardless if we use the switch or not. We tried the suggested help but still did not fix it. I am thinking that there is some issue with the if statement which is not implementing the switch we want.

Archangel
- 27th June 2008, 21:11
Our circuit is not conditioned to the switch. Everytime we turn on the circuit our led comes on regardless if we use the switch or not. We tried the suggested help but still did not fix it. I am thinking that there is some issue with the if statement which is not implementing the switch we want.


DEFINE OSC 20
PortA = %00000000
TRISB = %00000000 'setup PORTB; 0 for output, 1 for input
TRISA.0=1

loop:

IF PORTA.0 = 1 Then ledon
PORTB.0=0 'turn off LED

GOTO loop

ledon:

PORTB.0=1 'light LED
PAUSE 5000 'pause for 2 seconds
PORTB.0=0 'turn off LED
GOTO loop

END


put a pulldown resistor on porta.0
and a pullup on PortB.0

sayzer
- 28th June 2008, 10:51
Here, I think, what is needed.


<font color="#0000FF"><i>'All the usual stuff here.
'....
'....
'....

</i></font><b>Sw </b><font color="#000080"><b>VAR </b></font><b>PORTA.</b><font color="#FF0000"><b>0 </b></font><font color="#0000FF"><i>' Switch is connected to RA0.
</i></font><b>LED </b><font color="#000080"><b>VAR </b></font><b>PORTB.</b><font color="#FF0000"><b>0 </b></font><font color="#0000FF"><i>' LED is connected to RB0.
</i></font><b>Flag </b><font color="#000080"><b>VAR BIT </b></font><font color="#0000FF"><i>' Use a flag to have a virtual falling-edge,rising-edge thingie.

</i></font><b>Begin:
</b><font color="#000080"><b>INPUT </b></font><b>Sw </b><font color="#0000FF"><i>' Switch pin is an input pin. Tie it to V+ with a resistor.
</i></font><b>Flag = </b><font color="#FF0000"><b>0

</b></font><b>Loop:

</b><font color="#0000FF"><i>' When button is pressed, the LED turns ON, stays ON 5 secs and then turns OFF.
' While button is being pressed, the led does NOT repeat Turn ON action.
' When the button is released then Flag is cleared. Thus, what happens?

</i></font><font color="#000080"><b>IF </b></font><b>Sw = </b><font color="#FF0000"><b>0 </b></font><font color="#000080"><b>AND </b></font><b>Flag = </b><font color="#FF0000"><b>0 </b></font><font color="#000080"><b>THEN </b></font><b>LedOn </b><font color="#0000FF"><i>' SW is pressed.

</i></font><font color="#000080"><b>IF </b></font><b>Sw = </b><font color="#FF0000"><b>1 </b></font><font color="#000080"><b>AND </b></font><b>Flag = </b><font color="#FF0000"><b>1 </b></font><font color="#000080"><b>THEN </b></font><b>Flag = </b><font color="#FF0000"><b>0 </b></font><font color="#0000FF"><i>' SW is released.


</i></font><font color="#000080"><b>GOTO </b></font><b>Loop



LedOn:

LED = </b><font color="#FF0000"><b>1 </b></font><font color="#0000FF"><i>' Turn ON led.
</i></font><font color="#000080"><b>PAUSE </b></font><font color="#FF0000"><b>5000 </b></font><font color="#0000FF"><i>' Wait 5 secs.
</i></font><b>LED = </b><font color="#FF0000"><b>0 </b></font><font color="#0000FF"><i>' Turn OFF Led.

</i></font><b>Flag = </b><font color="#FF0000"><b>1

</b></font><font color="#000080"><b>GOTO </b></font><b>Loop

</b><font color="#000080"><b>END

</b></font>