【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】五、显示板的使用

描述

本文来源电子发烧友社区,作者:李元江, 帖子地址:https://bbs.elecfans.com/jishu_2021769_1_1.html

今天来写一篇关于使用显示板的帖子。主要完成温湿度传感器数据采集,把采集到的数据显示到OELD显示屏,显示时钟

一、硬件介绍
通过查看环境监测板的原理图,发现该板子主要有两个种资源:12864OLED显示屏,两个模拟按键。两个模拟按键分别按下时,因为电阻分压,在SWITCH端的电压都一样,根据测量这个SWITCH端电压值,可以读取到按键的状态。
HarmonyOS
其中OLED兼容两种通信接口SPI和I2C,这里我选择使用I2C。
HarmonyOS


它们使用主控的GPIO口分别为:AHT20温湿度传感器SDA        --》        GPIO13           可复用为I2C0_SDASCL        --》        GPIO14            可复用为I2C0_SCL模拟按键ADC       --》    GPIO05           可复用为ADC2
二、软件设计

1、新建文件
在wifi-iot/app目录下,新建OLED文件夹,存放与显示板和环境监测板相关的代码。所含文件如下所示。
HarmonyOS
其中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,然后再把时间戳转换为北京时间即可到的时间戳对应的北京时间。
  1. #include "timeconv.h"
  2. #include
  3. #include
  4. static uint8_t month_day[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //平年
  5. static uint8_t Leap_month_day[12]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年
  6. const uint16_t dayPerYear[4] = {365, 365, 365, 366};
  7.  
  8. // 判断是否是闰年
  9. // year: 需要判断的年
  10. // return:1:闰年
  11. //        0: 平年
  12. uint8_t isLeapYear(uint16_t year)
  13. {
  14. uint8_t res=0;
  15.  
  16. if(year%4 == 0) // 能够被4整除
  17. {
  18.   if((year%100 == 0) && (year%400 != 0)) //能够被100整除,但是不能够被400整除
  19.   {
  20.    res = 0;
  21.   }
  22.   else
  23.   {
  24.    res =1;
  25.   }
  26. }
  27. return res;
  28. }
  29.  
  30. // 将Unix时间戳转换为北京时间
  31. // unixTime: 需要判断的Unix时间戳
  32. // *tempBeijing:返回的北京时间
  33. // return:none
  34. // note:没对输入参数正确性做判断
  35. void covUnixTimeStp2Beijing(uint32_t unixTime, rtc_time_t *tempBeijing)
  36. {
  37.     uint32_t totleDaynum=0, totleSecNum=0;
  38.     uint16_t remainDayofYear;
  39.     uint8_t *pr, tempYear=0;
  40.     totleDaynum = (uint32_t)unixTime/(((uint32_t)24*60*60)); //总天数(注意加括号)
  41.     totleSecNum = (uint32_t)unixTime%((uint32_t)24*60*60); //当天剩余的秒速
  42.     memset(tempBeijing, 0x00, sizeof(rtc_time_t));
  43.    // 1.计算哪一年
  44.         tempBeijing->ui8Year = 1970 + (totleDaynum/((uint32_t)FOURYEARDAY))*4;
  45.     remainDayofYear = totleDaynum%((uint32_t)FOURYEARDAY)+1;
  46.         //Delay_1ms(1000);
  47.     while(remainDayofYear > dayPerYear[tempYear])
  48.         {
  49.         remainDayofYear -= dayPerYear[tempYear];
  50.         tempBeijing->ui8Year++;
  51.         tempYear++;
  52.     }
  53.     //2.计算哪一月的哪一天
  54.     pr = isLeapYear(tempBeijing->ui8Year)?Leap_month_day:month_day;
  55.     while(remainDayofYear > *(pr+tempBeijing->ui8Month))
  56.     {
  57.                 remainDayofYear -= *(pr+tempBeijing->ui8Month);
  58.         tempBeijing->ui8Month++;
  59.     }
  60.     tempBeijing->ui8Month++; //month
  61.     tempBeijing->ui8DayOfMonth = remainDayofYear; //day
  62.     //3.计算当天时间
  63.     tempBeijing->ui8Hour = (totleSecNum)/3600;
  64.     tempBeijing->ui8Minute = (totleSecNum%3600)/60; //error:变量搞错
  65.     tempBeijing->ui8Second = (totleSecNum%3600)%60;
  66.         
  67.     //4.时区调整
  68.     tempBeijing->ui8Hour +=TIMEZONE;
  69.     if(tempBeijing->ui8Hour>23){
  70.         tempBeijing->ui8Hour -= 24;
  71.         tempBeijing->ui8DayOfMonth++;
  72.     }
  73. }
  74.  
  75. // 将北京时间转换为Unix时间戳
  76. // year: 需要判断的年
  77. // return:Unix时间戳(从1970/1/1 00:00:00 到现在的秒数)
  78. // note:没对输入参数正确性做判断
  79. uint32_t covBeijing2UnixTimeStp(rtc_time_t *beijingTime)
  80. {
  81. uint32_t daynum=0, SecNum=0; //保存北京时间到起始时间的天数
  82. uint16_t tempYear=1970, tempMonth=0;
  83.  
  84.  
  85. //1.年的天数
  86. while(tempYear < beijingTime->ui8Year)
  87. {
  88.   if(isLeapYear(tempYear)){
  89.    daynum += 366;
  90.   }
  91.   else{
  92.    daynum += 365;
  93.   }
  94.   tempYear++;
  95. }
  96. //2.月的天数
  97.   while(tempMonth < beijingTime->ui8Month-1)
  98.   {
  99.         if(isLeapYear(beijingTime->ui8Year)){ //闰年
  100.             daynum += Leap_month_day[tempMonth];
  101.         }
  102.         else{
  103.       daynum += month_day[tempMonth];
  104.         }
  105.   tempMonth++;
  106. }
  107.     //3.天数
  108. daynum += (beijingTime->ui8DayOfMonth-1);
  109.  
  110.     //4.时分秒
  111.     SecNum = daynum*24*60*60; //s   
  112.     SecNum += beijingTime->ui8Hour*60*60;   
  113.     SecNum += beijingTime->ui8Minute*60;   
  114.     SecNum += beijingTime->ui8Second;
  115.  
  116.     //5.时区调整
  117.     SecNum -= TIMEZONE*60*60;
  118.  
  119.     return SecNum;
  120. }
复制代码
3.timeconv.h
  1. #ifndef __TIMECONV_H
  2. #define __TIMECONV_H
  3. #include
  4. #include
  5. #include
  6. #include "ohos_init.h"
  7. #include "cmsis_os2.h"
  8. #define FOURYEARDAY (((uint32_t)365+365+365+366))  //4年一个周期内的总天数(1970~2038不存在2100这类年份,故暂不优化)
  9. #define TIMEZONE    (8)                //北京时区调整
  10. typedef struct rtc_time_struct
  11. {
  12.     uint16_t ui8Year;       // 1970~2038
  13.     uint8_t ui8Month;       // 1~12
  14.     uint8_t ui8DayOfMonth;  // 1~31
  15.     uint8_t ui8Week;
  16.     uint8_t ui8Hour;        // 0~23
  17.     uint8_t ui8Minute;      // 0~59
  18.     uint8_t ui8Second;      // 0~59
  19.    
  20. }rtc_time_t;
  21. uint8_t isLeapYear(uint16_t year);
  22. void covUnixTimeStp2Beijing(uint32_t unixTime, rtc_time_t *tempBeijing);
  23. uint32_t covBeijing2UnixTimeStp(rtc_time_t *beijingTime);
  24. #endif /*__TIMECONV_H*/
复制代码
4、oled_demo.c
该c文件主要是oled显示屏显示程序、包括时间转换显示、温湿度显示。
  1. extern float humidity;
  2. extern float temperature;
  3. static void OledTask(void *arg)
  4. {
  5.     rtc_time_t mData;
  6.     (void)arg;
  7.     GpioInit();
  8.     OledInit();
  9.     OledFillScreen(0x00);
  10.     while (1) {
  11.         static char text[128] = {0};
  12.         unsigned short data = 0;
  13.         AdcRead(analog_KEY_CHAN_NAME, &data, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0);
  14.         float voltage = ConvertToVoltage(data);
  15.         snprintf(text, sizeof(text), "voltage: %.3f!", voltage);
  16.         //OledShowString(0, 5, text, 1);
  17.         OLED_ShowCHinese(0,3,0);//温
  18.         OLED_ShowCHinese(16,3,2);//度
  19.         OledShowChar(32,3,':',2);
  20.         sprintf((char *)TimeStr,"%02d",
  21.         (uint32_t)temperature);
  22.         OledShowString(40,3,(char *)TimeStr,2);
  23.         OLED_ShowCHinese(64,3,1);//湿
  24.         OLED_ShowCHinese(80,3,2);//度
  25.         OledShowChar(96,3,':',2);
  26.         sprintf((char *)TimeStr,"%02d",
  27.         (uint32_t)humidity);
  28.         OledShowString(104,3,(char *)TimeStr,2);
  29.         
  30.         timedata++;
  31.         covUnixTimeStp2Beijing(timedata, &mData);
  32.         sprintf((char *)TimeStr,"%04d-%02d-%02d",
  33.         mData.ui8Year, mData.ui8Month, mData.ui8DayOfMonth);
  34.         OledShowString(24,0,(char *)TimeStr,1);
  35.         sprintf((char *)TimeStr,"%02d:%02d:%02d",
  36.         mData.ui8Hour,mData.ui8Minute,mData.ui8Second);
  37.         OledShowString(32,1,(char *)TimeStr,1);
  38.         sleep(1);
  39.     }
  40. }
复制代码
5、oled_ssd1306.c
原来例程上是不能中文的,现在把显示中文功能加上,但是需要提前使用取模软件把字取模。
  1. //显示汉字
  2. void OLED_ShowCHinese(uint8_t  x,uint8_t  y,uint8_t  no)
  3. {                  
  4.     uint8_t  t,adder=0;
  5.     OledSetPosition(x,y);   
  6.     for(t=0;t<16;t++)
  7.         {
  8.                  WriteData(Hzk[2*no][t]);
  9.                 adder+=1;
  10.      }  
  11.         OledSetPosition(x,y+1);
  12.     for(t=0;t<16;t++)
  13.             {   
  14.         WriteData(Hzk[2*no+1][t]);
  15.         adder+=1;
  16.       }                 
  17. }
复制代码
字模数组
  1. static const unsigned char  Hzk[][16]={
  2. {0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00},
  3. {0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00},/*"温",0*/
  4. /* (16 X 16 , 宋体 )*/
  5. {0x10,0x60,0x02,0x8C,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00},
  6. {0x04,0x04,0x7E,0x01,0x44,0x48,0x50,0x7F,0x40,0x40,0x7F,0x50,0x48,0x44,0x40,0x00},/*"湿",1*/
  7. /* (16 X 16 , 宋体 )*/
  8. {0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00},
  9. {0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00},/*"度",2*/
  10. };
复制代码
6、BUILD.gn
BUILD.gn内容为
  1. # Copyright (c) 2020, HiHope Community.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are met:
  5. #
  6. # 1. Redistributions of source code must retain the above copyright notice, this
  7. #    list of conditions and the following disclaimer.
  8. #
  9. # 2. Redistributions in binary form must reproduce the above copyright notice,
  10. #    this list of conditions and the following disclaimer in the documentation
  11. #    and/or other materials provided with the distribution.
  12. #
  13. # 3. Neither the name of the copyright holder nor the names of its
  14. #    contributors may be used to endorse or promote products derived from
  15. #    this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. static_library("oled_example") {
  28.     sources = [
  29.         "oled_demo.c",
  30.         "oled_ssd1306.c",
  31.         "timeconv.c",
  32.         "envrionment_demo.c",
  33.         "aht20.c"
  34.     ]
  35.     include_dirs = [
  36.         "//utils/native/lite/include",
  37.         "//kernel/liteos_m/components/cmsis/2.0",
  38.         "//base/iot_hardware/inteRFaces/kits/wifiiot_lite",
  39.     ]
  40. }
复制代码
这样还不行,还要修改app文件夹下的BUILD.gn。在features下增加"OLED:oled_example",
  1. lite_component("app") {
  2.     features = [
  3.         "startup",
  4.         "SSL:ssl_example",
  5.         "EM:em_example",
  6.         "OLED:oled_example"
  7.     ]
  8. }
复制代码
三、 编译烧录
演示结果
 
四、总结
显示的时间是没有进行对时的,下一篇讲写关于如何连接网络,并且如何获取网络时间。然后把显示屏上时间更新为网络时间。
HarmonyOS
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分