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

📄 lcd2x16.c

📁 Text LCD drivers for 8051
💻 C
字号:

#define _LCD216_C_

#include <io51.h>
#include <string.h>
#include "lcd2x16.h"


/*
** Local (Module-Level) Function Prototypes
*/
void LCD_InitDriver (void);
void LCD_WriteControl (unsigned char dat);
void LCD_WriteData (unsigned char dat);



/* -------------------- High-Level LCD Routines ------------------------ */

/*
** LCD_Init: Initialize the LCD.
*/
void LCD_Init (void)
{
  LCD_InitDriver();
  LCD_Clear();
  LCD_CursorOff();
}

/*
** LCD_Clear: Clear the LCD screen (also homes cursor).
*/
void LCD_Clear (void)
{
  LCD_WriteControl(0x01);
}

#if 0
/*
** LCD_Home: Position the LCD cursor at row 1, col 1.
*/
void LCD_Home (void)
{
  LCD_WriteControl(0x02);
}
#endif

/*
** LCD_DisplayCharacter: Display a single character,
**                       at the current cursor location.
*/
void LCD_DisplayCharacter (char a_char)
{
  LCD_WriteData (a_char);
}

/*
** LCD_DisplayString: Display a string at the specified row and column.
*/
void LCD_DisplayString (char row, char column, char *string)
{
  LCD_Cursor (row, column);
  while (*string)
    LCD_DisplayCharacter (*string++);
}

#if 0
/*
** LCD_DisplayStringCentered: Display a string centered on the specified row.
*/
void LCD_DisplayStringCentered (char row, char *string)
{
  char len = strlen (string);

  if (len <= 16) 
  {
    /* if the string is less than one line, center it ...*/
    char i;
    LCD_Cursor (row, 1);
    for (i=0; i<20; i++)
      LCD_DisplayCharacter (' ');
    LCD_DisplayString(row,((20 - len) / 2)+1,string);
  }
  else 
  {
    /* if the string is more than one line, display first 20 characters */
    char xdata temp[17];
    strncpy(temp, string, 16);
    temp[16] = 0;
    LCD_DisplayString(row,1,temp);
  }
}
#endif

/*
** LCD_Cursor: Position the LCD cursor at "row", "column".
*/
void LCD_Cursor (char row, char column)
{
  switch (row) 
  {
    case 1: 
      LCD_WriteControl (0x80 + column - 1); 
      break;
    case 2: 
      LCD_WriteControl (0xc0 + column - 1); 
      break;
    default: 
      break;
  }
}

/*
** LCD_DisplayScreen: Display an entire screen (32 characters).
**
**  inputs: ptr = pointer to a string containing the entire screen
**
**  example:
**    char screen[] = "0123456789012345"
**                    " This is a test "
**    DisplayScreen(screen);
**
*/
void LCD_DisplayScreen (char *ptr)
{
  LCD_DisplayRow(1,ptr+ 0);
  LCD_DisplayRow(2,ptr+16);
}

#ifdef LCD_FULL_DRIVER

/*
** LCD_WipeOnLR: Display an entire screen (32 characters) by
**               "wiping" it on (left to right).
**
**  inputs: ptr = pointer to a string containing the entire screen
**
*/
void LCD_WipeOnLR (char *ptr)
{
  /* "wipe" on new screen */
  char i;
  for (i=0; i<16; i++) 
  {
    LCD_Cursor(1,i+1);
    LCD_DisplayCharacter(*(ptr+ 0+i));
    LCD_Cursor(2,i+1);
    LCD_DisplayCharacter(*(ptr+16+i));
  }
}

/*
** LCD_WipeOnLR: Display an entire screen (32 characters) by
**               "wiping" it on (right to left).
**
**  inputs: ptr = pointer to a string containing the entire screen
**
*/
void LCD_WipeOnRL (char *ptr)
{
  /* "wipe" on new screen */
  char i;
  for (i=16; i>0; i--) 
  {
    LCD_Cursor(1,i);
    LCD_DisplayCharacter(*(ptr+ 0+i-1));
    LCD_Cursor(2,i);
    LCD_DisplayCharacter(*(ptr+16+i-1));
  }
}

