STC89S52单片机定时器T2捕获

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/****************************************************************************** ***************
说明:定时器2捕获功能的使用。

由于只有三个三个数码管,帮只显示捕获的低八位,经验证,当P1.1(T2EX)
用定时器0控制发出一个下降沿,用T2的P1^1;引脚进行捕获,从而得到高电平时间用以测量波形的宽度
跳变时,数据被捕获
/****************************************************************************** ***************/
/****************************************************************************** ****************/
#include<reg52.h> //常用的头文件,52单片机包含定时器2
#include <intrins.h> //51基本运算(包括_nop_空函数)
#define uchar unsigned char
#define ulong unsigned long
#define uint unsigned int
sbit STC_WR=P3^6;
sbit IC138_C=P2^7;
sbit IC138_B=P2^6;
sbit IC138_A=P2^5;
sbit p10=P1^0; //有定时器0控制从P2.0口输出一个信号
bit flag=0;
#define Port_Data P0
uint tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar count=0; //注,使用扩展RAM时一定要注意初始化
uchar temp[]={0,0,0,0,0,0,0,0}; //用于赋捕获值
/****************************************************************************** ****************/
void delay(uint z) //ms级延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/****************************************************************************** ****************/
void BUZZ_Y5C()
{
STC_WR=0;
IC138_C=1;
IC138_B=0;
IC138_A=1;
}
void we_Y6C()
{
STC_WR=0;
IC138_C=1;
IC138_B=1;
IC138_A=0;
}
void du_Y7C()
{
STC_WR=0;
IC138_C=1;
IC138_B=1;
IC138_A=1;
}
void up_we_Y6C()
{
STC_WR=1;
IC138_C=1;
IC138_B=1;
IC138_A=0;
}
void up_du_Y7C()
{
STC_WR=1;
IC138_C=1;
IC138_B=1;
IC138_A=1;
}
void display() //显示程序{
du_Y7C();
Port_Data=tab[temp[0]];
up_du_Y7C();
we_Y6C();
Port_Data=0x01;
up_we_Y6C();
delay(1);
du_Y7C();
Port_Data=tab[temp[1]]; up_du_Y7C();
we_Y6C();
Port_Data=0x02;
up_we_Y6C();
delay(1);
du_Y7C();
Port_Data=tab[temp[2]]; up_du_Y7C();
we_Y6C();
Port_Data=0x04;
up_we_Y6C();
delay(1);
du_Y7C();
Port_Data=tab[temp[3]]; up_du_Y7C();
we_Y6C();
Port_Data=0x08;
up_we_Y6C();
delay(1);
du_Y7C();
Port_Data=tab[temp[4]]; up_du_Y7C();
we_Y6C();
Port_Data=0x10;
up_we_Y6C();
delay(1);
du_Y7C();
Port_Data=tab[temp[5]];
up_du_Y7C();
we_Y6C();
Port_Data=0x20;
up_we_Y6C();
delay(1);
du_Y7C();
Port_Data=tab[temp[6]];
up_du_Y7C();
we_Y6C();
Port_Data=0x40;
up_we_Y6C();
delay(1);
du_Y7C();
Port_Data=tab[temp[7]];
up_du_Y7C();
we_Y6C();
Port_Data=0x80;
up_we_Y6C();
delay(1);
}
void gaibian()
{
uint x;
x=RCAP2H*256+RCAP2L;
temp[7]=x%10;
x=x/10;
temp[6]=x%10;
x=x/10;
temp[5]=x%10;
x=x/10;
temp[4]=x%10;
x=x/10;
temp[3]=x%10;
x=x/10;
temp[2]=x%10;
x=x/10;
temp[1]=x%10;
temp[0]=x/10;
RCAP2H=0;
RCAP2L=0;
TH2=0;
TL2=0;
flag=0;
}
/****************************************************************************** ****************/
void time2_init(void) //捕获模式,当EXEN2=1,外部T2EX为负跳变时,则将定时器中的值捕获到RCAP2L和RCAP2H
{
TH2=0; //初值为0,不断自加,当溢出时中断,或者外部T2EX由1到0时产生中断
TL2=0;
RCAP2L=0;
RCAP2H=0; // 允许T2定时器中断
T2CON=0x09; //必须将T2CON.7位置1才能进行外部捕获/或者外部T2EX由1到0时产生中断
}
void time_init()
{
TMOD=0X12;
TH0=256-12;
TL0=256-12;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
ET1=1;
ET2=1;
TR0=1;
TR1=1;
TR2=1;
}
/****************************************************************************** ****************/
void main()
{
time2_init();
time_init();
BUZZ_Y5C();
Port_Data=0X00;
while(1)
{
if(flag==1)
gaibian();
display(); //只用来显示低八位
}
}
void timer2(void) interrupt 5
{
if(TF2)
{
TF2=0; //若定时器溢出则产生中断清0
RCAP2H=0;
RCAP2L=0;
TH2=0;
TL2=0;
}
else
{
TR0=0;
TR2=0;
flag=1;
EXF2=0; //若外部负跳变时产生中断清0
}
}
void timer0() interrupt 1
{
p10=~p10;
}
void timer1() interrupt 3
{
static uchar x=0;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
x++;
if(x==20)
{
TR2=1;
TR0=1;
x=0;
RCAP2H=0;
RCAP2L=0;
TH2=0;
TL2=0;
}
}。

相关文档
最新文档