我把程序发上来,给位帮忙看一下,看哪里能改一下!
#include <reg52.H>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define LED_DAT P1
#define TIME_CYLC 100 //12M晶振,定时器10ms 中断一次 1秒计算一次转速 // 1000ms/10ms = 100
#define PLUS_PER 100 //码盘的齿数 ,这里假定码盘上有100个齿,即传感器检测到10个脉冲,认为1圈
#define K 10 //校准系数
sbit LED_SEG0 = P0^3;
sbit LED_SEG1 = P0^2;
sbit LED_SEG2 = P0^1;
sbit LED_SEG3 = P0^0;
sbit pin_SpeedSenser = P3^5; //光电传感器信号接在T1上
code uchar table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar data Disbuf[4];
uint Tcounter = 0; //时间计数器
uint speed = 0;
bit Flag_Fresh = 0; // 刷新标志
bit Flag_clac = 0; //计算转速标志
bit Flag_Err = 0; //超量程标志
void DisplayFresh(); //在数码管上显示一个四位数
void ClacSpeed(); //计算转速,并把结果放入数码管缓冲区
void init_timer0(); //初始化定时器T0
void init_timer1(); //初始化定时器T1
void Delay(uint ms); //延时函数
void Delay(uint ms) //延时函数
{
uchar i;
while(ms--)
for(i=0;i<100;i++);
}
void DisplayFresh() //在数码管上显示一个四位数
{
uchar i;
for(i = 0;i < 4;i++)
{
P2 |= 0xf0;
switch(i)
{
case 0: LED_SEG0 = 0;LED_SEG1 = 1;LED_SEG2 = 1;LED_SEG3 = 1; break;
case 1: LED_SEG0 = 1;LED_SEG1 = 0;LED_SEG2 = 1;LED_SEG3 = 1; break;
case 2: LED_SEG0 = 1;LED_SEG1 = 1;LED_SEG2 = 0;LED_SEG3 = 1; break;
case 3: LED_SEG0 = 1;LED_SEG1 = 1;LED_SEG2 = 1;LED_SEG3 = 0; break;
}
LED_DAT = table[Disbuf[i]];
Delay(2);
}
}
void ClacSpeed() //计算转速,并把结果放入数码管缓冲区
{
uint PlusCounter;
PlusCounter = TH1*256 + TL1;
speed = K*(PlusCounter/PLUS_PER)/60; //K是校准系数,如速度不准,调节K的大小
Disbuf[0] = (speed/1000)%10;
Disbuf[1] = (speed/100)%10;
Disbuf[2] = (speed/10)%10;
Disbuf[3] = speed%10;
}
void init_timer0() //初始化定时器T0
{
TMOD &= 0xf0; //定时10毫秒
TMOD |= 0x01;
TH0 = 0xC0;
TL0 = 0x00;
ET0=1;
EA=1;
TR0=1;
}
void init_timer1() //初始化计数器T1
{
TMOD &= 0x0F;
TMOD |= 0x50;
TH1 = 0x00;
TL1 = 0x00;
ET1 = 1;
EA = 1;
TR1 = 1;
}
void main(void)
{
Disbuf[0] = 0;
Disbuf[1] = 0;
Disbuf[2] = 0;
Disbuf[3] = 0;
init_timer0();
init_timer1();
while(1)
{
if(Flag_Fresh == 1)
{
Flag_Fresh = 0;
DisplayFresh(); // 定时刷新数码管显示
}
if(Flag_clac == 1)
{
Flag_clac = 0;
ClacSpeed(); //计算转速,并把结果放入数码管缓冲区
Tcounter = 0; //周期定时 清零
TH1 = TL1 = 0x00; //脉冲计数清零
}
if(Flag_Err == 1) //超量程处理
{
uchar i;
for(i = 0;i < 4;i++)
{
P2 |= 0xF0;
LED_DAT = table[9];
}
while(1)
{
DisplayFresh();//不再测速 等待复位i
}
}
}
}
void it_timer0() interrupt 1
{
TF0 = 0; //定时器 T0用于数码管的动态刷新
TH0 = 0xC0;
TL0 = 0x00;
Flag_Fresh = 1;
Tcounter++;
if(Tcounter > TIME_CYLC)
{
Flag_clac = 1; //周期到,该重新计算转速了
}
}
void it_timer1() interrupt 3
{
TF1 = 0; //定时器T1用于单位时间内收到的脉冲数
TH1 = 0x00;
TL1 = 0x00;
Flag_Err = 1;
}
|