stm32f103oled4线spi配置函数
时间: 2023-08-04 18:04:56 浏览: 160
stm32的spi设置
以下是基于标准库的STM32F103C8T6与OLED 0.96寸4线SPI的配置函数示例,供参考:
```c
#include "stm32f10x.h"
#include "oled.h"
void OLED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void OLED_SPI_Config(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_Config();
OLED_SPI_Config();
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
void OLED_WriteByte(uint8_t data)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, data);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
void OLED_SetPos(uint8_t x, uint8_t y)
{
OLED_WriteByte(0xb0+y);
OLED_WriteByte(((x&0xf0)>>4)|0x10);
OLED_WriteByte((x&0x0f)|0x01);
}
void OLED_Fill(uint8_t fill_Data)
{
uint8_t m,n;
for(m=0;m<8;m++)
{
OLED_WriteByte(0xb0+m);
OLED_WriteByte(0x00);
OLED_WriteByte(0x10);
for(n=0;n<128;n++)
{
OLED_WriteByte(fill_Data);
}
}
}
void OLED_CLS(void)
{
OLED_Fill(0x00);
}
void OLED_ON(void)
{
GPIO_SetBits(GPIOB, GPIO_Pin_13);
}
void OLED_OFF(void)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_13);
}
void OLED_ShowStr(uint8_t x,uint8_t y,uint8_t ch[])
{
uint8_t c=0,i=0,j=0;
while (ch[j]!='\0')
{
c = ch[j]-32;
if(x>120)
{
x=0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<6;i++)
{
OLED_WriteByte(F6x8[c][i]);
}
x += 6;
j++;
}
}
```
其中,`OLED_GPIO_Config()`和`OLED_SPI_Config()`函数用于初始化OLED的GPIO和SPI配置,`OLED_Init()`函数用于初始化OLED,`OLED_WriteByte()`函数用于向OLED写入数据,`OLED_SetPos()`函数用于设置OLED显示位置,`OLED_Fill()`函数用于填充OLED屏幕,`OLED_CLS()`函数用于清空OLED屏幕,`OLED_ON()`和`OLED_OFF()`函数用于控制OLED的开关,`OLED_ShowStr()`函数用于在OLED上显示字符串。
阅读全文