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

📄 lcdok.c

📁 LCD1602的完整程序,调试OK 希望对新人有帮助
💻 C
字号:
#include<reg51.h>
#include<string.h>
#include"lcd.h"
    unsigned char l;
	unsigned char m;
	unsigned char n;
	unsigned char tmp; 
void delay_ms(int k)/*delay 1ms*,11.0592Mhz*/
{
    int i,j;
    for(i=0;i<k;i++)
      for(j=0;j<121;j++);
}
unsigned char ReadStatusLCM(void)
{
   P1=0xff;
   LCM_RS=0;/*Select Instruction Register*/
   LCM_RW=1;/*Reading*/
   LCM_EN=0;
   LCM_EN=0;/*forbid*/
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=1;
   while(P1&Busy);/*Busy Checker*/
   return P1;
}
unsigned char ReadDataLCM(void)
{
   LCM_RS=1;/*Select Data Register*/
   LCM_RW=1;/*Reading*/
   LCM_EN=0;
   LCM_EN=0;/*increase 1*/
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=1;/*enable*/
   return P1;
}

void WriteCommandLCM(unsigned char WCLCM,unsigned char BusyC)/*Instruction Register*/
{
      if(BusyC)
        ReadStatusLCM();/*If Busy Checker Ing then step here!*/
	  P1=WCLCM;
	  LCM_RS=0;/*instruction register*/
	  LCM_RW=0;
	  LCM_EN=0;/*Startup traciver Signal*/
	  LCM_EN=0;
	  LCM_EN=0;
	  LCM_EN=0;
      LCM_EN=0;
	  LCM_EN=0;
	  LCM_EN=0;
      LCM_EN=0;
	  LCM_EN=0;
	  LCM_EN=0;
	  LCM_EN=1;
}
void WriteDataLCM(unsigned char WDLCM)/*Writer Data to Register*/
{
   ReadStatusLCM();
   P1=WDLCM;   
   LCM_RS=1;/*data register*/
   LCM_RW=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=0;
   LCM_EN=1;
}
void InitLcd(void)
{
   delay_ms(200);/*Power On than delay_ms(15ms)*/
   WriteCommandLCM(0x01,1);/*clear screen*/
   delay_ms(5);
   WriteCommandLCM(0x04,1);/*note0x0d*/
   delay_ms(5);
   WriteCommandLCM(0x0c,1);/*0000 11000x0c*/
   delay_ms(5);
   WriteCommandLCM(0x14,1);/*0x06AC increasing*/
   delay_ms(5);
   WriteCommandLCM(0x3c,1);/*0x38*2 line ,5*7 dot ,8 data bit*/
   delay_ms(5);
   WriteCommandLCM(0x80,1);/*1000 0000*/
   delay_ms(5); 
}
void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData)
{
   Y&=1;/*Row*/
   X&=15;/*column*/
   if(Y)/*-----------------------First Location ,Second Let charater in the DDRAM to Display--------------------------------*/
    X|=0x40;
   X|=0x80;
   WriteCommandLCM(X,0);/*First Location,...... */
   delay_ms(18);/*the delay_ms it's not necessary,It must be delete*/
   WriteDataLCM(DData);/*......Second display character in the address*/
   delay_ms(25);/*This Delay function is very Important for the LCD Displaying Programe*/
}

void DisplayListChar(unsigned char X,unsigned char Y,unsigned char code *DData)
{
    unsigned char ListLength=0;
    Y&=0x01;
    X&=0x0f;
    while(X<=15)
    {
        DisplayOneChar(X,Y,DData[ListLength]);
        ListLength++;
        X++; 
        /*-------------------modification as follows-------------------*/
        if(X==0x0F)
        {
           X=0;
	       Y=!Y;
        }
       /*--------End Modification Switch 1 or 2 line Display---------*/
        delay_ms(100);
    } 
}

unsigned char table1[]={0x03,0x07,0x0f,0x1f,0x1f,0x1f,0x1f,0x1f,
                           0x18,0x1E,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
                           0x07,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
                           0x10,0x18,0x1c,0x1E,0x1E,0x1E,0x1E,0x1E,
                           0x0f,0x07,0x03,0x01,0x00,0x00,0x00,0x00,
                           0x1f,0x1f,0x1f,0x1f,0x1f,0x0f,0x07,0x01,
                           0x1f,0x1f,0x1f,0x1f,0x1f,0x1c,0x18,0x00,
                          0x1c,0x18,0x10,0x00,0x00,0x00,0x00,0x00};/*8 Line,8 Row*/
void main()
{
    
 unsigned char code str0[]="hello!Computer!Design By Palamer!This is my first LCD Programme!";
  InitLcd();
  delay_ms(200);
  DisplayListChar(0,0,str0);
  delay_ms(10);
  /*tmp=0x40;
  n=0;/
 
 
 for(l=0;l<8;l++)/first write your data to the CGRAM ADR 0x00-0x08/
  { 
        for(m=0;m<8;m++)
    	{
	      WriteCommandLCM((tmp+m),1);
	      delay_ms(200);
	      WriteDataLCM(table1[m]);
	      n++;
	      delay_ms(400);  
	    }
	    tmp=tmp+8;
  }
  for(l=0;l<4;l++)/display Up 4 line//
  {
   DisplayOneChar(0,(4+l),l);
   delay_ms(150);
  }
  for(l=4;l<8;l++)/display Down 4line
  {
   DisplayOneChar(1,(4+l-4),l);
   delay_ms(150);
  }*/
  
  for(;;);
}

⌨️ 快捷键说明

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