编写stm32f103c6t6的12864代码
时间: 2023-07-02 07:14:29 浏览: 115
好的,下面是一个简单的stm32f103c6t6驱动12864液晶屏的代码示例:
```c
#include "stm32f10x.h"
#include "12864.h"
void delay_us(uint32_t nus)
{
uint32_t ticks;
ticks = nus*(SystemCoreClock/1000000);
while(ticks--);
}
void GLCD_WriteCommand(uint8_t cmd)
{
GPIO_ResetBits(GPIOA, RS_Pin);
GPIO_Write(GPIOB, cmd);
GPIO_SetBits(GPIOA, RW_Pin);
delay_us(1);
GPIO_SetBits(GPIOA, EN_Pin);
delay_us(1);
GPIO_ResetBits(GPIOA, EN_Pin);
delay_us(50);
}
void GLCD_WriteData(uint8_t data)
{
GPIO_SetBits(GPIOA, RS_Pin);
GPIO_Write(GPIOB, data);
GPIO_SetBits(GPIOA, RW_Pin);
delay_us(1);
GPIO_SetBits(GPIOA, EN_Pin);
delay_us(1);
GPIO_ResetBits(GPIOA, EN_Pin);
delay_us(50);
}
void GLCD_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = RS_Pin | RW_Pin | EN_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GLCD_DB0_Pin | GLCD_DB1_Pin | GLCD_DB2_Pin | GLCD_DB3_Pin |
GLCD_DB4_Pin | GLCD_DB5_Pin | GLCD_DB6_Pin | GLCD_DB7_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
delay_us(20000);
GLCD_WriteCommand(0x30);
delay_us(5000);
GLCD_WriteCommand(0x30);
delay_us(500);
GLCD_WriteCommand(0x30);
GLCD_WriteCommand(0x38);
GLCD_WriteCommand(0x08);
GLCD_WriteCommand(0x01);
GLCD_WriteCommand(0x06);
GLCD_WriteCommand(0x0C);
delay_us(500);
}
void GLCD_Clear(void)
{
GLCD_WriteCommand(0x01);
delay_us(500);
}
void GLCD_SetCursor(uint8_t x, uint8_t y)
{
uint8_t addr;
if(x < 16 && y < 4)
{
switch(y)
{
case 0: addr = 0x80 + x; break;
case 1: addr = 0x90 + x; break;
case 2: addr = 0x88 + x; break;
case 3: addr = 0x98 + x; break;
}
GLCD_WriteCommand(addr);
}
}
void GLCD_WriteChar(char c)
{
GLCD_WriteData(c);
}
void GLCD_WriteString(char *s)
{
while(*s)
{
GLCD_WriteChar(*s++);
}
}
int main(void)
{
GLCD_Init();
GLCD_Clear();
GLCD_SetCursor(0,0);
GLCD_WriteString("Hello world!");
while(1);
}
```
这个代码示例使用了C语言和STM32的HAL库驱动了一个12864液晶屏,通过调用`GLCD_Init()`进行初始化、`GLCD_Clear()`清屏、`GLCD_SetCursor(x, y)`设置光标位置、`GLCD_WriteChar(c)`写入单个字符和`GLCD_WriteString(s)`写入字符串等函数来显示文本。同时,这个代码示例使用了GPIO口控制`RS`、`RW`、`EN`和`DB0~DB7`等引脚。需要根据自己的实际硬件连接情况进行修改。
阅读全文