用stm32f407的iic驱动4针OLED显示屏的代码
时间: 2024-04-30 11:18:04 浏览: 134
下面是用STM32F407的I2C驱动4针OLED显示屏的代码,假设使用的OLED屏幕为128x64的分辨率:
```c
#include "stm32f4xx.h"
#include "delay.h"
#include "oled.h"
void I2C_GPIO_Config(void);
void I2C_Config(void);
void I2C_Start(void);
void I2C_Stop(void);
void I2C_SendByte(uint8_t byte);
uint8_t I2C_ReadByte(void);
void I2C_SendACK(void);
void I2C_SendNACK(void);
void OLED_WriteCmd(uint8_t cmd);
void OLED_WriteData(uint8_t data);
void OLED_Init(void);
int main(void)
{
I2C_GPIO_Config();
I2C_Config();
OLED_Init();
OLED_Fill(0);
OLED_ShowStr(0, 0, "Hello World!", 1);
while (1)
{
}
}
void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
}
void I2C_Config(void)
{
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C_DeInit(I2C1);
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);
}
void I2C_Start(void)
{
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
}
void I2C_Stop(void)
{
I2C_GenerateSTOP(I2C1, ENABLE);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));
}
void I2C_SendByte(uint8_t byte)
{
I2C_SendData(I2C1, byte);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
uint8_t I2C_ReadByte(void)
{
I2C_AcknowledgeConfig(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
uint8_t byte = I2C_ReceiveData(I2C1);
return byte;
}
void I2C_SendACK(void)
{
I2C_AcknowledgeConfig(I2C1, ENABLE);
I2C_GenerateSTOP(I2C1, DISABLE);
}
void I2C_SendNACK(void)
{
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, DISABLE);
}
void OLED_WriteCmd(uint8_t cmd)
{
I2C_Start();
I2C_SendByte(0x78);
I2C_SendByte(0x00);
I2C_SendByte(cmd);
I2C_Stop();
}
void OLED_WriteData(uint8_t data)
{
I2C_Start();
I2C_SendByte(0x78);
I2C_SendByte(0x40);
I2C_SendByte(data);
I2C_Stop();
}
void OLED_Init(void)
{
OLED_WriteCmd(0xAE);
OLED_WriteCmd(0x20);
OLED_WriteCmd(0x10);
OLED_WriteCmd(0xb0);
OLED_WriteCmd(0xc8);
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(0xd3);
OLED_WriteCmd(0x00);
OLED_WriteCmd(0xd5);
OLED_WriteCmd(0xf0);
OLED_WriteCmd(0xd9);
OLED_WriteCmd(0x22);
OLED_WriteCmd(0xda);
OLED_WriteCmd(0x12);
OLED_WriteCmd(0xdb);
OLED_WriteCmd(0x20);
OLED_WriteCmd(0x8d);
OLED_WriteCmd(0x14);
OLED_WriteCmd(0xaf);
}
void OLED_Fill(uint8_t dat)
{
uint8_t i, j;
for (i = 0; i < 8; i++)
{
OLED_WriteCmd(0xb0 + i);
OLED_WriteCmd(0x00);
OLED_WriteCmd(0x10);
for (j = 0; j < 128; j++)
{
OLED_WriteData(dat);
}
}
}
void OLED_ShowStr(uint8_t x, uint8_t y, uint8_t *str, uint8_t size)
{
uint8_t c = 0, i = 0, j = 0;
while (str[j] != '\0')
{
c = str[j] - 32;
if (x > 120)
{
x = 0;
y++;
}
OLED_ShowChar(x, y, c, size);
x += size / 2;
j++;
}
}
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr, uint8_t size)
{
uint8_t c = 0, i = 0, j = 0;
c = chr - 32;
if (x > 120)
{
x = 0;
y++;
}
OLED_SetPos(x, y);
for (i = 0; i < size; i++)
{
for (j = 0; j < size / 2; j++)
{
OLED_WriteData(F6x8[c][i * size / 2 + j]);
}
}
}
void OLED_SetPos(uint8_t x, uint8_t y)
{
OLED_WriteCmd(0xb0 + y);
OLED_WriteCmd(((x & 0xf0) >> 4) | 0x10);
OLED_WriteCmd(x & 0x0f);
}
```
需要注意的地方:
1. OLED屏幕的I2C地址为0x78,因此在发送数据时需要将地址左移一位后再发送
2. OLED屏幕在初始化时需要发送一系列的命令才能正常工作,具体命令可以参考OLED的数据手册
3. OLED屏幕的显示需要将数据进行分页,每页8行128列,每个像素点使用一个字节表示
4. OLED屏幕的显示需要使用ASCII码表,因此需要对字符进行转换才能正确显示
以上代码应该可以满足你的要求,如果还有问题可以再和我交流。
阅读全文