stm32c8t6驱动oled显示屏的代码
时间: 2023-09-17 11:07:58 浏览: 145
STM32的OLED驱动例程
下面是一个基于STM32C8T6的驱动OLED显示屏的代码示例。代码使用I2C协议与OLED通信,使用的OLED显示屏是SSD1306,但是可以根据具体情况进行修改。
注:以下代码仅供参考,具体实现还需要根据具体情况进行修改。
```c
#include "stm32f10x.h" //包含STM32标准库
#include "oled.h" //包含OLED显示屏函数库
int main(void) {
//初始化I2C接口
I2C_init();
//初始化OLED显示屏
OLED_init();
//在OLED显示屏上显示字符串
OLED_showString(0, 0, "Hello, world!");
while (1) {
//循环执行
}
}
//初始化I2C接口
void I2C_init(void) {
//配置GPIO引脚
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//配置I2C接口
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
//向OLED显示屏发送数据
void OLED_writeData(uint8_t data) {
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, OLED_ADDRESS, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, OLED_DATA_MODE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, data);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
}
//向OLED显示屏发送命令
void OLED_writeCmd(uint8_t cmd) {
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, OLED_ADDRESS, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, OLED_CMD_MODE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, cmd);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
}
//初始化OLED显示屏
void OLED_init(void) {
OLED_writeCmd(0xAE); //关闭显示
OLED_writeCmd(0x20); //设置内存地址模式
OLED_writeCmd(0x10); //水平地址模式
OLED_writeCmd(0xb0); //设置起始行
OLED_writeCmd(0xc8); //设置COM扫描方向
OLED_writeCmd(0x00); //设置低列地址
OLED_writeCmd(0x10); //设置高列地址
OLED_writeCmd(0x40); //设置显示起始线
OLED_writeCmd(0x81); //设置对比度
OLED_writeCmd(0xff); //设置对比度值
OLED_writeCmd(0xa1); //设置段重定向
OLED_writeCmd(0xa6); //设置正常显示
OLED_writeCmd(0xa8); //设置多路复用比率
OLED_writeCmd(0x3f); //设置多路复用比率值
OLED_writeCmd(0xa4); //设置全局显示开启
OLED_writeCmd(0xd3); //设置显示偏移
OLED_writeCmd(0x00); //设置显示偏移值
OLED_writeCmd(0xd5); //设置显示时钟分频
OLED_writeCmd(0xf0); //设置显示时钟分频值
OLED_writeCmd(0xd9); //设置预充电周期
OLED_writeCmd(0x22); //设置预充电周期值
OLED_writeCmd(0xda); //设置COM硬件设置
OLED_writeCmd(0x12); //设置COM硬件设置值
OLED_writeCmd(0xdb); //设置VCOMH
OLED_writeCmd(0x20); //设置VCOMH值
OLED_writeCmd(0x8d); //设置电荷泵
OLED_writeCmd(0x14); //设置电荷泵开启
OLED_writeCmd(0xaf); //开启显示
}
//在OLED显示屏上显示字符串
void OLED_showString(uint8_t x, uint8_t y, uint8_t *str) {
uint8_t c = 0, i = 0, j = 0;
while (str[j] != '\0') {
c = str[j] - 32;
if (x > 120) {
x = 0;
y++;
}
OLED_setPos(x, y);
for (i = 0; i < 6; i++) {
OLED_writeData(F6x8[c][i]);
}
x += 6;
j++;
}
}
//设置OLED显示屏的位置
void OLED_setPos(uint8_t x, uint8_t y) {
OLED_writeCmd(0xb0 + y);
OLED_writeCmd(((x & 0xf0) >> 4) | 0x10);
OLED_writeCmd((x & 0x0f) | 0x01);
}
```
阅读全文