/*
** LCD_WipeOffLR: "Wipe" screen left-to-right.
*/
void LCD_WipeOffLR (void)
{
  /* "wipe" off old screen (left to right) */
  char i;
  for (i=1; i<17; i++) 
  {
    #define BLOCK 0xff
    LCD_Cursor(1,i);
    LCD_DisplayCharacter(BLOCK);
    LCD_Cursor(2,i);
    LCD_DisplayCharacter(BLOCK);
  }
}

/*
** LCD_WipeOffRL: "Wipe" screen right-to-left.
*/
void LCD_WipeOffRL (void)
{
  /* "wipe" off old screen (right to left) */
  char i;
  for (i=16; i>0; i--) 
  {
    #define BLOCK 0xff
    LCD_Cursor(1,i);
    LCD_DisplayCharacter(BLOCK);
    LCD_Cursor(2,i);
    LCD_DisplayCharacter(BLOCK);
  }
}

#endif

/*
** LCD_DisplayRow: Display a string at the specified row.
*/
void LCD_DisplayRow (char row, char *string)
{
  char i;
  LCD_Cursor (row, 1);
  for (i=0; i<16; i++)
    LCD_DisplayCharacter (*string++);
}

#ifdef LCD_FULL_DRIVER

/*
** LCD_CursorLeft: Move the cursor left by one character.
*/
void LCD_CursorLeft (void)
{
  LCD_WriteControl (0x10);
}

/*
** LCD_CursorRight: Move the cursor right by one character.
*/
void LCD_CursorRight (void)
{
  LCD_WriteControl (0x14);
}

/*
** LCD_CursorOn: Turn the cursor on.
*/
void LCD_CursorOn (void)
{
  LCD_WriteControl (0x0d);
}

#endif

/*
** LCD_CursorOff: Turn the cursor off.
*/
void LCD_CursorOff (void)
{
  LCD_WriteControl (0x0c);
}

#ifdef LCD_FULL_DRIVER

/*
** LCD_DisplayOff: Turn Off LCD.
*/
void LCD_DisplayOff (void)
{
  LCD_WriteControl(0x08);
}

/*
** LCD_DisplayOn: Turn On LCD.
*/
void LCD_DisplayOn (void)
{
  LCD_WriteControl(0x0c);
}

#endif

/*
** LCD_PrintAsciiHexChar: Simply decodes chatr to ascii hex representation.
*/
void LCD_PrintAsciiHexChar(char row, char column, char buff)
{
  char msb,lsb;
  char tempBuff[2];
  tempBuff[1]=0;
  msb = (buff & 0xf0)>>4;
  lsb = (buff & 0x0f);
  
  if (msb<10)
  {
    tempBuff[0]=msb+'0';
    LCD_DisplayString (row, column, tempBuff);
  }
  else
  {
    tempBuff[0]=msb-0xa+'A';
    LCD_DisplayString (row, column, tempBuff);
  }

  if (lsb<10)
  {
    tempBuff[0]=lsb+'0';
    LCD_DisplayString (row, column+1, tempBuff);
  }
  else
  {
    tempBuff[0]=lsb-0xa+'A';
    LCD_DisplayString (row, column+1, tempBuff);
  }
}

/* -------------------- Low-Level LCD Routines ------------------------ */

/*
** LCD_InitDriver: Initialize the LCD driver.
*/
void LCD_InitDriver (void)
{
  LCD_WriteControl(0x38);
  LCD_WriteControl(0x38);
  LCD_WriteControl(0x38);
  LCD_WriteControl(0x06);
  LCD_WriteControl(0x0c);
}

/*
** LCD_WriteControl: Write a control instruction to the LCD
*/
void LCD_WriteControl (unsigned char dat)
{
  int xdata t1;
  LCD_RS_PIN = 0;
  for (t1=(int)0x10;t1>0;t1--);
  LCD_BUS_ADDRESS=dat;
  for (t1=(int)0x300;t1>0;t1--);
  LCD_RS_PIN = 1;
}

/*
** LCD_WriteData: Write one byte of dat to the LCD
*/
void LCD_WriteData (unsigned char dat)
{
  int xdata t1;
  LCD_BUS_ADDRESS=dat;
  for (t1=0x3;t1>0;t1--);
}

#undef _LCD216_C_

⌨️ 快捷键说明

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