//---------------------------------------------------------------------- //---------------------------------------------------------------------- // this program consists of 1 tasks + interrupt handler // // example of simple program that reads 2 switches and flashes // 1 LED // // the switches are debounced in the foreground (they are debounced // every time they are read) // // 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 main() long j byte k, k2 byte switch1_value, prev_switch1_value byte switch2_value, prev_switch2_value 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 switch1_value = 0 prev_switch1_value = 0 switch2_value = 0 prev_switch2_value = 0 flash_cnt = 3 while 1 do PORTA = 0 for k=0 while k<(flash_cnt*2)-1 step k+=1 do for j=0 while j<10000 step j+=1 do k2 = 0 done PORTA = PORTA ^ 1 done for j=0 while j<40000 step j+=1 do k2 = 0 done // porta bit 6 switch1_value = read_porta(0x40) if switch1_value != prev_switch1_value then // the current switch position is not the same as the // last time we looked at it, so there has been a // transition from open to closed or closed to open if switch1_value == 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) // adjust range from 1..8 to 0..7 flash_cnt = flash_cnt - 1 // decrease number of flashes flash_cnt = flash_cnt - 1 // keep flashes in range 0..7 flash_cnt = flash_cnt & 7 // adjust range from 0..7 to 1..8 flash_cnt = flash_cnt + 1 endif prev_switch1_value = switch1_value endif // porta bit 7 switch2_value = read_porta(0x80) if switch2_value != prev_switch2_value then // the current switch position is not the same as the // last time we looked at it, so there has been a // transition from open to closed or closed to open if switch2_value == 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) // adjust range from 1..8 to 0..7 flash_cnt = flash_cnt - 1 // increase number of flashes flash_cnt = flash_cnt + 1 // keep flashes in range 0..7 flash_cnt = flash_cnt & 7 // adjust range from 0..7 to 1..8 flash_cnt = flash_cnt + 1 endif prev_switch2_value = switch2_value endif done endproc proc intserv() endproc