- //************************************************************************************
- //URM37V3.0超声波模块是通过串口通讯的,也可以设置成PWM输出模式及开关量输出模式,
- //这些都是通过串口操作,具体详情请到http://www.dfrobot.cn下载使用手册查看!
- //本程序是使用DF-URFV1.0作控制板,串口读取方式(比较实用、简单),使用1602LCD显示,
- //使用MODE及+/L两个按键作功能切换和舵机角度控制,具体程序如下:
- //************************************************************************************
- //***********************************URM37_51DEMO.H*******************************
- #include<AT89X51.H>
- #include<INTRINS.H>
- #define uchar unsigned char
- #define uint unsigned int
- #define LCD_E P0_7 //使能线,R/W = 0,E电平低有效;R/W = 1,E电平高有效
- #define LCD_RW P0_6 //'1'读操作,'0'写操作
- #define LCD_RS P0_5 //'0'操作指令寄存器,'1'操作数据寄存器
- #define LED P0_4 //LCD背光
- #define ModeKey P0_3 //模式选择按钮
- #define TemperKey P0_2
- #define LCD_DATA P2 //8数据线
- //*********************************************************************************
- //***********************************HD44780.C*************************************
- #include"URM37_51demo.H"
- void Delay_ms(uchar ms)
- {
- uchar i;
- for(;ms>0;ms--)
- for(i=250;i>0;i--)
- {
- _nop_();
- _nop_();
- }
- }
- void LCD_WRITE_COMM(uchar comm) //写命令
- {
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_E = 1;
- LCD_DATA = comm; //写8位
- LCD_E = 0;
- Delay_ms(2);
- }
- void LCD_WRITE_DATA(uchar Data)//写数据
- {
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_E = 1;
- LCD_DATA = Data; //写8位
- LCD_E = 0;
- Delay_ms(2);
- }
- void LCD_Init(void)
- {
- Delay_ms(15);
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_E = 1;
- LCD_DATA = 0x00; //上电初使
- LCD_E = 0;
- Delay_ms(5);
- LCD_E = 1;
- LCD_E = 0;
- Delay_ms(5);
- LCD_E = 1;
- LCD_E = 0;
- Delay_ms(5);
- LCD_E = 1;
- LCD_DATA = 0x02; //2行显示,5×7点阵
- LCD_E = 0;
- Delay_ms(5);
- LCD_WRITE_COMM(0x0c); //开显示,开光标,开字符闪烁
- LCD_WRITE_COMM(0x06); //显示后自动加一,光标移动
- LCD_WRITE_COMM(0x01); //清显示
- LCD_WRITE_COMM(0x38); //选择8线接口方式,2行显示,5×7点阵
- LCD_WRITE_COMM(0x80); //
- }
- //******************************************************************************
- //*****************************URM37_COM.C***********************************
- #include"URM37_51demo.H"
- extern void LCD_Init(void);
- extern void LCD_WRITE_DATA(uchar);
- extern void LCD_WRITE_COMM(uchar);
- extern void Delay_ms(uchar);
- uchar Data_buf[4];
- uchar Send_buf[4];
- uchar Receive_buf[4];
- uchar Temper_Oder[4]={0x11,0x00,0x00,0x11};//读温度命令
- uchar Du_dec[7]={29,65,100,135,170,206,241};//舵机角度
- uchar time,sum,Mode,Mode_flag;
- void MCU_Initi(void) //MCU初始化
- {
- TMOD = 0X21;//波特率发生器T1工作在模式2上
- SCON = 0X50;//设串口工作方式1,收发状态
- PCON = 0X00;//波特率不加倍
- TL1 = 0XFD;//设波特率为9600
- TH1 = 0XFD;
- TR1 = 1; //开
- }
- void LCD_logo(void) //LOGO显示
- {
- uchar i;
- uchar LOGO[]=" URM37 V3.0 ";
- i = 0;
- LCD_WRITE_COMM(0x80);
- while(LOGO!=NULL)
- {
- LCD_WRITE_DATA(LOGO);
- i++;
- }
- }
- void Receive(uchar *p)//串口接收
- {
- uchar i,n=100;
- for(i=0;i<4;i++)
- {
- while(RI==0); //等待串行中断
- *(p+i)=SBUF;
- RI = 0; //清TI,必须软件清除
- while(--n); //延时
- }
- }
- void Send_com(uchar *p)//串口发送
- {
- uchar i,n=100;
- for(i=0;i<4;i++)
- {
- SBUF = *(p+i);
- while(TI==0); //等待串行中断
- TI = 0; //清TI,必须软件清除
- while(--n); //延时
- }
- }
- uchar Efficacy(uchar x)//效验和
- {
- uchar i,temp=0;
- if(x==1)
- {
- for(i=0;i<3;i++)
- {
- temp=temp+Send_buf;//发送效验和
- }
- }
- if(x==2)
- {
- for(i=0;i<3;i++)
- {
- temp=temp+Receive_buf;//接收效验和
- }
- }
- return temp;
- }
- void HEX_TO_DEC(uint source,uchar * object)//16进制转10进制
- {
- uchar i;
- for(i=0;i<4;i++)
- {
- *(object+i) = source % 10;
- source/=10;
- }
- return;
- }
- void Dis_Temper(void)//温度显示
- {
- uchar i;
- uchar Temp;
- uchar x;
- uchar y;
- uchar TemperData;
- uchar Dis[]="Temperature: ";
- i = 0;
- LCD_WRITE_COMM(0xc0);
- while(Dis!=NULL)
- {
- LCD_WRITE_DATA(Dis);
- i++;
- }
- x=Receive_buf[1]<<4;
- y=Receive_buf[2]>>4;
- TemperData = ((x&0xf0)|(y&0x0f))&0xff;
- Temp = TemperData/10;
- LCD_WRITE_DATA(0x30+Temp);
- Temp= TemperData%10;
- LCD_WRITE_DATA(0x30+Temp);
- LCD_WRITE_DATA('C');
- }
- void Dis_Distance(void)//角度与距离显示
- {
- uchar i;
- uchar tem;
- uchar temp;
- uchar Data[4];
- uchar du[]="Du: ";
- uchar Distance[]="D: ";
- i=0;
- LCD_WRITE_COMM(0xc0);
- while(du!=NULL)
- {
- LCD_WRITE_DATA(du);
- i++;
- }
- tem=Du_dec[Mode]/100;
- LCD_WRITE_DATA(0x30+tem);
- tem=(Du_dec[Mode]%100)/10;
- LCD_WRITE_DATA(0x30+tem);
- tem=(Du_dec[Mode]%100)%10;
- LCD_WRITE_DATA(0x30+tem);
- LCD_WRITE_DATA(0x20);
- i=0;
- while(Distance!=NULL)
- {
- LCD_WRITE_DATA(Distance);
- i++;
- }
- tem=Receive_buf[1];
- temp=(uchar)(((tem<<8)&0xff00)|Receive_buf[2]);
- HEX_TO_DEC(temp,Data);
- LCD_WRITE_DATA(0x30+Data[2]);//米
- LCD_WRITE_DATA('.');
- LCD_WRITE_DATA(0x30+Data[1]);//分米
- LCD_WRITE_DATA(0x30+Data[0]);//厘米
- LCD_WRITE_DATA('m');
- }
- void main(void)
- {
- uchar i;
- uchar Mod,flag; //模式记数
- MCU_Initi();
- LCD_Init();
- LCD_logo();
- Mode_flag=0;
- sum=0;
- Mode = 0;
- flag = 0;
- Mod = 0;
- while(1)
- {
- LED = 0;//背光亮
- TemperKey = 1;
- if(TemperKey == 0)//按键舵机角度控制
- {
- Delay_ms(10);
- if(TemperKey == 0)
- {
- if(Mode==0)
- flag=0;
- if(Mode==6)
- flag=1;
- if(flag==0)
- Mode++;
- else Mode--;
- }
- while(!TemperKey);
- }
- ModeKey = 1;
- if(ModeKey == 0)//功能切换模式
- {
- Delay_ms(10);
- if(ModeKey == 0)
- {
- if(Mod == 1)
- Mod = 0;
- else
- Mod++;
- }
- while(!ModeKey);
- }
- switch(Mod)
- {
- case 0: for(i=0;i<4;i++)//温度读取
- Send_buf=Temper_Oder;
- Send_com(Send_buf);
- Receive(Receive_buf);
- Dis_Temper();
- Delay_ms(100);
- break;
- case 1: Send_buf[0]=0x22;//距离读取
- Send_buf[1]=0x05+Mode*6;
- Send_buf[2]=0x00;
- Send_buf[3]=Efficacy(1);
- Delay_ms(50);
- Send_com(Send_buf);
- Receive(Receive_buf);
- Dis_Distance();
- Delay_ms(50);
- break;
- default : break;
- }
- }
- }
- //************************************************************************
复制代码
[
4
|
|
|
|