📄 lcd_lm016l.c
字号:
// ************************************************
// *** LCD_4BIT Driver V1.1 ***
// *** LCD.C ***
// ************************************************
//#define LCD_C
#include <pic.h>
// ***** Define I/O pins ***** //
#define BIT7 0x80
#define BIT6 0x40
#define BIT5 0x20
#define BIT4 0x10
#define BIT3 0x08
#define BIT2 0x04
#define BIT1 0x02
#define BIT0 0x01
#define XTAL 4 //晶振频率,单位MHz
/****************************************************************/
#define LCD_EN (1 << 2) //引脚定义
#define LCD_RS (1 << 0)
#define LCD_RW (1 << 1)
#define lcd_set_e() (PORTD |= LCD_EN) //置位与清零
#define lcd_set_rs() (PORTD |= LCD_RS)
#define lcd_set_rw() (PORTD |= LCD_RW)
#define lcd_clear_e() (PORTD &= ~LCD_EN)
#define lcd_clear_rs() (PORTD &= ~LCD_RS)
#define lcd_clear_rw() (PORTD &= ~LCD_RW)
/****************************************************************/
#define LCD_ON 0x0C
#define LCD_CURS_ON 0x0E
#define LCD_OFF 0x08
#define LCD_HOME 0x02
#define LCD_CLEAR 0x01
#define LCD_NEW_LINE 0xC0
#define LCD_FUNCTION_SET 0x28
#define LCD_MODE_SET 0x06
//******************************************************
void delay_1us(void) //1us延时函数
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<(unsigned int)(XTAL*143-2);i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
{
delay_1ms();
}
}
void LCD_INIT(void)
{
TRISD=0X00; // LCD port output
//PORTD = 0x30; // Load high-data to port
lcd_clear_rw(); // Set LCD to write
lcd_clear_rs(); // Set LCD to command
lcd_set_e(); // Write data to LCD
asm("nop");
asm("nop");
lcd_clear_e(); // Disable LCD
delay_nus(40);
//lcd_clear_rw() ; // Set LCD to write
//lcd_clear_rs(); // Set LCD to command
//lcd_set_e(); // Write data to LCD
//asm("nop");
//asm("nop");
//lcd_clear_e(); // Disable LCD
//delay_nus(40);
//lcd_set_e(); // Write data to LCD
//asm("nop");
//asm("nop");
//lcd_clear_e(); // Disable LCD
//delay_nus(40);
PORTD = 0x20;
lcd_set_e(); // Write data to LCD
asm("nop");
asm("nop");
lcd_clear_e(); // Disable LCD
delay_nus(40);
}
//*****************************************************//
// This routine will return the busy flag from the LCD //
//*****************************************************//
void LCD_Busy ( void )
{
unsigned char high,low;
TRISD=0XF0; // Make I/O Port input
do
{
lcd_set_rw(); // Set LCD to READ
lcd_clear_rs();
lcd_set_e();
delay_nus(3);
high = PORTD; // read the high nibble.
lcd_clear_e(); // Disable LCD
lcd_set_e();
asm("nop");
asm("nop");
low = PORTD; // read the low nibble.
lcd_clear_e(); // Disable LCD
} while(high & 0x80);
delay_nus(20);
}
// ********************************************** //
// *** Write a control instruction to the LCD *** //
// ********************************************** //
void LCD_WriteControl (unsigned char CMD)
{
//char temp;
LCD_Busy(); // Test if LCD busy
TRISD=0X00; // LCD port output
//temp=PORTD;
//temp=temp&BIT3;
PORTD =CMD & 0xf0; // Load high-data to port ??????????????
lcd_clear_rw(); // Set LCD to write
lcd_clear_rs(); // Set LCD to command
lcd_set_e(); // Write data to LCD
asm("nop");
asm("nop");
lcd_clear_e(); // Disable LCD
PORTD =CMD<<4; // Load low-data to port
lcd_clear_rw(); // Set LCD to write
lcd_clear_rs(); // Set LCD to command
lcd_set_e(); // Write data to LCD
asm("nop");
asm("nop");
lcd_clear_e(); // Disable LCD
}
// ***************************************** //
// *** Write one byte of data to the LCD *** //
// ***************************************** //
void LCD_WriteData (unsigned char Data)
{
//char temp;
LCD_Busy(); // Test if LCD Busy
TRISD=0X00; // LCD port output
//temp=PORTD;
//temp=temp&BIT3;
PORTD =Data & 0xf0; // Load high-data to port
lcd_clear_rw() ; // Set LCD to write
lcd_set_rs(); // Set LCD to data
lcd_set_e(); // Write data to LCD
asm("nop");
asm("nop");
lcd_clear_e(); // Disable LCD
PORTD = Data << 4; // Load low-data to port
lcd_clear_rw() ; // Set LCD to write
lcd_set_rs(); // Set LCD to data
lcd_set_e(); // Write data to LCD
asm("nop");
asm("nop");
lcd_clear_e(); // Disable LCD
}
// ********************************* //
// *** Initialize the LCD driver *** //
// ********************************* //
void Init_LCD(void)
{
LCD_INIT();
//TRISD=0X00; // LCD port output
LCD_WriteControl (LCD_FUNCTION_SET);
LCD_WriteControl (LCD_OFF);
LCD_WriteControl (LCD_CLEAR);
LCD_WriteControl (LCD_MODE_SET);
LCD_WriteControl (LCD_ON);
LCD_WriteControl (LCD_HOME);
//LCD_WriteControl (0x90);
}
// ************************************************ //
// *** Clear the LCD screen (also homes cursor) *** //
// ************************************************ //
void LCD_Clear(void)
{
LCD_WriteControl(0x01);
}
// *********************************************** //
// *** Position the LCD cursor at row 1, col 1 *** //
// *********************************************** //
void LCD_Home(void)
{
LCD_WriteControl(0x02);
}
// ****************************************************************** //
// *** Display a single character, at the current cursor location *** //
// ****************************************************************** //
void LCD_DisplayCharacter (char Char)
{
LCD_WriteData (Char);
}
// *************************************************** //
// *** 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;
case 3: LCD_WriteControl (0x94 + column - 1); break;
case 4: LCD_WriteControl (0xd4 + column - 1); break;
default: break;
}
}
/*
// ********************************************************************* //
// *** Display a string at the specified row and column, using FLASH *** //
// ********************************************************************* //
void LCD_DisplayString_F (char row, char column , unsigned char *string)
{
LCD_Cursor (row, column);
while (*string)
{
LCD_DisplayCharacter (*string++);
}
}
*/
// ******************************************************************* //
// *** Display a string at the specified row and column, using RAM *** //
// ******************************************************************* //
void LCD_DisplayString (char row, char column ,unsigned char *string)
{
LCD_Cursor (row, column);
while (*string)
LCD_DisplayCharacter (*string++);
}
// ************************** //
// *** Turn the cursor on *** //
// ************************** //
void LCD_Cursor_On (void)
{
LCD_WriteControl (LCD_CURS_ON);
}
// *************************** //
// *** Turn the cursor off *** //
// *************************** //
void LCD_Cursor_Off (void)
{
LCD_WriteControl (LCD_ON);
}
// ******************** //
// *** Turn Off LCD *** //
// ******************** //
void LCD_Display_Off (void)
{
LCD_WriteControl(LCD_OFF);
}
// ******************* //
// *** Turn On LCD *** //
// ******************* //
void LCD_Display_On (void)
{
LCD_WriteControl(LCD_ON);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -