I have the Pic Mechatronics and also the BLDC development boards that includes code with several examples.
Looking over the Project5 which is a simple pot control of a DC motor with single output PWM.
The manual states that 8 A-D bits are sufficient for this control.
I am puzzled as to what is actually intended for the SetMotorSpeed: routine which just captures the 8MSB of the A-D, one, the ADCON0 GO_DONE bit is not tested for completion.
But the bit shuffling of the result as shown in the short set routine has me stumped.
Why not just read the 12bit A-D and use it for the PWM registers?
The routines appears to work as intended, so I am missing something.
I have trimmed the whole program down to the relevant routine.
Looking over the Project5 which is a simple pot control of a DC motor with single output PWM.
The manual states that 8 A-D bits are sufficient for this control.
I am puzzled as to what is actually intended for the SetMotorSpeed: routine which just captures the 8MSB of the A-D, one, the ADCON0 GO_DONE bit is not tested for completion.
But the bit shuffling of the result as shown in the short set routine has me stumped.
Why not just read the 12bit A-D and use it for the PWM registers?
The routines appears to work as intended, so I am missing something.
I have trimmed the whole program down to the relevant routine.
Code:
;
; 16F917.lkr
; using 16F917 Pic
;************************************************************************************
;
; Notes: This project controls the speed of a brushed DC motor. POT1 is used to
; set the speed of the motor. The optical interrupter provides speed
; feedback. This feedback is fed into the T1CKL pin and the speed of the
; motor is measured. The speed is then displayed on the LCD as RPM / 10^3.
;
;**********************************************************************
STARTUP code
goto Initialize
; Finish setting up A-to-D Module
movlw b'00000001' ; left justified, AN0 selected, module on
movwf ADCON0
; Configure Capture Compare PWM Module 2
clrf CCPR2L
movlw b'00001100' ; pwm mode
movwf CCP2CON
; Turn on Timer 2
bsf T2CON,TMR2ON ; turn on PWM
;************************************************************************************
; Main - Every 23.4ms POT1 is measured and moved into the dutycycle registers for
; CCP2. CCP2 drives the brushed dc motor, thereby controlling the speed. Timer 1
; is clocked by the Optical Interrupter. Every 3/4 of a second the value in Timer 1
; is converted to RPM and displayed as: RPM / 10^3.
;
;************************************************************************************
Main:
;
call SetMotorSpeed
call MeasureSpeed
btfsc STATUS,C
call DisplaySpeed
goto Loop
SetMotorSpeed:
movf ADRESH, W
movwf DutyCycle
bsf ADCON0,GO_DONE
bsf P1 ; Turn on motor
swapf DutyCycle, W ; bits 4 and 5 of CCP2CON
andlw 0x30
iorlw 0x8D
movwf CCP2CON
rrf DutyCycle, F ; move most significant 6 bits of duty cycle into
rrf DutyCycle, W ; CCPR2L
andlw 0x3F
movwf CCPR2L
return
MeasureSpeed: