⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd.c

📁 1602液晶显示程序代码
💻 C
字号:
#include "LCD1602/LCD1602.H"
extern unsigned char pattern[];

void Delay_us(unsigned char t)
{
while(--t);
}

void Delay_ms(unsigned char t)
{
while(t--)
{
    Delay_us(225);
    Delay_us(227);
}
}

void Init_1602(void)
{
Delay_ms(15);
Write_Char_1602(0x38,CMD,0); //don't check busy
Delay_ms(5);
Write_Char_1602(0x38,CMD,0);
Delay_ms(5);
Write_Char_1602(0x38,CMD,0);

Write_Char_1602(0x38,CMD,1);//8 wire,2 line display,5x10 char
Write_Char_1602(0x08,CMD,1);//close display,no cursor,don't blink
Write_Char_1602(0x01,CMD,1);//clear screen
Write_Char_1602(0x06,CMD,1);//the cursor move from left to right,the text don't move
Write_Char_1602(0x0c,CMD,1);//open display
}

void Write_Char_1602(unsigned char Data,bit CMD_DATA,bit Check)
{
if(Check) 
    Check_Busy_1602();
RS_1602=CMD_DATA;
RW_1602=WRITE;
DATA_1602=Data;
E_1602=1;
_nop_();
E_1602=0;
}

unsigned char Read_Char_1602(unsigned char Addr,bit Check)
{
unsigned char temp;
Write_Char_1602(Addr,CMD,1);
if(Check) 
    Check_Busy_1602();
DATA_1602=0xff;
RS_1602=DATA;
RW_1602=READ;
E_1602=1;
_nop_();
temp=DATA_1602;
E_1602=0;
return temp;
}

void Write_String_1602(unsigned char *P) //only can write from start to end
{
unsigned char i,len;
len=strlen(P);
Set_R_C(0,0);
if(len>16)
{
    for(i=0;i<16;i++)
    {
     Write_Char_1602(P[i],DATA,1);
    }
    Set_R_C(1,0);
    for(i=16;i<len;i++)
    {
     Write_Char_1602(P[i],DATA,1);
    }
}
else
{
    for(i=0;i<len;i++)
    {
     Write_Char_1602(P[i],DATA,1);
    }
}
}

void Set_R_C(unsigned char R,unsigned char C) //set row and column   R=0/1;C=0~F
{
R&=0x01;
C&=0x0f;
if(R)
    Write_Char_1602(0x80+0x40+C,CMD,1);
else
    Write_Char_1602(0x80+C,CMD,1);
}

void Check_Busy_1602(void)
{
DATA_1602=0xff;   //set as input port
RS_1602=CMD;
RW_1602=READ;
E_1602=1;
while(DATA_1602 & 0x80)//下降沿表示忙碌结束;
{
      E_1602=0;    //这两句protues仿真必须加
      E_1602=1;    //
}
E_1602=0;
}


//write the self_defined word to CGRAM and set the DDRAM's address and DDRAM's information to 
//display the self_defined char form pastion 0 to 7
//5x8 dot char
void Pattern(unsigned char *pattern)
{
unsigned char count;
Write_Char_1602(0x40,CMD,1); //set CGRAM address 0x00 
                       //(0~63) 8 self_defined char
for(count=0;count<64;count++)
{
   Write_Char_1602(pattern[count],DATA,1);
}
}

main.c:

#include <AT89X51.H>
#include "LCD1602\LCD1602.H"
#include "UART\UART.H"

//crystal 11.0592M
unsigned char Disp_Buffer[]="LCD_1602 Drive,write by myself.";
unsigned char code pattern[]={ 
0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,//年
0x0f,0x09,0x0f,0x09,0x0f,0x09,0x11,0x00,//月
0x0f,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00,//日
0x00,0x0f,0x09,0x0a,0x04,0x0a,0x10,0x01,//欢-左半部
0x08,0x0f,0x11,0x04,0x04,0x0a,0x11,0x00,//欢-右半部
0x0b,0x02,0x1a,0x0b,0x08,0x08,0x0e,0x13,//迎-左半部
0x17,0x05,0x15,0x17,0x14,0x14,0x04,0x1f,//迎-右半部
0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F};


void main(void)
{
unsigned char i;
Delay_ms(100);
Init_1602();
Init_UART();
// -----------------------------CGRAM的读写测试---------------------------------------------
Pattern(pattern);    //写入CGRAM
Write_Char_1602(0xC0,CMD,1);    //二行方式显示:0x80时写的是第一行 0xC0时写的是第二行 
for(i=0;i<8;i++)      //范围:0x80~0x80+0x27 0xC0~0xC0+0x27
{          //一行方式显示:只有0x80显示第一行,第二行不显示
   Write_Char_1602(i,DATA,1);   //范围:0x80~0x80+80
}
for(i=0;i<8;i++)
{
   Send_Char(Read_Char_1602(0x40+i,1));
}
//

while(1);
}




/* -----------------------------DDRAM的读写测试---------------------------------------------
Write_Char_1602(0x80,CMD,1);    //二行方式显示:0x80时写的是第一行 0xC0时写的是第二行 
for(i=0;i<40;i++)      //范围:0x80~0x80+0x27 0xC0~0xC0+0x27
{          //一行方式显示:只有0x80显示第一行,第二行不显示
   Write_Char_1602(0x30,DATA,1); //范围:0x80~0x80+80
}

for(i=0;i<40;i++)
{
   Send_Char(Read_Char_1602(0x80+i,1));
}
*/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -