目录
1、IIC的介绍
2、实验:使用STM32重写我们之前51的oled显示屏实验
1.函数封装
2.显示的数据内容
3.oled初始化函数、清屏函数、显示图片函数
4.main函数操作
5.效果展示
这里具体的IIC的介绍可以看我之前的文章:
51单片机使用IO口模拟IIC协议
之前学习51单片机的时候我们是使用io口模拟iic协议的,在STM32这里集成了硬件的iic可以不需要我们和之前51一样了,我们只需要在codeMX里配置好单片机然后调用HAL库的库函数就可以了。
用到的库函数
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 Write_Order(uint8_t Order)
{
HAL_I2C_Mem_Write(&hi2c1,0x78,0x00,I2C_MEMADD_SIZE_8BIT,&Order,1,100);
}
//写数据函数
void Write_Data(uint8_t Data)
{
HAL_I2C_Mem_Write(&hi2c1,0x78,0x40,I2C_MEMADD_SIZE_8BIT,&Data,1,100);
}
//图片在字模提取软件中对用的oled要点亮的点
unsigned char xf[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xE0,0xF0,0xF0,0xF8,0xF8,0xF8,
0xF8,0xF8,0xF0,0xF0,0xF0,0xE0,0xC0,0x80,0x00,0x00,0x80,0xC0,0xE0,0xE0,0xF0,0xF0,
0xF8,0xF8,0xF8,0xF8,0xF8,0xF0,0xF0,0xE0,0xE0,0xC0,0x80,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,0xC0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,
0x80,0x80,0x80,0x80,0x80,0xF8,0xFE,0xFF,0xFF,0xFF,0x0F,0x03,0x01,0x01,0x00,0x00,
0x00,0x01,0x01,0x03,0x07,0x0F,0x3F,0x7F,0xFF,0xFF,0x7F,0x3F,0x0F,0xC7,0xE3,0xF1,
0xF9,0xFC,0xFC,0x7C,0x3F,0x3F,0x3F,0x7F,0xFF,0xFF,0xFF,0xFE,0xF8,0xE0,0xE0,0xF0,
0xF8,0xF8,0xFC,0x7C,0x7C,0x3E,0x3E,0x3E,0x7C,0xFC,0xFC,0xF8,0xF0,0xF0,0xC0,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,0x03,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x0F,
0x0F,0x0F,0x0F,0x0F,0x0F,0x1F,0x1F,0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0x3E,0x3E,0x3E,
0x7E,0x7C,0x7C,0x7C,0x7C,0x7C,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xFF,0xFF,0xFF,0xFF,
0xFF,0xE1,0xE0,0xE0,0xE0,0xE0,0xE0,0xFC,0xFF,0xFF,0xFF,0xFF,0x8F,0x9F,0x9F,0x9F,
0x87,0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81,0xFF,0xFF,0xFF,0xFF,0x7F,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0xFC,0xFC,0xFC,0xF8,0xE0,0xC0,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,0x07,0x0F,0x3F,0x7F,0xFF,0xFE,0xF8,
0xF0,0xE0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x83,0xCF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x8F,0x07,0x07,0x07,0x07,0x0F,0x0F,0x0F,0x0F,
0x0F,0x1F,0x1F,0x1F,0x9F,0xDF,0xFF,0xFF,0xFE,0xFE,0xFF,0x7F,0x3F,0x7F,0x7F,0x7C,
0x7C,0x7C,0xFC,0xF8,0xF8,0xF8,0xF8,0xF8,0xF0,0xF0,0xF1,0xF7,0xFF,0xFF,0xFF,0xFF,
0xFC,0xF8,0xE0,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,0x01,
0x03,0x07,0x0F,0x0F,0x1F,0x3F,0x3F,0x7E,0x7C,0x7C,0x7E,0x3F,0x3F,0x1F,0x0F,0x0F,
0x07,0x03,0x03,0x03,0x07,0x0F,0x1F,0x3F,0x3F,0x7E,0xFC,0xFC,0xF8,0xF0,0xF0,0xF8,
0xFC,0xFC,0x7E,0x3F,0x3F,0x1F,0x0F,0x07,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xE0,0xF0,0xF0,0xF8,0xF8,0xFD,0x7F,0x7F,0x3F,0x3F,0x1F,0x1F,0x0F,
0x07,0x07,0x03,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,0x01,0x01,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,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,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x40,
0x5E,0x73,0x52,0x5E,0x00,0x7F,0x44,0x44,0x47,0x30,0x00,0xFF,0x00,0x04,0x24,0xE4,
0xC4,0x34,0x00,0x00,0xFF,0x00,0x80,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,0x00,0x00,0x00,0x00,0x00,0x04,0x03,0x00,
0x00,0x03,0x04,0x00,0x01,0x06,0x00,0x01,0x02,0x04,0x04,0x03,0x00,0x02,0x01,0x00,
0x00,0x01,0x02,0x00,0x03,0x04,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
//OLED初始化函数
void OLED_Initial()//oled初始化发送以下指令
{
//(01)display off (0xae)
Write_Order(0xae);
//(02)set low column address (0x00)
Write_Order(0x00);
//(03)set high column address (0x10)
Write_Order(0x10);
//(04)set start line address (0x40)
Write_Order(0x40);
//(05)set page address (0xb0)
Write_Order(0xb0);
//(06)contract control (0x81)
Write_Order(0x81);
//(07)send 0xff (多字节指令)
Write_Order(0xff);
//(08)set segment remap (0xa1)
Write_Order(0xa1);
//(09)set normal/reverse (0xa6)
Write_Order(0xa6);
//(10)set multiplex ratio (1 to 64) (0xa8 )
Write_Order(0xa8);
//(11)set duty 1/32 (0x3f)
Write_Order(0x3f);
//(12)com scan direction (0xc8)
Write_Order(0xc8);
//(13)set display offset (0xd3)
Write_Order(0xd3);
//(14)send 0x00
Write_Order(0x00);
//(15)set osc division (0xd5)
Write_Order(0xd5);
//(16)send 0x80
Write_Order(0x80);
//(17)set area color mode off (0xd8)
Write_Order(0xd8);
//(18)send 0x05
Write_Order(0x05);
//(19)set pre-charge period (0xd9)
Write_Order(0xd9);
//(20)send 0xf1
Write_Order(0xf1);
//(21)set com pin configuration (0xda)
Write_Order(0xda);
//(22)send 0x12
Write_Order(0x12);
//(23)set Vcomh (0xdb)
Write_Order(0xdb);
//(24)send 0x30
Write_Order(0x30);
//(25)set charge pump enable (0x8d)
Write_Order(0x8d);
//(26)send 0x14
Write_Order(0x14);
//(27)turn on oled panel(0xaf)
Write_Order(0xaf);
}
//OLED屏幕刷新函数
void CLS()//清屏函数
{
int i,j;
for(i = 0; i<8 ;i++){
Write_Order(0xB0+i);//page0--page7
//每次写的时候从第一列开始
Write_Order(0x00);
Write_Order(0x10);
for(j = 0;j<128;j++){
Write_Data(0x00);//每写入数据地址自动偏移
}
}
}
//显示上图片
void Oled_Huatu(unsigned char *image)
{
int i,j;
for(i = 0; i<8 ;i++){
Write_Order(0xB0+i);//page0--page7
//每次写的时候从第一列开始
Write_Order(0x00);
Write_Order(0x10);
for(j = i*128;j<128*(i+1);j++){
Write_Data(image[j]);//每写入数据地址自动偏移
}
}
}
//oled显示屏初始化
OLED_Initial();
//设置页寻址模式
//00100000
//0x02
Write_Order(0x02);
CLS();
//设置大行64行分为8个大行page
//10110xxx
Write_Order(0xb2);
//配置列,用一个高位一个低位设置
//0000xxxx
//0001xxxx
Write_Order(0x00);
Write_Order(0x10);
//写入数据
Write_Data(0xff);
Write_Data(0xff);
Write_Data(0xff);
Write_Data(0xff);
Write_Data(0xff);
Write_Data(0xff);
Write_Data(0xff);
CLS();
Oled_Huatu(xf);