//---------------------------------------------------------------------- //---------------------------------------------------------------------- // this program consists of 2 tasks + interrupt handler // // example of multitasking program that reads 2 switches and flashes // 1 LED // // the switches are debounced in the background by the debounce driver // which executes in its own task // // flashes LED between 1 and 8 times pauses then flashes again the // number of times the LED flashes between pauses is selected by the // user. By pressing one button the user increases the number of // flashes. By pressing the other button the user decreases the number // of flases //---------------------------------------------------------------------- //---------------------------------------------------------------------- include "hwreg-p16f628.h" // 16f628 config // enable internal oscillator FOSC2,FOSC1,FOSC0 = 100 // disable watchdog WDT = 0 // enable power up timer PWRTE = 0 // use MCLR as RA5 MCLRE = 0 // disable brown-out detect BODEN = 0 // low voltage prog disabled LVP = 0 // data memory not protected CPD = 1 // code memory not protected CP1,CP0,CP1,CP0 = 1111 // // see section 14.1 of PIC16F26X ref manual // microchip document name 40300b.pdf pragma cpu_config 0x3F10 include "fifo_lib.bas" FIFO switch_event_fifo[6] include "debounce.bas" //---------------------------------------------------------------------- //---------------------------------------------------------------------- proc task_1() debounce_init(0xC0, 0x00, &switch_event_fifo) while 1 do debounce_queue_service() task_wait 1 done endproc //---------------------------------------------------------------------- //---------------------------------------------------------------------- proc int adjust_flash_cnt(int flash_cnt, int val) // adjust range from 1..8 to 0..7 flash_cnt = flash_cnt - 1 // increment or decrement number of flashes flash_cnt = flash_cnt + val // keep flashes in range 0..7 flash_cnt = flash_cnt & 7 // adjust range from 0..7 to 1..8 flash_cnt = flash_cnt + 1 endproc //---------------------------------------------------------------------- //---------------------------------------------------------------------- proc main() byte k byte flash_cnt // set outputs to known value before enabling output drivers PORTA = 0 // set porta bit 0 to output, rest as inputs TRISA = 0xFE // set outputs to known value before enabling output drivers PORTB = 0 // set all portb to inputs TRISB = 0xFF // set PORTA to digital I/O instead of analog CMCON = 7 flash_cnt = 1 task_start task_1 while 1 do PORTA = 0 for k=0 while k<(flash_cnt*2)-1 step k+=1 do task_wait 5 PORTA = PORTA ^ 1 done task_wait 20 int switch_change while 1 do switch_change = fifo_read(&switch_event_fifo) if switch_change < 0 then break endif // a switch change has occured // NOTE: process all switch changes between each set // of flashes if (switch_change & 0x7f) == 6 then // porta bit 6 changed state if (switch_change & 0x80) == 0 then // NOTE: this switch reads 1 for open and 0 for closed // so look to 1 to 0 transition which means from // open to closed (not pressed to pressed) // decrease number of flashes flash_cnt = adjust_flash_cnt(flash_cnt, -1) endif else if (switch_change & 0x7f) == 7 then // porta bit 7 changed state if (switch_change & 0x80) == 0 then // NOTE: this switch reads 1 for open and 0 for closed // so look for 1 to 0 transition which means from // open to closed (not pressed to pressed) // increase number of flashes flash_cnt = adjust_flash_cnt(flash_cnt, 1) endif endif done done endproc //---------------------------------------------------------------------- //---------------------------------------------------------------------- proc intserv() endproc