PDA

View Full Version : Blink LED / toggle output with causing a software delay



timetec
- 27th November 2009, 16:12
Hello all - I'm very, very new to this, so please forgive me :o

I need to toggle an output @ 2hz (250ms High / 250ms Low etc), when a specific action has been performed, blinking an LED as a visual indicator.

The PULSOUT command stalls the program routine and for it's entire duration, in in my case, I'd be using a looped PULSOUT GPIO.1,250000

Is there a way to toggle an output for a fixed ON/OFF period, whilst the program can continue to run, monitoring inputs etc ?

Many thanks :)

Acetronics2
- 27th November 2009, 16:28
Hi, Tim

Welcome aboard ...

YES there are lots of different ways ...

BUT ... we , at least, need to know which processor you use and what you already use as resources ...

Shortly ...

Your program ALSO is welcome ...

Alain

Jerson
- 28th November 2009, 02:16
timetec

Think about using the inbuilt hardware timer and its associated interrupt. With this, you can flash leds in the background. But, like Alain says, please tell us more about your selected PIC for this to be meaningful to you

Welcome aboard

timetec
- 28th November 2009, 13:12
Many thanks for the replies :)

The MCU is a 12F629, using all GPIO ports.

Here's the routine :

To monitor 2 momentary switch inputs, & enable an output (blinking an LED @ 2Hz), when a switch is pressed.

There is a seperate O/P for each corresponding switch.

If both outputs become active, a 3rd output toggles 10 seconds high and then reverts back to a low state -- the LED outputs are then reset.

All working perfectly, but I'm having to use FLED's on the outputs.

Acetronics2
- 28th November 2009, 13:38
Hi, Tim

Sorry, but I do not understand what you want to do or actually do ... :

Blink outputs WHILE button pressed ?
Blink @ First press and stop @ second press ?

You talk about 2 Leds ???


the LED outputs are then reset.


... 2 inputs + 3 outputs + 2 Leds ...

are more pins than available on the 629 ...

soooo ... PLEASE : Your program .... so we can understand what you do !

Alain

timetec
- 28th November 2009, 16:18
5 I/O are used - 2 switch inputs, 2 LED outputs, 1 'reset' output

Example :

Switch 1

Blink LED output IF the Switch 1 has been pressed - keep it blinking @ 2Hz

If Switch 1 pressed again, keep it blinking ! - exactly the same for Switch 2

If both LED outputs are active - send 10 second pulse to 3rd output pin.
Then reset all switches / LED's & start again.

mackrackit
- 28th November 2009, 18:03
http://www.picbasic.co.uk/forum/showthread.php?t=1742

Code and schematic are key....

timetec
- 30th November 2009, 01:48
Really sorry about the delay in getting this up - major problems with my one and only 12F629 - lost the 'OSCCAL' number when programming,
somehow... :confused: -- so re-compiled it for a 12F509.

It's long-winded I know, but it works with steady outputs in conjunction with 2 x FLEDS or flashing LED's.
In the listing, there are 4 lines of commented (tried & unused) code for a simple 250ms on - 250ms off, port toggle for each LED output.
Of course, this is useless, as the program spends all it's time, toggling outputs..

No components involved, other than a 3 resistors for LED's, if you want to put some on GPIO.2 / GPIO.4 & GPIO.5 as status indicators to 0V.

The 2 switch inputs are single-pole tactile types pulled active LOW.

Switch input pins are GPIO.0 & GPIO.1 - all pin details in the listing.

Just need to get 2 standard LED's flashing @2Hz on the outputs, without this affecting the switch input 'scan' routine.

Many thanks all :)

Melanie
- 30th November 2009, 11:20
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...



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...



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

timetec
- 30th November 2009, 15:04
Thank you so much Melanie :)

What is more, it works perfectly ! - assembled 1st time, with no errors.

At one stage, I did actually think about using an incremental counter, with a Gosub/Return statement in the switch monitoring loop, to check the value of the counter and take appropiate action.

However, the problem still remained of having to 'toggle' the O/P's on & off for 250ms each (and the huge delays using the combined TOGGLE & PULSOUT commands).

Your solution is very clever - I guess simlilar to 'multiplexing' a multi-digit 7-segment LED display, when only 1 digit is on at any one time, - 'persistance of vision' fools your brain into thinking they are all on at once !

I'll let you know how I get on 'tweaking' the code & post the results here.

Thanks again :)

Acetronics2
- 30th November 2009, 15:58
What is more, it works perfectly ! - assembled 1st time, with no errors.


Eveybody Sing with me :

" ain't no sunshine when she's gone, ....." *big smile* - Melanie


exagerated a little bit ... yes, might be .

But soooo small a bit !!!

Alain