/**
* 超声波测距组件
* 文件名:ultrasonic.h
*/
#ifndef _ULTRASONIC_H
#define _ULTRASONIC_H
#define somenop(); { \
_nop_();_nop_();_nop_();_nop_();_nop_(); \
_nop_();_nop_();_nop_();_nop_();_nop_(); \
}
sbit TX = P1^0; //发射引脚
sbit RX = P1^1; //接收引脚
void tmr1_specialInit();
uint8_t ult_getDis();
#endif // _ULTRASONIC_H
/**
* 超声波测距组件
* 文件名:ultrasonic.c
*/
#include <stc15f2k60s2.h>
#include <intrins.h>
#include "stdint.h"
#include "ultrasonic.h"
/* 特殊配置定时器1,作为计时器 */
void tmr1_specialInit() { //不要开启中断,要将溢出标志位置0,不要开始运行
AUXR &= 0xBF;
TMOD &= 0x0F;
TMOD |= 0x10;
TL1 = 0; //!!!
TH1 = 0; //!!!
TF1 = 0; //!!!
}
//TX引脚发送40KHz方波信号驱动超声波发送探头
static void ult_sendWave() {
uint8_t cnt =