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

📄 lcd3.c

📁 这是一段开始学单片机时写的LCD程序
💻 C
字号:
//*******************************************************
//该程序实现用液晶显示器LCD显示已定义的字符串中的某一字符
//程序启发:用查表法把数据送到LCD显示
//作者:李锡坚
//完成时间:2007.07.24.21:43
//*******************************************************
/********************************************************
与前两个程序LCD1.C,LCD2.C的不同处:
sbit LCD_RS=P3^4;			   //定义LCD的RS控制位
sbit LCD_RW=P3^5;			   //定义LCD的RW控制位
sbit LCD_DISPLAY_START=P1^0;   //LCD开始显示的指示灯
sbit LCD_E=P1^1;			   //定义LCD的E控制位
写操作时用:LCD_E=1;
           _nop_();
		   LCD_E=0;
		   代替
*********************************************************/
//****************************************
#include<reg52.h>	        //包含常用头文件
#include<stdio.h>
#include<intrins.h>
#include<absacc.h>
#define uchar unsigned char	   //定义常用数据类型
int cnt;
void LCD_INIT(void);		   //LCD的初始化函数
void LCD_DISPLAY_STR(uchar *DATA);//在指定的位置显示字符串
void LCD_CLR(uchar y);							 //清除LCD指定的行
void LCD_SEND_COMMAND(uchar COMMAND);			 //向LCD发送命令
void LCD_SEND_DATA(uchar DATA);					 //向LCD发送数据
void LCD_WAIT(void);	                         //检查LCD空闲
uchar LCD_GET_FLAG(void);						 //检查LCD状态
void DELAY(void);								 //延时
/*定义所要显示的数据*/
char code DISPLAY[]="asdf ghjk";//{1,2,3,0x1f,0x04,0x1f,0x04,0x04};//"It goes without saying that this picture aims at revealing a current problem: what kind of attitude we will choose when facing difficulties and challenges. In this drawing, a football-player is prepared to kick a ball towards the net, where a goal-keeper keeps guard. However, in the player’s mind appears a scene in which the keeper becomes a giant covering the net completely, while the latter imagines that he turns out to be a dwarf standing below the huge net. Obviously, both of them lack courage and confidence in front of challenges.These two players represent those who often choose to magnify their enemies and dangers, and lose their confidence to fight against them. As a result, what they can achieve in the end is nothing but failure. This sad situation can be best illustrated in the fact that some people lose their chance of success in the entrance examination for the MA program. When preparing for the exam, they often feel depressed thinking that they are never well-prepared. In fact, they will soon realize that it is not as difficult as they thought before. In a word, they suffer from underestimating their abilities.In our life, what we need most is self-insurance and a proper view of challenges before us. Therefore, we should bear in mind that our competitors may not be as terrible as expected, and our painstaking efforts will pay off as long as we arm ourselves with courage and confidence. Only in this way can we overcome any difficulties and challenges.";
/*定义LCD控制字*/
#define LCD_MODE 0x3C        /* 接口数据8位,显示2行,字体为1号 */          
#define LCD_NO_FLASH 0x0C    /* 屏幕显示开,无光标 */        
#define LCD_HIDE 0x08        /* 屏幕显示关 */       
#define LCD_FLASH 0x0D       /* 屏幕显示开,并打开闪烁光标 */    
#define LCD_SHIFT 0x07       /* 模块数据输入为增量方式,显示内容移动 */      
#define LCD_NO_SHIFT 0x06    /* 模块数据输入为增量方式,显示光标移动 */     
#define LCD_SH 0x14          /* 移动光标及整体显示 */ 
#define LCD_LINE1  0x80		 /*第一行DDRAM起始地址*/
#define LCD_LINE2  0xc0		 /*第二行DDRAM起始地址*/
#define SEND_IN  P0          /*XBYTE[0xff00] /*定义LCD的实际地址*/
sbit LCD_RS=P3^6;			   //定义LCD的RS控制位
sbit LCD_RW=P3^7;			   //定义LCD的RW控制位
sbit LCD_DISPLAY_START=P1^0;   //LCD开始显示的指示灯
sbit LCD_E=P1^4;			   //定义LCD的E控制位
int t=0;					   //中断计数
//*************************************************
//LCD显示字符串的主程序
//利用中断间隔循环显示
//
//*************************************************  
main()
{
LCD_INIT();					  //初始化LCD
do
 {
   LCD_DISPLAY_START=0;		  //开LCD显示的指示灯
   DELAY();
   LCD_DISPLAY_START=1;		  //灭LCD显示的指示灯
   LCD_DISPLAY_STR(DISPLAY);  //显示字符串
   }while(1);
}
//*************************************************
//函数功能:LCD初始化
//输入变量:无
//输出变量:无
//调用模块:LCD_SEND_COMMAND(),LCD_CLR()
//*************************************************
void LCD_INIT(void)
  {								   
   LCD_SEND_COMMAND(LCD_MODE);	   //设置工作方式
   LCD_SEND_COMMAND(LCD_NO_FLASH); //设置显示方式
   LCD_SEND_COMMAND(LCD_NO_SHIFT); //设置光标画面滚动方式
   LCD_SEND_COMMAND(LCD_SH);	   //设置输入方式
   LCD_CLR(1);					   //清除LCD第一行
   LCD_CLR(2);					   //清除LCD第二行
    }
