LCD1602(Liquid Crystal Display)是一種工業(yè)字符型液晶,能夠同時(shí)顯示 16×02 即 32 字符(16列兩行)
第 1 腳: VSS 為電源地
第 2 腳: VDD 接 5V 正電源
第 3 腳: VL 為液晶顯示器對(duì)比度調(diào)整端,接正電源時(shí)對(duì)比度最弱,接地時(shí)對(duì)比度最高,對(duì)比度 過(guò)高時(shí)會(huì)產(chǎn)生“鬼影”,使用時(shí)可以通過(guò)一個(gè)
10K 的電位器調(diào)整對(duì)比度。第 4 腳:RS 為寄存器選擇,高電平時(shí)選擇數(shù)據(jù)寄存器、低電平時(shí)選擇指令寄存器。
第 5 腳:R/W 為讀寫(xiě)信號(hào)線,高電平時(shí)進(jìn)行讀操作,低電平時(shí)進(jìn)行寫(xiě)操作。當(dāng) RS 和 R/W 共 同為低電平時(shí)可以寫(xiě)入指令或者顯示地址,當(dāng)RS 為低電平 R/W 為高電平時(shí)可以讀忙信號(hào), 當(dāng) RS 為高電平 R/W 為低電平時(shí)可以寫(xiě)入數(shù)據(jù)。
第 6 腳:E 端為使能端,當(dāng) E 端由高電平跳變成低電平時(shí),液晶模塊執(zhí)行命令。
第 7-14 腳:D0~D7 為 8 位雙向數(shù)據(jù)線。
第 15 腳:背光源正極。
第 16 腳:背光源負(fù)極。
開(kāi)發(fā)邏輯
- 在哪顯示
例如第二行第一個(gè)字符的地址是 40H,那么是否直接寫(xiě)入 40H 就可以將光標(biāo)定位在第二行第一個(gè)字符的位置呢?這樣不行,因?yàn)閷?xiě)入顯示地址時(shí)要求最高位 D7 恒定為高電平 1 所以實(shí)際寫(xiě)入的數(shù)據(jù)應(yīng)該是 01000000B(40H) +10000000B(80H)=11000000B(C0H)
- 顯示什么
讀寫(xiě)操作時(shí)序
讀操作
檢測(cè)忙信號(hào)的時(shí)候需要讀時(shí)序
/*當(dāng) RS 為低電平 R/W 為高電平時(shí)可以讀忙信號(hào)讀忙信號(hào)和光標(biāo)地址。
BF(bit7):為忙標(biāo)志位,高電平表示忙,此時(shí)模塊不能接收命令或者數(shù)據(jù),
如果為低電平表示不忙。
*/
void check_busy()//檢測(cè)忙信號(hào)
{
char tmp = 0x80;
dataBuff = 0x80;
while(tmp & 0x80){//檢測(cè)BF,1忙 0閑
RS = 0;
RW = 1;
EN = 0;
_nop_();
EN = 1;
_nop_();
_nop_();
tmp = dataBuff;
_nop_();
EN = 0;
_nop_();
}
}
寫(xiě)操作
數(shù)序參數(shù)
/*
RS 為寄存器選擇,高電平時(shí)選擇數(shù)據(jù)寄存器、低電平時(shí)選擇指令寄存器。
*/
void Write_Cmd_Func(char cmd)
{
check_busy();
RS = 0;
RW = 0;
EN = 0;
_nop_();
dataBuff = cmd;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void Write_Data_Func(char dataShow)
{
check_busy();
RS = 1;
RW = 0;
EN = 0;
_nop_();
dataBuff = dataShow;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
LCD1602的初始化函數(shù)
void LCD1602_INIT()
{
//(1)延時(shí) 15ms
Delay15ms();
//(2)寫(xiě)指令 38H(不檢測(cè)忙信號(hào))
Write_Cmd_Func(0x38);
//(3)延時(shí) 5ms
Delay5ms();
//(4)以后每次寫(xiě)指令,讀/寫(xiě)數(shù)據(jù)操作均需要檢測(cè)忙信號(hào)
check_busy();
//(5)寫(xiě)指令 38H:顯示模式設(shè)置
Write_Cmd_Func(0x38);
//(6)寫(xiě)指令 08H:顯示關(guān)閉
Write_Cmd_Func(0x08);
//(7)寫(xiě)指令 01H:顯示清屏
Write_Cmd_Func(0x01);
//(8)寫(xiě)指令 06H:顯示光標(biāo)移動(dòng)設(shè)置
Write_Cmd_Func(0x06);
//(9)寫(xiě)指令 0CH:顯示開(kāi)及光標(biāo)設(shè)置}
Write_Cmd_Func(0x0c);
}
C51實(shí)驗(yàn)顯示:hello world
#include "reg52.h"
#include "intrins.h"
#define dataBuff P0
sbit RS = P2^6;
sbit RW = P2^5;
sbit EN = P2^7;
/*當(dāng) RS 為低電平 R/W 為高電平時(shí)可以讀忙信號(hào)讀忙信號(hào)和光標(biāo)地址。
BF(bit7):為忙標(biāo)志位,高電平表示忙,此時(shí)模塊不能接收命令或者數(shù)據(jù),
如果為低電平表示不忙。
*/
void check_busy()//檢測(cè)忙信號(hào)
{
char tmp = 0x80;
dataBuff = 0x80;
while(tmp & 0x80){//檢測(cè)BF,1忙 0閑
RS = 0;
RW = 1;
EN = 0;
_nop_();
EN = 1;
_nop_();
_nop_();
tmp = dataBuff;
_nop_();
EN = 0;
_nop_();
}
}
/*
RS 為寄存器選擇,高電平時(shí)選擇數(shù)據(jù)寄存器、低電平時(shí)選擇指令寄存器。
*/
void Write_Cmd_Func(char cmd)
{
check_busy();
RS = 0;
RW = 0;
EN = 0;
_nop_();
dataBuff = cmd;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void Write_Data_Func(char dataShow)
{
check_busy();
RS = 1;
RW = 0;
EN = 0;
_nop_();
dataBuff = dataShow;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 9;
j = 244;
do
{
while (--j);
} while (--i);
}
void Delay15ms() //@11.0592MHz
{
unsigned char i, j;
i = 27;
j = 226;
do
{
while (--j);
} while (--i);
}
void LCD1602_INIT()
{
//(1)延時(shí) 15ms
Delay15ms();
//(2)寫(xiě)指令 38H(不檢測(cè)忙信號(hào))
Write_Cmd_Func(0x38);
//(3)延時(shí) 5ms
Delay5ms();
//(4)以后每次寫(xiě)指令,讀/寫(xiě)數(shù)據(jù)操作均需要檢測(cè)忙信號(hào)
check_busy();
//(5)寫(xiě)指令 38H:顯示模式設(shè)置
Write_Cmd_Func(0x38);
//(6)寫(xiě)指令 08H:顯示關(guān)閉
Write_Cmd_Func(0x08);
//(7)寫(xiě)指令 01H:顯示清屏
Write_Cmd_Func(0x01);
//(8)寫(xiě)指令 06H:顯示光標(biāo)移動(dòng)設(shè)置
Write_Cmd_Func(0x06);
//(9)寫(xiě)指令 0CH:顯示開(kāi)及光標(biāo)設(shè)置}
Write_Cmd_Func(0x0c);
}
void lcd1602_Show_line(int row,int col,char *SendData)
{
switch(row){
case 1:
Write_Cmd_Func(0x80+col);
while(*SendData){
Write_Data_Func(*SendData);
SendData++;
}
break;
case 2:
Write_Cmd_Func(0x80+0x40+col);
while(*SendData){
Write_Data_Func(*SendData);
SendData++;
}
break;
}
}
void main()
{
LCD1602_INIT();
lcd1602_Show_line(1,3,"hello world");
}
STM32HAL庫(kù)
RS、RW、EN三根信號(hào)線經(jīng)常需要進(jìn)行拉高/拉低操作,可以進(jìn)行封裝
#define RS_GPIO_Port GPIOB
#define RW_GPIO_Port GPIOB
#define EN_GPIO_Port GPIOB
#define RS_Pin GPIO_PIN_1
#define RW_Pin GPIO_PIN_2
#define EN_Pin GPIO_PIN_10#define RS_HIGH HAL_GPIO_WritePin(RS_GPIO_Port, RS_Pin, GPIO_PIN_SET)
#define RS_LOW HAL_GPIO_WritePin(RS_GPIO_Port, RS_Pin, GPIO_PIN_RESET)
#define RW_HIGH HAL_GPIO_WritePin(RW_GPIO_Port, RW_Pin, GPIO_PIN_SET)
#define RW_LOW HAL_GPIO_WritePin(RW_GPIO_Port, RW_Pin, GPIO_PIN_RESET)
#define EN_HIGH HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_SET)
#define EN_LOW HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_RESET)
如何將一個(gè)字節(jié)的數(shù)據(jù)按位一次性發(fā)送到GPIOA的8個(gè)管腳?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-440070.html
GPIOA->ODR = cmd;
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-440070.html
#define RS_GPIO_Port GPIOB
#define RW_GPIO_Port GPIOB
#define EN_GPIO_Port GPIOB
#define RS_Pin GPIO_PIN_1
#define RW_Pin GPIO_PIN_2
#define EN_Pin GPIO_PIN_10
#define RS_HIGH HAL_GPIO_WritePin(RS_GPIO_Port, RS_Pin, GPIO_PIN_SET)
#define RS_LOW HAL_GPIO_WritePin(RS_GPIO_Port, RS_Pin, GPIO_PIN_RESET)
#define RW_HIGH HAL_GPIO_WritePin(RW_GPIO_Port, RW_Pin, GPIO_PIN_SET)
#define RW_LOW HAL_GPIO_WritePin(RW_GPIO_Port, RW_Pin, GPIO_PIN_RESET)
#define EN_HIGH HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_SET)
#define EN_LOW HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_RESET)
void Write_Cmd_Func(uint8_t cmd)
{
RS_LOW;
RW_LOW;
EN_LOW;
GPIOA->ODR = cmd;
HAL_Delay(5);
EN_HIGH;
HAL_Delay(5);
EN_LOW;
}
void Write_Data_Func(uint8_t dataShow)
{
RS_HIGH;
RW_LOW;
EN_LOW;
GPIOA->ODR = dataShow;
HAL_Delay(5);
EN_HIGH;
HAL_Delay(5);
EN_LOW;
}
void LCD1602_INIT(void)
{
//(1)延時(shí) 15ms
HAL_Delay(15);
//(2)寫(xiě)指令 38H(不檢測(cè)忙信號(hào))
Write_Cmd_Func(0x38);
//(3)延時(shí) 5ms
HAL_Delay(5);
//(4)以后每次寫(xiě)指令,讀/寫(xiě)數(shù)據(jù)操作均需要檢測(cè)忙信號(hào)
//(5)寫(xiě)指令 38H:顯示模式設(shè)置
Write_Cmd_Func(0x38);
//(6)寫(xiě)指令 08H:顯示關(guān)閉
Write_Cmd_Func(0x08);
//(7)寫(xiě)指令 01H:顯示清屏
Write_Cmd_Func(0x01);
//(8)寫(xiě)指令 06H:顯示光標(biāo)移動(dòng)設(shè)置
Write_Cmd_Func(0x06);
//(9)寫(xiě)指令 0CH:顯示開(kāi)及光標(biāo)設(shè)置}
Write_Cmd_Func(0x0c);
}
void LCD1602_showLine(char row, char col, char *string)
{
switch(row){
case 1:
Write_Cmd_Func(0x80+col);
while(*string){
Write_Data_Func(*string);
string++;
}
break;
case 2:
Write_Cmd_Func(0x80+0x40+col);
while(*string){
Write_Data_Func(*string);
string++;
}
break;
}
}
main函數(shù)里:
//char position = 0x80 + 0x05;
//char dataShow = 'C';
LCD1602_INIT();
//Write_Cmd_Func(position);//選擇要顯示的地址
//Write_Data_Func(dataShow);//發(fā)送要顯示的字符
LCD1602_showLine(1,5,"hello world");
到了這里,關(guān)于STM32—LCD1602的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!