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