标题:
关于单片机超声波控制数码管程序 改变距离数码管无反应
[打印本页]
作者:
1888888
时间:
2022-3-24 13:31
标题:
关于单片机超声波控制数码管程序 改变距离数码管无反应
51hei截图20220324132517.png
(64.8 KB, 下载次数: 30)
下载附件
2022-3-24 13:30 上传
单片机源程序如下:
#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();
}
}
复制代码
作者:
1888888
时间:
2022-3-24 13:32
超声波改变距离数码管无反应,求大佬们支个招
作者:
lkc8210
时间:
2022-3-25 00:41
1. Delay(0.01) = Delay(0), Delay(X) X只可以是0~65535的正整数
2. TrigL=1;到TrigL=0;之间要最少要有10us延时,弄个i=3;while(i--);就够了(unsigned int i)
3. 由于Trig启动不成功,程序在while(!Echo)那里卡死了
4. Nixie();只接受0~10, 也有可能是distL和dist大于10所以没反应
5. switch(distR%11)switch(distL%11)加个取余看看
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1