oled12864与STM32实现I2C通信
时间: 2023-11-18 07:04:31 浏览: 354
STM32 STM32F103驱动I2C_12864OLED的例程
要在STM32上实现与OLED12864的I2C通信,你需要完成以下步骤:
1. 确认OLED12864的I2C地址,一般为0x3C或0x3D。
2. 在STM32上配置I2C接口,设置时钟频率、地址模式等参数。
3. 初始化OLED12864,发送相应的初始化指令。
4. 发送数据到OLED12864,例如显示字符或图片等。
5. 关闭I2C接口和OLED12864。
以下是一个简单的例程,可以帮助你了解如何在STM32上实现I2C通信和控制OLED12864显示:
```c
#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#include "oled12864.h"
#define OLED_I2C I2C1
#define OLED_I2C_CLK RCC_APB1Periph_I2C1
#define OLED_I2C_SCL_GPIO GPIOB
#define OLED_I2C_SCL_PIN GPIO_Pin_6
#define OLED_I2C_SDA_GPIO GPIOB
#define OLED_I2C_SDA_PIN GPIO_Pin_7
#define OLED_I2C_SPEED 400000
#define OLED_I2C_ADDR 0x3C
void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
/* Enable I2C and GPIO clocks */
RCC_APB1PeriphClockCmd(OLED_I2C_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure I2C pins */
GPIO_InitStructure.GPIO_Pin = OLED_I2C_SCL_PIN | OLED_I2C_SDA_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(OLED_I2C_SCL_GPIO, &GPIO_InitStructure);
/* Configure I2C */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = OLED_I2C_SPEED;
I2C_Init(OLED_I2C, &I2C_InitStructure);
/* Enable I2C */
I2C_Cmd(OLED_I2C, ENABLE);
}
void OLED_Init(void)
{
uint8_t init_cmd[] = {0xAE, 0xD5, 0x80, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0x8D, 0x14, 0x20, 0x00, 0xA1, 0xC8, 0xDA, 0x12, 0x81, 0xCF, 0xD9, 0xF1, 0xDB, 0x40, 0xA4, 0xA6, 0x2E, 0xAF};
uint8_t i;
/* Send initialization commands */
for (i = 0; i < sizeof(init_cmd); i++) {
OLED_WriteCommand(init_cmd[i]);
}
}
void OLED_WriteCommand(uint8_t cmd)
{
/* Send command */
while (I2C_GetFlagStatus(OLED_I2C, I2C_FLAG_BUSY)) ;
I2C_GenerateSTART(OLED_I2C, ENABLE);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_MODE_SELECT)) ;
I2C_Send7bitAddress(OLED_I2C, OLED_I2C_ADDR, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) ;
I2C_SendData(OLED_I2C, 0x00);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
I2C_SendData(OLED_I2C, cmd);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
I2C_GenerateSTOP(OLED_I2C, ENABLE);
}
void OLED_WriteData(uint8_t data)
{
/* Send data */
while (I2C_GetFlagStatus(OLED_I2C, I2C_FLAG_BUSY)) ;
I2C_GenerateSTART(OLED_I2C, ENABLE);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_MODE_SELECT)) ;
I2C_Send7bitAddress(OLED_I2C, OLED_I2C_ADDR, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) ;
I2C_SendData(OLED_I2C, 0x40);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
I2C_SendData(OLED_I2C, data);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
I2C_GenerateSTOP(OLED_I2C, ENABLE);
}
int main(void)
{
/* Configure I2C */
I2C_Configuration();
/* Initialize OLED */
OLED_Init();
/* Display "Hello, world!" */
OLED_GotoXY(0, 0);
OLED_Puts("Hello, world!");
while (1) {
/* Do something */
}
}
```
阅读全文