stm32f103c8t6下lcd12864串行通用程序
时间: 2023-08-24 11:07:04 浏览: 223
STM32F103C8T6 串行驱动 12864屏幕
3星 · 编辑精心推荐
下面是一个基于STM32F103C8T6和LCD12864的串行通用程序示例,供参考:
```c
#include "stm32f10x.h"
#define LCD_DIN_GPIO GPIOA
#define LCD_DIN_PIN GPIO_Pin_7
#define LCD_CLK_GPIO GPIOA
#define LCD_CLK_PIN GPIO_Pin_5
#define LCD_CS_GPIO GPIOA
#define LCD_CS_PIN GPIO_Pin_4
#define LCD_RST_GPIO GPIOA
#define LCD_RST_PIN GPIO_Pin_3
#define DELAY_TIME 5
void delay_us(uint32_t nus);
void lcd_write_byte(uint8_t data);
void lcd_write_command(uint8_t cmd);
void lcd_write_data(uint8_t data);
void lcd_initialize();
void lcd_clear();
void lcd_display_string(uint8_t x, uint8_t y, char* str);
int main()
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LCD_DIN_PIN | LCD_CLK_PIN | LCD_CS_PIN | LCD_RST_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
lcd_initialize();
while(1)
{
lcd_clear();
lcd_display_string(0, 0, "Hello World!");
delay_us(1000000);
}
}
void delay_us(uint32_t nus)
{
uint32_t ticks;
ticks = nus * (SystemCoreClock / 1000000);
while(ticks--);
}
void lcd_write_byte(uint8_t data)
{
uint8_t i;
GPIO_ResetBits(LCD_CS_GPIO, LCD_CS_PIN);
for(i = 0; i < 8; i++)
{
if(data & 0x80)
GPIO_SetBits(LCD_DIN_GPIO, LCD_DIN_PIN);
else
GPIO_ResetBits(LCD_DIN_GPIO, LCD_DIN_PIN);
GPIO_SetBits(LCD_CLK_GPIO, LCD_CLK_PIN);
delay_us(DELAY_TIME);
GPIO_ResetBits(LCD_CLK_GPIO, LCD_CLK_PIN);
delay_us(DELAY_TIME);
data <<= 1;
}
GPIO_SetBits(LCD_CS_GPIO, LCD_CS_PIN);
}
void lcd_write_command(uint8_t cmd)
{
GPIO_ResetBits(LCD_RST_GPIO, LCD_RST_PIN);
delay_us(DELAY_TIME);
GPIO_SetBits(LCD_RST_GPIO, LCD_RST_PIN);
delay_us(DELAY_TIME);
GPIO_ResetBits(LCD_CS_GPIO, LCD_CS_PIN);
GPIO_ResetBits(LCD_CLK_GPIO, LCD_CLK_PIN);
GPIO_SetBits(LCD_CLK_GPIO, LCD_CLK_PIN);
GPIO_ResetBits(LCD_CLK_GPIO, LCD_CLK_PIN);
GPIO_ResetBits(LCD_CS_GPIO, LCD_CS_PIN);
lcd_write_byte(cmd);
}
void lcd_write_data(uint8_t data)
{
lcd_write_command(0x01);
GPIO_ResetBits(LCD_CS_GPIO, LCD_CS_PIN);
GPIO_SetBits(LCD_CLK_GPIO, LCD_CLK_PIN);
GPIO_ResetBits(LCD_CLK_GPIO, LCD_CLK_PIN);
GPIO_SetBits(LCD_CS_GPIO, LCD_CS_PIN);
GPIO_ResetBits(LCD_CS_GPIO, LCD_CS_PIN);
lcd_write_byte(data);
}
void lcd_initialize()
{
lcd_write_command(0x30);
lcd_write_command(0x0c);
lcd_write_command(0x01);
delay_us(DELAY_TIME);
lcd_write_command(0x06);
lcd_write_command(0x02);
}
void lcd_clear()
{
uint8_t i, j;
for(i = 0; i < 8; i++)
{
lcd_write_command(0x80 | (i << 3));
for(j = 0; j < 16; j++)
lcd_write_data(' ');
}
}
void lcd_display_string(uint8_t x, uint8_t y, char* str)
{
uint8_t i;
lcd_write_command(0x80 | (y << 3) | x);
for(i = 0; str[i] != '\0'; i++)
lcd_write_data(str[i]);
}
```
该示例程序使用PA3作为LCD的复位信号,PA4作为片选信号,PA5作为时钟信号,PA7作为数据信号。在main函数中,首先进行GPIO初始化,然后调用lcd_initialize()函数对LCD进行初始化。在循环中,调用lcd_clear()函数清除屏幕,并调用lcd_display_string()函数在指定位置显示字符串。其中,lcd_write_byte()函数用于向LCD写入一个字节的数据或命令,lcd_write_command()函数用于向LCD写入一个命令,lcd_write_data()函数用于向LCD写入一个数据,lcd_initialize()函数用于对LCD进行初始化,lcd_clear()函数用于清除屏幕,lcd_display_string()函数用于在指定位置显示字符串。函数中使用了delay_us()函数进行微秒级延时,该函数的实现可以根据具体的硬件平台进行修改。
阅读全文