|
这是我们实习的一个作业,我组员发烧,就只有我一个在做 做不出来,明天就要交了
作业要求:51单片机,串口输出或者数码管显示距离,距离过近报警
#include <REGX51.H>
#include "stdio.h"
#include "UART.h"
sbit TRIG=P3^6;
sbit ECHO=P3^7;
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x0c,0x89,0xbf};
unsigned int sec,Time,meter;
unsigned char num;
unsigned char a;
void Delay10us()
{
unsigned char i;
i = 2;
while (--i);
}
void Delay(unsigned int xms)
{
unsigned int i,j;
for(i=0;i<xms;i++)
{
for(j=0;j<120;j++);
}
}
void Time_Init()
{
PCON &= 0x7F;
SCON = 0x50;
TMOD=0x21;
TH1=TL1=0xFD;
TR1=1;
EA=1;
ES=1;
}
void multiple()
{
TRIG=0;
TRIG=1;
Delay10us();
TRIG=0;
while(!ECHO);
TR0=1;
while(ECHO);
Time=(TH0<<8)|TL0;
TR0=0;
TL0=TH0=0;
meter=(Time*34)/2000;
}
void display()
{
printf("METER=%d\r\n",meter);
}
void main()
{
Time_Init();
while(1)
{
multiple();
display();
if(meter<2)
{
P0_7=0;
Delay(500);
P0_7=1;
}
}
}
void ser() interrupt 4 //当接收到消息的时候RI=1;
{
RI=0;//接收到数据后,将RI置0
meter=SBUF;//接收数据
num=1;//成功接收标志位
} |
|