刚学没多久51,想使用两个74HC595组成的8位一体数码管显示超声波测距的结果,但是代码有些问题不知道怎么解决,代码应该是在Getdistance函数卡住了,不过相同的测距函数我放在LCD1602上就可以正常显示,把Getdistance注释掉单独测试数码管显示函数也是正常的,真的很迷惑,希望有大佬能帮忙看一下,不胜感激
#include <AT89X51.H>
#include <stdlib.h>
#include <intrins.h>
int m,h,f,d,p;
float Distance;
sbit DIO = P1^0;
sbit RCLK = P1^1;
sbit SCLK = P1^2;
unsigned char LED[8];
sbit Trig = P2^6;
sbit Echo = P2^5;
void Delayms(int ms)
{
unsigned char i, j;
int k;
for(k=0;k<ms;k++){
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
void Timer0_Init()
{
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = 0;
TL0 = 0;
ET0 = 1;
TR0 = 0;
}
void Delay10us(void)
{
unsigned char i;
_nop_();
i = 2;
while (--i);
}
float GetDistance()
{
unsigned long timer_count;
float distance;
Trig = 1;
Delay10us();
Trig = 0;
while(!Echo);
TR0 = 1;
while(Echo);
TR0 = 0;
timer_count = TH0;
timer_count <<= 8;
timer_count |= TL0;
distance = timer_count * 1.7/100;
TH0 = 0;
TL0 = 0;
return distance;
}
void LED_display(float value)
{
int temp_int=(int)value;
int a;
float temp_f=value-temp_int;
LED[6]=temp_int/100;
LED[5]=temp_int%100/10;
LED[4]=temp_int%10;
a=temp_f*100;
LED[3]=a/10;
LED[2]=a%10;
}
void LED_OUT(unsigned char X)
{
unsigned char i;
for(i=8;i>=1;i--)
{
if (X&0x80) DIO=1; else DIO=0;
X<<=1;
SCLK = 0;
SCLK = 1;
}
}
unsigned char code LED_0F[] =
{
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90
};
void LED4_Display (void)
{
unsigned char code *led_table;
unsigned char i;
led_table = LED_0F + LED[0];
i = *led_table;
LED_OUT(i);
LED_OUT(0x01);
RCLK = 0;
RCLK = 1;
led_table = LED_0F + LED[1];
i = *led_table;
LED_OUT(i);
LED_OUT(0x02);
RCLK = 0;
RCLK = 1;
led_table = LED_0F + LED[2];
i = *led_table;
LED_OUT(i);
LED_OUT(0x04);
RCLK = 0;
RCLK = 1;
led_table = LED_0F + LED[3];
i = *led_table;
LED_OUT(i);
LED_OUT(0x08);
RCLK = 0;
RCLK = 1;
led_table = LED_0F + LED[4];
i = *led_table;
LED_OUT(i);
LED_OUT(0x10);
RCLK = 0;
RCLK = 1;
led_table = LED_0F + LED[5];
i = *led_table;
LED_OUT(i);
LED_OUT(0x20);
RCLK = 0;
RCLK = 1;
led_table = LED_0F + LED[6];
i = *led_table;
LED_OUT(i);
LED_OUT(0x40);
RCLK = 0;
RCLK = 1;
led_table = LED_0F + LED[7];
i = *led_table;
LED_OUT(i);
LED_OUT(0x80);
RCLK = 0;
RCLK = 1;
}
void main (void)
{
Timer0_Init();
while(1)
{
Distance=GetDistance();
LED_display(Distance);
LED4_Display();
Delayms(1);
}
}