程序:
#include
***it Dck=P1^0;
***it Led1=P1^1;
***it Led2=P1^2;
***it Seg1=P1^3;
***it Seg2=P1^4;
***it Seg3=P1^5;
***it Seg4=P1^6;
***it Mink=P1^7;
***it Led3=P3^1;
***it Add=P3^2;
***it Dec=P3^3;
#define Disdata P0
#define Dac P2
#define uchar unsigned char
#define uint unsigned int
#define MARK 4 //用于输出任意分频的pwm波
uchar code dis_7[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};// 共阳极段选码
vola
tile uchar data display[4]={0x00,0x00,0x00,0x00}; //
用于暂存数码管的输出数据流int Tnum=0, //定时器0定时次数的记录
Pnum=2; //用于和Tnum比较产生需要的pwm波
snum=0;
uint full=0; //用于记录定时器1的溢出次数
/*************延时程序************************/
void delay(uint t)
{
for(;t>0;t--);
}
/**************数码管显示*********************/
/***********pwm波定时发生*********************/
void Time0_srv() interrupt 1
{
snum++;
if(snum>40)
snum=0;
if(snum>0&&snum<=10)
{
Seg4=1;
Disdata=~dis_7[display[0]];
delay(30);
Seg1=0;
}
if(snum>10&&snum<=20)
{
Seg1=1;
Disdata=(~dis_7[display[1]])|0x80;
delay(30);
Seg2=0;
}
if(snum>20&&snum<=30)
{
Seg2=1;
Disdata=~dis_7[display[2]];
delay(30);
Seg3=0;
}
if(snum>30&&snum<=40)
{
Seg3=1;
Disdata=~dis_7[display[3]];
delay(30);
Seg4=0;
}
{
TH0=(65536-425)/256;
TL0=(65536-425)%256;
if(Tnum
{
Dck=1;
}
else
{
Dck=0;
}
Tnum=Tnum+1;
Tnum=Tnum%MARK;
}
}
/*****************按键程序1*****************/
void key1() interrupt 0
{
delay(4000);
if(Add==0 )
{
Pnum++;
if(Pnum>=MARK+1)
Pnum=MARK;
}
switch(Pnum)
{
case 0: Dac=0x00;break; //--
case 1: Dac=0x3f;break; //-
case 2: Dac=0x7f;break; //0
case 3: Dac=0xbf;break; //+
case 4: Dac=0xff;break;//++
}
}
/****************按键程序2******************/
void key2() interrupt 2
{
delay(4000);
if(Dec==0)
{
Pnum--;
Tnum=0;
if(Pnum<0)
Pnum=0;
}
switch(Pnum)
{
case 0: Dac=0x00;break; //--
case 1: Dac=0x3f;break; //-
case 2: Dac=0x7f;break; //0
case 3: Dac=0xbf;break; //+
case 4: Dac=0xff;break;//++
}
}
/**************测速程序1*******************/
void Time1_srv() interrupt 3
{
full++;
if(full==10000)
{
TR0=0;
full=0;
TH1=0;
TL1=0;
Led3=1;
}
}
/*************测速程序2*****************/
void Freeback()
{ static uchar dj=0;
float time=0.0;
uint rad=0;
if(Mink==0)
{
TR1=1;
dj++;
Led3=0;
}
while(!Mink);
if(dj==2)
{
TR1=0;
time=full*65.536+TH1*0.256+TL1*0.001;
full=0;
TH1=0;
TL1=0;
rad=(2*3.14/(time*0.001)+0.5)*10;
display[3]=rad/1000;
display[2]=rad%1000/100;
display[1]=rad%100/10;
display[0]=rad%10;
dj=0;
}
}
/***************模块初始化****************/
void initi()
{
TMOD=0X11;
EA=1;
ET0=1;
ET1=1;
PT0=1;
TR0=1;
Dck=1;
TH0=(65536-425)/256;
TL0=(65536-425)%256;
TH1=0;
TL1=0;
EX0=1;
IT0=1;
PX0=1;
EX1=1;
IT1=1;
PX1=1;
Mink=1;
Dac=0x7f;
}
/****************主函数*******************/
void main()
{
initi();
while(1)
{
Freeback();
}
}