IIC介绍
51是由于芯片功能不齐全,以至于需要软件编写IIC
而STM32芯片足够将IIC配置在硬件当中以至于直接读写即可
忘记了可回顾51的16.IIC 协议 和 OLED_oled,iic通信波特率-CSDN博客
在STM32中使用IIC可以直接调用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,使用的是IIC协议:
在对51的学习,可以轻易的写出对于OLED写命令和写数据的函数封装:
对OLED写命令:
void OLED_writecmd(uint8_t dataCmd)
{
HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT, &dataCmd, 1, 0xff);
}
对OLED写数据:
void OLED_writedata(uint8_t dataData)
{
HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT, &dataData, 1, 0xff);
}
在STM32里再次实现OLED的显示
假如我选择页寻址模式,显示以下图像的话
unsigned char image[] = {//使用unsigned可以使得原本从负数到正数的范围扩展到两倍的正数,适用于大量无符号数存在时
/*-- 宽度x高度=64x64 64*8*8(16进制)--*/
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0x7F,0x3F,0xBF,0x9F,0xDF,0xDF,0xEF,0xEF,0xEF,0xE7,0xEF,0xEF,
0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FF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x7F,0xFF,0xFF,0xFF,0xFF,0xFC,0xFC,0xFC,0xFE,
0xFF,0xFF,0xFE,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0x1F,0x0F,
0x0F,0x0F,0x1F,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
CubeMX
1. 常规配置
SYS->Debug->Serial Wire
RCC->High Speed Clock(HSE)->Crystal/Ceramic Resonator
时钟树HSE、PLLCLK打开,HCLK设置成72MHz
打开I2C1,选择I2C:
2.查看GPIO setting或者原理图,可知 OLED的SCL和SDA应该分别接在 PB6 和 PB7 !
3.生成代码
Keil
unsigned char image[] = {
/*-- 宽度x高度=64x64 64*8*8(16进制)--*/
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0x7F,0x3F,0xBF,0x9F,0xDF,0xDF,0xEF,0xEF,0xEF,0xE7,0xEF,0xEF,
0xFF,0xEF,0xEF,0xFF,0xFF,0xDF,0xDF,0xBF,0xBF,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3F,0xCF,
0xF3,0xF9,0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,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uint8_t dataCmd)
{
HAL_I2C_Mem_Write(&hi2c1,0x78,0x00,I2C_MEMADD_SIZE_8BIT,&dataCmd,1,0xff);
}
void OLED_writedata(uint8_t data)
{
HAL_I2C_Mem_Write(&hi2c1,0x78,0x40,I2C_MEMADD_SIZE_8BIT,&data,1,0xff);
}
void OLED_Init()
{
OLED_writecmd(0xAE);//--display off
OLED_writecmd(0x00);//---set low column address
OLED_writecmd(0x10);//---set high column address
OLED_writecmd(0x40);//--set start line address
OLED_writecmd(0xB0);//--set page address
OLED_writecmd(0x81); // contract control
OLED_writecmd(0xFF);//--128
OLED_writecmd(0xA1);//set segment remap
OLED_writecmd(0xA6);//--normal / reverse
OLED_writecmd(0xA8);//--set multiplex ratio(1 to 64)
OLED_writecmd(0x3F);//--1/32 duty
OLED_writecmd(0xC8);//Com scan direction
OLED_writecmd(0xD3);//-set display offset
OLED_writecmd(0x00);//
OLED_writecmd(0xD5);//set osc division
OLED_writecmd(0x80);//
OLED_writecmd(0xD8);//set area color mode off
OLED_writecmd(0x05);//
OLED_writecmd(0xD9);//Set Pre-Charge Period
OLED_writecmd(0xF1);//
OLED_writecmd(0xDA);//set com pin configuartion
OLED_writecmd(0x12);//
OLED_writecmd(0xDB);//set Vcomh
OLED_writecmd(0x30);//
OLED_writecmd(0x8D);//set charge pump enable
OLED_writecmd(0x14);//
OLED_writecmd(0xAF);//--turn on oled panel
}
void OLED_Clear()
{
int i,j;
for(i = 0;i<8;i++)
{
OLED_writecmd(0xB0 + i); //page 0 --page7
//每个page从列
OLED_writecmd(0x00);//选择0列
OLED_writecmd(0x10);//选择0列
//由于地址会自动偏移,所以只要重复写128次全0,就可以清一个PAGE
for(j = 0;j < 128;j++)
{
OLED_writedata(0x00);
}
}
}
void OLED_Pic(unsigned char *image)
{
unsigned char i;
unsigned int j;
for(i = 0;i<8;i++)
{
OLED_writecmd(0xB0 + i); //page 0 --page7
//每个page从列
OLED_writecmd(0x00);
OLED_writecmd(0x10);
//0到64列,以此写入0,每写入数据,列地址自动偏移
for(j = 64*i;j < 64*(i+1);j++)
{
OLED_writedata(image[j]);
}
}
}
int main(void)
{
// 1.OLED初始化
OLED_Init();
OLED_Clear();
//2.选择一个位置
// 2.1确认页寻址模式
OLED_writecmd(0x20);
OLED_writecmd(0x02);
// 2.2选择page0 1011 0000
OLED_writecmd(0xB0);
OLED_Pic(image);
while (1)
{
}
}
实验效果