LCD12864并行驱动(STM32)5V和3.3V

一、前期准备
单片机:STM32F103ZET6
开发环境:MDK5.14
库函数:标准库V3.5
LCD12864模块:淘宝有售
LCD12864并行驱动(STM32)5V和3.3V_第1张图片

二、实验效果
3.3V LCD12864
LCD12864并行驱动(STM32)5V和3.3V_第2张图片

5V LCD12864
LCD12864并行驱动(STM32)5V和3.3V_第3张图片

三、驱动原理
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]

你可能感兴趣的:(嵌入式显示驱动)