#include <p18f2480.h>
void VectorHigh (void); void HandlerHigh (void);
#pragma code VectorHigh = 0x08 void VectorHigh (void) { _asm goto HandlerHigh //jump to interrupt routine _endasm } #pragma code
#pragma interrupt HandlerHigh void HandlerHigh () { //定时器3中断----------------------------------
if(PIR1bits.CCP1IF) { PIR1bits.CCP1IF = 0; //一定要在中断里面赋值 //比较软件中断,要将定时器3的初值清0 //比较特殊事件触发,定时器自动重装 // TMR3H=0; // TMR3L=0; LATBbits.LATB1 = !LATBbits.LATB1; } } #pragma code
void main() { //初始化定时器3,Compare用----------------------------------------
TRISCbits.TRISC2= 0; //设定RC2为输出 LATCbits.LATC2 = 0; //CCP1引脚置低位 TRISB = 0;//PB.0-.7口作输出
CCP1CON = 0x0b;//CCP1比较模式, //0x02电平翻转,0x0a软件中断,0x0b特殊事件触发
INTCONbits.GIE = 1;//全局中断打开 INTCONbits.PEIE=1;//外围设备中断打开
PIE2bits.TMR3IE=1;//定时器3中断打开 PIR2bits.TMR3IF=0;//定时器3溢出标志 IPR2bits.TMR3IP = 1;//定时器3溢出中断优先级为高中断
CCPR1H=0x03;//高8位初值 CCPR1L=0xe8;//低8位初值
PIE1bits.CCP1IE=1;//CCP1中断打开 PIR1bits.CCP1IF=0;//CCP1溢出标志 IPR1bits.CCP1IP = 1;//CCP1溢出中断优先级为高中断
T3CONbits.RD16 = 0;//关闭定时器3的16位读写 T3CONbits.T3ECCP1 = 1;//定时器3是CCP1的时钟源 T3CONbits.T3CKPS1 = 0;//分频1:1 T3CONbits.T3CKPS0 = 0; T3CONbits.T3CCP1 = 0;//定时器3是CCP1的时钟源 T3CONbits.TMR3CS = 0;//定时器3选择定时方式 T3CONbits.T3SYNC = 0;//无效 T3CONbits.TMR3ON = 1;//打开定时器3
for(;;);
}
|