;****MAIN PROGRAM
MAIN
;turn off LED RC0 & RC6
banksel LATE
bcf LATE,RE2 ; turn OFF LED @ RE2
;zero Timer0
banksel TMR0
clrf TMR0 ; clear Timer0
bcf INTCON,TMR0IF ; and clear Timer0 overflow flag
;wait for button press @ RB0
banksel PORTB
wait_dn btfsc PORTB,RB0 ; if RB => LOW, skip next instruction
goto wait_dn
;wait for button release @ RB0 and debounce by sampling @ 1mS
db_up movlw .10 ; max count = 10 (10ms @ 1ms/sample)
movwf db_cnt
up_smp movlw .42 ; sample every 1ms
up_dly decfsz WREG,f ; delay 42x3-1 = 125 inst. cycles
goto up_dly ; = 125 x 8us = 1ms
banksel PORTB
btfss PORTB,RB0 ; if button RB0 is pressed (RB0 => LOW)
goto db_up ; restart count
decfsz db_cnt,f ; else repeat until max count reached
goto up_smp
;turn on LED @ RE2
banksel LATE
bsf LATE,RE2 ; turn ON LED @ RE2
;delay
dly_tmr btfss INTCON,TMR0IF ; if Timer0 overflow occured
goto dly_tmr ;
banksel LATE
bcf LATC,RE2
;repeat forever
goto MAIN
END
;****CONFIGURE OSCILLATOR
movlw b'00000111' ; configure OPTION_REG
; 0------- clear NOT_WPUEN bit; enable weak pull-ups
; -0------ clear INTEDG bit; int. on falling edge
; --0----- clear TMR0CS bit; Int. instruction cycle
; ----0--- clear PSA bit: Prescaler to Timer0 module
; -----111 set prescaler to 1:256
banksel OPTION_REG
movwf OPTION_REG
DavidBLit wrote:bcf LATC,RE2
clrf swold ; clear switch state latch |00
;********************************************************************
; main loop *
;********************************************************************
loop
call delay25ms ; 25-ms debounce/loop interval |00
comf PORTB,W ; sample active lo RB0 switch |00
movwf swnew ; save new sample |00
newrelease
btfss swnew,0 ; new == 0? no, skip (exit), else |00
btfss swold,0 ; old == 1? yes, skip, else |00
bra newpress ; branch (not new release) |00
newpress
btfsc swnew,0 ; new == 1? no, skip (exit), else |00
btfsc swold,0 ; old == 0? yes, skip, else |00
bra swlatch ; branch (not new press) |00
movlw 1<<RE2 ; bit mask for LED on RE2 |00
xorwf PORTE,F ; toggle LED (RE2) |00
movlw 800/25+1 ; 800-ms / 25-ms loop time |00
movwf ledtmr ; reset 800-msec LED timer |00
swlatch
movf swnew,W ; update switch state latch |00
movwf swold ; |00
ontimer
btfsc PORTE,RE2 ; LED on? no, skip (exit), else |00
decfsz ledtmr,F ; 800-ms time out? yes, skip |00
bra loop ; |00
bcf PORTE,RE2 ; turn LED off |00
bra loop ; |00
;********************************************************************
Return to 14-Bit Core (enhanced)
Users browsing this forum: No registered users and 2 guests