STM32F103ZE通过字符串对R61509VN显示屏画线的程序
时间: 2023-12-23 13:03:18 浏览: 99
stm32f103 OLED显示,stm32f103oled显示屏模块程序,C,C++
5星 · 资源好评率100%
以下是基于STM32F103ZE通过字符串对R61509VN显示屏画线的程序的示例代码:
```c
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "stm32f10x.h"
#define LCD_CMD (*((volatile unsigned short *)0x60000000))
#define LCD_DATA (*((volatile unsigned short *)0x60020000))
void LCD_Init(void)
{
LCD_CMD = 0x0000; //软复位
delay(1000);
LCD_CMD = 0x00E3; //Power Control 2
LCD_DATA = 0x3008;
LCD_DATA = 0x0013;
LCD_CMD = 0x00E7; //Driver Output Control
LCD_DATA = 0x0012;
LCD_CMD = 0x00EF; //Power Control 3
LCD_DATA = 0x1231;
LCD_CMD = 0x0025; //VCOM Control
LCD_DATA = 0x0023;
LCD_CMD = 0x00B1; //Frame Rate Control
LCD_DATA = 0x000B;
LCD_DATA = 0x0003;
LCD_CMD = 0x00B4; //Display Inversion Control
LCD_DATA = 0x0000;
LCD_CMD = 0x00C0; //Power Control 1
LCD_DATA = 0x0026;
LCD_DATA = 0x0004;
LCD_CMD = 0x00C1; //Power Control 2
LCD_DATA = 0x0011;
LCD_CMD = 0x00C5; //VCOM Control 2
LCD_DATA = 0x0035;
LCD_DATA = 0x003E;
LCD_CMD = 0x00C7; //VCOM Control 3
LCD_DATA = 0x00BE;
LCD_CMD = 0x003A; //Pixel Format Set
LCD_DATA = 0x0005;
LCD_CMD = 0x0036; //Memory Access Control
LCD_DATA = 0x0028;
LCD_CMD = 0x002A; //Column Address Set
LCD_DATA = 0x0000;
LCD_DATA = 0x0000;
LCD_DATA = 0x0001;
LCD_DATA = 0x003F;
LCD_CMD = 0x002B; //Page Address Set
LCD_DATA = 0x0000;
LCD_DATA = 0x0000;
LCD_DATA = 0x0001;
LCD_DATA = 0x00DF;
LCD_CMD = 0x0029; //Display On
}
void LCD_WriteReg(unsigned short index, unsigned short value)
{
LCD_CMD = index;
LCD_DATA = value;
}
void LCD_SetWindow(unsigned short x0, unsigned short y0, unsigned short x1, unsigned short y1)
{
LCD_CMD = 0x002A; //Column Address Set
LCD_DATA = x0 >> 8;
LCD_DATA = x0 & 0xFF;
LCD_DATA = x1 >> 8;
LCD_DATA = x1 & 0xFF;
LCD_CMD = 0x002B; //Page Address Set
LCD_DATA = y0 >> 8;
LCD_DATA = y0 & 0xFF;
LCD_DATA = y1 >> 8;
LCD_DATA = y1 & 0xFF;
LCD_CMD = 0x002C; //Memory Write
}
void LCD_DrawLine(unsigned short x0, unsigned short y0, unsigned short x1, unsigned short y1, unsigned short color)
{
int dx = x1 - x0;
int dy = y1 - y0;
int stepx, stepy;
if (dy < 0) {
dy = -dy;
stepy = -1;
} else {
stepy = 1;
}
if (dx < 0) {
dx = -dx;
stepx = -1;
} else {
stepx = 1;
}
dy <<= 1;
dx <<= 1;
LCD_SetWindow(x0, y0, x0, y0);
LCD_DATA = color;
if (dx > dy) {
int fraction = dy - (dx >> 1);
while (x0 != x1) {
if (fraction >= 0) {
y0 += stepy;
fraction -= dx;
}
x0 += stepx;
fraction += dy;
LCD_SetWindow(x0, y0, x0, y0);
LCD_DATA = color;
}
} else {
int fraction = dx - (dy >> 1);
while (y0 != y1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
LCD_SetWindow(x0, y0, x0, y0);
LCD_DATA = color;
}
}
}
void delay(unsigned int n)
{
volatile unsigned int i;
for (i = 0; i < n; i++);
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
LCD_Init();
LCD_WriteReg(0x002c, 0x0000); //Clear Screen
LCD_DrawLine(0, 0, 319, 239, 0xF800); //Draw Red Line
while(1) {
}
}
```
在上述代码中,我们首先定义了一些常量和函数,如LCD_CMD和LCD_DATA分别表示LCD的命令和数据,LCD_Init用于初始化LCD,LCD_WriteReg用于向LCD写入寄存器,LCD_SetWindow用于设置LCD的窗口范围,LCD_DrawLine用于在LCD上绘制线段,delay用于延时。
在main函数中,我们首先初始化GPIO和LCD,然后调用LCD_WriteReg清屏,最后调用LCD_DrawLine绘制一条红色的直线。
需要注意的是,此代码仅为示例代码,具体实现可能需要根据具体的硬件和接口进行修改。
阅读全文