lcd.c

来自「单片机开发板源码 适合初学者 第四部分 字符型LCD显示1-2」· C语言 代码 · 共 76 行

C
76
字号
/*实验名称:字符型LCD接口实验
  功能:在LCD上显示
	 AT89S51 DEMO
	www.mcuprog.com
  作者:http://www.mcuprog.com
        05-02-01
*/
#include <reg51.h>
sbit E=P2^2;
sbit RW=P2^1;
sbit RS=P2^0;	
typedef unsigned char uchar;
//-------------------------------------
void Delay(unsigned int t)   // delay 40us
{
 for(;t!=0;t--) ;
}
//=============================================
void SendCommandByte(unsigned char ch)
{
   RS=0;
   RW=0;
   P0=ch;
   E=1;
    Delay(1);
   E=0;
   Delay(100);  //delay 40us
}
//-------------------------------------------------------
void SendDataByte(unsigned char ch)
{  RS=1;
   RW=0;
   P0=ch;
   E=1;
       Delay(1);
   E=0;
   Delay(100); //delay 40us
}
//-------------------------------------------------
void InitLcd()
{SendCommandByte(0x30);
 SendCommandByte(0x30);
 SendCommandByte(0x30);
 SendCommandByte(0x38);	//设置工作方式
 SendCommandByte(0x0c); //显示状态设置
 SendCommandByte(0x01); //清屏
 SendCommandByte(0x06); //输入方式设置
}
//=============================================
void DisplayMsg1(uchar *p)
{ 
 unsigned char count;
 SendCommandByte(0x80);  //设置DDRAM地址
 for(count=0;count<16;count++)
    {SendDataByte(*p++);
   	}
} 
//=============================================
void DisplayMsg2(uchar *p)
{ 
 unsigned char count;
 SendCommandByte(0xc0);  //设置DDRAM地址
 for(count=0;count<16;count++)
    {SendDataByte(*p++);
   	}
}
//=============================================
main()
{char msg1[16]="REF POWER 18.23 ";
 char msg2[16]="    SWR    1.26 ";
 InitLcd();
 DisplayMsg1(msg1);
 DisplayMsg2(msg2);
 while(1);
}

⌨️ 快捷键说明

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