STM32系列(HAL库)——F103C8T6使用SPI方式点亮OLED
时间: 2023-11-21 11:06:49 浏览: 264
好的,下面是使用STM32 HAL库,通过SPI方式驱动OLED的基本步骤:
1. 初始化SPI接口
在使用SPI接口前需要先对其进行初始化,包括SPI引脚配置、时钟配置和SPI参数配置等,具体代码如下:
```c
/*定义SPI句柄*/
SPI_HandleTypeDef hspi1;
/*初始化SPI*/
void MX_SPI1_Init(void)
{
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
```
2. 初始化OLED
在使用OLED前需要对其进行初始化,包括GPIO配置、时钟配置和OLED参数配置等,具体代码如下:
```c
/*OLED硬件SPI引脚定义*/
#define OLED_SPI_CS_GPIO_Port GPIOA
#define OLED_SPI_CS_Pin GPIO_PIN_4
#define OLED_SPI_DC_GPIO_Port GPIOA
#define OLED_SPI_DC_Pin GPIO_PIN_3
/*初始化OLED*/
void OLED_Init(void)
{
/*配置OLED SPI接口的片选引脚*/
HAL_GPIO_WritePin(OLED_SPI_CS_GPIO_Port, OLED_SPI_CS_Pin, GPIO_PIN_SET);
/*配置OLED SPI接口的DC引脚*/
HAL_GPIO_WritePin(OLED_SPI_DC_GPIO_Port, OLED_SPI_DC_Pin, GPIO_PIN_SET);
/*配置OLED SPI接口的RESET引脚*/
OLED_RST_Set();
HAL_Delay(100);
OLED_RST_Clr();
HAL_Delay(100);
OLED_RST_Set();
HAL_Delay(100);
/*初始化OLED*/
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
OLED_WR_Byte(0x00,OLED_CMD);//-not offset
OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_WR_Byte(0x02,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
OLED_Clear();
}
```
3. 实现SPI传输函数
SPI传输函数是使用SPI接口与OLED进行数据传输的函数,具体代码如下:
```c
/*OLED硬件SPI引脚定义*/
#define OLED_SPI_CS_GPIO_Port GPIOA
#define OLED_SPI_CS_Pin GPIO_PIN_4
#define OLED_SPI_DC_GPIO_Port GPIOA
#define OLED_SPI_DC_Pin GPIO_PIN_3
/*OLED写数据*/
void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
{
if(cmd)
{
HAL_GPIO_WritePin(OLED_SPI_DC_GPIO_Port, OLED_SPI_DC_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(OLED_SPI_DC_GPIO_Port, OLED_SPI_DC_Pin, GPIO_PIN_RESET);
}
/*使能OLED*/
HAL_GPIO_WritePin(OLED_SPI_CS_GPIO_Port, OLED_SPI_CS_Pin, GPIO_PIN_RESET);
/*通过SPI发送数据*/
HAL_SPI_Transmit(&hspi1,&dat,1,1000);
/*关闭OLED*/
HAL_GPIO_WritePin(OLED_SPI_CS_GPIO_Port, OLED_SPI_CS_Pin, GPIO_PIN_SET);
}
/*OLED写数据*/
void OLED_WR_LByte(uint8_t dat,uint8_t cmd)
{
uint8_t data[2];
data[0] = dat;
data[1] = 0x00;
if(cmd)
{
HAL_GPIO_WritePin(OLED_SPI_DC_GPIO_Port, OLED_SPI_DC_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(OLED_SPI_DC_GPIO_Port, OLED_SPI_DC_Pin, GPIO_PIN_RESET);
}
/*使能OLED*/
HAL_GPIO_WritePin(OLED_SPI_CS_GPIO_Port, OLED_SPI_CS_Pin, GPIO_PIN_RESET);
/*通过SPI发送数据*/
HAL_SPI_Transmit(&hspi1,data,2,1000);
/*关闭OLED*/
HAL_GPIO_WritePin(OLED_SPI_CS_GPIO_Port, OLED_SPI_CS_Pin, GPIO_PIN_SET);
}
```
4. 实现OLED显示函数
OLED显示函数是向OLED发送数据实现显示的函数,具体代码如下:
```c
/*OLED写数据*/
void OLED_WR_BP(uint8_t dat)
{
uint8_t i;
for(i=0;i<8;i++)
{
if(dat&0x80)
OLED_WR_Byte(0xff,OLED_DATA);
else
OLED_WR_Byte(0x00,OLED_DATA);
dat<<=1;
}
}
/*清屏*/
void OLED_Clear(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte(0xb0+i,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0x10,OLED_CMD);
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
}
}
```
5. 调用OLED显示函数
初始化完成后,就可以调用OLED显示函数来实现向OLED发送数据实现显示了,具体代码如下:
```c
int main(void)
{
/*初始化*/
HAL_Init();
MX_SPI1_Init();
OLED_Init();
/*显示*/
OLED_Clear();
OLED_ShowString(0,0,"Hello World!",16);
while (1)
{
}
}
```
以上就是通过SPI接口驱动OLED的基本步骤,需要注意的是具体的硬件引脚配置和参数配置需要根据实际情况进行修改。
阅读全文