//*************************************************
//函数功能:清除LCD指定行
//输入变量:y
//输出变量:无
//调用模块:LCD_SEND_COMMAND(),LCD_SEND_DATA()
//************************************************* 
void LCD_CLR(uchar y)
   {
    uchar i;
	i=0;
	if(y==1)
	    {
		 LCD_SEND_COMMAND(LCD_LINE1);  //发送命令使LCD指向第一行
		 i=16;
		 }
    if(y==2)
	    {
		 LCD_SEND_COMMAND(LCD_LINE2);  //发送命令使LCD指向第二行
		 i=16;
		 }
	 if(i!=0)
		 {
		  do
		   {
		    LCD_SEND_DATA(' ');		   //让LCD的相应位置显示空格
		   }while(--i!=0);
     }
 }
//*************************************************
//函数功能:向LCD发送命令
//输入变量:COMMAND
//输出变量:无
//调用模块:LCD_WAIT()
//*************************************************
void LCD_SEND_COMMAND(uchar COMMAND)
{
  LCD_WAIT();	 //等待空闲
  LCD_RS=0;		 //命令方式
  LCD_RW=0;		 //写方式
  LCD_E=1;
  SEND_IN=COMMAND;//写实际的命令到LCD
  LCD_E=0;
}
//*************************************************
//函数功能:向LCD发送数据
//输入变量:DATA
//输出变量:无
//调用模块:LCD_WAIT()
//*************************************************
void LCD_SEND_DATA(uchar DATA)
{
  LCD_WAIT();  //等待空闲
  LCD_RS=1;	   //数据方式
  LCD_RW=0;	   //写方式
  LCD_E=1;
  SEND_IN=DATA;//写实际的数据到LCD
  LCD_E=0;
}
//*************************************************
//函数功能:等待LCD空闲
//输入变量:无
//输出变量:无
//调用模块:LCD_GET_FLAG()
//*************************************************
void LCD_WAIT(void)
{
 uchar i;
 i=1000;  //定义等待时间,可以防止由于LCD损坏而使程序死循环
 do
   {
   	if((LCD_GET_FLAG()&0x80)==0) //判断BF是否为0
       {
	    break; 
		 }
	 }while(--i!=0); 

}
//*************************************************
//函数功能:检查LCD状态
//输入变量:无
//输出变量:LCD显示的当前状态
//调用模块:无
//*************************************************
uchar LCD_GET_FLAG(void)
{
  SEND_IN=0xff;
  LCD_RS=0;
  LCD_RW=1;
  LCD_E=1;
  _nop_();
  _nop_();
  return(SEND_IN);
}
//*************************************************
//函数功能:检查LCD状态
//输入变量:无
//输出变量:LCD显示的当前状态
//调用模块:无
//*************************************************
void LCD_DISPLAY_STR(uchar *DATA)
{
 int x=1,y=1,i=0;
// do
//  {
   if(y==1)
   {
    LCD_CLR(1);
    LCD_SEND_COMMAND(LCD_LINE1);//发送显示位置命令
    //for(;x<(17)&&*DATA!='\0';x++)
	  //for(;x<(17)&&i<8;x++)
       //{
       LCD_SEND_DATA(DATA[7]);		 //发送数据
	 //  }
		/* if(*DATA!='\0')				 //判断是否发送完毕
		     {
			  x=1;
			  y=2;						 //未完毕转到第二行显示
			  }	*/
	  DELAY();
/*	   }
	if(y==2)
    {
	 LCD_CLR(2);
	 LCD_SEND_COMMAND(LCD_LINE2);
	 for(;x<(17)&&*DATA!='\0';x++)
	   {
	   	LCD_SEND_DATA(DATA[i++]);
	    }
		if(*DATA!='\0')				 //判断是否发送完毕
		     {
			  x=1;
			  y=1;						 //未完毕转到第一行显示
			  }
	  DELAY();
	  }
   }while(*DATA!='\0');	*/
}
}
//*************************************************
//函数功能:延时3秒
//输入变量:无
//输出变量:无
//调用模块:无
//*************************************************
void DELAY(void)
{
 TMOD=0x02;
 TH0=0x06;
 TL0=0x06;
 TR0=1;
 ET0=1;
 EA=1;
 while(t!=8000);  //延时2秒   
 TR0=0;
 ET0=0;
 EA=0;
 t=0;
 }
//*****************************************
//
//定时器0的溢出中断程序
//
//*****************************************
void timer0(void) interrupt 1 using 0
{
 t++;

 }

⌨️ 快捷键说明

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