单片机源程序如下:
- #include<reg52.h>
- #include <stdio.h>
- typedef unsigned char uchar;
- typedef unsigned int uint;
- typedef unsigned char uint8;
- typedef unsigned int uint16;
- #define LCD1602_DATAPINS P0 //仅用高4位
- sbit LCD1602_E=P3^4; // 使能端(0有效)
- sbit LCD1602_RW=P3^6; // 读(1)写(0)端口
- sbit LCD1602_RS=P3^5; // 数据(1)命令(0)选择端口
- unsigned char zuigao,shiwei,gewei,rh, USART_RX_BUF[3],i,ii;
- unsigned char Rx_buf,temp,rcount;
- uint8 code num[10]="0123456789";
- //sbit sin=P3^2;
- //sbit ten=P3^1;
- //sbit hund=P3^0;
- //temp1=0x00;
- //temp2=0x00;
- // uchar dd ;
- uchar table[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
- //
- //uchar re_buf =0;
- unsigned char temp1=0xff;
- unsigned char temp2=0xff;
- void Lcd1602_Lcd1602_Delay1ms1ms(uint c) //误差 0us
- {
- uchar a,b;
- for (; c>0; c--)
- {
- for (b=199;b>0;b--)
- {
- for(a=1;a>0;a--);
- }
- }
-
- }
- void LcdWriteCom(uchar com) //写入命令
- {
- LCD1602_E = 0; //使能清零
- LCD1602_RS = 0; //选择写入命令
- LCD1602_RW = 0; //选择写入
- LCD1602_DATAPINS = com; //由于4位的接线是接到P0口的高四位,所以传送高四位不用改
- Lcd1602_Lcd1602_Delay1ms1ms(1);
- LCD1602_E = 1; //写入时序
- Lcd1602_Lcd1602_Delay1ms1ms(5);
- LCD1602_E = 0;
- LCD1602_DATAPINS = com << 4; //发送低四位
- Lcd1602_Lcd1602_Delay1ms1ms(1);
- LCD1602_E = 1; //写入时序
- Lcd1602_Lcd1602_Delay1ms1ms(5);
- LCD1602_E = 0;
- }
- void LcdWriteData(uchar dat) //写入数据
- {
- LCD1602_E = 0; //使能清零
- LCD1602_RS = 1; //选择写入数据
- LCD1602_RW = 0; //选择写入
- LCD1602_DATAPINS = dat; //由于4位的接线是接到P0口的高四位,所以传送高四位不用改
- Lcd1602_Lcd1602_Delay1ms1ms(1);
- LCD1602_E = 1; //写入时序
- Lcd1602_Lcd1602_Delay1ms1ms(5);
- LCD1602_E = 0;
- LCD1602_DATAPINS = dat << 4; //写入低四位
- Lcd1602_Lcd1602_Delay1ms1ms(1);
- LCD1602_E = 1; //写入时序
- Lcd1602_Lcd1602_Delay1ms1ms(5);
- LCD1602_E = 0;
- }
- void LcdInit() //LCD初始化子程序
- {
- LcdWriteCom(0x32); //将8位总线转为4位总线
- LcdWriteCom(0x28); //在四位线下的初始化
- LcdWriteCom(0x0c); //开显示不显示光标
- LcdWriteCom(0x06); //写一个指针加1
- LcdWriteCom(0x01); //清屏 LcdWriteCom(0x80); //设置数据指针起点
- LcdWriteCom(0x80);
- LcdWriteData('F');
-
- LcdWriteData('r');
-
- LcdWriteData('e');
-
- LcdWriteData(':');
- LcdWriteCom(0x89);
- LcdWriteData('K');
-
- LcdWriteData('h');
-
- LcdWriteData('z');
- }
- void Lcd1602_Delay1ms(int i)
- {
- int j=0;
- for(;i>0;i--)
- {
- for(j=0;j<125;j++);
- }
- }
- void InitUART(void) //这是串口的基本配置,配置他的波特率是9600.这些参数都是标准的。
- {
- TMOD = 0x20;
- SCON = 0x50;
- TH1 = 0xFD;
- TL1 = TH1;
- PCON = 0x00;
- EA = 1;
- ES = 1;
- TR1 = 1;
- }
- void main()
- {
-
- InitUART() ;
- LcdInit();
-
- while(1)
- {
- LcdWriteCom(0x85);
- LcdWriteData(USART_RX_BUF[0]);
- LcdWriteData(USART_RX_BUF[1]);
- LcdWriteData(USART_RX_BUF[2]);
- }
- }
- void UARTInterrupt(void) interrupt 4
- {
- ES=0; //关闭中断
- if(RI) //接收到数据
- { Rx_buf=SBUF;
- if(rcount!=0) // date!='\0' 没有数据传输了 rcount!=0 收到@后的数据
- {
- USART_RX_BUF[i]=Rx_buf;
- i++;
- if(i>2)
- {
- i=0;
- rcount=0;
- }
- }
- if(Rx_buf=='@')
- {rcount=1;
- USART_RX_BUF[3]="000" ;
- }
- RI=0;
- ES=1; //关闭中断
- }
- }
复制代码
所有资料51hei提供下载:
蓝牙发送文本框.zip
(199.6 KB, 下载次数: 66)
|