Microcontroller: PIC 16F1829
Language: MPASM
I am using the CCP2 module to create a servo pulse. The interrupt is called at 20-ms intervals by TMR2.
This code works. Once there is a match between the CCPR2L/H registers, which are set before the interrupt, the CCP2IF is set and the CCP2 pin is driven low. CCP2IF need to be cleared by software. An interrupt is not enabled for CCP2IF.
I cannot find in the datasheet or in this example (Example 14-4, https://ww1.microchip.com/downloads/en/DeviceDoc/31014a.pdf )any description for how long the pin is driven low by the match, whether the module must be reset after each compare, nor the proper sequence for resetting the module (i.e, clrf CCP2CON) and clearing the flag (CCP2IF). I have tried various ways to get that information. For example, if I try to depend on CCP2IF to keep the pin low and not clear the flag until the next TMR2 interrupt, it doesn't work. It seems logical to me that resetting the module (i.e., clrf CCP2CON) should be done before clearing the flag, but either sequence works, even when separated by a few steps.
What is the proper sequence and, if there is a proper sequence, why does it matter? Does CCP2CON need to be reset for each compare?
Regards, John
Language: MPASM
I am using the CCP2 module to create a servo pulse. The interrupt is called at 20-ms intervals by TMR2.
Code:
;******************************************************************************
; INTERRUPT SERVCE ROUTINE
;******************************************************************************
ORG 0x0004
nop
movlb 5 ; |B5
movlw b'00001001' ;set ccp pin high until match |B5
movwf CCP2CON ; |B5
movlb 0 ;MC example sets TMR1 after CCP2CON |B0
bsf T1CON,TMR1ON ; |B0
btfss PIR2,CCP2IF ; |B0
bra $-1 ;wait for match, which sends CCP output low |B0
clrf PIR1 ;TMR1IF,TMR2IF |B0
; clrf PIR2 ;CCP2IF |B0
bcf T1CON,TMR1ON ; |B0
movlb 5 ;CCP2 registers |B5
clrf CCP2CON ;force CCP2 output low |B5
movlb 0 ; |B0
clrf PIR2 ;CCP2IF |B0
clrf TMR1L ; |B0
clrf TMR1H ; |B0
bsf Flag0,0 ; |B0
retfie
I cannot find in the datasheet or in this example (Example 14-4, https://ww1.microchip.com/downloads/en/DeviceDoc/31014a.pdf )any description for how long the pin is driven low by the match, whether the module must be reset after each compare, nor the proper sequence for resetting the module (i.e, clrf CCP2CON) and clearing the flag (CCP2IF). I have tried various ways to get that information. For example, if I try to depend on CCP2IF to keep the pin low and not clear the flag until the next TMR2 interrupt, it doesn't work. It seems logical to me that resetting the module (i.e., clrf CCP2CON) should be done before clearing the flag, but either sequence works, even when separated by a few steps.
What is the proper sequence and, if there is a proper sequence, why does it matter? Does CCP2CON need to be reset for each compare?
Regards, John