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

📄 移动显示.c

📁 keil c环境下的1602的显示程序
💻 C
字号:
 //=======================================================//
//
//            1602液晶显示,左移,右移显示字符
//      作者:朱海峰
//========================================================//
#include <at89x51.h>
#include <intrins.h>

#define uchar unsigned char 
#define uint unsigned int
#define PORT P0

sbit RS=P3^7;
sbit RW=P3^6;
sbit EN=P3^5;



void delay(uint ms);
void w_cmd(uchar cmd);
void w_data(uchar dat);
void initial();
void clr();
//void flash();
void position(uchar pos);
bit check_busy();
//========================================================//
uchar code display1[]={"I LOVE C51!"};
//========================================================//
void main()
{ uchar i,j;
 delay(10);
 initial();
 while (1)
 { clr();
  position(0x10);
  i=0;
  while(display1[i]!= '\0')
  {
   w_data(display1[i]);
   i++;
  }
  for(j=0;j<14;j++)
  {
   w_cmd(0x18);
   delay(300);
  }
  delay(600);
  w_cmd(0x08);
  delay(200);
  w_cmd(0x0c);
  delay(500);
  position(0x50); //移位的同时,显示地址也随之移动
  i=0;
  while(display1[i]!= '\0')
  {
   w_data(display1[i]);
   delay(20);
   i++;
  }
  delay(600);
 }
}
//===========================================================//
void delay(uint ms)
{                                                                              
    int i;                    
    while(ms--)           
    {                                                                                       
   for(i = 0; i< 250; i++)          
       {                                      
        _nop_();     //相当于汇编中的NOP指令                                                

        _nop_();                                                                            

        _nop_();                                                                            

        _nop_();                                                                            

       }                                                                                    

    }                                               
} 
//===========================================================//
void clr()
{
 w_cmd(0x01);
 delay(5);
}
//===========================================================//
void w_data(uchar dat)
{
 while(check_busy());    //检测是否忙,忙则原地等待,不忙则向下运行。                

      RS = 1;  
  RW = 0;
  EN = 0; 
  PORT = dat;                                                                           

      _nop_();                                                                            

      _nop_();                                                                            

      _nop_();                                                                            

      _nop_();                                                                            

      EN = 1; 
  _nop_();                                                                            

      _nop_();                                                                            

      _nop_();                                                                            

      _nop_();                                                                            

      EN = 0;                                                                         
}
//===========================================================//
void w_cmd( uchar cmd)
{
 while(check_busy());        //检测是否忙,忙则原地等待,不忙则向下运行。            

  
      RS = 0;                                                                         
      RW = 0;                                                                         
      EN = 0;                                                                         
      _nop_();                                                                            
      _nop_();                                                                            
      PORT = cmd;                                                                           
      _nop_();                                                                            
      _nop_();                                                                            
      _nop_();                                                                            
      _nop_();                                                                            
      EN = 1;                                                                         
      _nop_();                                                                            
      _nop_();                                                                            
   _nop_();                                                                    
      _nop_();                                                                            
      EN = 0;                                                                         
}
//===========================================================//
void position(uchar pos)
{
 w_cmd(pos|0x80);        //数据指针=80+地址变量  
}
//==========================================================//
void initial()
{
 delay(15);               //等待LCD电源稳定                                    
    w_cmd(0x38);             //16*2显示,5*7点阵,8位数据]                            
    delay(5);                                                                              
    w_cmd(0x38);                                                                        
    delay(5);                                                                              
    w_cmd(0x38);                                                                        
    delay(5);                                                                              
    w_cmd(0x0c);             //显示开,光标关                                           
    delay(5);                                                                              
    w_cmd(0x06);             //移动光标                                                 
    delay(5);                                                                              
    w_cmd(0x01);             //清除LCD的显示内容                                        
    delay(5); 
}
//==========================================================//
bit check_busy()
{
     bit result;                                                                            
     RS = 0;                                                                            
     RW = 1;                                                                            
     EN = 1;                                                                            
     _nop_();                                                                               
     _nop_();                                                                               
     _nop_();                                                                               
     _nop_();                                                                               
     result = (bit)(PORT&0x80); //xxxx'xxxx                                                   
     EN = 0;               //&1000'0000                                               

    
     return result;          //=1000'0000 /0000'0000                                        
}                            //(bit)1000'0000=1/(bit)0000'0000=0  
//===========================================================//

⌨️ 快捷键说明

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