IIC和OLED再认识

 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,0x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F,0xFF,
0xFF,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的SCLSDA应该分别接在 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,
0x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ED_writecmd(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 offOLED_writecmd(0x00);//---set low column addressOLED_writecmd(0x10);//---set high column addressOLED_writecmd(0x40);//--set start line address  OLED_writecmd(0xB0);//--set page addressOLED_writecmd(0x81); // contract controlOLED_writecmd(0xFF);//--128   OLED_writecmd(0xA1);//set segment remap OLED_writecmd(0xA6);//--normal / reverseOLED_writecmd(0xA8);//--set multiplex ratio(1 to 64)OLED_writecmd(0x3F);//--1/32 dutyOLED_writecmd(0xC8);//Com scan directionOLED_writecmd(0xD3);//-set display offsetOLED_writecmd(0x00);//OLED_writecmd(0xD5);//set osc divisionOLED_writecmd(0x80);//OLED_writecmd(0xD8);//set area color mode offOLED_writecmd(0x05);//OLED_writecmd(0xD9);//Set Pre-Charge PeriodOLED_writecmd(0xF1);//OLED_writecmd(0xDA);//set com pin configuartionOLED_writecmd(0x12);//OLED_writecmd(0xDB);//set VcomhOLED_writecmd(0x30);//OLED_writecmd(0x8D);//set charge pump enableOLED_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,就可以清一个PAGEfor(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 0000OLED_writecmd(0xB0);OLED_Pic(image);while (1){}}

实验效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/623984.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024.4.16

三个按键的中断 do_irq.c #include "mykey.h" extern void printf(const char *fmt, ...); unsigned int i 0; void do_irq(void) {//获取中断号unsigned int irqno (GICC->IAR&0x3ff);switch (irqno){case 99://中断处理逻辑printf("KEY1_INTC\n&q…

java绘图在ubuntu报错

把JRT网站部署到ubuntu桌面系统上&#xff0c;开始没测试绘图部分功能&#xff0c;只试了连PostGreSql部分正常。后面试了生成位图部分发现报错。 报下面错误&#xff1a; (ColorModel.java:220)\n\tat java.desktop/java.awt.image.BufferedImage.(BufferedImage.java:286)\n…

大历史下的 tcp:从早期拥塞控制 到 bbr 再到未来

TCP协议有了拥塞控制机制&#xff0c;为什么还会网络拥塞&#xff1f; 随着骨干带宽增长&#xff0c;拥塞被阻滞在接入网&#xff0c;大规模拥塞崩溃难再呈现&#xff0c;tcp 拥塞控制(不仅限于 tcp&#xff0c;但以 tcp 为主线来说)从避免崩溃&#xff0c;保证可用性逐渐转到…

【从浅学到熟知Linux】进程控制下篇=>进程程序替换与简易Shell实现(含替换原理、execve、execvp等接口详解)

&#x1f3e0;关于专栏&#xff1a;Linux的浅学到熟知专栏用于记录Linux系统编程、网络编程等内容。 &#x1f3af;每天努力一点点&#xff0c;技术变化看得见 文章目录 进程程序替换什么是程序替换及其原理替换函数execlexeclpexecleexecvexecvpexecvpeexecve 替换函数总结实现…

JavaScript基础:js介绍、变量、数据类型以及类型转换

目录 介绍 引入方式 内部方式 外部形式 注释和结束符 单行注释 多行注释 结束符 输入和输出 输出 输入 变量 声明 赋值 关键字 变量名命名规则 常量 数据类型 数值类型 字符串类型 布尔类型 undefined 类型转换 隐式转换 显式转换 Number ✨介绍 &a…

500元左右的运动耳机怎么选?五大质量超群品牌分享

在运动中&#xff0c;一款合适的耳机不仅可以提升运动的乐趣&#xff0c;更能激励我们坚持锻炼&#xff0c;在市场上的运动耳机种类繁多&#xff0c;价格不一&#xff0c;如何选择一款既适合自己又物有所值的运动耳机呢&#xff1f;特别是在500元左右的预算范围内&#xff0c;我…

多规格产品应该如何设置呢?

今天一用户从供应商手中拿到产品价目表&#xff0c;但是设置起来蒙圈了&#xff0c;接下来我们就一起设置一下吧&#xff5e; 一、产品价格表 我们通过供应商手中拿到产品价目表是这个样子的&#xff1a; 5bea1202312081615331303.png 我们可以看到此产品的销售客价根据不同…

【中间件】ElasticSearch简介和基本操作

一、简介 Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎&#xff0c;支持各种数据类型&#xff0c;包括文本、数字、地理、结构化、非结构化 ,可以让你存储所有类型的数据&#xff0c;能够解决不断涌现出的各种用例。其构成如下&#xff1a; 说明&#xff1…

【SVN】clean up报错:Cleanup failed to process the following paths 解决方法

报错来源&#xff1a;代码更新有一个文件既不能接受自己的也不能接受别人的&#xff0c;只能取消&#xff0c;再提交提醒clean up&#xff0c;随后报标题错误。 解决方法&#xff1a;参考https://www.cnblogs.com/pinpin/p/11395438.html 注&#xff1a;如果clean up的时候有…

【亲测有用】2024年timm从本地加载模型

有图有真相&#xff1a;https://github.com/huggingface/pytorch-image-models/discussions/1826 好使。 其他的过时的方法就别看了。&#xff08;当然我这个过几年也会过时&#xff09;

亚信安慧AntDB:为安全加码

亚信安慧AntDB分布式数据库凭借平滑扩展、高可用性和低成本三大核心优势&#xff0c;在业界获得了极高的评价和认可。这些优点不仅为AntDB提供了巨大的市场发展潜力&#xff0c;也使其成为众多企业在数据管理上的首选解决方案。 AntDB的平滑扩展特性极大地提升了企业的灵活性和…

asyncio异步编程

参考视频&#xff1a;02 协程_哔哩哔哩_bilibili 1.协程&#xff1a;&#xff08;不是计算机中真实存在的&#xff0c;人为创造出的概念&#xff09;&#xff0c;也可称为微线程。 本质是用一个线程在一段代码中来会切换游走的线程&#xff0c;是一种用户态内的上下文切换技术…