本文來源電子發燒友社區,作者:李元江, 帖子地址: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 = [
- "http://utils/native/lite/include",
- "http://kernel/liteos_m/components/cmsis/2.0",
- "http://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文章
2169瀏覽量
124824 -
HarmonyOS
+關注
關注
79文章
1982瀏覽量
30413 -
HiSpark
+關注
關注
1文章
156瀏覽量
6945
發布評論請先 登錄
相關推薦
Wi-Fi 8要來了!未來Wi-Fi技術演進方向揭秘
產品銷售中,雖然Wi-Fi 7產品的銷量份額快速從年初的個位數百分比增長至14%,但Wi-Fi 6產品的銷量份額依然穩定在60%左右。如果從存量設備來看,那么Wi-Fi 7路由器的占比將遠遠小于這個數。 ? 智能手機等終端設備已
華為海思正式進入Wi-Fi FEM賽道?
大家都知道了,2018年11 月離開銳迪科(RDA),前往福建晉江創立三伍微,專注于Wi-Fi射頻前端芯片,從路由器Wi-Fi FEM,到手機Wi-Fi FEM,再到IoT FEM全覆
發表于 12-11 17:42
摩爾斯微電子發布Wi-Fi HaLow評估套件,加速物聯網開發
近日,全球領先的Wi-Fi HaLow解決方案提供商摩爾斯微電子正式推出一款開創性的評估套件——MM6108-EKH05。這款完全整合的開發平臺,旨在推動各行業物聯網解決方案的快速發展
從Wi-Fi 4到Wi-Fi 7:網速飆升40倍的無線革命
1 Wi-Fi技術的快速發展 每一代新的Wi-Fi協議發布,都會帶來更高的無線速率。 ? 從Wi-Fi 4到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也在不斷演進和
評論