新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32 LCD1602驅(qū)動(dòng)程序

STM32 LCD1602驅(qū)動(dòng)程序

作者: 時(shí)間:2016-11-20 來源:網(wǎng)絡(luò) 收藏
#ifndef LCD1602_STM32_H

#define LCD1602_STM32_H

本文引用地址:http://butianyuan.cn/article/201611/318951.htm

#define LCD_RS_1 GPIOE->BSRR=GPIO_Pin_0 //PE.0--(LCD)RS
#define LCD_RS_0 GPIOE->BRR =GPIO_Pin_0
#define LCD_RW_1 GPIOE->BSRR=GPIO_Pin_1//PE.1--(LCD)RW
#define LCD_RW_0 GPIOE->BRR =GPIO_Pin_1
#define LCD_EN_1 GPIOE->BSRR=GPIO_Pin_2 //PE.2--(LCD)E
#define LCD_EN_0 GPIOE->BRR =GPIO_Pin_2
#define DATAOUT GPIOD->ODR //PD[0..7]--(LCD)D0~D7

#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif

extern void delay_nms(unsigned long n);
extern void delay_nus(unsigned long n);

uint8_t LCD_busy(void); //讀LCD忙碌狀態(tài),直到LCD1602不忙為止
void Wait_Leisure(void);//一直等待到LCD1602空閑
void LCD_WriteInitcmd(uchar cmd);//寫入LCD初始化時(shí)的命令,不可以檢測忙狀態(tài)
void LCD_Writecmd(uchar cmd);//寫指令到LCD1602,指令數(shù)據(jù)占一個(gè)字節(jié)
void LCD_Writedata(uchar dat);//寫一字節(jié)數(shù)據(jù)到LCD1602
void LCD_pos(uchar pos);//設(shè)定顯示位置 00h~27h,40h~47h
void LCD_Setpos(uchar row,uchar col);//根據(jù)習(xí)慣設(shè)定顯示位置
void LCD_DispChar(char ch);//顯示一個(gè)字符
void LCD_Setpos_DispChar(uchar row,uchar col,char ch);//在指定位置顯示一個(gè)字符
void LCD_DispString(char str[]);//使LCD1602顯示一個(gè)字符串,顯示位置需提前設(shè)定
void LCD_Setpos_DispString(uchar row,uchar col,char str[]);//使LCD1602從指定位置開始顯示一個(gè)字符串
void LCD_Dispnum(uint32_t num);//顯示一個(gè)不超過8位的整數(shù),顯示位置需提前設(shè)置
void LCD_Setpos_Dispnum(uchar row,uchar col,uint32_t num);//在指定位置顯示一個(gè)不超過8位的整數(shù)
void LCD_DispDecimal(uint32_t num,uchar dot);//顯示一個(gè)有效位不超過8位的浮點(diǎn)數(shù),顯示位置需要提前設(shè)定
//在指定位置顯示一個(gè)有效位不超過8位的浮點(diǎn)數(shù)
void LCD_Setpos_DispDecimal(uchar row,uchar col,uint32_t num,uchar dot);
//顯示日歷,顯示日期與時(shí)間
void LCD_DispDateTime(uint32_t year,uchar month,uchar day,uchar hour,uchar min,uchar sec);
//顯示秒表,顯示時(shí),分,秒,10毫秒,精確到10ms
void LCD_DispStopWatch(uchar hour,uchar min,uchar sec,uchar tenms);
//顯示頻率計(jì),動(dòng)態(tài)自動(dòng)調(diào)整頻率顯示
void LCD_DispFreq(uint32_t freq);
void LCD_ShiftLeft(void);//屏幕整體左移一格,用于滾動(dòng)顯示
void LCD_ShiftRight(void);//屏幕整體右移一格,用于滾動(dòng)顯示
void LCD_Clear(void);//清屏,清除顯示
void LCD_Return(void);//使光標(biāo)還回原點(diǎn)位置
void LCD_Close(void);//關(guān)顯示
void LCD_Open(void);//開顯示
void LCD_FlickerChar(uchar row,uchar col);//使指定位置字符閃爍,不顯示光標(biāo)
void LCD_CloseFlicker(void);//關(guān)閉字符閃爍
void LCD_FlickerScreen(void);//屏幕秒閃爍一次

void LCD_Init(void);

#endif


#include "stm32f10x.h"
#include "LCD1602.h"
#ifndef LCD1602_STM32_C
#define LCD1602_STM32_C


uint8_t LCD_busy(void)
{
LCD_RS_0;
delay_nus(1);
LCD_RW_1;
delay_nus(1);
do
{
LCD_EN_0;
delay_nus(200);
LCD_EN_1;
delay_nus(200);
}
while(GPIOD->IDR & 0x80);
LCD_EN_0;
return (uint8_t)0;
}

void Wait_Leisure(void)
{
LCD_busy();
}

void LCD_WriteInitcmd(uchar cmd)
{
LCD_RS_0;
delay_nus(1);
LCD_RW_0;
delay_nus(1);
DATAOUT = cmd;
LCD_EN_0;
delay_nus(300);
LCD_EN_1;
delay_nus(300);
LCD_EN_0;
delay_nms(2);
}

void LCD_Writecmd(uchar cmd)
{
while(LCD_busy());
LCD_RS_0; //對同一個(gè)寄存器的兩次寫入,中間延時(shí)一會(huì)
delay_nus(1);
LCD_RW_0;
delay_nus(1);
LCD_EN_0;
delay_nus(300);//產(chǎn)生脈沖
DATAOUT = cmd;
LCD_EN_1;
delay_nus(300);//必要的延時(shí)
LCD_EN_0; //下降沿,LCD1602開始工作
}

void LCD_Writedata(uchar dat)
{
while(LCD_busy());//等待LCD1602空閑
LCD_RS_1;
delay_nus(1);
LCD_RW_0;
delay_nus(1);
DATAOUT = dat;
LCD_EN_1;//先拉高
delay_nus(300);//很重要的延時(shí),經(jīng)調(diào)試,延時(shí)300us以上才可以
LCD_EN_0; //下降沿,開始寫入有效數(shù)據(jù)
}

void LCD_pos(uchar pos)
{
LCD_Writecmd(pos | 0x80);
}

void LCD_Setpos(uchar row,uchar col)
{
if(row==1) LCD_Writecmd(col | 0x80);
else LCD_Writecmd(col | 0xC0);
}

void LCD_DispChar(char ch)
{
LCD_Writedata(ch);
}

void LCD_Setpos_DispChar(uchar row,uchar col,char ch)
{
LCD_Setpos(row,col);
LCD_Writedata(ch);
}

void LCD_DispString(char str[])
{
uchar i=0;
while(str[i] !=