1.主要代码部分
read_AHT20_once函数 void read_AHT20_once(void) { delay_ms(10); reset_AHT20();//重置AHT20芯片 delay_ms(10); init_AHT20();//初始化AHT20芯片 delay_ms(10); startMeasure_AHT20();//开始测试AHT20芯片 delay_ms(80); read_AHT20();//读取AHT20采集的到的数据 delay_ms(5); }
2.AHT20芯片读取数据
void read_AHT20(void) { uint8_t i; for(i=0; i<6; i++) { readByte[i]=0; } I2C_Start();//I2C启动 I2C_WriteByte(0x71);//I2C写数据 ack_status = Receive_ACK();//收到的应答信息 readByte[0]= I2C_ReadByte();//I2C读取数据 Send_ACK();//发送应答信息 readByte[1]= I2C_ReadByte(); Send_ACK(); readByte[2]= I2C_ReadByte(); Send_ACK(); readByte[3]= I2C_ReadByte(); Send_ACK(); readByte[4]= I2C_ReadByte(); Send_ACK(); readByte[5]= I2C_ReadByte(); SendNot_Ack(); //Send_ACK(); I2C_Stop();//I2C停止函数 //判断读取到的第一个字节是不是0x08,0x08是该芯片读取流程中规定的,如果读取过程没有问题,就对读到的数据进行相应的处理 if( (readByte[0] & 0x68) == 0x08 ) { H1 = readByte[1]; H1 = (H1<<8) | readByte[2]; H1 = (H1<<8) | readByte[3]; H1 = H1>>4; H1 = (H1*1000)/1024/1024; T1 = readByte[3]; T1 = T1 & 0x0000000F; T1 = (T1<<8) | readByte[4]; T1 = (T1<<8) | readByte[5]; T1 = (T1*2000)/1024/1024 - 500; AHT20_OutData[0] = (H1>>8) & 0x000000FF; AHT20_OutData[1] = H1 & 0x000000FF; AHT20_OutData[2] = (T1>>8) & 0x000000FF; AHT20_OutData[3] = T1 & 0x000000FF; } else { AHT20_OutData[0] = 0xFF; AHT20_OutData[1] = 0xFF; AHT20_OutData[2] = 0xFF; AHT20_OutData[3] = 0xFF; printf("读取失败!!!"); } printf("rn"); //根据AHT20芯片中,温度和湿度的计算公式,得到最终的结果,通过串口显示 printf("温度:%d%d.%d",T1/100,(T1/10)%10,T1%10); printf("湿度:%d%d.%d",H1/100,(H1/10)%10,H1%10); printf("rn"); }
3.运行结果
1.显示自己的学号和姓名
(示例代码:http://www.lcdwiki.com/res/Program/OLED/0.96inch/SPI_SSD1306_MSP096X_V1.0/0.96inch_SPI_OLED_Module_SSD1306_MSP096X_V1.0.zip)
(1)使用样本代码,打开项目
(2)更改设备(改为STM32F103C8芯片),改宏定义大小
(3)在原有的工程中重新选择芯片,需要删除掉多的启动文件,保留一个
(4)使用PCtoLCD2002完美版工具生成汉字字膜
(5)添加到oledfont.h
(6)修改test.c函数
(7)结果图
2.0.96寸OLED在STM32f103上实现滚动
(1)垂直和水平滚动
OLED_WR_Byte(0x2e,OLED_CMD); //关闭滚动 OLED_WR_Byte(0x29,OLED_CMD); //水平垂直和水平滚动左右 OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节 OLED_WR_Byte(0x00,OLED_CMD); //起始页 0 OLED_WR_Byte(0x07,OLED_CMD); //滚动时间间隔 OLED_WR_Byte(0x07,OLED_CMD); //终止页 1 OLED_WR_Byte(0x01,OLED_CMD); //垂直滚动偏移量 OLED_WR_Byte(0x2F,OLED_CMD); //开启滚动
(2)水平左右滚动
OLED_WR_Byte(0x2E,OLED_CMD); //关闭滚动 OLED_WR_Byte(0x26,OLED_CMD); //水平向左或者右滚动 26/27 OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节 OLED_WR_Byte(0x00,OLED_CMD); //起始页 0 OLED_WR_Byte(0x07,OLED_CMD); //滚动时间间隔 OLED_WR_Byte(0x07,OLED_CMD); //终止页 7 OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节 OLED_WR_Byte(0xFF,OLED_CMD); //虚拟字节 OLED_WR_Byte(0x2F,OLED_CMD); //开启滚动
(3)取模并添加到oledfont.h
(4)main代码
int main(void) { delay_init(); //延时函数初始化 NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 OLED_Init(); //初始化OLED OLED_Clear(0); //清屏(全黑) OLED_WR_Byte(0x2e,OLED_CMD); //关闭滚动 OLED_WR_Byte(0x29,OLED_CMD); //水平垂直和水平滚动左右 29/2a OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节 OLED_WR_Byte(0x00,OLED_CMD); //起始页 0 OLED_WR_Byte(0x07,OLED_CMD); //滚动时间间隔 OLED_WR_Byte(0x07,OLED_CMD); //终止页 1 OLED_WR_Byte(0x01,OLED_CMD); //垂直滚动偏移量 TEST_MainPage(); OLED_WR_Byte(0x2F,OLED_CMD); //开启滚动 }
(5)TEST_MainPage()代码部分
GUI_ShowCHinese(10,0,16,"无边丝雨细如愁",1); GUI_ShowCHinese(28,20,16,"王小二",1); GUI_ShowString(4,48,"6319070604xx",16,1); delay_ms(1500); delay_ms(1500);
(6)结果图
通过这一次实验,我对OLED屏的显示有了一定的了解,以及学会了如何垂直和水平滚动显示长字符串。
4、参考文献https://blog.csdn.net/qq_43279579/article/details/111414037?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163713016716780262544908%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=163713016716780262544908&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_v2~rank_v29-3-111414037.pc_v2_rank_blog_default&utm_term=OLED&spm=1018.2226.3001.4450
https://blog.csdn.net/qq_43279579/article/details/111500137
https://blog.csdn.net/darkchipman0/article/details/115455332?spm=1001.2101.3001.6650.4&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-4.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-4.no_search_link
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)