本文来源电子发烧友社区,作者:李元江, 帖子地址:https://bbs.elecfans.com/jishu_2021769_1_1.html
今天来写一篇关于使用显示板的帖子。主要完成温湿度传感器数据采集,把采集到的数据显示到OELD显示屏,显示时钟。
今天来写一篇关于使用显示板的帖子。主要完成温湿度传感器数据采集,把采集到的数据显示到OELD显示屏,显示时钟。
一、硬件介绍
通过查看环境监测板的原理图,发现该板子主要有两个种资源:12864OLED显示屏,两个模拟按键。两个模拟按键分别按下时,因为电阻分压,在SWITCH端的电压都一样,根据测量这个SWITCH端电压值,可以读取到按键的状态。
它们使用主控的GPIO口分别为:AHT20温湿度传感器SDA --》 GPIO13 可复用为I2C0_SDASCL --》 GPIO14 可复用为I2C0_SCL模拟按键ADC --》 GPIO05 可复用为ADC2
二、软件设计1、新建文件
其中aht20.c、aht20.h、envrionment_demo.c。是从上一篇帖子移植过来的,主要跟环境监测板有关。oled_demo.c 、oled_fonts.h 、 oled_ssd1306.h 、oled_ssd1306.c从许思维老师例程中移植过来。timeconv.c timeconv.h与时间转换有关。
2、timeconv.c
该c文件主要跟时间戳转换有关,在这个例程中,每一秒时间戳数值加1,然后再把时间戳转换为北京时间即可到的时间戳对应的北京时间。
- #include "timeconv.h"
- #include
- #include
-
- static uint8_t month_day[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //平年
- static uint8_t Leap_month_day[12]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年
- const uint16_t dayPerYear[4] = {365, 365, 365, 366};
- // 判断是否是闰年
- // year: 需要判断的年
- // return:1:闰年
- // 0: 平年
- uint8_t isLeapYear(uint16_t year)
- {
- uint8_t res=0;
- if(year%4 == 0) // 能够被4整除
- {
- if((year%100 == 0) && (year%400 != 0)) //能够被100整除,但是不能够被400整除
- {
- res = 0;
- }
- else
- {
- res =1;
- }
- }
- return res;
- }
- // 将Unix时间戳转换为北京时间
- // unixTime: 需要判断的Unix时间戳
- // *tempBeijing:返回的北京时间
- // return:none
- // note:没对输入参数正确性做判断
- void covUnixTimeStp2Beijing(uint32_t unixTime, rtc_time_t *tempBeijing)
- {
- uint32_t totleDaynum=0, totleSecNum=0;
- uint16_t remainDayofYear;
- uint8_t *pr, tempYear=0;
- totleDaynum = (uint32_t)unixTime/(((uint32_t)24*60*60)); //总天数(注意加括号)
- totleSecNum = (uint32_t)unixTime%((uint32_t)24*60*60); //当天剩余的秒速
- memset(tempBeijing, 0x00, sizeof(rtc_time_t));
- // 1.计算哪一年
- tempBeijing->ui8Year = 1970 + (totleDaynum/((uint32_t)FOURYEARDAY))*4;
- remainDayofYear = totleDaynum%((uint32_t)FOURYEARDAY)+1;
- //Delay_1ms(1000);
- while(remainDayofYear > dayPerYear[tempYear])
- {
- remainDayofYear -= dayPerYear[tempYear];
- tempBeijing->ui8Year++;
- tempYear++;
- }
- //2.计算哪一月的哪一天
- pr = isLeapYear(tempBeijing->ui8Year)?Leap_month_day:month_day;
- while(remainDayofYear > *(pr+tempBeijing->ui8Month))
- {
- remainDayofYear -= *(pr+tempBeijing->ui8Month);
- tempBeijing->ui8Month++;
- }
- tempBeijing->ui8Month++; //month
- tempBeijing->ui8DayOfMonth = remainDayofYear; //day
- //3.计算当天时间
- tempBeijing->ui8Hour = (totleSecNum)/3600;
- tempBeijing->ui8Minute = (totleSecNum%3600)/60; //error:变量搞错
- tempBeijing->ui8Second = (totleSecNum%3600)%60;
- //4.时区调整
- tempBeijing->ui8Hour +=TIMEZONE;
- if(tempBeijing->ui8Hour>23){
- tempBeijing->ui8Hour -= 24;
- tempBeijing->ui8DayOfMonth++;
- }
- }
- // 将北京时间转换为Unix时间戳
- // year: 需要判断的年
- // return:Unix时间戳(从1970/1/1 00:00:00 到现在的秒数)
- // note:没对输入参数正确性做判断
- uint32_t covBeijing2UnixTimeStp(rtc_time_t *beijingTime)
- {
- uint32_t daynum=0, SecNum=0; //保存北京时间到起始时间的天数
- uint16_t tempYear=1970, tempMonth=0;
- //1.年的天数
- while(tempYear < beijingTime->ui8Year)
- {
- if(isLeapYear(tempYear)){
- daynum += 366;
- }
- else{
- daynum += 365;
- }
- tempYear++;
- }
- //2.月的天数
- while(tempMonth < beijingTime->ui8Month-1)
- {
- if(isLeapYear(beijingTime->ui8Year)){ //闰年
- daynum += Leap_month_day[tempMonth];
- }
- else{
- daynum += month_day[tempMonth];
- }
- tempMonth++;
- }
- //3.天数
- daynum += (beijingTime->ui8DayOfMonth-1);
- //4.时分秒
- SecNum = daynum*24*60*60; //s
- SecNum += beijingTime->ui8Hour*60*60;
- SecNum += beijingTime->ui8Minute*60;
- SecNum += beijingTime->ui8Second;
- //5.时区调整
- SecNum -= TIMEZONE*60*60;
- return SecNum;
- }
- #ifndef __TIMECONV_H
- #define __TIMECONV_H
-
- #include
- #include
- #include
- #include "ohos_init.h"
- #include "cmsis_os2.h"
- #define FOURYEARDAY (((uint32_t)365+365+365+366))//4年一个周期内的总天数(1970~2038不存在2100这类年份,故暂不优化)
- #define TIMEZONE (8) //北京时区调整
-
- typedef struct rtc_time_struct
- {
- uint16_t ui8Year; // 1970~2038
- uint8_t ui8Month; // 1~12
- uint8_t ui8DayOfMonth;// 1~31
- uint8_t ui8Week;
- uint8_t ui8Hour; // 0~23
- uint8_t ui8Minute; // 0~59
- uint8_t ui8Second; // 0~59
- }rtc_time_t;
-
- uint8_t isLeapYear(uint16_t year);
- void covUnixTimeStp2Beijing(uint32_t unixTime, rtc_time_t *tempBeijing);
- uint32_t covBeijing2UnixTimeStp(rtc_time_t *beijingTime);
-
- #endif /*__TIMECONV_H*/
-
该c文件主要是oled显示屏显示程序、包括时间转换显示、温湿度显示。
- extern float humidity;
- extern float temperature;
- static void OledTask(void *arg)
- {
- rtc_time_t mData;
- (void)arg;
-
- GpioInit();
-
- OledInit();
- OledFillScreen(0x00);
- while (1) {
- static char text[128] = {0};
- unsigned short data = 0;
- AdcRead(analog_KEY_CHAN_NAME, &data, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0);
- float voltage = ConvertToVoltage(data);
- snprintf(text, sizeof(text), "voltage: %.3f!", voltage);
- //OledShowString(0, 5, text, 1);
-
- OLED_ShowCHinese(0,3,0);//温
- OLED_ShowCHinese(16,3,2);//度
- OledShowChar(32,3,':',2);
- sprintf((char *)TimeStr,"%02d",
- (uint32_t)temperature);
- OledShowString(40,3,(char *)TimeStr,2);
- OLED_ShowCHinese(64,3,1);//湿
- OLED_ShowCHinese(80,3,2);//度
- OledShowChar(96,3,':',2);
- sprintf((char *)TimeStr,"%02d",
- (uint32_t)humidity);
- OledShowString(104,3,(char *)TimeStr,2);
- timedata++;
- covUnixTimeStp2Beijing(timedata, &mData);
- sprintf((char *)TimeStr,"%04d-%02d-%02d",
- mData.ui8Year, mData.ui8Month, mData.ui8DayOfMonth);
- OledShowString(24,0,(char *)TimeStr,1);
- sprintf((char *)TimeStr,"%02d:%02d:%02d",
- mData.ui8Hour,mData.ui8Minute,mData.ui8Second);
- OledShowString(32,1,(char *)TimeStr,1);
- sleep(1);
- }
- }
原来例程上是不能中文的,现在把显示中文功能加上,但是需要提前使用取模软件把字取模。
- //显示汉字
- void OLED_ShowCHinese(uint8_tx,uint8_ty,uint8_tno)
- {
- uint8_tt,adder=0;
- OledSetPosition(x,y);
- for(t=0;t<16;t++)
- {
- WriteData(Hzk[2*no][t]);
- adder+=1;
- }
- OledSetPosition(x,y+1);
- for(t=0;t<16;t++)
- {
- WriteData(Hzk[2*no+1][t]);
- adder+=1;
- }
- }
字模数组
- static const unsigned charHzk[][16]={
- {0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00},
- {0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00},/*"温",0*/
- /* (16 X 16 , 宋体 )*/
- {0x10,0x60,0x02,0x8C,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00},
- {0x04,0x04,0x7E,0x01,0x44,0x48,0x50,0x7F,0x40,0x40,0x7F,0x50,0x48,0x44,0x40,0x00},/*"湿",1*/
- /* (16 X 16 , 宋体 )*/
- {0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00},
- {0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00},/*"度",2*/
- };
BUILD.gn内容为
- # Copyright (c) 2020, HiHope Community.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions are met:
- #
- # 1. Redistributions of source code must retain the above copyright notice, this
- # list of conditions and the following disclaimer.
- #
- # 2. Redistributions in binary form must reproduce the above copyright notice,
- # this list of conditions and the following disclaimer in the documentation
- # and/or other materials provided with the distribution.
- #
- # 3. Neither the name of the copyright holder nor the names of its
- # contributors may be used to endorse or promote products derived from
- # this software without specific prior written permission.
- #
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- static_library("oled_example") {
- sources = [
- "oled_demo.c",
- "oled_ssd1306.c",
- "timeconv.c",
- "envrionment_demo.c",
- "aht20.c"
- ]
-
- include_dirs = [
- "//utils/native/lite/include",
- "//kernel/liteos_m/components/cmsis/2.0",
- "//base/iot_hardware/inteRFaces/kits/wifiiot_lite",
- ]
- }
这样还不行,还要修改app文件夹下的BUILD.gn。在features下增加"OLED:oled_example",
- lite_component("app") {
- features = [
- "startup",
- "SSL:ssl_example",
- "EM:em_example",
- "OLED:oled_example"
- ]
- }
演示结果
显示的时间是没有进行对时的,下一篇讲写关于如何连接网络,并且如何获取网络时间。然后把显示屏上时间更新为网络时间。
`
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。
举报投诉
-
wi-fi
+关注
关注
14文章
2113浏览量
124331 -
HarmonyOS
+关注
关注
79文章
1967浏览量
30027 -
HiSpark
+关注
关注
1文章
156浏览量
6904
发布评论请先 登录
相关推荐
Wi-Fi 8要来了!未来Wi-Fi技术演进方向揭秘
产品销售中,虽然Wi-Fi 7产品的销量份额快速从年初的个位数百分比增长至14%,但Wi-Fi 6产品的销量份额依然稳定在60%左右。如果从存量设备来看,那么Wi-Fi 7路由器的占比将远远小于这个数。 智能手机等终端设备已
Wi-Fi 7与Wi-Fi 6E有什么区别
也许很多人还在考虑是否要将使用的Wi-Fi设备升级到Wi-Fi 6或Wi-Fi 6E,而这些标准的继任者却已经开始“登堂入室”了。Wi-Fi 7是新一代
IR900 Wi-Fi联网的配置过程
, 通过SSID扫描可以查看;输入密码,点击保存;
在SSID扫描页面中查看是否连接成功, 如果没有请检查认证方式、加密方式、密码是否输入正确;
Wi-Fi扫描显示“已连接”,进入“快速向导-新建WAN
发表于 07-25 06:09
未来的Wi-Fi路由器
转载自——钟林谈芯 没有创新就没有未来,Wi-Fi路由器也是如此。 进入2024年以后,Wi-Fi路由器市场变得更加艰难,利润大幅下滑,BOM成本价高于市场价,接不接订单,都陷入两难。 国内主芯片
DA16200 超低功耗 Wi-Fi 模块开发套件 Pro数据手册
电子发烧友网站提供《DA16200 超低功耗 Wi-Fi 模块开发套件 Pro数据手册.rar》资料免费下载
发表于 05-30 17:53
•1次下载
DA16200 超低功耗 Wi-Fi 模块开发套件数据手册
电子发烧友网站提供《DA16200 超低功耗 Wi-Fi 模块开发套件数据手册.rar》资料免费下载
发表于 05-30 17:13
•0次下载
验证物联网Wi-Fi HaLow用例的MM6108-EKH08开发套件来啦
验证物联网Wi-Fi HaLow用例的MM6108-EKH08开发套件来啦 MM6108-EKH08开发套件专为验证物联网Wi-Fi HaLow用例而设计。该
Wi-Fi HaLow和传统Wi-Fi的区别
Wi-Fi HaLow和传统Wi-Fi的区别 Wi-Fi是一种无线网络技术,可以连接到互联网或局域网,为用户提供无线上网的便利。随着科技的发展和互联网的普及,Wi-Fi也在不断演进和
康普携手Wi-Fi联盟,RUCKUS Wi-Fi 7系列接入点成Wi-Fi
康普高级副总裁兼网络、智能蜂窝和安全解决方案总裁Bart Giordano对此表示: “我们非常荣幸能与Wi-Fi Alliance形成长期合作伙伴关系,将我们的RUCKUS Wi-Fi 7 接入点平台作为Wi-Fi CERT
BT Wi-Fi模式是否可以通过ModustoolBox对套件进行编程来实现?
想配置用于分析 CYW43439 的 CY8CPROTO-062S2-43439 原型开发套件。
浏览文档我无法弄清楚如何在不同的 Wi-Fi 和蓝牙模式(电源模式、连接模式等)下配置套件。
在
发表于 01-22 06:19
Wi-Fi 6和Wi-Fi 5之间有哪些区别呢?
Wi-Fi 6和Wi-Fi 5之间有哪些区别呢? Wi-Fi 6和Wi-Fi 5是无线局域网标准的两个版本,它们之间存在很多区别。Wi-Fi
评论