lcd_drvrs.c

来自「Realtime clock DS12c887」· C语言 代码 · 共 131 行

C
131
字号
/* ================== LCD Drivers =============*/

// AUTHOR : R Raghunathan

/*======== Brief Description of program =======*/

/* Software drivers for 2L x 16C LCD module 
   with HD44780.

   Processor used is Silabs C8051F020 with a 2.0 MHz 
   crystal.  Each M/C cycle 0.5us 
      
   The LCD time delays are interrupt based from MAIN
   and after intialization, the Busy Bit is checked 
   before writing  */

/*==============Include Files =================*/

#include <reg52.h>
#include <stdio.h>
#include <string.h>


/*============== Bit Defines ==================*/

sbit LCD_RS   = P2^1;                            // Register Select
sbit LCD_RW   = P2^2;                            // Read write
sbit LCD_ENB  = P2^3;
sbit LCD_BUSY = P0^7;                            // LCD Data Port is P0;


/*=============================================*/

#define     LCD_PORT    P0                       // PO connected to LCD ( 8 bit mode )
sfr         P0MDOUT =   0xA4 ;

/*===============  Globals  ===================*/

unsigned char  string[32];

/*==========Function Prototypes ===============*/

extern void msDelay (int);
void chkBusy (void);
void wr_lcdReg (char);
void wr_lcdDat (char);
void init_lcd (void);
void display (char *);	

 /*============================================*/

 /*============================================*/

void init_lcd(void)								 // Init routine as per HD44780 data sheet
{
    msDelay(16);                                 // Wait more than 15ms
    wr_lcdReg(0x38);                             // Initialize for 8 bit , 2 line
    msDelay(5);                                  // Wait more than 4.1ms
    wr_lcdReg(0x38); 
    msDelay(1);                                  // Wait more than 0.1ms
    wr_lcdReg(0x38);    
    msDelay(1);                                  // Wait more than 0.1ms
    wr_lcdReg(0x0C);                             // Display On; Cursor Off; Blink off
    wr_lcdReg(0x01);                             // Clear the display
    msDelay(5);                                  // Wait more than 1.64ms
}

/*================================================*/

void wr_lcdReg(char command)					 // Write a command to the LCD.
{
    chkBusy();                                   // Is the LCD ready to take the command?
    LCD_RS = 0;                                  // Register Select Low for writing command
    LCD_RW = 0;                                  // R!W low for write
    LCD_PORT = command;                          // Command byte to register
    LCD_ENB = 1;
    LCD_ENB = 0;                                 // Pulse the enable bit high to write
}

/*=============================================*/

void wr_lcdDat(char ascii)						 // Write a character to the LCD.
{
    chkBusy();                                   // Is the LCD ready to take the word?
    LCD_RS = 1;                                  // Register Select High for writing data
    LCD_RW = 0;                                  // R!W low for write
    LCD_PORT = ascii;                            // Data byte to data port
    LCD_ENB = 1;
    LCD_ENB = 0;                                 // Pulse the enable bit high to write
}

/*==============================================*/

void chkBusy(void)
{
    P0 = 0xff ;                                  // Set port as input
	P0MDOUT  = 0x7F;                             // Switch P0.7 alone to Open Drain
	LCD_RS   = 0;
	LCD_RW   = 1;

	while (LCD_BUSY)
 	{
  	  LCD_ENB = 0;
	  LCD_ENB = 1;
    }
    P0MDOUT   = 0xFF;                            // Back to P0 as Push Pull
}


/*==============================================*/

void display (char *string)						 // Write the 2 line message to LCD
{
  int i;

  wr_lcdReg(0x01);                               // Clear the display
  msDelay(5);

  for (i=0; i<=strlen(string)-1; ++i)
   {
    if (i==16)
       wr_lcdReg(0xc0);
    if (i==32)
       wr_lcdReg(0x80);
    wr_lcdDat(string[i]);
   }
}

/*=============================================*/

⌨️ 快捷键说明

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