;*** PIC18F452 Program to measure period of an incoming waveform ;*** Source is connected to CCP1/RC2 pin ;*** Use CCP1 module to capture incoming waveform using Timer1 as clk source to CCP1 module ; Capture 2 consecutive rising pulses & record timer values at those times ; Subtract the values to get the duration measured in # of clock cycles MAIN BSF TRISC,CCP1 ;CCP1 (RC2) PIN AS INPUT MOVLW B'10000001' ;Timer1 as clk for CCP1 (T3CON<6,3>=00), timer3 on in 16-bit mode MOVWF T3CON MOVLW B'10000001' ;enable Timer1 in 16-bit mode, set prescaler to 1, use instruction clk cycle MOVWF T1CON MOVLW B'00000101' ;Set CCP1 to capture mode, capture on every rising edge MOVWF CCP1CON CALL PULSE ;record timer reading when 1st pulse arrives MOVFF CCPR1L,0X0 ;save timer value in mem addresses 0x0 & 0x1 when 1st pulse arrives MOVFF CCPR1H,0X1 CALL PULSE ;record timer reading when 2nd pulse arrives CLRF CCP1CON ;disable more captures MOVFF CCPR1L,0x2 ;save timer value in mem addresses 0x2 & 0x3 when 2nd pulse arrives MOVFF CCPR1H,0X3 CALL RESULT ;compute period measured in clk cycles HERE BRA HERE PULSE BCF PIR1,CCP1IF ;clear timer int flag CHECK BTFSS PIR1,CCP1IF ;rising edge arrived? (bit test, skip if set) BRA CHECK ;if not loop back RETURN RESULT MOVF 0X0,W ;1st-pulse low byte reading to W SUBWF 0X2,F ;subtract W from 2nd-pulse low byte reading, save in mem address 0x2 MOVF 0X1,W ;2nd-pulse high byte reading to W SUBWFB 0x3,1 ;subtract W from 2nd-pulse high byte reading, save in mem address 0x3