写一份stm32f103c8t6驱动lcd12864的完整程序模板
时间: 2023-09-08 21:10:27 浏览: 52
以下是一个基本的STM32F103C8T6与LCD12864的驱动程序模板:
```c
#include "stm32f10x.h"
#include "delay.h"
#define LCD_RS_PIN GPIO_Pin_0
#define LCD_RS_PORT GPIOA
#define LCD_RW_PIN GPIO_Pin_1
#define LCD_RW_PORT GPIOA
#define LCD_E_PIN GPIO_Pin_2
#define LCD_E_PORT GPIOA
#define LCD_DB4_PIN GPIO_Pin_4
#define LCD_DB4_PORT GPIOB
#define LCD_DB5_PIN GPIO_Pin_5
#define LCD_DB5_PORT GPIOB
#define LCD_DB6_PIN GPIO_Pin_6
#define LCD_DB6_PORT GPIOB
#define LCD_DB7_PIN GPIO_Pin_7
#define LCD_DB7_PORT GPIOB
void LCD_Config(void);
void LCD_SendCommand(uint8_t cmd);
void LCD_SendData(uint8_t data);
void LCD_SendString(char *str);
void LCD_SetCursor(uint8_t row, uint8_t col);
int main(void)
{
LCD_Config();
while(1)
{
LCD_SetCursor(0, 0);
LCD_SendString("Hello, world!");
DelayMs(1000);
}
}
void LCD_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN | LCD_RW_PIN | LCD_E_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LCD_RS_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_DB4_PIN | LCD_DB5_PIN | LCD_DB6_PIN | LCD_DB7_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LCD_DB4_PORT, &GPIO_InitStructure);
DelayMs(15);
LCD_SendCommand(0x02);
LCD_SendCommand(0x28);
LCD_SendCommand(0x0C);
LCD_SendCommand(0x06);
LCD_SendCommand(0x01);
DelayMs(2);
}
void LCD_SendCommand(uint8_t cmd)
{
GPIO_ResetBits(LCD_RS_PORT, LCD_RS_PIN);
GPIO_ResetBits(LCD_RW_PORT, LCD_RW_PIN);
GPIO_WriteBit(LCD_DB4_PORT, LCD_DB4_PIN, (BitAction)((cmd >> 4) & 0x01));
GPIO_WriteBit(LCD_DB5_PORT, LCD_DB5_PIN, (BitAction)((cmd >> 5) & 0x01));
GPIO_WriteBit(LCD_DB6_PORT, LCD_DB6_PIN, (BitAction)((cmd >> 6) & 0x01));
GPIO_WriteBit(LCD_DB7_PORT, LCD_DB7_PIN, (BitAction)((cmd >> 7) & 0x01));
GPIO_SetBits(LCD_E_PORT, LCD_E_PIN);
DelayUs(1);
GPIO_ResetBits(LCD_E_PORT, LCD_E_PIN);
DelayUs(50);
GPIO_WriteBit(LCD_DB4_PORT, LCD_DB4_PIN, (BitAction)(cmd & 0x01));
GPIO_WriteBit(LCD_DB5_PORT, LCD_DB5_PIN, (BitAction)((cmd >> 1) & 0x01));
GPIO_WriteBit(LCD_DB6_PORT, LCD_DB6_PIN, (BitAction)((cmd >> 2) & 0x01));
GPIO_WriteBit(LCD_DB7_PORT, LCD_DB7_PIN, (BitAction)((cmd >> 3) & 0x01));
GPIO_SetBits(LCD_E_PORT, LCD_E_PIN);
DelayUs(1);
GPIO_ResetBits(LCD_E_PORT, LCD_E_PIN);
DelayMs(1);
}
void LCD_SendData(uint8_t data)
{
GPIO_SetBits(LCD_RS_PORT, LCD_RS_PIN);
GPIO_ResetBits(LCD_RW_PORT, LCD_RW_PIN);
GPIO_WriteBit(LCD_DB4_PORT, LCD_DB4_PIN, (BitAction)((data >> 4) & 0x01));
GPIO_WriteBit(LCD_DB5_PORT, LCD_DB5_PIN, (BitAction)((data >> 5) & 0x01));
GPIO_WriteBit(LCD_DB6_PORT, LCD_DB6_PIN, (BitAction)((data >> 6) & 0x01));
GPIO_WriteBit(LCD_DB7_PORT, LCD_DB7_PIN, (BitAction)((data >> 7) & 0x01));
GPIO_SetBits(LCD_E_PORT, LCD_E_PIN);
DelayUs(1);
GPIO_ResetBits(LCD_E_PORT, LCD_E_PIN);
DelayUs(50);
GPIO_WriteBit(LCD_DB4_PORT, LCD_DB4_PIN, (BitAction)(data & 0x01));
GPIO_WriteBit(LCD_DB5_PORT, LCD_DB5_PIN, (BitAction)((data >> 1) & 0x01));
GPIO_WriteBit(LCD_DB6_PORT, LCD_DB6_PIN, (BitAction)((data >> 2) & 0x01));
GPIO_WriteBit(LCD_DB7_PORT, LCD_DB7_PIN, (BitAction)((data >> 3) & 0x01));
GPIO_SetBits(LCD_E_PORT, LCD_E_PIN);
DelayUs(1);
GPIO_ResetBits(LCD_E_PORT, LCD_E_PIN);
DelayMs(1);
}
void LCD_SendString(char *str)
{
while(*str)
{
LCD_SendData(*str++);
}
}
void LCD_SetCursor(uint8_t row, uint8_t col)
{
uint8_t address = 0;
if(row == 0)
{
address = 0x00 + col;
}
else if(row == 1)
{
address = 0x40 + col;
}
LCD_SendCommand(0x80 | address);
}
```
注意:该模板仅为参考,可能需要根据具体的硬件连接和库文件进行修改。