用stm32f407的库函数驱动四针OLED 屏幕
时间: 2023-12-01 19:02:17 浏览: 202
基于STM32的OLED程序
首先,你需要下载并安装STM32F4xx库函数,然后按照以下步骤进行操作:
1. 创建一个新工程,并在工程中添加OLED屏幕驱动所需的文件。
2. 在main.c文件中添加以下代码:
```c
#include "stm32f4xx.h"
#include "OLED.h"
int main(void)
{
OLED_Init(); //初始化OLED屏幕
OLED_Clear(); //清屏
OLED_ShowString(0,0,"Hello World!"); //在屏幕上显示字符串
while (1);
}
```
3. 在OLED.c文件中添加以下代码:
```c
#include "stm32f4xx.h"
#include "OLED.h"
//OLED屏幕初始化函数
void OLED_Init(void)
{
//将OLED屏幕的引脚设置为输出模式
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_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;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
OLED_WriteCmd(0xAE); //display off
OLED_WriteCmd(0x20); //Set Memory Addressing Mode
OLED_WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
OLED_WriteCmd(0xB0); //Set Page Start Address for Page Addressing Mode,0-7
OLED_WriteCmd(0xC8); //Set COM Output Scan Direction
OLED_WriteCmd(0x00); //---set low column address
OLED_WriteCmd(0x10); //---set high column address
OLED_WriteCmd(0x40); //--set start line address
OLED_WriteCmd(0x81); //--set contrast control register
OLED_WriteCmd(0xFF);
OLED_WriteCmd(0xA1); //--set segment re-map 0 to 127
OLED_WriteCmd(0xA6); //--set normal display
OLED_WriteCmd(0xA8); //--set multiplex ratio(1 to 64)
OLED_WriteCmd(0x3F); //
OLED_WriteCmd(0xA4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
OLED_WriteCmd(0xD3); //-set display offset
OLED_WriteCmd(0x00); //-not offset
OLED_WriteCmd(0xD5); //--set display clock divide ratio/oscillator frequency
OLED_WriteCmd(0xF0); //--set divide ratio
OLED_WriteCmd(0xD9); //--set pre-charge period
OLED_WriteCmd(0x22); //
OLED_WriteCmd(0xDA); //--set com pins hardware configuration
OLED_WriteCmd(0x12);
OLED_WriteCmd(0xDB); //--set vcomh
OLED_WriteCmd(0x20); //0x20,0.77xVcc
OLED_WriteCmd(0x8D); //--set DC-DC enable
OLED_WriteCmd(0x14); //
OLED_WriteCmd(0xAF); //--turn on oled panel
}
//向OLED屏幕写入命令
void OLED_WriteCmd(uint8_t cmd)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4); //RS=0,写命令
GPIO_ResetBits(GPIOA, GPIO_Pin_5); //RW=0,写入模式
GPIO_SetBits(GPIOA, GPIO_Pin_6); //E=1,允许写入
GPIOA->ODR &= 0xFF00;
GPIOA->ODR |= cmd; //写入命令
GPIO_ResetBits(GPIOA, GPIO_Pin_6); //E=0,禁止写入
}
//向OLED屏幕写入数据
void OLED_WriteData(uint8_t data)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4); //RS=1,写数据
GPIO_ResetBits(GPIOA, GPIO_Pin_5); //RW=0,写入模式
GPIO_SetBits(GPIOA, GPIO_Pin_6); //E=1,允许写入
GPIOA->ODR &= 0xFF00;
GPIOA->ODR |= data; //写入数据
GPIO_ResetBits(GPIOA, GPIO_Pin_6); //E=0,禁止写入
}
//清屏
void OLED_Clear(void)
{
uint8_t i,j;
for(i=0;i<8;i++)
{
OLED_WriteCmd(0xb0+i); //设置页地址
OLED_WriteCmd(0x00); //设置列地址低4位
OLED_WriteCmd(0x10); //设置列地址高4位
for(j=0;j<128;j++)
{
OLED_WriteData(0x00); //清空该页的所有列
}
}
}
//在OLED屏幕上显示字符串
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr)
{
uint8_t c=0,i=0,j=0;
while (chr[c]!='\0')
{
j=chr[c]-32;
if(x>120){x=0;y++;}
OLED_SetPos(x,y);
for(i=0;i<8;i++)
{
OLED_WriteData(F8X16[j*16+i]);
}
OLED_SetPos(x,y+1);
for(i=0;i<8;i++)
{
OLED_WriteData(F8X16[j*16+i+8]);
}
x+=8;
c++;
}
}
//设置OLED屏幕某个像素的状态
void OLED_SetPixel(uint8_t x,uint8_t y,uint8_t color)
{
uint8_t page;
uint8_t column;
page=y/8;
column=x;
OLED_SetPos(column,page);
if(color==1)
{
OLED_WriteData(0xff<<(y%8));
}
else
{
OLED_WriteData(0x00<<(y%8));
}
}
//设置OLED屏幕显示位置
void OLED_SetPos(uint8_t x,uint8_t y)
{
OLED_WriteCmd(0xb0+y);
OLED_WriteCmd(((x&0xf0)>>4)|0x10);
OLED_WriteCmd(x&0x0f);
}
```
4. 在OLED.h文件中添加以下代码:
```c
#ifndef __OLED_H
#define __OLED_H
#include "stm32f4xx.h"
//OLED屏幕初始化函数
void OLED_Init(void);
//向OLED屏幕写入命令
void OLED_WriteCmd(uint8_t cmd);
//向OLED屏幕写入数据
void OLED_WriteData(uint8_t data);
//清屏
void OLED_Clear(void);
//在OLED屏幕上显示字符串
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr);
//设置OLED屏幕某个像素的状态
void OLED_SetPixel(uint8_t x,uint8_t y,uint8_t color);
//设置OLED屏幕显示位置
void OLED_SetPos(uint8_t x,uint8_t y);
#endif
```
5. 编译并下载到STM32F407芯片中,即可看到OLED屏幕上显示“Hello World!”的字样。
需要注意的是,以上代码仅供参考,具体的代码实现可能会有所不同,具体实现方式需要根据所使用的OLED屏幕型号和具体的需求进行调整。
阅读全文