收藏 分销(赏)

超声波1602.doc

上传人:xrp****65 文档编号:7656820 上传时间:2025-01-11 格式:DOC 页数:4 大小:27KB
下载 相关 举报
超声波1602.doc_第1页
第1页 / 共4页
超声波1602.doc_第2页
第2页 / 共4页
点击查看更多>>
资源描述
#include<pic.h>  //为超生波测距发送和用液晶显示距离    #include <delay_ms.h> __CONFIG(HS&PROTECT&PWRTEN&BOREN&WDTDIS&LVPDIS); #define uchar unsigned char #define uint  unsigned int //********************液晶功能控制引脚配置*************// #define LCD   PORTD        //LCD的数据口 #define RS      RA2  //LCD数据/命令选择端(H/L) #define RW     RA3  //LCD  读/写  选择端(H/L) #define EN  RA5  //LCD使能控制端 //-----------------------------------------------------// //*************超声波功能控制参数********************// #define ECHO RC6 #define TRIG RC7 volatile uchar k1=0,k2=0,k3=0; long int a=0; //-----------------------------------------------------// void init_sys(void){  ADCON1=0X07;  INTCON=0X00;  TRISD=0X00;  PORTD=0X0FF;  TRISA=0X00;  TRISC=0X40;//0100 0000   } void check_BF(void)     //检查LCD状态函数 {  unsigned char i;  for(i=0;i<15;i++);    //proteus中只能用延时了,检测BF位进行不了 } void write_inst(char inst)   //写入指令函数 {  RS=0;  delay_ms(1);  RW=0;  delay_ms(1);  EN=1;  delay_ms(1);   LCD=inst;  delay_ms(1);  EN=0;  check_BF(); } void write_char(char character)  //写入数据函数 {  RS=1;  delay_ms(1);  RW=0;  delay_ms(1);  EN=1;  delay_ms(1);  delay_ms(1);  LCD=character;  delay_ms(1);  EN=0;  check_BF(); } //=====写入指定位置函数====== void display_str(unsigned char line,unsigned char location,unsigned char str)//line行,location列,str字符 {  RS=0;  delay_ms(1);  RW=0;  delay_ms(1);  EN=1;  delay_ms(1);  if (line==0)    //line=0,第一行   LCD=0x80+location;  if (line==1)    //line=1,第二行   LCD=0xC0+location;  //1602-LCD的第二行首地址为:0xA8  EN=0;  check_BF();  write_char(str); } void init_lcd(void)      //液晶初始化函数 {  write_inst(0x30); //设定功能  delay_ms(5);  write_inst(0x30); //设定功能  delay_ms(5);  write_inst(0x30); //设定功能  delay_ms(5);  write_inst(0x38); //设定俩列  delay_ms(5);  write_inst(0x08); //关闭显示功能  delay_ms(5);  write_inst(0x01); //清除显示屏  delay_ms(5);  write_inst(0x0c); //开启显示功能  delay_ms(5);  }   //输出一串数字到LCD void LCD_prints(unsigned char line,unsigned char location,unsigned int uiNumber)//line行,location列,uiNumber数字 {  unsigned char ucaNumber[5],ucCount;  ucaNumber[0]=(uiNumber/10000);  ucaNumber[1]=(uiNumber/1000)%10;  ucaNumber[2]=(uiNumber/100)%10;  ucaNumber[3]=(uiNumber/10)%10;  ucaNumber[4]=uiNumber%10;  RS=0;  delay_ms(1);  RW=0;  delay_ms(1);  EN=1;  delay_ms(1);  if (line==0)    //line=0,第一行   LCD=0x80+location;  if (line==1)    //line=1,第二行   LCD=0xC0+location;  //1602-LCD的第二行首地址为:0xA8  EN=0;  check_BF();   for(ucCount=0;ucCount<5;ucCount++)   {   write_char(ucaNumber[ucCount]+48);    //从首位到末位逐一输出。   } } void main(void){  init_sys();   init_lcd();  display_str(0,0,'D');  display_str(0,1,'i');  display_str(0,2,'s');  display_str(0,3,'t');  display_str(0,4,'a');  display_str(0,5,'n');  display_str(0,6,'c');  display_str(0,7,'e');  display_str(1,0,'V');  display_str(1,1,'a');  display_str(1,2,'l');  display_str(1,3,'u');  display_str(1,4,'e');  display_str(1,5,'=');   display_str(1,11,'(');      display_str(1,12,'C');  display_str(1,13,'M');   display_str(1,14,')');                                       while(1)   {    uchar i;    k1=0;k2=0;k3=0;   /* TRIG=1;    delay_ms(1);    TRIG=0;    while(ECHO==0);    while(ECHO==1) ++a;    delay_ms(30);    a=(108*a*17)/30000; //20 */       for(i=10;i>0;i--){      a=0;    TRIG=1;    delay_ms(1);    TRIG=0;    while(ECHO==0);    while(ECHO==1) ++a;    delay_ms(3);    a=(108*a*17)/30000;    if(a>50&&a<130) k1++;    else if(a<=50) k2++;    else k3++;    delay_ms(10);    }    for(i=1;i<25;i++)    display();     LCD_prints(1,6,a);    a=0;            delay_ms(200);  } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服