PDA

View Full Version : Toggled Pic flip flop Help Please



g7jiq
- 21st March 2009, 18:01
Hi all

I have had a bit of a blonde moment and wonder if anyone could help me,

Thanks...

Can anyone suggest a small bit of self contained code I can add at the end of my existing code, That will give me a toggled flip-flop output.

EG, gp0 i/p (triggered high to low via push button)
gp1 o/p (low at switch on)
gp2 o/p (high at switch on)

When i/p triggered the o/p's swap over and latch in that state until the button is pressed again, after it will then return back to the original state.

Also how difficult is it to get the pic to remember its last state after a power up/down

This is a test program I tried to get working, before modding and inserting into my existing code.


' Define Hardware
' ---------------
InputTrigger var GPIO.2 ' Input normally HIGH, goes LOW to Trigger
OutputLine0 var GPIO.0 ' Normally LOW, goes HIGH when triggered
OutputLine1 var GPIO.1 ' Normally LOW, goes HIGH when triggered
'
' Initialise PIC
' --------------
Reset:
TRISIO=%00000100 ' Preset I/O
CMCON=%00000111 ' Disable Comparators
ANSEL=%00000000 ' Disable ADC
pause 200
HIGH OutputLine0 ' Set output
LOW outputline1 ' Set output

if outputline0=0 then outputline1=1 (these are the two lines i am having)
if outputline0=1 then ouputline1=0 ( problems with )

' Main Program Loop
' -----------------
Loop:
'
'
' Test for Trigger
' ----------------
If InputTrigger=0 then ' Test for Trigger
toggle OutputLine0 ' Enable Output

endif
Goto Loop

paul borgmeier
- 22nd March 2009, 05:44
I am not sure why you have these two lines – they will always be false since they follow your HIGH and LOW statements ... delete them (in my opinion)



if outputline0=0 then outputline1=1 (these are the two lines i am having)
if outputline0=1 then ouputline1=0 ( problems with )

To get you going, try this for your Loop – Goto Loop section (written close to your style).


Loop:
'
'
'Test for Trigger
'----------------
If InputTrigger=0 then
pause 30 ' Debounce time
If InputTrigger = 1 then Loop ' Debounce check – exit out of loop if not low after 30 mS

While InputTrigger=0 ' Wait for button release before updating
Wend ' so you do not race through your Loop routine a zillion times before you release the button

GPIO = GPIO ^ 3 ' Toggle bits 0 and 1 - See PBP manual section 4.17.14
Endif
Goto Loop

After this works, you can work on saving the state of the toggle in EEPROM. What PIC are you using?

g7jiq
- 22nd March 2009, 10:09
Thanks for your help,

I have transfered your code (i can see where I went wrong) (yours is a better way)
and it works well,
I have decided to run the switching code in a 12F675 on its own as I had run out of ports on my 16F627,

(Just to put you in the picture the unit is part of a two way radio repeater, and the coding was to switch between to linking radios.)

Cheers
Dave...