超声波测距程序,数码管显示乱码是怎么回事?
时间:10-02
整理:3721RD
点击:
各位大大,这是我用的超声波测距程序,问题是数码管显示乱码,数码管的程序没问题,从逻辑上又找不到是哪里错误,恳请指出。
#include <stc12c5a60s2.h>
#include <intrins.h>
#define ulong4 unsigned long // 宏定义
#define uChar8 unsigned char
#define uInt16 unsigned int
uInt16 time = 0;
uInt16 timer = 0;
ulong4 S = 0;
uChar8 flag = 0;
sbit ECHO = P1^1; // 外部中断0 接收端
sbit TRIG = P1^0; // 发射端
sbit led0 =P2^4; // 数码管
sbit led1 =P2^5;
sbit led2 =P2^6;
sbit led3 =P2^7;
uChar8 discode[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x3f,0x06,0x5b};
uChar8 LedBuff[4]={ 0x00, 0x00, 0x00, 0x00};
void Timer_ISR() // 定时器初始化
{
TMOD = 0x11; //超声波就用定时器1
TH0=(65536-100)/256; //0.1mS定时
TL0=(65536-100)%256;
TR0 = 1;
ET0 = 1;
TR1 = 0;
ET1 = 1;
EA = 1;
}
void Display(void) //扫描数码管
{
static unsigned char i = 1;
P0 = 0x00;
switch(i)
{
case 0: led3 = 0; led1 = 1; i++; P0=LedBuff[0];break;
case 1: led2 = 0; led3 = 1; i++; P0=LedBuff[1];break;
case 2: led1 = 0; led2 = 1; i=0; P0=LedBuff[2];break;
default:break;
}
}
void Delay20us() //@12.000MHz
{
unsigned char i;
i = 57;
while (--i);
}
void StartModule() // 启动测距信号
{
TRIG=1; // 给发送端一个启动信号
Delay20us();
TRIG=0;
}
void Conut(void) //计算超声波测距的距离
{
while(!ECHO); //当RX为零时等待
TR1 = 1; //开启计数
while(ECHO); //当RX为1计数并等待
TR1 = 0;
time=TH1*256+TL1; //读取脉宽长度
TH1=0;
TL1=0;
S =(time*1.7)/100; //算出来是CM
if((S>=420)) //超出测量范围显示“F”
{
flag=0;
S=0;
LedBuff[0] = discode[15];
LedBuff[1] = discode[15];
LedBuff[2] = discode[15];
}
else
{
LedBuff[0] = discode[S%10];
LedBuff[1] = discode[S/10%10];
LedBuff[2] = discode[S/100%10];
}
}
void main(void)
{
Timer_ISR();
while(1)
{
if(timer == 2000)
{
timer = 0;
StartModule();
Conut(); //计算距离
}
}
}
void timer0()interrupt 1
{
TH0=(65536-100)/256; //S定时
TL0=(65536-100)%256;
Display();
timer++;
}
void time1()interrupt 3
{
flag=1;
}
#include <stc12c5a60s2.h>
#include <intrins.h>
#define ulong4 unsigned long // 宏定义
#define uChar8 unsigned char
#define uInt16 unsigned int
uInt16 time = 0;
uInt16 timer = 0;
ulong4 S = 0;
uChar8 flag = 0;
sbit ECHO = P1^1; // 外部中断0 接收端
sbit TRIG = P1^0; // 发射端
sbit led0 =P2^4; // 数码管
sbit led1 =P2^5;
sbit led2 =P2^6;
sbit led3 =P2^7;
uChar8 discode[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x3f,0x06,0x5b};
uChar8 LedBuff[4]={ 0x00, 0x00, 0x00, 0x00};
void Timer_ISR() // 定时器初始化
{
TMOD = 0x11; //超声波就用定时器1
TH0=(65536-100)/256; //0.1mS定时
TL0=(65536-100)%256;
TR0 = 1;
ET0 = 1;
TR1 = 0;
ET1 = 1;
EA = 1;
}
void Display(void) //扫描数码管
{
static unsigned char i = 1;
P0 = 0x00;
switch(i)
{
case 0: led3 = 0; led1 = 1; i++; P0=LedBuff[0];break;
case 1: led2 = 0; led3 = 1; i++; P0=LedBuff[1];break;
case 2: led1 = 0; led2 = 1; i=0; P0=LedBuff[2];break;
default:break;
}
}
void Delay20us() //@12.000MHz
{
unsigned char i;
i = 57;
while (--i);
}
void StartModule() // 启动测距信号
{
TRIG=1; // 给发送端一个启动信号
Delay20us();
TRIG=0;
}
void Conut(void) //计算超声波测距的距离
{
while(!ECHO); //当RX为零时等待
TR1 = 1; //开启计数
while(ECHO); //当RX为1计数并等待
TR1 = 0;
time=TH1*256+TL1; //读取脉宽长度
TH1=0;
TL1=0;
S =(time*1.7)/100; //算出来是CM
if((S>=420)) //超出测量范围显示“F”
{
flag=0;
S=0;
LedBuff[0] = discode[15];
LedBuff[1] = discode[15];
LedBuff[2] = discode[15];
}
else
{
LedBuff[0] = discode[S%10];
LedBuff[1] = discode[S/10%10];
LedBuff[2] = discode[S/100%10];
}
}
void main(void)
{
Timer_ISR();
while(1)
{
if(timer == 2000)
{
timer = 0;
StartModule();
Conut(); //计算距离
}
}
}
void timer0()interrupt 1
{
TH0=(65536-100)/256; //S定时
TL0=(65536-100)%256;
Display();
timer++;
}
void time1()interrupt 3
{
flag=1;
}
如果数码管能正确显示任何你想显示的数字,那就是显示没问题,是数据格式转换的问题,
{
LedBuff[0] = discode[S%10];
LedBuff[1] = discode[S/10%10];
LedBuff[2] = discode[S/100%10];
}
你可以给S任意数字,看是否能正确显示
沙发说的对,能显示就说明程序是没问题的,显示乱码首先看数据转换是否写错或不全;再就是数码管的引脚是否连接正确!