$ifndef _HPWM10_INC_ $define _HPWM10_INC_ ' ' Note that these macros use FSR0L/H and INDF0 in order to achieve bankless operation ' ' Configure CCP1 as PWM with a resolution of 10-bits (0 - 1023) ' ' Turn on Timer2 with a Prescaler value of 1:1 ' Set PWM resolution to 10-bits ' Reset the CCPR1L register ' Turn on PWM Module 1 by setting bits 2 and 3 of CCP1CON ' Enable the PWM1 output pin $define OpenAnalog1() ' T2CON = %00000100 ' PR2 = 255 ' CCPR1L = 0 ' CCP1CON = %00001100 ' Output PORTC.2 ' ' Configure CCP2 as PWM with a resolution of 10-bits (0 - 1023) ' Turn on Timer2 with a Prescaler value of 1:1 ' Set PWM resolution to 10-bits ' Reset the CCPR2L register ' Turn on PWM Module 2 by setting bits 2 and 3 of CCP2CON ' Enable the PWM2 output pin ' $define OpenAnalog2() ' T2CON = %00000100 ' PR2 = 255 ' CCPR2L = 0 ' CCP2CON = %00001100 ' Output PORTC.1 '--------------------------------------------------------- $define WriteAnalog1(pDutyCycle) _mWriteAnalog1 pDutyCycle _mWriteAnalog1 Macro pDutyCycle #if(Prm_1 == Byte) Lfsr 0,(pDutyCycle) Movlw 207 ' CCP1CON = CCP1CON & %11001111 Andwf CCP1CON,f Swapf INDF0,w ' WREG = pDutyCycle << 4 Andlw 48 ' WREG = WREG & %00110000 Iorwf CCP1CON,f ' CCP1CON = CCP1CON | WREG Bcf STATUS,0 ' CCPR1L = pDutyCycle >> 2 Rrcf INDF0,w Bcf STATUS,0 Rrcf WREG,f Movwf CCPR1L Exitm #endif #if(Prm_1 == Word) || (Prm_1 == Dword) Lfsr 0,(pDutyCycle) Movlw 207 ' CCP1CON = CCP1CON & %11001111 Andwf CCP1CON,f Swapf POSTINC0,w ' WREG = pDutyCycle << 4 Andlw 48 ' WREG = WREG & %00110000 Iorwf CCP1CON,f ' CCP1CON = CCP1CON | WREG Rrcf POSTDEC0,w ' CCPR1L = pDutyCycle >> 2 Movwf PRODL,0 Rrcf INDF0,w Movwf CCPR1L Rrcf PRODL,f Rrcf CCPR1L,f Exitm #endif #if(Prm_1 == Num8) || (Prm_1 == Num16) || (Prm_1 == Num32) Movlw 207 Andwf CCP1CON,f Movlw ((pDutyCycle << 4) & 48) Iorwf CCP1CON,f Movlw (pDutyCycle >> 2) Movwf CCPR1L Exitm #endif Endm '--------------------------------------------------------- $define WriteAnalog2(pDutyCycle) _mWriteAnalog2 pDutyCycle _mWriteAnalog2 Macro pDutyCycle #if(Prm_1 == Byte) Lfsr 0,(pDutyCycle) Movlw 207 ' CCP2CON = CCP2CON & %11001111 Andwf CCP2CON,f Swapf INDF0,w ' WREG = pDutyCycle << 4 Andlw 48 ' WREG = WREG & %00110000 Iorwf CCP2CON,f ' CCP2CON = CCP2CON | WREG Bcf STATUS,0 ' CCPR2L = pDutyCycle >> 2 Rrcf INDF0,w Bcf STATUS,0 Rrcf WREG,f Movwf CCPR2L Exitm #endif #if(Prm_1 == Word) || (Prm_1 == Dword) Lfsr 0,(pDutyCycle) Movlw 207 ' CCP2CON = CCP2CON & %11001111 Andwf CCP2CON,f Swapf POSTINC0,w ' WREG = pDutyCycle << 4 Andlw 48 ' WREG = WREG & %00110000 Iorwf CCP2CON,f ' CCP2CON = CCP2CON | WREG Rrcf POSTDEC0,w ' CCPR2L = pDutyCycle >> 2 Movwf PRODL,0 Rrcf INDF0,w Movwf CCPR2L Rrcf PRODL,f Rrcf CCPR2L,f Exitm #endif #if(Prm_1 == Num8) || (Prm_1 == Num16) || (Prm_1 == Num32) Movlw 207 Andwf CCP2CON,f Movlw ((pDutyCycle << 4) & 48) Iorwf CCP2CON,f Movlw (pDutyCycle >> 2) Movwf CCPR2L Exitm #endif Endm '--------------------------------------------------------- ' Turn off PWM Module 1 by clearing CCP1CON ' Disable the PWM1 output pin $define CloseAnalog1() CCP1CON = 0 : Input PORTC.2 '--------------------------------------------------------- ' Turn off PWM Module 2 by clearing CCP1CON ' Disable the PWM2 output pin $define CloseAnalog2() CCP2CON = 0 : Input PORTC.1 $endif