PDA

View Full Version : Blink without the pause command



DanPBP
- 16th August 2007, 15:59
Hi All,

I want to blink a LED without the pause command because I want to do other things and not stop the entire loop every time the LED blinks.

For example, with the pause command the code looks like this:

main:
high portb.0
pause 500
low portb.0
pause 500
goto main

Without the pause command the code could look something like this:

count_on var byte
count_off var byte
pause_led var byte

count_on = 0
count_off = 0
pause_led = 5000

main:
if count_on = 0 then
high portb.0
endif

if count_on != pause_led
count_on = count_on + 1
endif

if count_on = pause_led then
low portb.0

count_off = count_off + 1

if count_off = pause_led then
count_on = 0
count_off = 0
endif

endif

goto main:

My question is, if I want to leave the LED turned on for 1 second, how many "if" cycles should I do?

Thanks!

Daniel.

mackrackit
- 16th August 2007, 17:02
One thing and one thing only can be done at a time, that is until parallel or "quantum" computers hit the market.

You will want to look at "interrupts" . That is how the impression of doing more than one thing at a time is accomplished. Even your desktop with all of the ram and cpu speed can only do one thing at a time - believe it or not.

http://www.picbasic.co.uk/forum/showthread.php?t=3251&highlight=interrupts

Look at the above link for a slick way of using interrupts with PICs or the manual for the old fashion way.

Bruce
- 16th August 2007, 18:11
I agree with Dave. But the approach to take really depends on what it is you need to do in
between the LED on\off periods, how long the LED should be on AND off, the oscillator speed,
and which PIC you're using.

Trying to work out the number of if-thens between toggling the LED on or off isn't a very
good approach, since the time will vary as your code in between the on\off periods will
change, and require recalibration with every change.

More information would really help folks here to help you..;o}

sayzer
- 16th August 2007, 18:53
Another way would be to use a timer module.

You can check the timer overflow bit and increment a variable at each overflow.

Then, you can ON and OFF the LED at any desired time using this variable.


--------------------

DanPBP
- 16th August 2007, 18:59
I'm using a pic 12F683 (internal oscillator) to turn on/off some lights on my R/C car...

I'm reading the output from the receiver with pulsin and then I can turn on/off the lights...

I'm using the pwm output to control the stop lights... Normally, they are 50% lit and when I hit the brakes they go 100% brightness...

Also, I want to blink two yellow LEDs as turn lights, but I cannot wait while the LEDs are blinking because I need to know if something else happens, like hitting the brakes....

So, that's why I wanted to know how to blink the LEDs without the pause command...

I think I could do something with the "ifs", but as you said Bruce, will have to recalibrate the code...

Thanks for your answers!

Daniel.

Bruce
- 16th August 2007, 19:32
I would go with Darrel Taylors' instant interrupts with timer1. Disable interrupts before using
PULSIN. Then re-enable them after PULSIN. A few mS either way shouldn't be a big deal for
a heart-beat type LED on/off.

Acetronics2
- 17th August 2007, 07:51
Hi, Daniel

the easy way with R/C signals ...

the servo signal is THE "missing timer" ... say the 1.5 ms is @ 40 Hz ( example )

if you want to blink @ 2Hz ...

just count the incoming 1.5 ms pulses and TOGGLE the led output for COUNT // 10 = 1,or 2 ... but not 0

takes very little CPU time.

hé,hé ...

Alain

Jerson
- 17th August 2007, 12:30
I want to blink a LED without the pause command because I want to do other things and not stop the entire loop every time the LED blinks.
SNIP
Without the pause command the code could look something like this:

count_on var byte
count_off var byte
pause_led var byte

count_on = 0
count_off = 0
pause_led = 5000

main:
if count_on = 0 then
high portb.0
endif

if count_on != pause_led
count_on = count_on + 1
endif

if count_on = pause_led then
low portb.0

count_off = count_off + 1

if count_off = pause_led then
count_on = 0
count_off = 0
endif

endif

goto main:


Daniel

I can suggest (without interrupts) the following



counter: var word
Led: var PortB.0

counter =0
main: if counter < HalfSecondValue then
counter=counter+1 ' increment the counter
else
counter = 0
Led = !Led ' toggle every half second
endif
' do whatever you have to do here
....
....
goto main ' go back to the top


Here, HalfSecondValue is something you will have to determine either by counting the machine cycles or experimentally(easier)

Jerson

DanPBP
- 27th August 2007, 20:55
Thanks to all for your help!

I also found this from Darrel: http://www.darreltaylor.com/DT_INTS-18/blinky.html

Regards!

Daniel.