' ========================================================================= ' ' File...... Task_Blinker.SXB ' Purpose... ' Author.... Jon Williams, EFX-TEK ' E-mail.... jwilliams@efx-tek.com ' Started... ' Updated... 12 SEP 2008 ' ' ========================================================================= ' ------------------------------------------------------------------------- ' Program Description ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Conditional Compilation Symbols ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- ID "TaskBlnk" DEVICE SX28, OSCXT2, BOR42 FREQ 20_000_000 STACK 16 ' ------------------------------------------------------------------------- ' I/O Pins ' ------------------------------------------------------------------------- AlarmLed PIN RA.0 OUTPUT SystemLed PIN RA.1 OUTPUT ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- IsOn CON 1 IsOff CON 0 ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- flags VAR Byte isrFlag VAR flags.0 alarmOn VAR flags.1 sysReady VAR flags.2 ' ========================================================================= INTERRUPT 1_000 ' ISR rate = 1 ms ' ========================================================================= Mark_ISR: isrFlag = 1 Schedule_Tasks: TASKS RUN, 1 ' task tick = 1 ms RETURNINT ' ========================================================================= ' Subroutine / Function / Task Declarations ' ========================================================================= DELAY_MS SUB 1, 2 ' replaces PAUSE BLINK_LED TASK ' ========================================================================= PROGRAM Start ' ========================================================================= Start: PLP_A = %0000_0011 PLP_B = %0000_0000 PLP_C = %0000_0000 TASKS SET, 0, BLINK_LED, 250 ' 250 ms TASKS SET, 1, BLINK_LED, 100 ' 100 ms TASKS ENABLE alarmOn = IsOn sysReady = IsOn Main: ' both LEDs should be blinking GOTO Main ' ------------------------------------------------------------------------- ' Subroutine / Function / Task Code ' ------------------------------------------------------------------------- ' Use: DELAY_MS duration ' -- replaces PAUSE ' -- expects INTERRUPT rate setting of 1_000 SUB DELAY_MS tmr1ms VAR __WPARAM12 IF __PARAMCNT = 1 THEN tmr1ms_MSB = 0 ENDIF DO WHILE tmr1ms > 0 \ CLRB isrFlag \ JNB isrFlag, $ DEC tmr1ms LOOP ENDSUB ' ------------------------------------------------------------------------- TASK BLINK_LED slot VAR __PARAM1 IF slot = 0 THEN IF alarmOn THEN TOGGLE AlarmLed ELSE AlarmLed = IsOff ENDIF ELSEIF slot = 1 THEN IF sysReady THEN TOGGLE SystemLed ELSE SystemLed = IsOff ENDIF ENDIF ENDTASK ' ========================================================================= ' User Data ' ========================================================================= ' ========================================================================= ' ' File...... Soft_RTC.SXB ' Purpose... BCD RTC using SX/B 2.0 tasks ' Author.... Jon Williams, EFX-TEK ' E-mail.... jwilliams@efx-tek.com ' Started... ' Updated... 12 SEP 2008 ' ' ========================================================================= ' ------------------------------------------------------------------------- ' Program Description ' ------------------------------------------------------------------------- ' BCD clock to 7-Segment LED segments ' -- tested on Parallax PDB ' -- requires SX/B 2.x or later ' ------------------------------------------------------------------------- ' Conditional Compilation Symbols ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- ID "Soft RTC" DEVICE SX28, OSCXT2, BOR42 FREQ 20_000_000 STACK 16 ' for local vars ' ------------------------------------------------------------------------- ' I/O Pins ' ------------------------------------------------------------------------- Anodes PIN RC OUTPUT ' to 7-segs through 1K Cathodes PIN RB ' to common cathode Blinker PIN RA.0 OUTPUT ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- IsOn CON 1 IsOff CON 0 _0 CON %00111111 R_HUNS CON 0 ' hundredths register R_SECS CON 1 ' seconds register R_MINS CON 2 ' minutes register R_HRS CON 3 ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- flags VAR Byte isrFlag VAR flags.0 digSel VAR Byte ' digit select clock VAR Byte (4) huns VAR Byte @ clock(R_HUNS) secs VAR Byte @ clock(R_SECS) mins VAR Byte @ clock(R_MINS) hrs VAR Byte @ clock(R_HRS) segments VAR Byte (5) ' holds LED segments ' ========================================================================= INTERRUPT 10_000 ' ISR rate is 0.1 ms ' ========================================================================= Mark_ISR: isrFlag = 1 Schedule_Tasks: TASKS RUN, 10, 3 ' 1ms tick, 3 tasks max RETURNINT ' ------------------------------------------------------------------------- ' Subroutine / Function / Task Declarations ' ------------------------------------------------------------------------- DELAY_MS SUB 1, 2 ' replaces PAUSE UPDATE_LEDS TASK ' refresh 7-seg LEDs BCD_RTC TASK ' rtc, uses bcd regs RTC_TO_SEGS TASK ' move rtc to segments() ' ========================================================================= PROGRAM Start ' ========================================================================= Start: PLP_A = %0000_0000 ' pull up unused pins PLP_B = %0001_1111 Cathodes = %11111111 ' clear cathodes TRIS_B = %11100000 ' enable five columns PUT segments, _0, _0, _0, _0, _0 ' clear to zeroes TASKS SET, 0, UPDATE_LEDS, 1 ' update segments, 0.001s TASKS SET, 1, BCD_RTC, 10 ' update clock, 0.01s TASKS SET, 2, RTC_TO_SEGS, 50 ' rtc -> segments(), 0.05s TASKS ENABLE Main: Blinker = IsOn DELAY_MS 500 ' alwasy works Blinker = IsOff PAUSETICKS 500 ' tasks must be enabled! IF clock(R_SECS) = $10 THEN ' seconds register TASKS STOP, 1 ' stop the clock ENDIF GOTO Main ' ------------------------------------------------------------------------- ' Subroutine / Function / Task Code ' ------------------------------------------------------------------------- ' Use: DELAY_MS duration ' -- replaces PAUSE ' -- expects INTERRUPT rate setting of 10_000 SUB DELAY_MS tmr1ms VAR __WPARAM12 tixMs VAR __PARAM3 IF __PARAMCNT = 1 THEN tmr1ms_MSB = 0 ENDIF DO WHILE tmr1ms > 0 tixMs = 10 DO WHILE tixMs > 0 \ CLRB isrFlag \ JNB isrFlag, $ DEC tixMs LOOP DEC tmr1ms LOOP ENDSUB ' ------------------------------------------------------------------------- ' Updates RTC segments ' -- moves segments(digSel) to set anodes pins ' -- modifies: digSel TASK UPDATE_LEDS INC digSel IF digSel = 5 THEN ' at limit? digSel = 0 ' yes, roll over ENDIF Anodes = %00000000 ' prevent segments ghosting READ DigMap + digSel, Cathodes ' enable column cathode Anodes = segments(digSel) ' set anodes IF digSel.0 THEN ' if digits 1 or 3 Anodes.7 = 1 ' enable dec point ENDIF ENDTASK ' ------------------------------------------------------------------------- ' Real-time BCD clock, hh:mm:ss.xx ' -- modifies: clock(); values stored as BCD ' -- set task to run every 10 milliseconds TASK BCD_RTC BANK @clock INC huns ' increment register huns = huns + $06 ' test for digit carry IF DC = 0 THEN ' $0A -> $10? huns = huns - $06 ' no, readjust ENDIF IF huns <= $99 THEN RTC_Done ' exit if not at limit huns = $00 ' reset INC secs secs = secs + $06 IF DC = 0 THEN secs = secs - $06 ENDIF IF secs <= $59 THEN RTC_Done secs = $00 INC mins mins = mins + $06 IF DC = 0 THEN mins = mins - $06 ENDIF IF mins <= $59 THEN RTC_Done mins = $00 INC hrs hrs = hrs + $06 IF DC = 0 THEN hrs = hrs - $06 ENDIF IF hrs <= $23 THEN RTC_Done hrs = $00 RTC_Done: BANK ENDTASK ' ------------------------------------------------------------------------- ' Moves RTC values to segments() array ' -- modifies: segments() ' -- uses one byte from stack TASK RTC_TO_SEGS tIdx VAR Byte BANK @clock tIdx = huns ' xx:xx.0 SWAP tIdx ' swap to get high nib tIdx = tIdx & $0F ' isolate nibble READ SegMap + tIdx, segments(0) ' convert to segments tIdx = secs ' xx:x0.x tIdx = tIdx & $0F READ SegMap + tIdx, segments(1) tIdx = secs ' xx:0x.x SWAP tIdx tIdx = tIdx & $0F READ SegMap + tIdx, segments(2) tIdx = mins ' x0:xx.x tIdx = tIdx & $0F READ SegMap + tIdx, segments(3) tIdx = mins ' 0x:xx.x SWAP tIdx tIdx = tIdx & $0F READ SegMap + tIdx, segments(4) BANK ENDTASK ' ------------------------------------------------------------------------- ' User Data ' ------------------------------------------------------------------------- SegMap: ' segments maps ' .gfedcba DATA %00111111 ' 0 DATA %00000110 ' 1 DATA %01011011 ' 2 DATA %01001111 ' 3 DATA %01100110 ' 4 DATA %01101101 ' 5 DATA %01111101 ' 6 DATA %00000111 ' 7 DATA %01111111 ' 8 DATA %01100111 ' 9 DATA %01110111 ' A DATA %01111100 ' b DATA %00111001 ' C DATA %01011110 ' d DATA %01111001 ' E DATA %01110001 ' F DigMap: ' digit selection DATA %11111110 DATA %11111101 DATA %11111011 DATA %11110111 DATA %11101111 DATA %11011111 DATA %10111111 DATA %01111111 New Template for SX/B 2.0 ' ========================================================================= ' ' File...... ' Purpose... ' Author.... ' E-mail.... ' Started... ' Updated... ' ' ========================================================================= ' ------------------------------------------------------------------------- ' Program Description ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Conditional Compilation Symbols ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- ID "template" DEVICE SX28, OSCXT2, BOR42 FREQ 20_000_000 STACK 16 ' ------------------------------------------------------------------------- ' I/O Pins ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- ' ========================================================================= ' INTERRUPT {option} {rate} ' ========================================================================= ISR_Start: ISR_Exit: ' RETURNINT {cycles} ' ========================================================================= ' Subroutine / Function / Task Declarations ' ========================================================================= ' ========================================================================= PROGRAM Start ' ========================================================================= Start: PLP_A = %0000_0000 PLP_B = %0000_0000 PLP_C = %0000_0000 Main: GOTO Main ' ------------------------------------------------------------------------- ' Subroutine / Function / Task Code ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' ========================================================================= ' User Data ' =========================================================================