在keil里写oled显示 spi驱动代码
时间: 2024-02-22 22:56:51 浏览: 86
树莓派下OLED的spi驱动
以下是一个简单的使用SPI驱动OLED显示屏的Keil代码示例:
```c
#include "stm32f10x.h"
#define OLED_CS_LOW() GPIO_ResetBits(GPIOB, GPIO_Pin_12)
#define OLED_CS_HIGH() GPIO_SetBits(GPIOB, GPIO_Pin_12)
#define OLED_DC_LOW() GPIO_ResetBits(GPIOB, GPIO_Pin_13)
#define OLED_DC_HIGH() GPIO_SetBits(GPIOB, GPIO_Pin_13)
void OLED_Write_Command(uint8_t cmd) {
OLED_DC_LOW();
SPI_I2S_SendData(SPI2, cmd);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
}
void OLED_Write_Data(uint8_t data) {
OLED_DC_HIGH();
SPI_I2S_SendData(SPI2, data);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
}
void OLED_Init() {
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI2, &SPI_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
SPI_Cmd(SPI2, ENABLE);
OLED_Write_Command(0xAE); // display off
OLED_Write_Command(0xD5); // set display clock divide ratio/oscillator frequency
OLED_Write_Command(0x80);
OLED_Write_Command(0xA8); // set multiplex ratio
OLED_Write_Command(0x3F);
OLED_Write_Command(0xD3); // set display offset
OLED_Write_Command(0x00);
OLED_Write_Command(0x40); // set display start line
OLED_Write_Command(0x8D); // set charge pump
OLED_Write_Command(0x14);
OLED_Write_Command(0x20); // set memory mode
OLED_Write_Command(0x00);
OLED_Write_Command(0xA1); // set segment remap
OLED_Write_Command(0xC8); // set COM scan direction
OLED_Write_Command(0xDA); // set COM pins hardware configuration
OLED_Write_Command(0x12);
OLED_Write_Command(0x81); // set contrast control
OLED_Write_Command(0xCF);
OLED_Write_Command(0xD9); // set pre-charge period
OLED_Write_Command(0xF1);
OLED_Write_Command(0xDB); // set VCOMH deselect level
OLED_Write_Command(0x40);
OLED_Write_Command(0xA4); // disable entire display on
OLED_Write_Command(0xA6); // set normal display
OLED_Write_Command(0xAF); // display on
}
void OLED_Clear() {
for (uint16_t i = 0; i < 1024; ++i) {
OLED_Write_Data(0x00);
}
}
void OLED_Draw_Pixel(uint8_t x, uint8_t y) {
OLED_Write_Command(0x20); // set memory addressing mode
OLED_Write_Command(0x00); // horizontal addressing mode
OLED_Write_Command(0x21); // set column address
OLED_Write_Command(x);
OLED_Write_Command(x);
OLED_Write_Command(0x22); // set page address
OLED_Write_Command(y / 8);
OLED_Write_Command(y / 8);
OLED_Write_Data(1 << (y % 8));
}
int main() {
OLED_Init();
OLED_Clear();
OLED_Draw_Pixel(10, 20);
while (1);
}
```
这个代码示例使用SPI2控制OLED显示屏,使用了GPIOB的12、13、14、15四个引脚。代码中OLED_Write_Command和OLED_Write_Data函数用于向OLED发送命令和数据,OLED_Init函数用于初始化OLED显示屏,OLED_Clear函数用于清空屏幕,OLED_Draw_Pixel函数用于在指定的像素位置绘制一个像素。
阅读全文