我在学习蓝桥杯单片机超声波测距模块时,发现我最多只能测到70cm左右,再大的话,数码管就显示231了,可是按我写的代码来说溢出的话应该显示999呀?而且测70cm就溢出了吗?好奇怪呀?
mian.c
#include<STC15F2K60S2.H>
#include"hardware.h"
#include"intrins.h"
sbit TX=P1^0;
sbit RX=P1^1;
unsigned int distance=0;
//------------------------------------------------------------------
void Delay14us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 39;
while (--i);
}
//------------------------------------------------------------------
void sendwave()//产生8个超声波信号,通过TX引脚发射出去
{
unsigned char i;
for(i=0;i<8;i++)
{
TX=1;
Delay14us();
TX=0;
Delay14us();
}
}
//-----------------------------------------------------------------
unsigned char csb_read()//超声波测距
{
unsigned int dis=0;
TH1=0x00;
TL1=0x00;//计数初值设为0
sendwave();//发送8个超声波
TR1=1;//开始计数
while((RX==1)&&(TF1==0));//当接收到超声波且计数未溢出时
TR1=0; //关闭计数
if(TF1==0)//未溢出
{
dis=(TH1<<8|TL1)*0.017+0.5;//计算距离
}
else//溢出
{
TF1=0;//溢出标志置0
dis=999;//距离为999(最大值)
}
return dis;
}
//------------------------------------------------------------------
void SMG_display()
{
SMG_buf[0]=32;
SMG_buf[1]=16;
SMG_buf[2]=16;
SMG_buf[3]=16;
SMG_buf[4]=16;
if(distance/100!=0)
{
SMG_buf[5]=distance/100;
SMG_buf[6]=distance%100/10;
SMG_buf[7]=distance%10;
}
else if(distance/10!=0)
{
SMG_buf[5]=16;
SMG_buf[6]=distance/10;
SMG_buf[7]=distance%10;
}
else
{
SMG_buf[5]=16;
SMG_buf[6]=16;
SMG_buf[7]=distance;
}
}
//------------------------------------------------------------------
void main()
{
led_output();
ws_output();
Timer0Init();
Timer1Init();
while(1)
{
SMG_display();
if(timer500ms>=125)
{
timer500ms=0;
distance=csb_read();
}
}
}
hardware.c
#include<STC15F2K60S2.H>
#include"hardware.h"
/************* 本地常量声明 **************/
unsigned char code t_display[]={ //标准字库
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y 超声波标志
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e, 0x3c,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
unsigned char code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
unsigned char led_buf=0xff;
unsigned char ws_buf=0x00;
unsigned char SMG_buf[]={16,16,16,16,16,16,16,16};
unsigned int timer10ms=0;
unsigned int timer10ms2=0;
unsigned int timer500ms=0;
unsigned char key_stat=0;
unsigned char key_value=0;
bit bz_5s=0;
bit bz_50ms=0;
//----------------------------------------------------------------------
void Timer0Init() //1毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
void Timer1Init() //@12.000MHz ////每次计数相当于1us
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TF1 = 0; //清除TF1标志
}
//---------------------------------------------------------------------
void SelectHC573(unsigned char 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 led_output()
{
P0=led_buf;
SelectHC573(4);
SelectHC573(0);
}
//---------------------------------------------------------------------
void ws_output()
{
P0=ws_buf;
SelectHC573(5);
SelectHC573(0);
}
//---------------------------------------------------------------------
void SMG_output()
{
static unsigned char SMG_com=0;
P0=0x00;
SelectHC573(6);
SelectHC573(0);
P0=T_COM[SMG_com];
SelectHC573(6);
SelectHC573(0);
P0=~t_display[SMG_buf[SMG_com]];
SelectHC573(7);
SelectHC573(0);
if(++SMG_com>=8)
{
SMG_com=0;
}
}
//---------------------------------------------------------------------
void ServiceTimer0()interrupt 1
{
SMG_output();
timer10ms++;
timer10ms2++;
timer500ms++;
}
//----------------------------------------------------------------------
void Scankey()
{
P3|=0x0f;
if(~P3&0x0f)
{
if(key_stat<255)
key_stat++;
if(P33==0)
key_value=4;
if(P32==0)
key_value=5;
if(P31==0)
key_value=6;
if(P30==0)
key_value=7;
}
else
{
key_stat=0;
key_value=0;
}
}