1.3寸7针oled显示屏STM32子程序设计
时间: 2023-10-29 18:07:16 浏览: 194
基于STM32的OLED程序
以下是一个简单的STM32子程序设计,用于控制1.3寸7针OLED显示屏:
```c
#include "stm32f10x.h"
#include "oled.h"
void OLED_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void OLED_SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void OLED_Init(void)
{
OLED_GPIO_Configuration();
OLED_SPI_Configuration();
OLED_RST = 0;
delay_ms(100);
OLED_RST = 1;
delay_ms(100);
OLED_WR_Byte(0xAE,OLED_CMD);//关闭显示
OLED_WR_Byte(0xD5,OLED_CMD);//设置时钟分频因子/震荡频率
OLED_WR_Byte(80,OLED_CMD); //[3:0]分频因子,[7:4]震荡频率
OLED_WR_Byte(0xA8,OLED_CMD);//设置驱动路数
OLED_WR_Byte(0x3F,OLED_CMD);//默认0x3F(1/64)
OLED_WR_Byte(0xD3,OLED_CMD);//设置显示偏移
OLED_WR_Byte(0x00,OLED_CMD);//默认为0
OLED_WR_Byte(0x40,OLED_CMD);//设置显示开始行 [5:0]行数
OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵设置
OLED_WR_Byte(0x14,OLED_CMD);//bit2,开启/关闭
OLED_WR_Byte(0x20,OLED_CMD);//设置内存地址模式
OLED_WR_Byte(0x02,OLED_CMD);//[1:0],00,列地址模式;01,行地址模式;10,页地址模式;默认10;
OLED_WR_Byte(0xA0,OLED_CMD);//设置段重定义
OLED_WR_Byte(0xC0,OLED_CMD);//bit3,0,0->127;1,127->0;
OLED_WR_Byte(0xDA,OLED_CMD);//设置COM硬件引脚配置
OLED_WR_Byte(0x12,OLED_CMD);//默认0x12(0x02)
OLED_WR_Byte(0x81,OLED_CMD);//对比度设置
OLED_WR_Byte(0xEF,OLED_CMD);//1~255;默认0x7F(亮度调节)
OLED_WR_Byte(0xD9,OLED_CMD);//设置预充电周期
OLED_WR_Byte(0xf1,OLED_CMD);//[3:0],PHASE 1;[7:4],PHASE 2;
OLED_WR_Byte(0xDB,OLED_CMD);//设置VCOMH 电压倍率
OLED_WR_Byte(0x40,OLED_CMD);//默认0x20,0.77xVcc
OLED_WR_Byte(0xA4,OLED_CMD);//全局显示开启;bit0,1,开启;0,关闭;(白屏/黑屏)
OLED_WR_Byte(0xA6,OLED_CMD);//设置显示方式;bit0,1,反相显示;0,正常显示
OLED_WR_Byte(0xAF,OLED_CMD);//开启显示
}
void OLED_WR_Byte(u8 dat,u8 cmd)
{
if(cmd)
OLED_DC = 1;
else
OLED_DC = 0;
OLED_CS = 0;
SPI1->DR = dat;
while((SPI1->SR&SPI_I2S_FLAG_TXE)==0);
while((SPI1->SR&SPI_I2S_FLAG_RXNE)==0);
while((SPI1->SR&SPI_I2S_FLAG_BSY)!=0);
OLED_CS = 1;
OLED_DC = 1;
}
```
在这个程序中,我们定义了 `OLED_GPIO_Configuration()` 用于配置OLED的GPIO端口, `OLED_SPI_Configuration()` 用于配置OLED的SPI接口, `OLED_Init()` 用于初始化OLED, `OLED_WR_Byte()` 用于向OLED写入数据或命令。
这个程序是基于STM32F10x系列的单片机编写的,使用了STM32的SPI接口来控制OLED。你需要根据自己的硬件接口进行相应的修改。
阅读全文