main.c(这次模拟题无其他驱动函数)
#include <REGX52.H>
#include <intrins.H>
#include <stdio.H>
#define u8 unsigned char
#define u16 unsigned int
#define LED(X) {hc573(0);P0 = X;hc573(4);hc573(0);}
#define BUZ(X) {hc573(0);P0 = X;hc573(5);hc573(0);}
#define COM(X) {hc573(0);P0 = X;hc573(6);hc573(0);}
#define SMG(X) {hc573(0);P0 = X;hc573(7);hc573(0);}
sfr P4 = 0xc0;
sfr AUXR = 0x8e;
sfr T2H=0xD6;
sfr T2L=0xD7;
sfr IE2=0xaf;
sbit R1 = P4^4;
sbit R2 = P4^2;
sbit C1 = P3^0;
sbit C2 = P3^1;
sbit C3 = P3^2;
sbit TX = P1^0;
sbit RX = P1^1;
code u8 Seg_Table[] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xc1};
u16 cs=30,flag_s,flag_L3,s;
u8 flag_key,flag_tab,a,flag_led,tab[8]={10},ui,flag_taskkey,keystatus,keyvalue,dat,led=0xff;
//================================函数声明区=============================
void Delay12us(); //@12.000MHz
void hc573(u8 channel);
void Timer2_Init(void);
void display();
char putchar(char c);
void SendByte(u8 date);
//======================================================================
void Delay12us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 33;
while (--i);
}
void hc573(u8 channel)
{
switch(channel)
{
case 4: P2 = (P2&0x1f)|0x80; break;
case 5: P2 = (P2&0x1f)|0xa0; break;
case 6: P2 = (P2&0x1f)|0xc0; break;
case 7: P2 = (P2&0x1f)|0xe0; break;
case 0: P2 = (P2&0x1f)|0x00; break;
}
}
void Timer2_Isr(void) interrupt 12 //定时器2用作参数定时
{
display();
if(flag_key<20) ++flag_key;
if(flag_tab<20) ++flag_tab;
if(flag_taskkey<20) ++flag_taskkey;
if(flag_s<500) ++flag_s;
if(flag_led<30) ++flag_led;
if(flag_L3<200) ++flag_L3;
}
void KeyScan()
{
if(flag_key<20) return;
flag_key = 0;
TR1 = 0;
switch(keystatus)
{
case 0:
R1=0;R2=C1=C2=C3=1;
if(C1==0||C2==0||C3==0)
{
keystatus = 1;
return;
}
R2=0;R1=C2=C3=C1=1;
if(C1==0||C2==0)
{
keystatus = 2;
return;
}
break;
case 1:
if(C1 == 0) keyvalue = 7;
if(C2 == 0) keyvalue = 6;
if(C3 == 0) keyvalue = 5;
keystatus = 3;
break;
case 2:
if(C1 == 0) keyvalue = 11;
if(C2 == 0) keyvalue = 10;
keystatus = 3;
break;
case 3:
if(C1==1&&C2==1&&C3==1)
keystatus = 0;
break;
}
TR1 = 1;
P3 = 0xff;
}
void taskKey()
{
switch(keyvalue)
{
case 7: ui = (ui+1)%2;keyvalue=0;break;
case 11:if(ui == 0)cs=s;keyvalue=0;break;
case 6:if(ui==1)cs+=10;keyvalue=0;break;
case 10:if(cs>0&&ui==1) cs-=10;keyvalue=0;break;
case 5:ES=1;printf("Distance:%ucm",s);ES=0;keyvalue=0;break;
default :keyvalue=0;break;
}
}
void Settab()
{
if(flag_tab<20) return;
flag_tab = 0;
switch(ui)
{
case 0:
tab[0] = 11;
tab[1] = 1;
tab[2] = 10;
tab[3] = 10;
tab[4] = 10;
tab[5] = s/100;
tab[6] = s%100/10;
tab[7] = s%10;
break;
case 1:
tab[0] = 11;
tab[1] = 2;
tab[2] = 10;
tab[3] = 10;
tab[4] = 10;
tab[5] = cs/100;
tab[6] = cs%100/10;
tab[7] = cs%10;
break;
}
}
void display()
{
static u8 i;
SMG(0xff);
COM(0x01<<i);
SMG(Seg_Table[tab[i]]);
i = (i+1)%8;
}
void Timer2_Init(void)//定时器2用作参数定时
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初始值
T2H = 0xFC; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
IE2 |= 0x04; //使能定时器2中断
EA = 1;
}
void SendWave()
{
u8 i;
for(i=0;i<=7;i++)
{
TX = 1;
Delay12us();
TX = 0;
Delay12us();
}
}
void Dis_measure()
{
if(flag_s<500) return;
flag_s = 0;
TMOD &= 0xF0; //定时器0用作测距
TL0 = 0x00; //设置定时初始值
TH0 = 0x00; //设置定时初始值
TF0 = 0; //清除TF1标志
TR0 = 0;
SendWave();
TR0 = 1;
while(RX==1&&TF0==0);
TR0 = 0;
if(TF0==1)
{
s = 255;
}
if(RX==0)
{
s = (TH0<<8|TL0)*0.017;
}
}
void UartInit(void) //定时器1用作串口
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xC7; //设置定时初始值
TH1 = 0xFE; //设置定时初始值
ET1 = 0; //禁止定时器中断
TR1 = 1; //定时器1开始计时
}
void Receive() interrupt 4
{
if(RI == 1)
{
RI = 0;
dat = SBUF;
}
}
void SendByte(u8 date)
{
SBUF = date;
while(TI == 0);
TI = 0;
}
char putchar(char c)
{
SendByte(c);
return c;
}
void LED_control()
{
if(flag_led<30) return;
flag_led=0;
if(ui == 0) led&=~1;
else led|=1;
if(ui == 1) led&=~2;
else led|=2;
LED(led)
}
void L3()
{
if(flag_L3<200) return;
else flag_L3=0;
if(s>cs)
led^=4;
else led |= 4;
LED(led);
}
void main()
{
BUZ(0x00);
LED(0xff);
Timer2_Init();
UartInit();
while(1)
{
Settab();
taskKey();
KeyScan();
Dis_measure();
LED_control();
L3();
}
}