You know in programming and Computers there's no such thing as "immediate" or "without delay". Even the world's fastest supercomputer still takes time to execute an instruction. Now the big decision is "How fast is fast enough"?
If it's Push-Buttons (ie human involvement), then 1mS is more than adequate time to kill before detecting that a Button has been pressed. If it's an Optical Sensor detecting a Bullet whizzing past it, then obviously 1mS is totally inadequate.
So let's pretend that we can afford to waste 1mS keeping time without missing a Button Press... so what we'll do is have our MainLoop keeping our 250mS toggle continuously in 1mS steps, and in between those 1mS steps, we'll do all our checking and counting...
Break it all down into easy manageable processes... I've not included any code for disabling Comparators or ADC's or Voltage Refernce modules... that's up to you with your chosen PIC whatever it may be...
Code:
CounterSmall var BYTE ' 1mS Counter
CounterBIG var BYTE ' 250mS Counter
SystemFlags var BYTE
LEDToggle var SystemFlags.0 ' LED Toggle Flag
OutputAFlag var SystemFlags.1 ' Button A Active Flag
OutputBFlag var SystemFlags.2 ' Button B Active Flag
ResetPulse con 40 ' Length of time of RESET Output
' calculated in steps of 250mS
'
' Hardware Defines
' ----------------
InputA var GPIO.0
InputB var GPIO.1
OutputA var GPIO.2
OutputB var GPIO.4
OutputReset var GPIO.5
'
Initialise:
TRISIO=%00001011
Low OutputA
Low OutputB
Low OutputReset
CounterBig=0
SystemFlags=0
'
MainLoop:
'
' Detect Button A
' ---------------
If InputA=0 then
OutputAFlag=1
else
OutputAFlag=0
endif
'
' Detect Button B
' ---------------
If InputB=0 then
OutputBFlag=1
else
OutputBFlag=0
endif
'
' Process LEDA
' ------------
If OutputAFlag=1 and LEDToggle=1 then
High OutputA
else
Low OutputA
endif
'
' Process LEDB
' ------------
If OutputBFlag=1 and LEDToggle=1 then
High OutputB
else
Low OutputB
endif
'
' Process RESET Output
' --------------------
If OutputAFlag=1 then
If OutputBFlag=1 then
If CounterBIG=0 then
OutputRESET=1
CounterBIG=ResetPulse
endif
endif
endif
If CounterBIG=0 then OutputRESET=0
'
' Keep Time
' ---------
Pause 1
CounterSmall=CounterSmall+1
If CounterSmall=250 then
LEDToggle=LEDToggle^1
If CounterBIG > 0 then CounterBIG=CounterBIG-1
endif
'
Goto MainLoop
Now the above Code applied to have the LED's blinking ONLY whilst the Buttons are pressed.
Re-reading your posts, I'm not sure if you want them to LATCH-ON and only reset once the 10-Second Pulse resets everything... in which case you need a small variant of the above...
Code:
CounterSmall var BYTE ' 1mS Counter
CounterBIG var BYTE ' 250mS Counter
SystemFlags var BYTE
LEDToggle var SystemFlags.0 ' LED Toggle Flag
OutputAFlag var SystemFlags.1 ' Button A Active Flag
OutputBFlag var SystemFlags.2 ' Button B Active Flag
ResetPulse con 40 ' Length of time of RESET Output
' calculated in steps of 250mS
'
' Hardware Defines
' ----------------
InputA var GPIO.0
InputB var GPIO.1
OutputA var GPIO.2
OutputB var GPIO.4
OutputReset var GPIO.5
'
Initialise:
TRISIO=%00001011
Low OutputA
Low OutputB
Low OutputReset
CounterBig=0
SystemFlags=0
'
MainLoop:
'
' Detect Button A
' ---------------
If InputA=0 then OutputAFlag=1
'
' Detect Button B
' ---------------
If InputB=0 then OutputBFlag=1
'
' Process LEDA
' ------------
If OutputAFlag=1 and LEDToggle=1 then
High OutputA
else
Low OutputA
endif
'
' Process LEDB
' ------------
If OutputBFlag=1 and LEDToggle=1 then
High OutputB
else
Low OutputB
endif
'
' Process RESET Output
' --------------------
If OutputAFlag=1 then
If OutputBFlag=1 then
If CounterBIG=0 then
OutputRESET=1
CounterBIG=ResetPulse
endif
endif
endif
If CounterBIG=0 then OutputRESET=0
'
' Keep Time
' ---------
Pause 1
CounterSmall=CounterSmall+1
If CounterSmall=250 then
LEDToggle=LEDToggle^1
If CounterBIG > 0 then
CounterBIG=CounterBIG-1
If CounterBIG=0 then
OutputAFlag=0
OutputBFlag=0
endif
endif
endif
'
Goto MainLoop
Bookmarks