;********************************************************************* ; * ; Tutorial 15 PWM Analysis * ; * ;********************************************************************* ;* TARGET PIC16F877 ;********************************************************************* ; Uses the two capture and compare modules to capture and ; analyse a PWM signal. The 16 bit values for period and ; duty are displayed on PortsB,D. Which is displayed is ; selected by toggling PIN C7, where HIGH displays duty, LOW ; displays period. The prescalor is also set using C.. ; ;********************************************************************* ;* ;* @version 0.1, 24 September 2001 ;* @author James Caska ;********************************************************************* list p=16F877 #include DUTY_L EQU .32 DUTY_H EQU .33 FLAGS EQU .34 DUTYSAVED EQU .0 ORG .0 GOTO INIT ;********************************************************************** ;* InterruptVector ;********************************************************************** ;* Two interrupts occur each cycle, one for each CPP module ; Period Interrupt occurs at the end of the period on the rising edge ;********************************************************************** ORG .4 InterruptVector BTFSS PIR2,CCP2IF GOTO DisplayPeriodOrDuty SaveDutyForDisplayAtEndOfPeriod BTFSC FLAGS,DUTYSAVED GOTO DisplayPeriodOrDuty MOVF CCPR2L,W MOVWF DUTY_L MOVF CCPR2H,W MOVWF DUTY_H BSF FLAGS,DUTYSAVED BTFSC PORTC,5 GOTO DisplayPeriodOrDuty RLF DUTY_L,F ;Duty can only be read 1:1, scale * 4 if 1:4 RLF DUTY_H,F BCF DUTY_L,0 RLF DUTY_L,F ;Duty can only be read 1:1, scale * 4 if 1:4 RLF DUTY_H,F BCF DUTY_L,0 BTFSC PORTC,6 GOTO DisplayPeriodOrDuty RLF DUTY_L,F ;Duty can only be read 1:1, scale * 4 if 1:4 RLF DUTY_H,F BCF DUTY_L,0 RLF DUTY_L,F ;Duty can only be read 1:1, scale * 4 if 1:4 RLF DUTY_H,F BCF DUTY_L,0 DisplayPeriodOrDuty BTFSS PIR1,CCP1IF GOTO Display.End BTFSS PORTC,7 GOTO DisplayDuty MOVF CCPR1L,W MOVWF PORTB MOVF CCPR1H,W MOVWF PORTD GOTO Display.ClearIfPeriod DisplayDuty MOVF DUTY_L,W MOVWF PORTB MOVF DUTY_H,W MOVWF PORTD BCF FLAGS,DUTYSAVED Display.ClearIfPeriod BTFSS PIR1,CCP1IF GOTO Display.End CLRF TMR1L CLRF TMR1H Display.End BCF PIR1,CCP1IF BCF PIR2,CCP2IF RETFIE INIT ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;======Begin PORT Configuration BSF STATUS, RP0 ;PORT TRIS Registers are on page 1 ;======PORT B MOVLW .0 ;PORTB Configuration Bits MOVWF TRISB ;======PORT C MOVLW .255 ;PORTC Configuration Bits MOVWF TRISC ;======PORT D MOVLW .0 ;PORTd Configuration Bits MOVWF TRISD BCF STATUS, RP0 ;Restore Page0 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;======Timer1 Timer Mode, uses Internal clock BCF T1CON, TMR1CS ;TMR1CS 0 = Internal clock (FOSC/4) ;======Prescalor Configuration BCF T1CON, T1CKPS0 ;1:1 BCF T1CON, T1CKPS1 ;======Turn on Timer1 BSF T1CON, TMR1ON ;TMR1ON 1 = Enables Timer1 ;========================================== ;Autogenerated configuration code PIC16F877 ;Config Wiz V0.1, www.virtualbreadboard.com ;========================================== ;====== CONFIGURE CCP1 CLRF CCP1CON ;Turn CCP module off MOVLW B'00000111' ;Capture mode, every 16th rising edge BTFSC PORTC,6 MOVLW B'00000110' ;Capture mode, every 4th rising edge BTFSC PORTC,5 MOVLW B'00000101' ;0101 Capture mode, every rising edge MOVWF CCP1CON ;======CCP1 Interrupt BCF PIR1,CCP1IF ;Ensure CCP1IF InterruptFlag Clear BSF STATUS,RP0 ;Access Bank1 BSF PIE1,CCP1IE ;Enable Timer1 Interrupts on Page1 BCF STATUS,RP0 ;Restore Bank0 ;====== CONFIGURE CCP2 CLRF CCP2CON ;Turn CCP module off MOVLW B'00000100' ;0100 Capture mode, every falling edge MOVWF CCP2CON ;======CCP2 Interrupt BCF PIR2,CCP2IF ;Ensure CCP1IF InterruptFlag Clear BSF STATUS,RP0 ;Access Bank1 BSF PIE2,CCP2IE ;Enable CCP2 Interrupts on Page1 BCF STATUS,RP0 ;Restore Bank0 BSF INTCON, PEIE ;Enabled Peripheral Interrupts ;Enable Peripheral Interrupts BSF INTCON,GIE ;Turn on Global Interrupt MAIN GOTO MAIN END