stm32f103c8t64针OLED显示屏代码
时间: 2023-08-29 18:05:43 浏览: 99
STM32F103C8T6硬件IIC控制4针0.96寸OLED显示屏
以下是将STM32F103C8T6与128x64 OLED显示屏连接并显示一些文本的示例代码:
```C
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_spi.h"
#include "fonts.h"
// OLED屏幕定义
#define OLED_RESET_PIN GPIO_Pin_0
#define OLED_RESET_PORT GPIOB
#define OLED_DC_PIN GPIO_Pin_1
#define OLED_DC_PORT GPIOB
#define OLED_CS_PIN GPIO_Pin_12
#define OLED_CS_PORT GPIOB
#define OLED_SPI SPI2
#define OLED_SPI_CLK RCC_APB1Periph_SPI2
#define OLED_SPI_GPIO_CLK RCC_APB2Periph_GPIOB
// OLED屏幕初始化
void OLED_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
// 打开GPIO和SPI的时钟
RCC_APB2PeriphClockCmd(OLED_SPI_GPIO_CLK, ENABLE);
RCC_APB1PeriphClockCmd(OLED_SPI_CLK, ENABLE);
// 配置SPI引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
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_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 配置OLED引脚
GPIO_InitStructure.GPIO_Pin = OLED_RESET_PIN | OLED_DC_PIN | OLED_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(OLED_RESET_PORT, &GPIO_InitStructure);
// 初始化OLED屏幕
GPIO_SetBits(OLED_RESET_PORT, OLED_RESET_PIN);
GPIO_SetBits(OLED_DC_PORT, OLED_DC_PIN);
GPIO_SetBits(OLED_CS_PORT, OLED_CS_PIN);
SPI_Cmd(OLED_SPI, ENABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
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_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(OLED_SPI, &SPI_InitStructure);
// 清屏
OLED_Clear();
}
// 写入OLED命令
void OLED_WriteCmd(uint8_t cmd) {
GPIO_ResetBits(OLED_DC_PORT, OLED_DC_PIN);
GPIO_ResetBits(OLED_CS_PORT, OLED_CS_PIN);
SPI_I2S_SendData(OLED_SPI, cmd);
while (SPI_I2S_GetFlagStatus(OLED_SPI, SPI_I2S_FLAG_BSY) == SET);
GPIO_SetBits(OLED_CS_PORT, OLED_CS_PIN);
}
// 写入OLED数据
void OLED_WriteData(uint8_t data) {
GPIO_SetBits(OLED_DC_PORT, OLED_DC_PIN);
GPIO_ResetBits(OLED_CS_PORT, OLED_CS_PIN);
SPI_I2S_SendData(OLED_SPI, data);
while (SPI_I2S_GetFlagStatus(OLED_SPI, SPI_I2S_FLAG_BSY) == SET);
GPIO_SetBits(OLED_CS_PORT, OLED_CS_PIN);
}
// 清除OLED屏幕
void OLED_Clear(void) {
uint8_t i, j;
for (i = 0; i < 8; i++) {
OLED_WriteCmd(0xb0 + i);
OLED_WriteCmd(0x00);
OLED_WriteCmd(0x10);
for (j = 0; j < 128; j++) {
OLED_WriteData(0x00);
}
}
}
// 在OLED屏幕上显示文本
void OLED_ShowString(uint8_t x, uint8_t y, const char *str) {
uint8_t c = 0, i = 0, j = 0;
while (str[j] != '\0') {
c = str[j] - 32;
if (x > 128 - 1) {
x = 0;
y++;
}
if (y > 8 - 1) {
y = x = 0;
OLED_Clear();
}
OLED_WriteCmd(0xb0 + y);
OLED_WriteCmd(((x & 0xf0) >> 4) | 0x10);
OLED_WriteCmd(x & 0x0f);
for (i = 0; i < 8; i++) {
OLED_WriteData(FONT8x8[c][i]);
}
x += 8;
j++;
}
}
int main(void) {
// 初始化OLED屏幕
OLED_Init();
// 在OLED屏幕上显示文本
OLED_ShowString(0, 0, "Hello World!");
while (1);
}
```
需要注意的是,该代码使用了SPI2来驱动OLED屏幕,因此需要在STM32的GPIOB引脚上连接SPI2的SCK、MOSI和MISO引脚。另外,该代码假定OLED屏幕已经按照128x64的格式连接到STM32F103C8T6的引脚上。
阅读全文