0.96寸OLED STM32F103程序
时间: 2023-06-20 11:10:08 浏览: 111
0.96OLED显示屏STM32F103C8T6_SPI例程_0.96寸oled_STM32F103C8T6SPI_stm32f
5星 · 资源好评率100%
以下是一个STM32F103芯片驱动0.96寸OLED屏幕的示例程序,使用SPI协议进行通信。请确保正确连接硬件,并按照注释修改代码中的引脚和SPI口。
```c
#include "stm32f10x.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#define OLED_SPI_PORT SPI1
#define OLED_SPI_GPIO_PORT GPIOA
#define OLED_SPI_CS_PIN GPIO_Pin_4
#define OLED_SPI_SCK_PIN GPIO_Pin_5
#define OLED_SPI_MOSI_PIN GPIO_Pin_7
void delay_ms(uint32_t ms)
{
while (ms--) {
volatile uint32_t x = 8000;
while (x--);
}
}
void OLED_SPI_Init(void)
{
SPI_InitTypeDef spi_init;
/* Enable clock for GPIOA and SPI1 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
/* Configure SPI GPIO pins */
GPIO_InitTypeDef gpio_init;
gpio_init.GPIO_Pin = OLED_SPI_SCK_PIN | OLED_SPI_MOSI_PIN;
gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(OLED_SPI_GPIO_PORT, &gpio_init);
/* Configure SPI CS pin */
gpio_init.GPIO_Pin = OLED_SPI_CS_PIN;
gpio_init.GPIO_Mode = GPIO_Mode_Out_PP;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(OLED_SPI_GPIO_PORT, &gpio_init);
GPIO_SetBits(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN);
/* Configure SPI */
SPI_StructInit(&spi_init);
spi_init.SPI_Mode = SPI_Mode_Master;
spi_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
spi_init.SPI_CRCPolynomial = 7;
SPI_Init(OLED_SPI_PORT, &spi_init);
/* Enable SPI */
SPI_Cmd(OLED_SPI_PORT, ENABLE);
}
void OLED_Write_Command(uint8_t cmd)
{
GPIO_ResetBits(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN);
SPI_I2S_SendData(OLED_SPI_PORT, 0x00);
while (SPI_I2S_GetFlagStatus(OLED_SPI_PORT, SPI_I2S_FLAG_BSY) == SET);
SPI_I2S_SendData(OLED_SPI_PORT, cmd);
while (SPI_I2S_GetFlagStatus(OLED_SPI_PORT, SPI_I2S_FLAG_BSY) == SET);
GPIO_SetBits(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN);
}
void OLED_Write_Data(uint8_t data)
{
GPIO_ResetBits(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN);
SPI_I2S_SendData(OLED_SPI_PORT, 0x01);
while (SPI_I2S_GetFlagStatus(OLED_SPI_PORT, SPI_I2S_FLAG_BSY) == SET);
SPI_I2S_SendData(OLED_SPI_PORT, data);
while (SPI_I2S_GetFlagStatus(OLED_SPI_PORT, SPI_I2S_FLAG_BSY) == SET);
GPIO_SetBits(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN);
}
void OLED_Init(void)
{
OLED_Write_Command(0xAE); //display off
OLED_Write_Command(0xA8); //set multiplex ratio
OLED_Write_Command(0x3F); //1/64 duty
OLED_Write_Command(0xD3); //set display offset
OLED_Write_Command(0x00);
OLED_Write_Command(0x40); //set start line
OLED_Write_Command(0xA0); //segment remap
OLED_Write_Command(0xC0); //COM scan direction
OLED_Write_Command(0xDA); //set COM pins
OLED_Write_Command(0x12);
OLED_Write_Command(0x81); //set contrast
OLED_Write_Command(0xCF);
OLED_Write_Command(0xD9); //set precharge period
OLED_Write_Command(0xF1);
OLED_Write_Command(0xDB); //set VCOMH deselect level
OLED_Write_Command(0x30);
OLED_Write_Command(0xA4); //disable display all on
OLED_Write_Command(0xA6); //normal display
OLED_Write_Command(0xAF); //display on
}
void OLED_Set_Pos(uint8_t x, uint8_t y)
{
OLED_Write_Command(0xB0 + y);
OLED_Write_Command(((x & 0xF0) >> 4) | 0x10);
OLED_Write_Command(x & 0x0F);
}
void OLED_Clear_Screen(void)
{
uint8_t i, j;
for (i = 0; i < 8; i++) {
OLED_Set_Pos(0, i);
for (j = 0; j < 128; j++) {
OLED_Write_Data(0x00);
}
}
}
void OLED_Show_Char(uint8_t x, uint8_t y, uint8_t ch)
{
uint8_t i, j;
ch = ch - ' ';
OLED_Set_Pos(x, y);
for (i = 0; i < 6; i++) {
OLED_Write_Data(Font6x8[ch][i]);
}
}
void OLED_Show_String(uint8_t x, uint8_t y, char *str)
{
uint8_t i = 0;
while (str[i]) {
OLED_Show_Char(x, y, str[i]);
x += 6;
i++;
}
}
int main(void)
{
OLED_SPI_Init();
OLED_Init();
OLED_Clear_Screen();
OLED_Show_String(0, 0, "Hello, World!");
while (1) {
delay_ms(1000);
}
}
```
阅读全文