本帖最后由 terrydkt12 于 2011-7-1 12:23 编辑
小弟最近写了一个程序,用到了3个定时器
T0用于计算测试P3^2口的高电平脉宽
T1用于定时跳入中断,执行串口数据发送
T2作为串口的数据传输率发生器
可是全速运行后发现
T0无法跳入中断。。。
#include <reg52.h>
#include <stdio.h>
#define length 16
#define L 1
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar inbuf[length];
uint count;
uint count1;
uchar xx;
ulong sum;
bit TimeOn0 = 0;
sbit P3_2=P3^2;
void init_interrupt(void);
void delay(void);
void send_char(uchar);
void send_string(uchar *s,uchar,uchar);
void data_Process(void);
void wait(void);
void main(void)
{
uchar inbuf[length]={0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f};
init_interrupt();//串口及寄存器初始化
xx = 0;
count = 0;
count1 = 0;
while(1)
{
wait();
}
}
/*------------------------------------------------
函数
------------------------------------------------*/
void wait(void)
{
while(P3_2);//循环至INT0输出高电平
if(P3_2 == 0)
{ if(!TimeOn0)
{ TH0 = 0x00;//初值为0000 0000
TL0 = 0x00;
TR0 = 1;//定时器0打开
TimeOn0 = 1;
}
}
while(P3_2 == 0);//循环至INT0输出低电平
if(P3_2 == 1)
{ if(TimeOn0)
{ TR0 = 0;//定时器0关闭
TimeOn0 = 0;
}
}
TR0 = 0;
data_Process();
count = 0;
}
/*------------------------------------------------
延时函数
------------------------------------------------*/
void delay(void)
{ uchar i;
for(i=0x00;i>0xff;i++);
}
/*------------------------------------------------
串口、中断初始化设定
------------------------------------------------*/
void init_interrupt(void)
{
SCON |= 0x50;//SCOM=0101 0000,方式1,允许接收数据
TMOD |= 0x11;//TMOD=0001 0001,定时器1,模式1,16位定时器;定时器0,模式1,16位定时器
PCON |= 0x80;//SMOD=1,表示数据传输率加倍
T2CON |= 0x30;//定时器2的设置
//TH2 = 0xff;//11.0592MHz,4800
//TL2 = 0xf4;
RCAP2H = 0x00;//T2的装填值
RCAP2L = 0x00;
TH1 = 0xFE;//400us定时器
TL1 = 0x8F;
TH0 = 0x00;//初值为0000 0000
TL0 = 0x00;
TR0 = 0;//停止T0
IE |= 0x9F;//1001 1111,EA = 1,ES=1,定时器允许中断允许,外部中断允许
//IT0 = 1;//下降沿触发 外部中断0
TR1 = 1;//开启定时器1
TR2 = 1;//开启定时器2
}
/*------------------------------------------------
向串口发送字节程序
------------------------------------------------*/
void send_char(uchar x)
{
SBUF = x;
while(TI == 0);
TI = 0;
}
void send_string(uchar *s,uchar string_length,uchar number)
{
unsigned int i=0;
do
{
send_char(*(s+number));
i++;
}
while (i<string_length);
}
/*------------------------------------------------
“定时器0”溢出处理程序
------------------------------------------------*/
void timer0() interrupt 1
{
TH0 = 0x00;//定时器0赋初值
TL0 = 0x00;
count++;//定时器0溢出次数统计
if(count > 100)
{
count = 0;
}
}
/*------------------------------------------------
“定时器1”处理程序
------------------------------------------------*/
void timer1() interrupt 3
{
TH1 = 0xFE;//定时器1赋初值
TL1 = 0x8F;
count1++;
if(count1 > 10000)
{ count1 = 0;
}
send_string(inbuf,L,xx);
}
/*------------------------------------------------
data_Process数据处理
------------------------------------------------*/
void data_Process(void)
{
ulong sum1 = 0;
uint sum2 = 0;
uchar sum3 = 0;
// ulong sum = 0;
sum1 = 65536*count;
sum2 = 256*TH0;
sum3 = TL0;
sum = sum1+sum2+sum3;
xx = sum / 1000;
}
求大侠指导~ |