Re: need led blink program
Hi,
First of all, welcome!
There are many many ways of doing what you describe and selecting which approach to take depends on a lot of things. For example, what kind of accuracy you want and if the program is supposed to other things as well. Here's one possible approach which is straight forward but not the most accurrate:
Code:
LED1_Time CON 30 '30 seconds
LED2_Time CON 60 '60 seconds
LED3_Time CON 120 '120 seconds
LED1_Count VAR BYTE ' Timekeeping variables
LED2_Count VAR BYTE
LED3_Count VAR BYTE
LED1 VAR PortB.0 ' Pin aliases for the LEDs
LED2 VAR PortB.1
LED3 VAR PortB.2
Start:
TRISB = %11111000 ' PortB.0-2 outputs.
' Preload timekeeping variables
LED1_Count = LED1_Time
LED2_Count = LED2_Time
LED3_Count = LED3_Time
Main:
' Decrement counters
LED1_Count = LED1_Count - 1
LED2_Count = LED2_Count - 1
LED3_Count = LED3_Count - 1
' Time for LED1 to toggle?
IF LED1_Count = 0 THEN
TOGGLE LED1
LED1_Count = LED1_Time ' Reload counter for next period.
ENDIF
'Time for LED2 to toggle?
IF LED2_Count = 0 THEN
TOGGLE LED2
LED2_Count = LED2_Time ' Reload counter for next period.
ENDIF
' Time for LED3 to toggle
IF LED3_Count = 0 THEN
TOGGLE LED3
LED3_Count = LED3_Time ' Reload counter for next period.
ENDIF
PAUSE 1000
GOTO Main
It does compile without errors and I think it should be fine but I have not tested it.
As you can imagine the accuracy of this isn't the best but again, it might work perfectly fine for your needs. If better accuracy is needed you could change the PAUSE 1000 to use PAUSEUS in a loop and tweak the value. Using a timer interrupt is of course "the best" solution but it's much more complicated and may not be needed, again YMMV.
Finally, define osc 4 won't work. It needs to be define OSC 4. 4MHz is the default so in this particular case it doesn't matter but had you done define osc 8 instead of define OSC 8 the timings would have been wrong.
/Henrik.
Re: need led blink program
hi,henric thanks for replying ,i tried ur programm but its not working ,i guess i could not describe my requirement clearly,let me explain again ,
here i have this simple led blink programm and in this program led on portb.7 is blinking endlessly with the pause of 500ms
now i want another led to blink on portb.6 with pause of 1000 ms ,along with the led which is already blinking with pause of 500ms on portb.7 , both led on portb.6 and portb.7 should blink endlessly but different speed.
I dont need this programm in any project i am just trying to learn ,Thankyou ,i hope now i have made my requirement clear
Re: need led blink program
Hi,
Really, is "It's not working" the best description you can come up with?
What's "not working"? What DOES it do? What DOESN'T it do?
In your original post you said PortB.0 and 30seconds, PortB.2 and 1 minute, PortB.3 and 2 minutes.....
Now you're saying PortB.7 and 500ms, PortB.6, 1000ms.... So, no you probably didn't explain your requirements very well.
I've now tried my previously posted code and it works just fine as per your original specifications. I then changed it to match the revised specification, tested it again at it works just fine here. I'll post it here again:
Code:
' Specify delay time in units of 100ms. 10=1second, 25=2.5seconds etc
LED1_Time CON 5 ' 0.5 seconds
LED2_Time CON 10 ' 1 seconds
LED3_Time CON 30 ' 3 seconds
LED1_Count VAR BYTE ' Timekeeping variables
LED2_Count VAR BYTE
LED3_Count VAR BYTE
LED1 VAR PortB.7 ' Pin aliases for the LEDs
LED2 VAR PortB.6
LED3 VAR PortB.5
TRISB = %00011111
Start:
' Preload timekeeping variables
LED1_Count = LED1_Time
LED2_Count = LED2_Time
LED3_Count = LED3_Time
Main:
' Decrement counters
LED1_Count = LED1_Count - 1
LED2_Count = LED2_Count - 1
LED3_Count = LED3_Count - 1
' Time for LED1 to toggle?
IF LED1_Count = 0 THEN
TOGGLE LED1
LED1_Count = LED1_Time ' Reload counter for next period.
ENDIF
'Time for LED2 to toggle?
IF LED2_Count = 0 THEN
TOGGLE LED2
LED2_Count = LED2_Time ' Reload counter for next period.
ENDIF
' Time for LED3 to toggle
IF LED3_Count = 0 THEN
TOGGLE LED3
LED3_Count = LED3_Time ' Reload counter for next period.
ENDIF
PAUSE 100
GOTO Main
Now, I've tested this on another device than what you're using so it's possible that there's something device specific that needs to be set. If you can't get this to work then please describe what it does or doesn't do and post your complete code.
/Henrik.
Re: need led blink program
hi
sorry henrick ,i told u i am new and it is my very first post ,it was my fault i couldnt explain properly and i wrote minute but actually i wanted to write seconds dont mind i will learn rules fast ,
now this second program of u is working exactly as i wanted it to work ,Thanks a lot you r very helpful ,soon i will approach u with another problem and i hope u will keep helping me
Re: need led blink program
actually i have another question to ask related to this post ,can i write this programme like following
------
clear
define osc 4
loop1:
high portb.7
pause 500
low portb.7
pause 500
goto loop1
loop2:
high portb.6
pause 1000
low portb.6
pause 1000
goto loop2
loop3:
high portb.5
pause 2000
low portb.5
pause 2000
goto loop3
end
----------
i tried this way but it didnt work please tell me what is wrong if i write like above
Re: need led blink program
Hi,
You can write it like that and it will compile but it won't work the way you want it to.
The program starts from the beginning and executes one instruction at the time.
* Turns on the LED
* Waits 500ms
* Turns off the LED
* Waits 500ms
* Jump to Loop1 and starts over.
The program will act as if the Loop2 and Loop3 routines wasn't even there.
And again, make a habit of writing your defines in UPPER CASE otherwise they'll have no effect.
Code:
define OSC 4 ' <----- OSC must be uppercase or it will not have the desired effect!
/Henrik.