stm32f103并口驱动HG12232
时间: 2023-08-28 11:07:52 浏览: 82
HG12232是一种128x64点阵图形液晶显示器,可以使用STM32F103的并口驱动器连接。下面是一个基本的并口驱动程序,可以将其修改为适合您的应用程序。请注意,该代码仅提供参考。
```c
#include "stm32f10x.h"
#include "delay.h"
#define LCD_DATA_PORT GPIOA
#define LCD_DATA_PIN GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|\
GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7
#define LCD_CMD_PORT GPIOB
#define LCD_CMD_PIN_RS GPIO_Pin_8
#define LCD_CMD_PIN_RW GPIO_Pin_9
#define LCD_CMD_PIN_E GPIO_Pin_10
void LcdWriteCmd(unsigned char cmd)
{
GPIO_ResetBits(LCD_CMD_PORT, LCD_CMD_PIN_RS);
GPIO_ResetBits(LCD_CMD_PORT, LCD_CMD_PIN_RW);
GPIO_SetBits(LCD_CMD_PORT, LCD_CMD_PIN_E);
GPIO_Write(LCD_DATA_PORT, cmd);
Delay_us(1);
GPIO_ResetBits(LCD_CMD_PORT, LCD_CMD_PIN_E);
Delay_us(1);
}
void LcdWriteData(unsigned char data)
{
GPIO_SetBits(LCD_CMD_PORT, LCD_CMD_PIN_RS);
GPIO_ResetBits(LCD_CMD_PORT, LCD_CMD_PIN_RW);
GPIO_SetBits(LCD_CMD_PORT, LCD_CMD_PIN_E);
GPIO_Write(LCD_DATA_PORT, data);
Delay_us(1);
GPIO_ResetBits(LCD_CMD_PORT, LCD_CMD_PIN_E);
Delay_us(1);
}
void LcdInit(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LCD_DATA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_CMD_PIN_RS|LCD_CMD_PIN_RW|LCD_CMD_PIN_E;
GPIO_Init(LCD_CMD_PORT, &GPIO_InitStructure);
Delay_ms(50);
LcdWriteCmd(0x3f);
Delay_ms(5);
LcdWriteCmd(0xc0);
Delay_ms(5);
LcdWriteCmd(0x01);
Delay_ms(5);
}
void LcdClear(void)
{
LcdWriteCmd(0x01);
Delay_ms(5);
}
void LcdSetPos(unsigned char x, unsigned char y)
{
unsigned char pos;
if(y==0) pos=x;
else pos=x+0x40;
LcdWriteCmd(pos|0x80);
}
void LcdDrawPixel(unsigned char x, unsigned char y, unsigned char color)
{
LcdSetPos(x, y/8);
LcdWriteData(1<<(y%8));
}
void LcdDrawLine(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char color)
{
int dx,dy,e;
dx=x2-x1;
dy=y2-y1;
if(dx>=0)
{
if(dy>=0)
{
if(dx>=dy)
{
e=dy-dx/2;
while(x1<=x2)
{
LcdDrawPixel(x1, y1, color);
if(e>0){y1+=1;e-=dx;}
x1+=1;
e+=dy;
}
}
else
{
e=dx-dy/2;
while(y1<=y2)
{
LcdDrawPixel(x1, y1, color);
if(e>0){x1+=1;e-=dy;}
y1+=1;
e+=dx;
}
}
}
else
{
dy=-dy;
if(dx>=dy)
{
e=dy-dx/2;
while(x1<=x2)
{
LcdDrawPixel(x1, y1, color);
if(e>0){y1-=1;e-=dx;}
x1+=1;
e+=dy;
}
}
else
{
e=dx-dy/2;
while(y1>=y2)
{
LcdDrawPixel(x1, y1, color);
if(e>0){x1+=1;e-=dy;}
y1-=1;
e+=dx;
}
}
}
}
else
{
dx=-dx;
if(dy>=0)
{
if(dx>=dy)
{
e=dy-dx/2;
while(x1>=x2)
{
LcdDrawPixel(x1, y1, color);
if(e>0){y1+=1;e-=dx;}
x1-=1;
e+=dy;
}
}
else
{
e=dx-dy/2;
while(y1<=y2)
{
LcdDrawPixel(x1, y1, color);
if(e>0){x1-=1;e-=dy;}
y1+=1;
e+=dx;
}
}
}
else
{
dy=-dy;
if(dx>=dy)
{
e=dy-dx/2;
while(x1>=x2)
{
LcdDrawPixel(x1, y1, color);
if(e>0){y1-=1;e-=dx;}
x1-=1;
e+=dy;
}
}
else
{
e=dx-dy/2;
while(y1>=y2)
{
LcdDrawPixel(x1, y1, color);
if(e>0){x1-=1;e-=dy;}
y1-=1;
e+=dx;
}
}
}
}
}
void LcdDrawRect(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char color)
{
LcdDrawLine(x1, y1, x2, y1, color);
LcdDrawLine(x1, y1, x1, y2, color);
LcdDrawLine(x2, y1, x2, y2, color);
LcdDrawLine(x1, y2, x2, y2, color);
}
void LcdDrawCircle(unsigned char x, unsigned char y, unsigned char r, unsigned char color)
{
int a,b,num;
a=0;b=r;num=0;
while(1)
{
LcdDrawPixel(x+b, y+a, color);
LcdDrawPixel(x-a, y+b, color);
LcdDrawPixel(x-b, y-a, color);
LcdDrawPixel(x+a, y-b, color);
num+=1;
a+=num;
if(2*a>=3*b) num-=1;
b+=num;
if(a>r) break;
}
}
void LcdDrawBitmap(unsigned char x, unsigned char y, unsigned char *bitmap, unsigned char w, unsigned char h)
{
unsigned char i,j;
for(j=0; j<h/8; j++)
{
for(i=0; i<w; i++)
{
LcdSetPos(x+i, y+j);
LcdWriteData(bitmap[j*w+i]);
}
}
}
int main(void)
{
LcdInit();
LcdClear();
LcdDrawRect(0, 0, 127, 63, 1);
LcdDrawLine(0, 0, 127, 63, 1);
LcdDrawLine(0, 63, 127, 0, 1);
LcdDrawCircle(63, 31, 20, 1);
unsigned char bitmap[] = {0x3C,0x42,0x95,0xA1,0xA1,0x95,0x42,0x3C};
LcdDrawBitmap(64, 24, bitmap, 8, 8);
while(1);
}
```
在此示例中,我们使用了STM32F103的GPIOA和GPIOB端口来控制HG12232的并口。我们使用了8位数据总线,分别连接到LCD_DATA_PORT的GPIO_Pin_0至GPIO_Pin_7引脚上。我们还使用了LCD_CMD_PORT的GPIO_Pin_8、GPIO_Pin_9和GPIO_Pin_10引脚来控制RS、RW和E信号。我们使用了一个基本的初始化函数来初始化LCD,并且提供了一些简单的绘图函数,如清除LCD、设置像素、绘制线、绘制矩形、绘制圆形和绘制位图。这些函数可以根据需要进行修改和扩展,以实现更复杂的绘图功能。
阅读全文