lcd.c
来自「8051试验程序 基础教材」· C语言 代码 · 共 141 行
C
141 行
#include <string.h>
#include "target.h"
#include "lcd.h"
#define LCD_COMMAND LCD_PORT_LOW
#define LCD_CHARACTER LCD_PORT_HIGH
static void E_pulse()
{
/* set E high */
LCD_E = LCD_PORT_HIGH;
/* delay */
ms_delay( 10 );
/* set E low */
LCD_E = LCD_PORT_LOW;
}
static void set_data( unsigned char data )
{
LCD_D4 = (data & 0x01) ? 1 : 0;
LCD_D5 = (data & 0x02) ? 1 : 0;
LCD_D6 = (data & 0x04) ? 1 : 0;
LCD_D7 = (data & 0x08) ? 1 : 0;
}
static void send_to_lcd( unsigned char data, unsigned char type )
{
/* enable write */
LCD_RW = LCD_PORT_LOW;
/* 2 ms delay */
ms_delay( 2 );
/* set high nibble of command */
set_data( (data >> 4) );
/* set RS port to command mode */
LCD_RS = type;
/* pulse to set D4-D7 bits */
E_pulse();
/* set low nibble of command */
set_data( (data & 0x0F) );
/* set RS port to command mode */
LCD_RS = type;
/* pulse to set D4-D7 bits */
E_pulse();
}
void lcd_init()
{
/* LCD initialization */
/* step by step (from Gosho) - from DATASHEET */
/* clear "command" bits */
LCD_RS = LCD_PORT_LOW;
LCD_RW = LCD_PORT_LOW;
LCD_E = LCD_PORT_LOW;
/* 110 ms delay */
ms_delay( 110 );
/* set D4 and D5 high */
LCD_D4 = LCD_PORT_HIGH;
LCD_D5 = LCD_PORT_HIGH;
E_pulse();
/* 10 ms delay */
ms_delay( 10 );
/* set D4 and D5 high */
LCD_D4 = LCD_PORT_HIGH;
LCD_D5 = LCD_PORT_HIGH;
E_pulse();
/* 10 ms delay */
ms_delay( 10 );
/* set D4 and D5 high */
LCD_D4 = LCD_PORT_HIGH;
LCD_D5 = LCD_PORT_HIGH;
E_pulse();
/* 10 ms delay */
ms_delay( 10 );
/* set D4 to low and D5 to high */
LCD_D4 = LCD_PORT_LOW;
LCD_D5 = LCD_PORT_HIGH;
E_pulse();
/* enable display */
send_lcd_command( DISP_ON );
/* clear display */
send_lcd_command( CLR_DISP );
}
void lcd_light( unsigned char on )
{
/* turn on/off lcd light */
if( on )
{
LCD_LIGHT = 1;
}
else
{
LCD_LIGHT = 0;
}
}
void send_lcd_command( unsigned char command )
{
send_to_lcd( command, LCD_COMMAND );
}
void send_lcd_char( unsigned char c )
{
send_to_lcd( c, LCD_CHARACTER );
}
void send_lcd_text( char* str )
{
/* loop over string and send each character */
for( int i=0; i<strlen(str); ++i)
{
send_lcd_char( str[i] );
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?