完整的CCP1比较模式程序(MCC18-PIC18F2480)

[复制链接]
3482|1
 楼主| hahahaluo 发表于 2007-12-26 13:28 | 显示全部楼层 |阅读模式
#include&nbsp;&ltp18f2480.h&gt&nbsp;<br /><br />void&nbsp;VectorHigh&nbsp;(void);<br />void&nbsp;HandlerHigh&nbsp;(void);<br /><br />#pragma&nbsp;code&nbsp;VectorHigh&nbsp;=&nbsp;0x08<br />void&nbsp;VectorHigh&nbsp;(void)<br />{<br />&nbsp;&nbsp;_asm<br />&nbsp;&nbsp;&nbsp;&nbsp;goto&nbsp;HandlerHigh&nbsp;//jump&nbsp;to&nbsp;interrupt&nbsp;routine<br />&nbsp;&nbsp;_endasm<br />}<br />#pragma&nbsp;code<br /><br />#pragma&nbsp;interrupt&nbsp;HandlerHigh<br />void&nbsp;HandlerHigh&nbsp;()<br />{<br />//定时器3中断----------------------------------<br /><br />&nbsp;if(PIR1bits.CCP1IF)&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PIR1bits.CCP1IF&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//一定要在中断里面赋值<br />//比较软件中断,要将定时器3的初值清0<br />//比较特殊事件触发,定时器自动重装<br />//&nbsp;&nbsp;&nbsp;&nbsp;TMR3H=0;&nbsp;&nbsp;&nbsp;&nbsp;<br />//&nbsp;&nbsp;&nbsp;&nbsp;TMR3L=0;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;LATBbits.LATB1&nbsp;=&nbsp;!LATBbits.LATB1;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br />#pragma&nbsp;code<br /><br />void&nbsp;main()&nbsp;<br />{&nbsp;<br />//初始化定时器3,Compare用----------------------------------------<br /><br />TRISCbits.TRISC2=&nbsp;0;&nbsp;//设定RC2为输出<br />LATCbits.LATC2&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;//CCP1引脚置低位<br />TRISB&nbsp;=&nbsp;0;//PB.0-.7口作输出<br /><br />CCP1CON&nbsp;=&nbsp;0x0b;//CCP1比较模式,<br />&nbsp;&nbsp;&nbsp;&nbsp;//0x02电平翻转,0x0a软件中断,0x0b特殊事件触发<br /><br />INTCONbits.GIE&nbsp;=&nbsp;1;//全局中断打开<br />INTCONbits.PEIE=1;//外围设备中断打开<br /><br />PIE2bits.TMR3IE=1;//定时器3中断打开<br />PIR2bits.TMR3IF=0;//定时器3溢出标志<br />IPR2bits.TMR3IP&nbsp;=&nbsp;1;//定时器3溢出中断优先级为高中断<br /><br />CCPR1H=0x03;//高8位初值<br />CCPR1L=0xe8;//低8位初值<br /><br />PIE1bits.CCP1IE=1;//CCP1中断打开<br />PIR1bits.CCP1IF=0;//CCP1溢出标志<br />IPR1bits.CCP1IP&nbsp;=&nbsp;1;//CCP1溢出中断优先级为高中断<br /><br />T3CONbits.RD16&nbsp;=&nbsp;0;//关闭定时器3的16位读写<br />T3CONbits.T3ECCP1&nbsp;=&nbsp;1;//定时器3是CCP1的时钟源<br />T3CONbits.T3CKPS1&nbsp;=&nbsp;0;//分频1:1<br />T3CONbits.T3CKPS0&nbsp;=&nbsp;0;<br />T3CONbits.T3CCP1&nbsp;=&nbsp;0;//定时器3是CCP1的时钟源<br />T3CONbits.TMR3CS&nbsp;=&nbsp;0;//定时器3选择定时方式<br />T3CONbits.T3SYNC&nbsp;=&nbsp;0;//无效<br />T3CONbits.TMR3ON&nbsp;=&nbsp;1;//打开定时器3<br /><br />for(;;);<br /><br />}&nbsp;<br />
jantw996 发表于 2010-6-22 15:15 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

41

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部