在stm32f103c8t6上使用的的0.96寸4针oled显示屏代码
时间: 2023-09-02 18:14:14 浏览: 90
STM32F103C8T6驱动0.96英寸OLED屏幕源码
以下是使用STM32F103C8T6控制0.96寸4针OLED显示屏的代码示例:
```
#include "stm32f10x.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_gpio.h"
#define OLED_DC_PIN GPIO_Pin_5
#define OLED_DC_PORT GPIOB
#define OLED_RST_PIN GPIO_Pin_6
#define OLED_RST_PORT GPIOB
#define OLED_CS_PIN GPIO_Pin_7
#define OLED_CS_PORT GPIOB
void OLED_Init(void);
void OLED_Write_Command(uint8_t cmd);
void OLED_Write_Data(uint8_t data);
void OLED_Set_Pos(uint8_t x, uint8_t y);
void OLED_Fill_Screen(uint8_t data);
void OLED_Show_String(uint8_t x, uint8_t y, uint8_t *str);
void delay_ms(uint16_t n);
int main(void)
{
OLED_Init();
while(1)
{
OLED_Fill_Screen(0x00);
OLED_Show_String(0, 0, "Hello World!");
delay_ms(1000);
}
}
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = OLED_DC_PIN | OLED_RST_PIN | OLED_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(OLED_DC_PORT, &GPIO_InitStructure);
GPIO_SetBits(OLED_DC_PORT, OLED_CS_PIN | OLED_DC_PIN | OLED_RST_PIN);
delay_ms(100);
GPIO_ResetBits(OLED_RST_PORT, OLED_RST_PIN);
delay_ms(20);
GPIO_SetBits(OLED_RST_PORT, OLED_RST_PIN);
delay_ms(100);
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
OLED_Write_Command(0xAE); //display off
OLED_Write_Command(0x20); //Set Memory Addressing Mode
OLED_Write_Command(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
OLED_Write_Command(0xB0); //Set Page Start Address for Page Addressing Mode,0-7
OLED_Write_Command(0xC8); //Set COM Output Scan Direction
OLED_Write_Command(0x00); //---set low column address
OLED_Write_Command(0x10); //---set high column address
OLED_Write_Command(0x40); //--set start line address
OLED_Write_Command(0x81); //--set contrast control register
OLED_Write_Command(0xFF);
OLED_Write_Command(0xA1); //--set segment re-map 0 to 127
OLED_Write_Command(0xA6); //--set normal display
OLED_Write_Command(0xA8); //--set multiplex ratio(1 to 64)
OLED_Write_Command(0x3F); //
OLED_Write_Command(0xA4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
OLED_Write_Command(0xD3); //-set display offset
OLED_Write_Command(0x00); //-not offset
OLED_Write_Command(0xD5); //--set display clock divide ratio/oscillator frequency
OLED_Write_Command(0xF0); //--set divide ratio
OLED_Write_Command(0xD9); //--set pre-charge period
OLED_Write_Command(0x22); //
OLED_Write_Command(0xDA); //--set com pins hardware configuration
OLED_Write_Command(0x12);
OLED_Write_Command(0xDB); //--set vcomh
OLED_Write_Command(0x20); //0x20,0.77xVcc
OLED_Write_Command(0x8D); //--set DC-DC enable
OLED_Write_Command(0x14); //
OLED_Write_Command(0xAF); //--turn on oled panel
}
void OLED_Write_Command(uint8_t cmd)
{
GPIO_ResetBits(OLED_DC_PORT, OLED_DC_PIN);
GPIO_ResetBits(OLED_CS_PORT, OLED_CS_PIN);
SPI_I2S_SendData(SPI1, cmd);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
GPIO_SetBits(OLED_CS_PORT, OLED_CS_PIN);
}
void OLED_Write_Data(uint8_t data)
{
GPIO_SetBits(OLED_DC_PORT, OLED_DC_PIN);
GPIO_ResetBits(OLED_CS_PORT, OLED_CS_PIN);
SPI_I2S_SendData(SPI1, data);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
GPIO_SetBits(OLED_CS_PORT, OLED_CS_PIN);
}
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_Fill_Screen(uint8_t data)
{
uint8_t i, j;
for (i = 0; i < 8; i++)
{
OLED_Write_Command(0xB0 + i);
OLED_Write_Command(0x00);
OLED_Write_Command(0x10);
for (j = 0; j < 128; j++)
{
OLED_Write_Data(data);
}
}
}
void OLED_Show_String(uint8_t x, uint8_t y, uint8_t *str)
{
while (*str != '\0')
{
OLED_Set_Pos(x, y);
uint8_t i;
for (i = 0; i < 8; i++)
{
OLED_Write_Data(FONT8x8[(*str)][i]);
}
x += 8;
str++;
}
}
void delay_ms(uint16_t n)
{
uint16_t i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < 1141; j++);
}
}
```
其中,`OLED_Init()`进行OLED屏幕初始化,`OLED_Write_Command()`和`OLED_Write_Data()`用于向OLED屏幕写入指令和数据,`OLED_Set_Pos()`用于设置OLED屏幕显示位置,`OLED_Fill_Screen()`用于填充整个OLED屏幕,`OLED_Show_String()`用于在指定位置显示字符串。注意,此代码中使用的是8x8大小的字体,需要提供相应的字体库。
阅读全文