一、前期準備
單片機:STM32F103ZET6
開發環境:MDK5.14
庫函數:标準庫V3.5
LCD12864子產品:淘寶有售
二、實驗效果
3.3V LCD12864
5V LCD12864
三、驅動原理
5V和3.3V子產品均可以用STM32直接驅動,而不像1602那樣5V必須是用5V驅動。注意的是子產品上面的PSB腳接VCC,接VCC的話是采用并口傳輸資料,接GND采用序列槽傳輸,序列槽程式會在以後的部落格中跟新。RST腳接VCC,此處沒有用到硬體複位,是以直接接VCC,其複位低電平有效。驅動晶片資料,請自行搜尋ST7920,裡面自帶字庫。
需要完整工程或者有問題的請加QQ:1002521871,驗證:呵呵。
四、驅動代碼
LCD12864.h
#ifndef __LCD_12864_H__
#define __LCD_12864_H__
#include "stm32f10x.h"
#include "gpio.h"
#include "delay.h"
//IO Definitions
#define RS PCout(0)
#define RW PCout(1)
#define EN PCout(2)
//#define PSB PCout(3)
//#define RST PCout(4)
#define RS_Pin GPIO_Pin_0
#define RW_Pin GPIO_Pin_1
#define EN_Pin GPIO_Pin_2
//#define PSB_Pin GPIO_Pin_3
//#define RST_Pin GPIO_Pin_4
typedef enum
{
Command,
Data
}WR_Mode;
#define LINE1 0x80
#define LINE2 0x90
#define LINE3 0x88
#define LINE4 0x98
extern void LCD12864_Show(uint8_t line, uint8_t pos, uint8_t *str);
extern void LCD12864Configuration(void);
extern void LCD_Clr(void);
#endif
LCD12864.c
#include "lcd12864.h"
void LCD_Wait_Ready(void)
{
uint8_t status;
RS = 0;
RW = 1;
GPIOD->ODR = 0xFF;
do
{
EN = 1;
DelayMs(5);
status = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_7);
}while(status & 0x80);
EN = 0;
}
void LCD_Write_Cmd(uint8_t cmd)
{
LCD_Wait_Ready();
RS = 0;
RW = 0;
EN = 1;
GPIOD->ODR = cmd;
EN = 0;
}
void LCD_Write_Dat(uint8_t dat)
{
LCD_Wait_Ready();
RS = 1;
RW = 0;
EN = 1;
GPIOD->ODR = dat;
EN = 0;
}
void LCD_Init(void)
{
LCD_Write_Cmd(0x30);
LCD_Write_Cmd(0x0C);
LCD_Write_Cmd(0x01);
}
void LCD_Write_Word(uint8_t *str)
{
while (*str > 0)
{
LCD_Write_Dat(*str);
str ++;
}
}
void LCD_Write_Test(WR_Mode mode, uint8_t word)
{
switch(mode)
{
case Command:
LCD_Write_Cmd(word);
break;
case Data:
LCD_Write_Dat(word);
break;
}
}
void LCD_Clr(void)
{
LCD_Write_Test(Command, 0x01);
}
void LCM_CLR(void)
{
uint8_t clear[16] = " ";
// "1234567812345678"
LCD_Write_Test(Command, LINE1);
LCD_Write_Word(clear);
LCD_Write_Test(Command, LINE2);
LCD_Write_Word(clear);
LCD_Write_Test(Command, LINE3);
LCD_Write_Word(clear);
}
void LCD12864_Show(uint8_t line, uint8_t pos, uint8_t *str)
{
LCD_Write_Test(Command, line + pos);
LCD_Write_Word(str);
}
void LCD12864Configuration(void)
{
GPIO_InitTypeDef GPIO;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);
GPIO.GPIO_Pin = RS_Pin | RW_Pin | EN_Pin;//| PSB_Pin | RST_Pin;
GPIO.GPIO_Speed = GPIO_Speed_50MHz;
GPIO.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO);
//Data Port , PD0 ~ PD7
GPIO.GPIO_Pin = 0xFF;
GPIO_Init(GPIOD, &GPIO);
LCD_Init();
LCD_Clr();
}
由于作者能力有限,有不妥之處歡迎指正,郵箱[email protected]