单片机源程序如下:- #include <REGX52.H>
- sbit SH_CP1 = P0^1;
- sbit DS = P0^0;
- sbit ST_CP1 = P0^2;
- int distL,distR;
- unsigned char code table[]={
- 0x06,0x5B,0x4F,0x66,0x6D,
- 0x7D,0x07,0x7F,0x6F,0x3F};
- sbit TrigL = P2^4; // 超声波模块的Trig管脚
- sbit EchoL = P2^5; // 超声波模块的Echo管脚
- sbit TrigR = P2^6; // 超声波模块的Trig管脚
- sbit EchoR = P2^7; // 超声波模块的Echo管脚
- void Delay(unsigned int xms)
- {
- unsigned char i, j;
- while(xms--)
- {
- i = 2;
- j = 199;
- do
- {
- while (--j);
- } while (--i);
- }
- }
- void send_data_74hc595(int data_74hc595) //传送数据给74hc595
- {
- char a;
- SH_CP1 = 0;
- for(a = 0; a < 8; ++a)
- {
- DS = (data_74hc595 << a) & 0x80; //从高位开始传输
- SH_CP1 = 1;
- SH_CP1 = 0;
- }
-
-
- }
- void show_74hc595()
- //数据显示
- {
- ST_CP1 = 0;
- 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;
- case 10:send_data_74hc595(table[0]);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;
- case 10:send_data_74hc595(table[0]);show_74hc595();break;
-
- }
- }
- //模块程序
- void GetDistanceL()
- {
-
- TH0 = 0;
- TL0 = 0;
- TrigL=0;
- TrigL=1;
- Delay(0.001);
- TrigL=0;
- while(!EchoL);
- TR0=1; //开启计数
- while(EchoL);
- TR0=0; //关闭计数
- //timeL=TH0*256+TL0;
- // timeL *= 12/11.0592;
- //s=(timeL*1.7)/100; //算出来是CM
- distL=((TH0*256+TL0)*0.034)/2;
-
- }
- void GetDistanceR()
- {
- TH0 = 0;
- TL0 = 0;
- TrigR=0;
- TrigR=1;
- Delay(0.001);
- TrigR=0;
- while(!EchoR);
- TR0=1;
- while(EchoR);
- TR0=0;
- //timeR=TH0*256+TL0;
- //s=(timeR*1.7)/100; //算出来是CM
- distR=((TH0*256+TL0)*0.034)/2;
- }
- void set()
- {
- TMOD=0x21;
- TH0=0;
- TL0=0;
- ET0=1;
- EA=1;
- }
- void main()
- {
- while(1)
- {
- set();
- GetDistanceL();
- GetDistanceR();
- Nixie();
- }
- }
复制代码
|