复制代码
- #include<reg52.h>
- sbit E_And_RS = P1 ^ 2;
- sbit Data1 = P1 ^ 1;
- sbit Data2 = P1;
- typedef bit ubit;
- typedef unsigned char uint8;
- typedef unsigned int uint16;
- typedef unsigned long uint32;
- void delay(uint16 Time)
- {
- while(Time--);
- }
- void LCD_WriteDataAndComm(uint8 Data, ubit Control)
- {
- uint8 Flag;
- for(Flag = 0;Flag < 2;Flag++)
- {
- if(Control) //判断写命令还是写数据
- E_And_RS = 1; //写数据,对RS脚充电
- else
- E_And_RS = 0; //写命令,对RS脚放电
- if(Data & 0x20) //判断D7脚数据
- Data1 = 1; //为1则充电
- else
- Data1 = 0; //放电
- if(Data & 0x80) //判断D5脚数据
- Data2 = 1;
- else
- Data2 = 0;
- delay(30); //等待充满或放完
- if(Data & 0x10) //判断D4脚数据
- Data1 = 1; //输出数据
- else
- Data1 = 0;
- if(Data & 0x40) //判断D6脚数据
- Data2 = 1;
- else
- Data2 = 0;
- E_And_RS = 1; //拉高
- Data <<= 4;
- E_And_RS = 0; //数据写入.
- }
- delay(20);
- }
- void LCD_Init(void)
- {
- E_And_RS = 0; //RS脚放电
- Data1 = 1; //D5脚充电,
- Data2 = 0; //D7脚放电
- delay(30); //等待充满或放完
- Data1 = 0; //拉低D4
- E_And_RS = 1; //写入0x20,为4BIT模式
- delay(1);
- E_And_RS = 0; //写入数据,LCD1602为上升沿读出数据下降沿写入数据
- LCD_WriteDataAndComm(0x28, 0);
- LCD_WriteDataAndComm(0x08, 0);
- LCD_WriteDataAndComm(0x01, 0);
- LCD_WriteDataAndComm(0x06, 0);
- LCD_WriteDataAndComm(0x0C, 0);
- }
- void Show_Str(uint8 *Str, uint8 Addr)
- {
- LCD_WriteDataAndComm(Addr, 0);
- while(*Str)
- LCD_WriteDataAndComm(*Str++, 1);
- }
- void main(void)
- {
- LCD_Init();
- Show_Str("Tliang Holle!", 0x80);
- Show_Str("QQ:459436468", 0xc3);
- while(1);
- }
欢迎光临 圣龙扬特-AVR电子 (http://avr.cnta.net/) | Powered by Discuz! X2.5 |