Using CCP on 18f series Interupt problem

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
28,617
I am using 18f2221 with 32khz clk on T1OSC.
I want to prevent an interrupt from a pulse occurring on CCP1 (RC2) input until a ready input PB sw (RA0) is read.
I have turned off all interrupts (that I can think of!) and set the switch to read a low.
The problem is I cannot get the code to wait for the switch?
The interrupt appears to be occurring regardless the state of the switch that allow the interupts-on code?
the relevant code is here.
Non prioritized Interrupts.
Rich (BB code):
           ORG 0x0000
ResetCode:
           bra Start 
           ORG 0x0008
HighInt:
           bra    Intserv
     
Start:         
         bcf        INTCON, GIE    ;disable global interrupts 
         bcf        INTCON, PEIE    ;disable peripheral interrupts 
         bcf        PIE1, 0    ;disable timer 1 interrupts 
         bcf        PIE1, CCP1IE    ;disable CCP1 interrupts 
         bcf        PIR1, CCP1IE    ;clear CCP1 interrupt flag 
         clrf    flags 
         clrf    CCP1CON        ;CCP1 module off 
         movlw    b'00001010' ;tmr1 prescaler and TMR1 setup, 
         movwf    T1CON        ;   TMR1 off 
         clrf    TMR1H    ;clear timer 1 high 
         clrf    TMR1L    ;clear timer 1 low 
         movlw    b'00011111' ;port A inputs 
         movwf    TRISA 
         movlw    0x00 
         movwf    TRISB    ;Port B outputs. 
         clrf    PORTB    ;Clear port B 
         bsf        TRISC,    CCP1  ;CCP1 pin CCP input     
 WaitSW1: 
         btfsc    PORTA,  0    ;wait for ready P.B. 
         bra        WaitSW1    ; (0x00) resets/turns off CCP. 
         movlw    b'00000101' ;capture on every rising edge 
         movwf    CCP1CON        ; CCP1 module on 
         bsf        INTCON, GIE    ;enable global interrupts 
         bsf        INTCON, PEIE    ;enable peripheral interrupts 
         bsf        PIE1, TMR1IE        ;enable Timer1 O.F. interrupts 
         bsf        PIE1, CCP1IE    ;Enable capture int 
         bsf        T1CON, TMR1ON    ;Turn TMR1 on     
 etc etc
So basically the problem is how to prevent the CCP1 interrupt from happening.
Everything tried so far does not seem to work.
Ideas welcome!
Max.
 

JohnInTX

Joined Jun 26, 2012
4,787
You did not initialize ADCON1 so RA0 is an analog input and will read 0 always, making it look like the switch is pressed.

Also, be sure to clear any interrupt flags i.e. CCP1IF, TMR1IF before enabling their interrupts to clear out previous captures, timeouts etc.

Finally in the interrupt service routine, if you will be dissabling/enabling interrupts while running, be sure to check the interrupt IE as well as IF before servicing so that you don't service a 'disabled' interrupt. (The interrupt flag e.g. CCP1IF is active even if the interrupt is disabled.)

Rich (BB code):
 btfss TMR1IE
 bra no_tmr1
 btfss TRM1IF
 bra no_tmr1
 call serviceTMR1
no_tmr1:
 ...
BTW, there can be problems using r-m-w on TRISC i.e. bsf TRISC,x. Load W and use MOVWF. This is especially valid if you want to use I2C.

Have fun.
 
Last edited:
Top