目录
1.项目需求及IIC简介
3.HAL库IIC函数封装代码
4.CubeMX设置
5.函数代码
在oled屏幕上显示文字或者图像。
IIC协议——OLED屏幕_趣知boy的博客-CSDN博客
用到的库函数:
HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c,
uint16_t DevAddress,
uint16_t MemAddress,
uint16_t MemAddSize,
uint8_t *pData,
uint16_t Size,
uint32_t Timeout)
参数一:I2C_HandleTypeDef *hi2c,I2C设备句柄
参数二:uint16_t DevAddress,目标器件的地址,七位地址必须左对齐
参数三:uint16_t MemAddress,目标器件的目标寄存器地址
参数四:uint16_t MemAddSize,目标器件内部寄存器地址数据长度
参数五:uint8_t *pData,待写的数据首地址
参数六:uint16_t Size,待写的数据长度
参数七:uint32_t Timeout,超时时间
返回值:HAL_StatusTypeDef,HAL状态(OK,busy,ERROR,TIMEOUT)
//向OLED写入命令
void w_cmd_i2c(uint8_t cmd)
{
HAL_I2C_Mem_Write(&hi2c1,0x78,0x00,1,&cmd,1,0xff);
}
//向OLED写入数据
void w_data_i2c(uint8_t data1)
{
HAL_I2C_Mem_Write(&hi2c1,0x78,0x40,1,&data1,1,0xff);
}
1.重写OLED写命令函数、写数据函数
2.OLED初始化
3.OLED清屏
4.OLED写图像
void w_cmd_i2c(uint8_t cmd)
{
HAL_I2C_Mem_Write(&hi2c1,0x78,0x00,1,&cmd,1,0xff);
}
void w_data_i2c(uint8_t data1)
{
HAL_I2C_Mem_Write(&hi2c1,0x78,0x40,1,&data1,1,0xff);
}
void init_i2c(void)
{
w_cmd_i2c(0xAE);//--display off
w_cmd_i2c(0x00);//---set low column address
w_cmd_i2c(0x10);//---set high column address
w_cmd_i2c(0x40);//--set start line address
w_cmd_i2c(0xB0);//--set page address
w_cmd_i2c(0x81); // contract control
w_cmd_i2c(0xFF);//--128
w_cmd_i2c(0xA1);//set segment remap
w_cmd_i2c(0xA6);//--normal / reverse
w_cmd_i2c(0xA8);//--set multiplex ratio(1 to 64)
w_cmd_i2c(0x3F);//--1/32 duty
w_cmd_i2c(0xC8);//Com scan direction
w_cmd_i2c(0xD3);//-set display offset
w_cmd_i2c(0x00);//
w_cmd_i2c(0xD5);//set osc division
w_cmd_i2c(0x80);//
w_cmd_i2c(0xD8);//set area color mode off
w_cmd_i2c(0x05);//
w_cmd_i2c(0xD9);//Set Pre-Charge Period
w_cmd_i2c(0xF1);//
w_cmd_i2c(0xDA);//set com pin configuartion
w_cmd_i2c(0x12);//
w_cmd_i2c(0xDB);//set Vcomh
w_cmd_i2c(0x30);//
w_cmd_i2c(0x8D);//set charge pump enable
w_cmd_i2c(0x14);//
w_cmd_i2c(0xAF);//--turn on oled panel
}
void clear_oled(void)
{
char i; int j;
for(i=0;i<8;i++){
w_cmd_i2c(0xB0+i); //变换page
w_cmd_i2c(0x00); //设置起始地址 低位
w_cmd_i2c(0x10); //设置起始地址 高位
for(j=0;j<128;j++){
w_data_i2c(0x00); //写0就行
}
}
}
void write_image(char *tx)
{
int i,j;
for(i=0;i<8;i++){
w_cmd_i2c(0xB0+i); //变换page 共有0-7 8个
w_cmd_i2c(0x00); //设置起始地址 低位
w_cmd_i2c(0x10); //设置起始地址 高位
//i=3;
for(j=128*i; j<(128*(i+1)); j++){ //变换的是tx中的数据
w_data_i2c(tx[j]); //tx中的数据
}
}
}
char tx[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xF0,0x78,0x7C,0x7C,0xBE,0xBE,0xFE,
0xFE,0xFE,0xDE,0xCF,0xCF,0x9F,0x9F,0x8E,0x8E,0x87,0x83,0x80,0x80,0x80,0x80,0x80,
0x80,0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0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x01,0x01,0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x01,0x03,0x03,0x07,0x07,
0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x1F,0xFF,0xFF,0xFF,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFE,0x7E,0x7F,0x7F,0x7F,0x3F,0x30,0x38,0x3F,0x3F,0x3F,0x3F,
0x3F,0x3F,0x3F,0x1F,0x0F,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x07,0x0F,0x0F,0x1F,0x1F,0x1F,
0x3F,0x3F,0x3C,0x3C,0x7E,0x7E,0x7E,0x0E,0x1C,0x3C,0x30,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //这是page7的内容
main函数中代码
init_i2c();
clear_oled();
write_image(tx);