功能:
超声波测距仪,测量距离,用LCD12864显示信息。 lcd_writestr(0,0," 超声波测距仪 "); lcd_writestr(1,0,"********"); lcd_writestr(2,0,"当前被测距离为:"); lcd_writestr(3,0," 厘米 ");
程序运行图:
原理图:
部分程序:
/*****************************************************
* 功能:超声波测距
******************************************************/
#include #include "intrins.h" #include "12864LCDdriver.h" #define uchar unsigned char #define uint unsigned int #define ulong unsigned long int #define V=173 sbit CSOut=P1^7; sbit Start=P1^4; ulong TIME; uchar DISTANCE; uchar code discode[11]={'0','1','2','3','4','5','6','7','8','9',' '}; ulong data disnum[5]; bit data ReceiveOK=0; bit data TOut; bit data FLAG; /********************************************************* * 函数名称:void delay2(uchar x) * 功 能:延时函数 * 入口参数:无 * 出口参数:无 *********************************************************/ void delay2(uchar x) { uchar a,b; for(a=0;a } /********************************************************* * 函数名称:void init(void) * 功 能:初始化 * 入口参数:无 * 出口参数:无 *********************************************************/ void init(void) { lcd_init();//LCD初始化,开机调用一次 TMOD=0x01;//T0定时器方式1 IT0=0;//INT0负边沿触发 IP=0x01;//INT0高优先级 EA=1;//开总中断 } /********************************************************* * 函数名称: * 功 能:超声波发射函数 * 入口参数:无 * 出口参数:无 *********************************************************/ void CCOut(void) { TR0=1; //40KHz CSOut=0;//1 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_(); CSOut=1;//2 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_(); CSOut=0;//3 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_(); CSOut=1;//4 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();
相关文章









