Blink LED / toggle output with causing a software delay


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2009
    Posts
    5

    Default Blink LED / toggle output with causing a software delay

    Hello all - I'm very, very new to this, so please forgive me

    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

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink

    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Nov 2009
    Posts
    5


    Did you find this post helpful? Yes | No

    Default

    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.

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default

    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  6. #6
    Join Date
    Nov 2009
    Posts
    5


    Did you find this post helpful? Yes | No

    Default

    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.

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Nov 2009
    Posts
    5


    Did you find this post helpful? Yes | No

    Post

    Really sorry about the delay in getting this up - major problems with my one and only 12F629 - lost the 'OSCCAL' number when programming,
    somehow... -- 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
    Attached Files Attached Files

  9. #9
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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

  10. #10
    Join Date
    Nov 2009
    Posts
    5


    Did you find this post helpful? Yes | No

    Thumbs up

    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

  11. #11
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink a little Thank you ...

    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. Conway's Game Of Life
    By wellyboot in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 28th May 2020, 06:14
  2. Free Project - 245 LED Display
    By T.Jackson in forum Code Examples
    Replies: 221
    Last Post: - 16th August 2009, 04:59
  3. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  4. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  5. LCD will not start
    By btaylor in forum mel PIC BASIC Pro
    Replies: 49
    Last Post: - 24th May 2007, 02:30

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts