001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
|
;********************************************************
;
; The light control processing
;
; Device : PIC16F873
; Author : Seiichi Inoue
;********************************************************
list p=pic16f873
include p16f873.inc
__config _hs_osc & _wdt_off & _pwrte_on & _cp_off
;**************** Program Start ***********************
org 0 ;Reset Vector
goto init
org 4 ;Interrupt Vector
goto int
;**************** Initial Process *********************
init
;*** Port initialization
bsf status,rp0 ;Change to Bank1
movlw b'00000001' ;AN0 to input mode
movwf trisa ;Set TRISA register
clrf trisc ;Set TRISC to output mode
bcf status,rp0 ;Change to Bank0
;*** A/D converter initialization
movlw b'10000001' ;ADCS=10 CHS=AN0 ADON=ON
movwf adcon0 ;Set ADCON0 register
bsf status,rp0 ;Change to Bank1
movlw b'00001110' ;ADFM=0 PCFG=1110
movwf adcon1 ;Set ADCON1 register
bcf status,rp0 ;Change to Bank0
;*** PWM initialization
clrf tmr2 ;Clear TMR2 register
clrf ccpr1l ;Clear CCPR1L register
bsf status,rp0 ;Change to Bank1
movlw d'255' ;Period=1638.4usec(610Hz)
movwf pr2 ;Set PR2 register
bcf status,rp0 ;Change to Bank0
movlw b'00000110' ;Pst=1:1 TMR2=ON Pre=1:16
movwf t2con ;Set T2CON register
movlw b'00001100' ;CCP1XY=0 CCP1M=1100(PWM)
movwf ccp1con ;Set CCP1CON register
;*** Compare mode initialization
clrf tmr1h ;Clear TMR1H register
clrf tmr1l ;Clear TMR1L register
movlw h'09' ;H'9C4'=2500
movwf ccpr2h ;Set CCPR2H register
movlw h'c4' ;2500*0.4usec = 1msec
movwf ccpr2l ;Set CCPR2L register
movlw b'00000001' ;Pre=1:1 TMR1=Int TMR1=ON
movwf t1con ;Set T1CON register
movlw b'00001011' ;CCP2M=1011(Compare)
movwf ccp2con ;Set CCP2CON register
;*** Interruption control
bsf status,rp0 ;Change to Bank1
movlw b'00000001' ;CCP2IE=Enable
movwf pie2 ;Set PIE2 register
bcf status,rp0 ;Change to Bank0
movlw b'11000000' ;GIE=ON PEIE=ON
movwf intcon ;Set INTCON register
wait
goto $ ;Interruption wait
;*************** Interruption Process *****************
int
clrf pir2 ;Clear interruption flag
ad_check
btfsc adcon0,go ;A/D convert end ?
goto ad_check ;No. Again
movf adresh,w ;Read ADRESH register
movwf ccpr1l ;Set Duty cycle of PWM
;************ END of Interruption Process **************
retfie
;********************************************************
; END of light control processing
;********************************************************
end
|