怎么用stmf103c8t6驱动spi协议的oled
时间: 2023-06-18 11:08:32 浏览: 122
首先,你需要连接你的 OLED 显示屏到 STM32F103C8T6 开发板上。接下来,你需要进行以下步骤:
1. 配置 SPI 接口的时钟和引脚
```c
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; // SCK 和 MOSI 引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // OLED 的片选引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
```
2. 编写 OLED 显示函数
```c
void OLED_init(void)
{
OLED_write_cmd(0xAE);//--display off
OLED_write_cmd(0x00);//---set low column address
OLED_write_cmd(0x10);//---set high column address
OLED_write_cmd(0x40);//--set start line address
OLED_write_cmd(0xB0);//--set page address
OLED_write_cmd(0x81);// contract control
OLED_write_cmd(0xFF);//--128
OLED_write_cmd(0xA1);//set segment remap
OLED_write_cmd(0xA6);//--normal / reverse
OLED_write_cmd(0xA8);//--set multiplex ratio(1 to 64)
OLED_write_cmd(0x3F);//--1/64 duty
OLED_write_cmd(0xC8);//Com scan direction
OLED_write_cmd(0xD3);//-set display offset
OLED_write_cmd(0x00);//
OLED_write_cmd(0xD5);//set osc division
OLED_write_cmd(0x80);
OLED_write_cmd(0xD9);//set pre-charge period
OLED_write_cmd(0xF1);
OLED_write_cmd(0xDA);//set com pins hardware configuration
OLED_write_cmd(0x12);
OLED_write_cmd(0xDB);//--set vcomh
OLED_write_cmd(0x40);
OLED_write_cmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_write_cmd(0x02);//
OLED_write_cmd(0xAF);//--turn on oled panel
}
```
3. 写 OLED 写数据函数
```c
void OLED_write_data(uint8_t data)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4); // 使能 OLED
SPI_I2S_SendData(SPI1, data); // 发送数据
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); // 等待发送完毕
GPIO_SetBits(GPIOA, GPIO_Pin_4); // 禁能 OLED
}
```
4. 写 OLED 写命令函数
```c
void OLED_write_cmd(uint8_t cmd)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4); // 使能 OLED
SPI_I2S_SendData(SPI1, 0x00); // 发送命令
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); // 等待发送完毕
SPI_I2S_SendData(SPI1, cmd); // 发送命令
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); // 等待发送完毕
GPIO_SetBits(GPIOA, GPIO_Pin_4); // 禁能 OLED
}
```
5. 编写主函数
```c
int main(void)
{
OLED_init(); // 初始化 OLED
OLED_write_cmd(0xAF); // 打开 OLED
while(1)
{
// 在这里写你的代码
}
}
```
以上就是使用 STM32F103C8T6 驱动 SPI 协议的 OLED 显示屏的基本步骤。需要注意的是,以上代码仅供参考,具体的实现方式可能会因为硬件和软件环境的不同而有所变化。
阅读全文