Newbie - 16F628A and switch latching
Hi all,
My first post so be gentile with me :)
My first project using PBP is for my 12 year old son's model railway. I'm using a 16F628A with the internal 4Mhz OSC as timing is not an issue (and the internal OSC is claimed to be very precise anyway). The project is not rocket science, simply to detect a train via the momentarly activation of a reed switch. This pulse then turns on an LED which is latched on until the same reed switch is activated again.
I have two problems:
1) - The latching is not clean, in that sometimes the LED brightens and remains on when it should be off, or when off it is turned on by the reed switch, but fails to latch
2) - I'm using WinPIC pro to program the PIC. I have set the PIC up for internal OSC no clock out in the code, but I have to change the settings from XT to RC noClockout in the Winpic application. Is the an issue with the Winpic application, or the code (which compiles OK)
Anyway, here is my code.
Code:
@INTRC_OSC_NOCLKOUT
@WDT_ON
@PWRT_ON
@MCLR_OFF
@BOD_ON
@LVP_OFF
@CPD_OFF
@PROTECT_OFF
CMCON=7
TRISB=%11101111 'set RB6 as output and the rest input
SW var PORTB.0 'switch input pin 6 (RB0)
LED1 var PORTB.4 'LED1 on pin 10 (RB4)
Swset var bit
low led1 'RB4 set to LOW (LED1 is off)
Swset=0 'Var swset is set low
Main:
if SW=0 then 'If switch is LOW (Grounded) then
Toggle Swset 'change swset from 0 to 1
pauseus 200 'debounce delay
endif
If Swset=1 then 'if swset is 1
high led1 'then turn on RB4 (and hence the LED)
else
low led1 'else, LED is off
endif
Goto Main 'go back and keep checking
End
The reason I've only configured one output on port B is purely for testing..once I have this sorted, RB0, RB1, RB2 and RB3 will all be switch inpts, with RB4 - RB7 their coresponding outputs (we have 4 sidings to monitor )
TIA
Malcolm
The Forum' Highest secret ...
Quote:
Originally Posted by malc-c
Hee hee - fancy seeing you here ;)
Alain, let me into a secret, is that Melanie in her avitar... brains as well as beauty - wow !!
In fact, Mel is some like Charly in the "Charly's girls" TV series ... I do not think anyone on this forum knows her real appearance ...
We only know she says she likes to wear leather clothes at night, and is a bit bored about charming guys hoping to get something from her brain ...
May be she'd love to find a bunch of red roses and some Champagne at her door rather than the New up to date Compiler ....
Talk to her gently, and she'll give you the best from her ...
That's part of what I understood here ... not to break the magic.
Alain
lets get this back on topic before I need a cold shower !
I'm hoping that you guys and Miss whippy, sorry Mel can tell me why the following code won't work correctly.
Having managed to sort out the delay for the debounce I had each LED bing turned on, latched and turned off by activation of the corresponding reed switch - Great !
However I now want to evolve the code so that the following happens. The siding has the buffer at the left, and trains enter from the right. A reed switch (sw1) is placed at the start of the siding (on the right), a train enters the siding and activated sw1 and LED1 is lit. The train continues to the end of the siding where it activates the second reed switch (sw2) and thus LED2 is lit, and the operator shuts off the power. So far so good.
Now the operator can re-arrange the train and place the loco at the other end ready to go back on the main layout. When it does sw1 is then activated again, but this time I need it to clear both LED1 and LED2. Ok I thought, lets simply have a new varible (swset5) that is toggled only when swset1 and swset2 are high.. but it won't work. activating sw1, then sw2, then sw1 again just turns off LED1, LED2 remains lit
Here is my code (I've yet to try the BUTTON command, but maybe that will happen when I come to tidy up the code ??)
Code:
@INTRC_OSC_NOCLKOUT
@WDT_ON
@PWRT_ON
@MCLR_OFF
@BOD_ON
@LVP_OFF
@CPD_OFF
@PROTECT_OFF
CMCON=7
TRISB=%00000011 'set RB0 & 1 as input and the rest output
SW1 var PORTB.0 'switch input pin 6 (RB0)
SW2 var PORTB.1 'switch input pin 7 (RB1)
LED1 var PORTB.4 'LED1 on pin 10 (RB4)
LED2 var PORTB.5 'LED1 on pin 11 (RB5)
Swset1 var bit
Swset2 var bit
Swset5 var bit
low led1
low led2
Swset1=0
Swset2=0
swset5=0 'Var swset is set low
Main:
if SW1=0 then 'If switch is LOW (Grounded) then
Toggle Swset1 'change swset1 from 0 to 1
pause 500 'debounce delay
endif
if SW2=0 then 'If switch is LOW (Grounded) then
Toggle Swset2 'change swset2 from 0 to 1
pause 500 'debounce delay
endif
If Swset1=1 then 'if swset1 is 1
high led1 'then turn on RB4 (and hence the LED)
else
low led1 'else, LED is off
endif
If Swset2=1 then 'if swset2 is 1
high led2 'then turn on RB5 (and hence the LED)
else
low led2 'else, LED is off
endif
If swset1=1 and swset2=1 then
toggle swset5
endif
if swset5=1 and sw1=0 then
low led1
low led2
else
high led1
high led2
endif
End
I have even tried removing the "else" part at the end of the code so it reads
Code:
If swset1=1 and swset2=1 then
toggle swset5
endif
if swset5=1 and sw1=0 then
low led1
low led2
endif
But that still fails to work correctly.
Any advice guys and gals ??
1 Attachment(s)
One little last ...for the trip.
Ok let's stop it here ...
Thanks to Luciano, ve've a good idea of uptown girls at night ...
cool, Malc ... cool !!!
Alain
Don't you just love the manual.
Ok, trying to get my head round the BUTTON statement, and failing.. bu I just love the line
Quote:
In general, it is easier to simply read the state of the pin in an IF..THEN
than to use the BUTTON command as follows:
If PORTB.2 = 1 Then notpressed
which was what I was originally doing .....
Oh well ... I'll keep trying
Mel ... you're an angel ...
Our friend MUST discover some little things by experience ...
Copy and paste is really the worst solution !!!
He surely forgot that !!!
Alain