|
#include <REGX52.H>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit SH_CP1 = P0^1;
sbit DS = P0^0;
sbit ST_CP1 = P0^2;
int distL,distR;
unsigned char code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
sbit TrigL = P2^4; // 超声波模块的Trig管脚
sbit EchoL = P2^5; // 超声波模块的Echo管脚
sbit TrigR = P2^6; // 超声波模块的Trig管脚
sbit EchoR = P2^7; // 超声波模块的Echo管脚
//============MAIN===============================
void Delay(unsigned int xms) //延时
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
void send_data_74hc595(uint data_74hc595) //传送数据给74hc595
{
uchar a;
for(a = 0; a < 8; ++a)
{
DS = (data_74hc595 << a) & 0x80; //从高位开始传输
SH_CP1 = 0;
_nop_();
_nop_();
SH_CP1 = 1;
}
}
void show_74hc595() //数据显示
{
ST_CP1 = 0;
_nop_();
_nop_();
ST_CP1 = 1;
}
void Nixie()
{
switch(distR)
{
case 1:send_data_74hc595(table[1]);show_74hc595();break;
case 2:send_data_74hc595(table[2]);show_74hc595();break;
case 3:send_data_74hc595(table[3]);show_74hc595();break;
case 4:send_data_74hc595(table[4]);show_74hc595();break;
case 5:send_data_74hc595(table[5]);show_74hc595();break;
case 6:send_data_74hc595(table[6]);show_74hc595();break;
case 7:send_data_74hc595(table[7]);show_74hc595();break;
case 8:send_data_74hc595(table[8]);show_74hc595();break;
case 9:send_data_74hc595(table[9]);show_74hc595();break;
}
switch(distL)
{
case 1:send_data_74hc595(table[1]);show_74hc595();break;
case 2:send_data_74hc595(table[2]);show_74hc595();break;
case 3:send_data_74hc595(table[3]);show_74hc595();break;
case 4:send_data_74hc595(table[4]);show_74hc595();break;
case 5:send_data_74hc595(table[5]);show_74hc595();break;
case 6:send_data_74hc595(table[6]);show_74hc595();break;
case 7:send_data_74hc595(table[7]);show_74hc595();break;
case 8:send_data_74hc595(table[8]);show_74hc595();break;
case 9:send_data_74hc595(table[9]);show_74hc595();break;
} }
//模块程序
uint GetDistanceL(void)
{
uint s;
TH0 = 0;
TL0 = 0;
TrigL=1; //800MS 启动一次模块
Delay(0.001);
TrigL=0;
while(!EchoL); //当RX为零时等待
TR0=1; //开启计数
while(EchoL); //当RX为1计数并等待
TR0=0; //关闭计数
//timeL=TH0*256+TL0;
// timeL *= 12/11.0592;
//s=(timeL*1.7)/100; //算出来是CM
s=((TH0*256+TL0)*0.034)/2;
return s;
}
uint GetDistanceR(void)
{
uint s;
TH0 = 0;
TL0 = 0;
TrigR=1; //800MS 启动一次模块
Delay(0.001);
TrigR=0;
while(!EchoR); //当RX为零时等待
TR0=1; //开启计数
while(EchoR); //当RX为1计数并等待
TR0=0; //关闭计数
s=((TH0*256+TL0)*0.034)/2;
return s;
}
void main()
{
TMOD = 0x02;
ET0=0;
EA =1; // 选择定时器0,并且确定是工作方式1(为了超声波模块测量距离计时用的)
TrigL=0; // 初始化触发引脚为低电平
TrigR=0;
while(1)
{
//===========Ultrasonic measurement===========
//left
distL = GetDistanceL();
//right
distR = GetDistanceR();
Nixie();
}//end of while
}//end of main
|
|