Puzzing A-D PWM code.

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
28,702
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.


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:
 

AlbertHall

Joined Jun 4, 2014
12,347
The two LSBs of the PWM duty cycle are in CCP2CON <5:4>. Lines 50 to 53 move the appropriate bits from DutyCycle to CCP2CON.
Lines 54 to 57 shift those bits out of DutyCycle and place the remaining bits in CCPR2L.
Not included in the code you posted is the PR2 setting which I would expect to be 0x3F.
ADCON0,GO_DONE is set immediately after the ADC result is read to trigger the next reading. If it is known that the loop time is longer than the ADC conversion time then there is no need to check if the conversion has completed.
 

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
28,702
PR2 is 0x3f.
I will run it through the de-bugger again and see the result, I just don't know why not read all 12 bits and enter them direct in the PWM registers?
I ran it simulated and got weird results.
Max.
 

AlbertHall

Joined Jun 4, 2014
12,347
There woud still be some bit twiddling needed as the PWM is max 10 bits and the lower 2 of those 10 need to be in CCP2CON.
That program is using the PWM as only 8 bits and that may be so as to be able to use a suitable frequency. Lower PWM resolution allow higher PWM frequency.
 

AlbertHall

Joined Jun 4, 2014
12,347
The formulae in the datasheet will let you calculate a suitable timer prescale value and period (PR2). Then you can calculate what resolution you will get with those values.
 

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
28,702
One thing to watch with some of these programs is whether the same bits jive in the registers, for e.g. the pre-scaler bits may be different in some Micro's.
Max.
 

AlbertHall

Joined Jun 4, 2014
12,347
You need to check the values the program produces though that program says it covers the '917 (if that's the one you are going to use).
 

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
28,702
I just checked and compared the relevant registers and made any neccessary changes.
The basic numbers should be the same.
I don't quite see what the problem would be using the 12 A-D bit number directly into the PWM registers.
The two LSB's properly shifted of course.
Max.
 
Top