实验二 COMPARE 与PWM
一、试验目的:
1、 学习CCP功能模块的原理与应用 2、 进一步学习中断的概念及其编程 3、 应用CCP模块解决实际问题
二、试验参考程序
INCLUDE ORG GOTO ORG BCF BTFSC GOTO RETFIE START: CALL MAIN_LOOP:
NOP GOTO PIC_INIT:
CLRF BSF MOVLW MOVWF CLRF BSF BSF BCF CLRF MOVLW MOVWF MOVLW LED红灯亮 MOVWF \"P16f877a.inc\" 0x00 START 0x04 STATUS,RP0 PIR2,CCP2IF CCP_ISR PIC_INIT MAIN_LOOP INTCON STATUS, RP0 B'11111001' TRISC PIE2 PIE2,CCP2IE PIE1,CCP1IE STATUS, RP0 PORTC B'00110001' T1CON B'00001000' CCP2CON ; ; Test CCP2 Interrupt ; Goto CCP Interrup~ Service Routine ; ; Enable CCP2 Interrupt ; Enable CCP2 Interrupt ;比较方式,匹配时输出为1,双色; Compare Mode, Outpuu 1 as
; Choose Bankl ; Port C direction ; Interrupt MOVLW B'00001001' ;比较方式,匹配时输出为0双色LED绿灯灭 MOVWF CCP1CON ; Compare Mode, Outpuu 0 as Interrupt
MOVLW 0xF4 MOVWF CCPR2H MOVWF CCPR1H MOVLW 0x24 CCPR2H:CCPR2L=0xF424=62,500 MOVWF CCPR2L MOVWF CCPR1L BSF INTCON,PEIE BSF INTCON,GIE RETURN
CCP_ISR:
BCF PIR2,CCP2IF MOVLW B'00000001' XORWF CCP2CON,F 为0 BCF PIR1,CCP1IF Interruput Flag
MOVLW B'00000001' XORWF CCP1CON,F 为1 RETFIE END
; ; 62,500x8us=0.5s ; 62,500x8us=0.5s ; Enable Peripheral Interrupn ; Enable Global Interrupn ; Clear CCP2 Interruput Flag Toggle Bit0 of CCP2CON ; 1001=比较方式,匹配时输出 ; Clear CCP2 ; Toggle Bit0 of CCP1CON ; 1000=比较方式,匹配时输出 ;
三、试验参考电路:
12345678L1_KDVDDVCCC81R81A2KY14.000MHZC82R8330U813MCLR12345673334353637383940OSC1/CLKINMCLR/VPPRA0RA1RA2RA3RA4/T0CKIRA5/SSRB0/INTRB1RB2RB3RB4RB5RB6RB7RE0/RD//AN5RE1/WR/AN6PIC16F877ATitleOSC2/CLKOUTRC0/T1OSO/T1CKIRC1/T1OSI/CCP2RC2/CCP1RC3/SCK/SCLRC4/SDI/SDARC5/SDORC6/TX/CKRC7/RX/DTRD0/PSP0RD1/PSP1RD2/PSP2RD3/PSP3RD4/PSP4RD5/PSP5RD6/PSP6RD7/PSP7RE2/CS/AN7141516RC117RC2182324252619202122272829301030R84A__CL2K双色LEDDCVCC R31 R32R33 R34B R35 R36VCC R42TMR0123CVCCR43RC2CAPTUREBSizeAA2Date:File:123NumberRevisionA21-Apr-2009Sheet of E:\\lwh\\shiyandianluban\\实验电路板分功能块图Drawn By:.Ddb45678
四、扩展练习
请修改程序实现CAPTURE 或PWM功能,电路不需要修改.