#include<reg52.h>
#define LED_PORT P0
#define uchar unsigned char
#define uint unsigned int
sbit hc138_A = P2^2;
sbit hc138_B = P2^3;
sbit hc138_C = P2^4;
sbit Trig = P2^1;
sbit Echo = P3^1;
unsigned char ledx8_value[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar count,time;
bit flag;
void led_disy(uint temp_data);
void delay(uint time_t);
void timer();
void main(void)
{
Trig=0;
timer();
Trig=1;
delay(500);
Trig=0;
while(1)
{;}
}
void timer()
{
TMOD=0x01;
TH0=0xfc;
TL0=0x18;
EA=1;
ET0=1;
TR0=1;
}
void timer_interrupt() interrupt 1
{
TR0=0;
TH0=0xfc;
TL0=0x18;
if(Echo==1)
{
count++;
}
if(Echo==0)
{
time=count*17;
led_disy(time);
}
TR0=1;
}
void led_disy(uint temp)
{
uchar ge_data,shi_data,bai_data;
bai_data=temp/100;
temp=temp%100;
shi_data=temp/10;
temp=temp%10;
ge_data=temp;
LED_PORT=0x00;delay(1);hc138_C=0;hc138_B=1;hc138_A=0;LED_PORT=ledx8_value[bai_data];delay(100);
LED_PORT=0x00;delay(1);hc138_C=0;hc138_B=0;hc138_A=1;LED_PORT=ledx8_value[shi_data];delay(100);
LED_PORT=0x00;delay(1);hc138_C=0;hc138_B=0;hc138_A=0;LED_PORT=ledx8_value[ge_data];delay(100);
}
void delay(uint time_t)
{
while(time_t-->0);
}
以上是我写的程序,我知道问题大,思路明确,用1ms中断计数嘛,但是我就写成这样的,肯定不能完成预定效果,感觉头都大了,搞了一下午,求大神救救我,我c语言这么烂,是不是没救了,不适合玩单片机了,哎……
关键词:
单片机
超声波
测距
51单片机