Microchip Technology DM164130-9 User Manual

Page of 101
Lessons
 2012 Microchip Technology Inc.
DS41628B-page 71
3.9.5
Assembly
3.9.5.1
ENHANCED MID-RANGE
EXAMPLE 3-34: 
This fills the eight MSbs in the PWM register. The next few lines can be commented out 
and still provide the same perceived output. This is because the two LSbs do not play 
a significant role in terms of duty cycle resolution. This lesson uses all ten bits for com-
pleteness. 
EXAMPLE 3-35: 
, the program shifts the ADRESL register, which contains the two LSbs 
from the ADC result. Bits <5:0> are always cleared while bits <7:6> contain part of the 
ADC result. The PIC MCU will simply shift this register to the right twice so that they are 
in bits <5:4>. Notice how the result of the shift is saved in ADRESL and NOT in WREG. 
In the next three instructions: the first XOR clears bits that are the same and sets bits 
that are different. The result is in WREG. The next AND function clears all control bits 
in WREG, so they do not change in the final step. The final XOR changes the bits that 
changed and leaves everything else untouched. The result is saved to the CCP2CON 
register. A movwf or iorwf would not work, since it would not preserve the settings 
applied in the initialization. 
3.9.5.2
PIC18
The PIC18 substitutes the rrncf instruction with the lsrf instruction above, although 
a rrcf would also work.
3.9.5.3
C LANGUAGE
Nothing new.
  call     A2d         ;begin the Analog to Digital conversion
                       ;ADRESH and ADRESL are now both full of the ADC result!
  movf     ADRESH, w   ;Get the top 8 MSbs (remember that the ADC result is LEFT justified!)
  banksel  CCPR2L
  movwf    CCPR2L
    ;to fill all 10 bits of the duty cycle, the 2 LSbs will be put into the 
    ;Duty Cycle Bits (DC2B) of the CCP2CON register which are bits 5 and 4.
    ;So we need to shift these LSb into place and OR them with CCP2CON
    ; in order to save the control settings above and fill these last bits in
    banksel     ADRESL
                             ;ADRESL =  b'xx000000' where 'xx' are the 2 LSbs from the 
                             ;ADC result
    lsrf        ADRESL, f    ;ADRESL = b'0xx00000'
    lsrf        ADRESL, f    ;ADRESL = b'00xx0000'
   
 movf        ADRESL, w    ;now move into wreg
    
banksel     CCP2CON
    
xorwf       CCP2CON, w   ;move the 2 LSbs into place without disturbing the rest of 
                             ;CCP2CON settings
    andlw       B'00110000'
    xorwf       CCP2CON, f
    bra         MainLoop     ;do this forever