LCD 1602A
PIN
PIN | Symbol | Description |
---|---|---|
1 | VSS | 电源地 |
2 | VDD | 电源正极 |
3 | VL | 液晶显示偏压信号 |
4 | RS | 数据/命令选择端(H/L) |
5 | R/W | 读/写选择端(H/L) |
6 | E | 使能信号 |
7 | D0 | Data I/O |
8 | D1 | Data I/O |
9 | D2 | Data I/O |
10 | D3 | Data I/O |
11 | D4 | Data I/O |
12 | D5 | Data I/O |
13 | D6 | Data I/O |
14 | D7 | Data I/O |
15 | BLA | 背光源正极 |
16 | BLK | 背光源负极 |
/************************
AT89C51
LCD 1602A
*************************/
#include <reg52.h>
#include <intrins.h>
sbit RS = P1 ^ 0;
sbit RW = P1 ^ 1;
sbit EN = P2 ^ 5;
#define RS_CLR RS = 0
#define RS_SET RS = 1
#define RW_CLR RW = 0
#define RW_SET RW = 1
#define EN_CLR EN = 0
#define EN_SET EN = 1
#define DataPort P0
/***************************
* OSC 12 MHz
* T = tx2 + 5us
****************************/
void DelayUs2x(unsigned char t)
{
while (--t)
;
}
void DelayMs(unsigned char t)
{
while (--t)
{
// delay about 1ms
DelayUs2x(245);
DelayUs2x(245);
}
}
bit LCD_Check_Busy(void)
{
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80); // 0x80; 0x00;
}
void LCD_Write_Com(unsigned char com)
{
while (LCD_Check_Busy())
;
RS_CLR;
RW_CLR;
EN_SET;
DataPort = com;
_nop_();
EN_CLR;
}
void LCD_Write_Data(unsigned char Data)
{
while (LCD_Check_Busy())
;
RS_SET;
RW_CLR;
EN_SET;
DataPort = Data;
_nop_();
EN_CLR;
}
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
void LCD_Write_String(unsigned char x, unsigned char y, unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x); // Line 1
}
else
{
LCD_Write_Com(0xC0 + x); // Line 2
}
while (*s)
{
LCD_Write_Data(*s);
s++;
}
}
void LCD_Write_Char(unsigned char x, unsigned char y, unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data(Data);
}
void LCD_Init(void)
{
LCD_Write_Com(0x38); // display mode settings
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); // display off
LCD_Write_Com(0x01); // clear screen
LCD_Write_Com(0x06); // cursor display settings
DelayMs(5);
LCD_Write_Com(0x0C); // open display & cursor set
}
void main(void)
{
LCD_Init();
LCD_Clear();
while (1)
{
LCD_Write_Char(7, 0, '0');
LCD_Write_Char(8, 0, 'k');
LCD_Write_String(1, 1, "hello world");
while (1)
;
}
}
Page Source