|
- #include<reg52.h>
- #define LCD1602_DB P0
- sbit LCD1602_RS = P2^6;
- sbit LCD1602_RW = P2^5;
- sbit LCD1602_E = P2^7;
- typedef unsigned int uint;
- typedef unsigned char uchar;
- void Delay10us(uint i)
- {
- while(i--);
- }
- /* lcd1602寫入數(shù)據(jù)/命令(i=1/i=0) */
- void Write_Cmd_Dat(uchar i,uchar dat)
- {
- LCD1602_RS = i;
- LCD1602_RW = 0;
- LCD1602_E = 1;
- LCD1602_DB = dat;
- Delay10us(500); //不能少!!
- LCD1602_E = 0;
- }
- void LCD1602Init()
- {
- Write_Cmd_Dat(0,0x38);
- Write_Cmd_Dat(0,0x0c);
- Write_Cmd_Dat(0,0x06);
- Write_Cmd_Dat(0,0x01);
- }
- /* 設(shè)置LCD1602光標(biāo)位置 */
- void LcdSetCursor(uchar x,uchar y)
- {
- uchar addr;
- if(y==0)
- addr = 0x00 + x;
- else
- addr = 0x40 + x;
- Write_Cmd_Dat(0,addr |0x80);
- }
- /* LCD1602顯示字符串 */
- void DisplayString(uchar *s)
- {
- while(*s>0)
- {
- Write_Cmd_Dat(1,*s);
- s++;
- }
- }
- void main()
- {
- char code string_1[] = "sadfsadf";
- char code string_2[] = " 23";
- LCD1602Init();
- LcdSetCursor(0,0);
- DisplayString(string_1);
- LcdSetCursor(0,1);
- DisplayString(string_2);
- while(1);
- }
復(fù)制代碼 |
|