#include "STC12C2052AD.H"
#include "intrins.h"
sbit LED = P2^1; //测试LED
sbit LED2= P2^2; //接LED观察
unsigned int t0=1000; //定义16位变量t,定时1ms
unsigned int t1=3000; //定义16位变量t,定时3ms
unsigned int value0;
unsigned int value1;
void PCA_isr() interrupt 6
{
if(CCF0==1)
{
CCF0 = 0; //清中断,因PCA计数溢出中断已禁止,所以这里不必再清CF
CCAP0L = value0; //更新比较值
CCAP0H = value0>>8;
value0 += t0; //累加定时间隔值
LED =! LED; //LED取反
}
if (CCF1==1)
{
CCF1 = 0; //清中断,因PCA计数溢出中断已禁止,所以这里不必再清CF
CCAP1L = value1; //更新比较值
CCAP1H = value1>>8;
value1 += t1; //累加定时间隔值
LED2 =! LED2; //LED2取反
}
}
void main()
{
CCON=0;
CL=0;
CH=0;
value0 = t0;
CCAP0L = t0; //更新比较值
CCAP0H = t0>>8;
value0 += t0; //累加定时间隔值
value1 = t1; //如改成value1=3500,进中断后还是按照value1=3000,则可让波形2延时0.5ms,如下页波形图
CCAP1L = t1; //更新比较值
CCAP1H = t1>>8;
value1 += t1; //累加定时间隔值
CMOD = 0x00; //模式:FSOC/12、关ECF允许PCA中断
EPCA_LVD = 1; //打开PCA总中断
CCAPM0 = 0x49; //模块0设置16位定时器,并产生模块0的中断信息CCF0
CCAPM1 = 0x49; //模块1相同设置
EA=1;
CR=1;
while(1);
} |