assemblypwmpic18

PWM on a PIC18 using CCP4


I'm having trouble setting up PWM on my PIC18F45K22. I can get it working on CCP1 using timer2 but I would like to use CCP4 and timer4 as I plan to use CCP1 and timer2 elsewhere.

I am coding in assembly (mpasm v5.87). I am not sure why it won't on CCP4 with timer4, even when the same code works for CCP1 with timer2. Below is my code so hopefully a PIC genius can spot my error.

    ; Set oscillator speed at 4 MHz
    bsf     OSCCON, IRCF0
    bcf     OSCCON, IRCF1
    bsf     OSCCON, IRCF2

    ; Initialize Port D
    clrf    PORTD
    clrf    LATD
    bsf     TRISD, CCP4

    ; Set up Timer 4
    clrf    T4CON
    clrf    TMR4

    ; Select timer4 for PWM
    bcf     CCPTMRS1, C4TSEL1
    bsf     CCPTMRS1, C4TSEL0

    ; Setup period of PWM
    movlw   .10
    movwf   PR4

    ; Configure CCP4CON for PWM
    ; CCP4M<3:0> = 11XX for PWM
    movlw   b'00001100'
    movwf   CCP4CON

    ; Set duty cycle at 50%
    movlw   .5
    movwf   CCPR4L

    ; Clear timer4 interrupt flag
    bcf     PIR5, TMR4IF

    ; Start timer4
    bsf     T4CON, TMR4ON

    ; RD1 as output
    bcf     TRISD, CCP4

Solution

  • I have resolved the issue.

    I realised that T4CON, TMR4, CCPTMRS1, PR4 and CCPR4L are not in the access bank while the corresponding registers for timer2 and CCP1 are in the access bank.

    All I needed to do was select bank 15 with MOVLB 0x0F