12864b stm32代码
时间: 2023-07-02 22:05:49 浏览: 98
由于 12864B 液晶屏没有固定的接口标准,所以涉及到的代码会有所不同。下面是一个基于STM32的12864B液晶屏代码示例,仅供参考:
```c
#include "stm32f10x.h"
#include "delay.h"
#define LCD_DATA_PORT GPIOA //数据口
#define LCD_WR PAout(2)
#define LCD_RS PAout(3)
#define LCD_CS PAout(4)
#define LCD_DATA_OUT() {GPIOA->CRL&=0X00FFFFFF;GPIOA->CRL|=0X33000000;}
#define LCD_DATA_IN() {GPIOA->CRL&=0X00FFFFFF;GPIOA->CRL|=0X88000000;}
void LCD_WR_REG(u8 data) //写指令
{
LCD_DATA_OUT();
LCD_CS = 0;
LCD_RS = 0;
LCD_WR = 0;
GPIOA->ODR = data;
asm("nop");
LCD_WR = 1;
LCD_RS = 1;
LCD_CS = 1;
}
void LCD_WR_DATA(u8 data) //写数据
{
LCD_DATA_OUT();
LCD_CS = 0;
LCD_RS = 1;
LCD_WR = 0;
GPIOA->ODR = data;
asm("nop");
LCD_WR = 1;
LCD_RS = 1;
LCD_CS = 1;
}
void LCD_Init(void) //初始化
{
RCC->APB2ENR |= 1 << 2;
RCC->APB2ENR |= 1 << 3;
GPIOA->CRL &= 0xFFF0FFFF;
GPIOA->CRL |= 0x00030000;
GPIOA->CRH &= 0xFF0FFFFF;
GPIOA->CRH |= 0x00300000;
LCD_WR_REG(0x30);
delay_ms(20);
LCD_WR_REG(0x0C);
delay_ms(20);
LCD_WR_REG(0x01);
delay_ms(20);
LCD_WR_REG(0x06);
delay_ms(20);
LCD_WR_REG(0x80);
delay_ms(20);
}
void LCD_Clear(void) //清屏
{
u8 i, j;
for (i = 0; i < 8; i++)
{
LCD_WR_REG(0x80 | i);
LCD_WR_REG(0x40);
for (j = 0; j < 64; j++)
{
LCD_WR_DATA(0);
}
}
}
void LCD_DrawPoint(u8 x, u8 y, u8 t) //画点
{
u8 pos, bx, temp = 0;
if (x > 127 || y > 63)
{
return;
}
pos = 7 - y / 8;
bx = y % 8;
temp = 1 << (7 - bx);
LCD_WR_REG(0x80 | pos);
LCD_WR_REG(0x40 | x);
if (t)
{
LCD_WR_DATA(temp);
}
else
{
LCD_WR_DATA(~temp);
}
}
void LCD_ShowChar(u8 x, u8 y, u8 chr, u8 size, u8 mode) //显示字符
{
u8 i, j;
u8 temp;
u8 y0 = y;
chr = chr - ' '; //得到偏移后的值
for (i = 0; i < size; i++)
{
if (size == 12)
{
if (mode)
{
temp = asc2_1206[chr][i];
}
else
{
temp = ~asc2_1206[chr][i];
}
}
else
{
if (mode)
{
temp = asc2_1608[chr][i];
}
else
{
temp = ~asc2_1608[chr][i];
}
}
for (j = 0; j < 8; j++)
{
if (temp & 0x80)
{
LCD_DrawPoint(x, y, 1);
}
else
{
LCD_DrawPoint(x, y, 0);
}
temp <<= 1;
y++;
if ((y - y0) == size)
{
y = y0;
x++;
break;
}
}
}
}
void LCD_ShowString(u8 x, u8 y, u8 *p, u8 size, u8 mode) //显示字符串
{
while (*p != '\0')
{
if (x > 128 - size / 2)
{
x = 0;
y += size;
}
if (y > 64 - size)
{
y = x = 0;
LCD_Clear();
}
LCD_ShowChar(x, y, *p, size, mode);
x += size / 2;
p++;
}
}
int main(void)
{
delay_init();
LCD_Init();
LCD_Clear();
LCD_ShowString(0, 0, "Hello World!", 12, 1);
while (1)
{
}
}
```
代码中使用了delay.h库和一些自定义的函数,需要根据实际情况进行调整。同时,还需要根据实际的硬件电路进行引脚配置。
阅读全文