主要函数方法如下,全部代码见链接:https://download.csdn.net/download/qq_44624722/85013693
void main(void)
{
Lcd1602Init();
MotorRun();
HcSr04Test();
while (1);
}
void Lcd1602Init(void)
{
Lcd1602WriteCmd(0x38);
delay5ms();
Lcd1602WriteCmd(0x38);
delay5ms();
Lcd1602WriteCmd(0x38);
delay5ms();
Lcd1602WriteCmd(0x38);
Lcd1602WriteCmd(0x08);
Lcd1602WriteCmd(0x01);
Lcd1602WriteCmd(0x06);
Lcd1602WriteCmd(0x0c);
}
void HcSr04Test(void)
{
TMOD = 0x01;
TH0 = 0;
TL0 = 0;
ET0 = 1;
EA = 1;
while(1)
{
StartModule();
while (!gEcho);
TR0 = 1;
while (gEcho);
TR0 = 0;
MesureDistance();
delay100ms();
}
}
static void MesureDistance(void)
{
u16 time = 0;
unsigned long dCm = 0;
int i=0;
time = TH0 * 256 + TL0;
TH0 = 0;
TL0 = 0;
dCm = (time * 1.7) / 100;
if ((dCm >= 700) || (gFlag == 1))
{
gFlag = 0;
Lcd1602ShowStr(0, 0, "-.--M");
MotorRun();
}
else if (dCm<50)
{
gDisBuf[0] = '0' + dCm % 1000 / 100;
gDisBuf[2] = '0' + dCm % 1000 % 100 / 10;
gDisBuf[3] = '0' + dCm % 1000 % 10 % 10;
Lcd1602ShowStr(0, 0, gDisBuf);
while(i<200)
{
BuzzerOnOff();
delay20us;
i++;
}
MotorUNRun();
}
else
{
gDisBuf[0] = '0' + dCm % 1000 / 100;
gDisBuf[2] = '0' + dCm % 1000 % 100 / 10;
gDisBuf[3] = '0' + dCm % 1000 % 10 % 10;
Lcd1602ShowStr(0, 0, gDisBuf);
MotorRun();
}
}
|