include P16F628.INC
__CONFIG _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BODEN_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _XT_OSC
cblock 0x20 ; Block of variables starts at address 20h
w_temp
status_temp
d1,d2,d3
endc
; symbolic name for port connected to LEDs (PORTB)
LEDs EQU 6
; symbolic names for LEDs for bitwise operations
LD1 EQU 0
LD2 EQU 1
LD3 EQU 2
LD4 EQU 3
LD5 EQU 4
LD6 EQU 5
; symbolic name for port connected to switches (PORTA)
SWs EQU 5
; symbolic names for Switches for bitwise operations
SW1 EQU 0
SW2 EQU 1
SW3 EQU 2
SW4 EQU 3
org 0x0000
goto Main
org 0x0004
goto ISR
;************************************************************************************
ISR
; save STATUS and W register before processing interrupt
movwf w_temp
swapf STATUS,w
movwf status_temp
; restore the working and status register
movf status_temp,w
movwf STATUS
swapf w_temp,f
swapf w_temp,w
retfie
; subroutines
;************************************************************************************
delay movlw 0x2d ;around 0.1 second or a bit less
movwf d1
movlw 0xe7
movwf d2
movlw 0x01
movwf d3
delay_0 decfsz d1, f
goto dd2
decfsz d2, f
dd2 goto dd3
decfsz d3, f
dd3 goto delay_0
return
SW1_Pressed movlw D'4'
movwf CCPR1L
call delay
return
SW2_Pressed movlw D'16'
movwf CCPR1L
call delay
return
SW3_Pressed movlw D'64'
movwf CCPR1L
call delay
return
SW4_Pressed movlw D'255'
movwf CCPR1L
call delay
return
;******************************************************
; end of subroutines
Main nop
movlw 0x07 ;turn comparators off
movwf CMCON
bsf STATUS,RP0 ;bank 1
movlw 0x00 ;all pins outputs
movwf TRISA
movwf TRISB
movlw 0xff ;set period
movwf PR2
bcf STATUS,RP0 ;bank 0
movlw 0x80 ;set duty cycle
movwf CCPR1L
movlw 0x04 ;set timer2 prescale and enable it
movwf T2CON
movlw 0x0c ;set bottom 2 pwm bits and enable pwm
movwf CCP1CON
BSF T2CON, TMR2ON ;Timer2 starts to increment
call delay
MLoop nop
clrf PORTB
btfsc SWs,SW1
call SW1_Pressed
btfsc SWs,SW2
call SW2_Pressed
btfsc SWs,SW3
call SW3_Pressed
btfsc SWs,SW4
call SW4_Pressed
goto MLoop
;******************************************************
end