i am doing a 8 speed motor controller using PIC 16f876 using PWM. Pwm output from the chip is connected to the main motor using a n type mosfet. the main motor is connected to a detection motor using gears.the feedback from the detection motor is connected to a potentiometer which is then input into the AN0 of the chip.my motive is to have a 8 speed control using my potentiometer.i managed to do all the initialisation of the program.but i am having difficulties programming the speed detectikon part. for now my output will be LEDs to indicate which speed i am at each time the potentiometer is tuned. i have attached the first part that i have done...
hope someone can help me with the speed detection part..
#include<p16f876A.inc>
__config _hs_osc & _wdt_off & _pwrte_on & _lvp_off
; Register Definitions
_wdt_off EQU H'3FFB'
_pwrte_on EQU H'3FF7'
_hs_osc EQU H'3FFE'
_lvp_off EQU H'3F7F'
w EQU H'0000'
f EQU H'0001'
;----- Register Files------------------------------------------------------
status EQU H'0003'
portb EQU H'0006'
intcon EQU H'000B'
pir1 EQU H'000C'
pie1 EQU H'008C'
pir2 EQU H'000D'
tmr1l EQU H'000E'
tmr1h EQU H'000F'
t1con EQU H'0010'
tmr2 EQU H'0011'
t2con EQU H'0012'
ccp1con EQU H'0017'
ccpr2l EQU H'001B'
ccpr2h EQU H'001C'
ccp2con EQU H'001D'
adcon0 EQU H'001F'
trisa EQU H'0085'
trisb EQU H'0086'
trisc EQU H'0087'
pr2 EQU H'0092'
adcon1 EQU H'009F'
ccpr1l EQU H'0015'
pie2 EQU H'008D'
adresh EQU H'001E'
;----- STATUS Bits --------------------------------------------------------
rp0 EQU H'0005'
;----- ADCON0 Bits --------------------------------------------------------
go EQU H'0002'
org 0 ;Reset Vector
goto init
org 4 ;Interrupt Vector
goto int
init
; Port initialization
bsf status,rp0 ;Change to Bank1
movlw b'00000001' ;AN0 to input mode
movwf trisa ;Set TRISA register
clrf trisb ;Set TRISB to uotput mode
clrf trisc ;Set TRISC to output mode
bcf status,rp0 ;Change to Bank0
;A/D converter initialization
movlw b'10000001'
movwf adcon0
bsf status,rp0
movlw b'00001110'
movwf adcon1
bcf status,rp0
;*** PWM initialization
clrf tmr2
movlw b'11111111'
movwf ccpr1l
bsf status,rp0
movlw d'255' ;Period=1638.4usec(610Hz)
movwf pr2
bcf status,rp0
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
clrf tmr1l
movlw h'61' ;H'61A8'=25000
movwf ccpr2h
movlw h'A8'
movwf ccpr2l
movlw b'00000001'
movwf t1con
movlw b'00001011'
movwf ccp2con
;Interruption control
clrf pir1
bsf status,rp0
movlw b'01000000'
movwf pie1
bcf status,rp0
movlw b'11000000'
movwf intcon
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
movfw adresh ;Read ADRESH register
i am stuck here! pls help...
hope someone can help me with the speed detection part..
#include<p16f876A.inc>
__config _hs_osc & _wdt_off & _pwrte_on & _lvp_off
; Register Definitions
_wdt_off EQU H'3FFB'
_pwrte_on EQU H'3FF7'
_hs_osc EQU H'3FFE'
_lvp_off EQU H'3F7F'
w EQU H'0000'
f EQU H'0001'
;----- Register Files------------------------------------------------------
status EQU H'0003'
portb EQU H'0006'
intcon EQU H'000B'
pir1 EQU H'000C'
pie1 EQU H'008C'
pir2 EQU H'000D'
tmr1l EQU H'000E'
tmr1h EQU H'000F'
t1con EQU H'0010'
tmr2 EQU H'0011'
t2con EQU H'0012'
ccp1con EQU H'0017'
ccpr2l EQU H'001B'
ccpr2h EQU H'001C'
ccp2con EQU H'001D'
adcon0 EQU H'001F'
trisa EQU H'0085'
trisb EQU H'0086'
trisc EQU H'0087'
pr2 EQU H'0092'
adcon1 EQU H'009F'
ccpr1l EQU H'0015'
pie2 EQU H'008D'
adresh EQU H'001E'
;----- STATUS Bits --------------------------------------------------------
rp0 EQU H'0005'
;----- ADCON0 Bits --------------------------------------------------------
go EQU H'0002'
org 0 ;Reset Vector
goto init
org 4 ;Interrupt Vector
goto int
init
; Port initialization
bsf status,rp0 ;Change to Bank1
movlw b'00000001' ;AN0 to input mode
movwf trisa ;Set TRISA register
clrf trisb ;Set TRISB to uotput mode
clrf trisc ;Set TRISC to output mode
bcf status,rp0 ;Change to Bank0
;A/D converter initialization
movlw b'10000001'
movwf adcon0
bsf status,rp0
movlw b'00001110'
movwf adcon1
bcf status,rp0
;*** PWM initialization
clrf tmr2
movlw b'11111111'
movwf ccpr1l
bsf status,rp0
movlw d'255' ;Period=1638.4usec(610Hz)
movwf pr2
bcf status,rp0
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
clrf tmr1l
movlw h'61' ;H'61A8'=25000
movwf ccpr2h
movlw h'A8'
movwf ccpr2l
movlw b'00000001'
movwf t1con
movlw b'00001011'
movwf ccp2con
;Interruption control
clrf pir1
bsf status,rp0
movlw b'01000000'
movwf pie1
bcf status,rp0
movlw b'11000000'
movwf intcon
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
movfw adresh ;Read ADRESH register
i am stuck here! pls help...