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

2020-12-22 23:14:45  246 IoT OLED
分享
0
本帖最后由 self1 于 2020-12-31 21:42 编辑

今天来写一篇关于使用显示板的帖子。主要完成温湿度传感器数据采集,把采集到的数据显示到OELD显示屏,显示时钟
一、硬件介绍
通过查看环境监测板的原理图,发现该板子主要有两个种资源:12864OLED显示屏,两个模拟按键。两个模拟按键分别按下时,因为电阻分压,在SWITCH端的电压都一样,根据测量这个SWITCH端电压值,可以读取到按键的状态。
模拟按键.PNG
其中OLED兼容两种通信接口SPI和I2C,这里我选择使用I2C。
12864.PNG


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

1、新建文件
在wifi-iot/app目录下,新建OLED文件夹,存放与显示板和环境监测板相关的代码。所含文件如下所示。
目录.PNG
其中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 <stddef.h>
  3. #include <stdio.h>

  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. // year: 需要判断的年
  9. // return:1:闰年
  10. //        0: 平年
  11. uint8_t isLeapYear(uint16_t year)
  12. {
  13. uint8_t res=0;

  14. if(year%4 == 0) // 能够被4整除
  15. {
  16.   if((year%100 == 0) && (year%400 != 0)) //能够被100整除,但是不能够被400整除
  17.   {
  18.    res = 0;
  19.   }
  20.   else
  21.   {
  22.    res =1;
  23.   }
  24. }
  25. return res;
  26. }

  27. // 将Unix时间戳转换为北京时间
  28. // unixTime: 需要判断的Unix时间戳
  29. // *tempBeijing:返回的北京时间
  30. // return:none
  31. // note:没对输入参数正确性做判断
  32. void covUnixTimeStp2Beijing(uint32_t unixTime, rtc_time_t *tempBeijing)
  33. {
  34.     uint32_t totleDaynum=0, totleSecNum=0;
  35.     uint16_t remainDayofYear;
  36.     uint8_t *pr, tempYear=0;
  37.     totleDaynum = (uint32_t)unixTime/(((uint32_t)24*60*60)); //总天数(注意加括号)
  38.     totleSecNum = (uint32_t)unixTime%((uint32_t)24*60*60); //当天剩余的秒速
  39.     memset(tempBeijing, 0x00, sizeof(rtc_time_t));
  40.    // 1.计算哪一年
  41.         tempBeijing->ui8Year = 1970 + (totleDaynum/((uint32_t)FOURYEARDAY))*4;
  42.     remainDayofYear = totleDaynum%((uint32_t)FOURYEARDAY)+1;
  43.         //Delay_1ms(1000);
  44.     while(remainDayofYear > dayPerYear[tempYear])
  45.         {
  46.         remainDayofYear -= dayPerYear[tempYear];
  47.         tempBeijing->ui8Year++;
  48.         tempYear++;
  49.     }
  50.     //2.计算哪一月的哪一天
  51.     pr = isLeapYear(tempBeijing->ui8Year)?Leap_month_day:month_day;
  52.     while(remainDayofYear > *(pr+tempBeijing->ui8Month))
  53.     {
  54.                 remainDayofYear -= *(pr+tempBeijing->ui8Month);
  55.         tempBeijing->ui8Month++;
  56.     }
  57.     tempBeijing->ui8Month++; //month
  58.     tempBeijing->ui8DayOfMonth = remainDayofYear; //day
  59.     //3.计算当天时间
  60.     tempBeijing->ui8Hour = (totleSecNum)/3600;
  61.     tempBeijing->ui8Minute = (totleSecNum%3600)/60; //error:变量搞错
  62.     tempBeijing->ui8Second = (totleSecNum%3600)%60;
  63.         
  64.     //4.时区调整
  65.     tempBeijing->ui8Hour +=TIMEZONE;
  66.     if(tempBeijing->ui8Hour>23){
  67.         tempBeijing->ui8Hour -= 24;
  68.         tempBeijing->ui8DayOfMonth++;
  69.     }
  70. }

  71. // 将北京时间转换为Unix时间戳
  72. // year: 需要判断的年
  73. // return:Unix时间戳(从1970/1/1 00:00:00 到现在的秒数)
  74. // note:没对输入参数正确性做判断
  75. uint32_t covBeijing2UnixTimeStp(rtc_time_t *beijingTime)
  76. {
  77. uint32_t daynum=0, SecNum=0; //保存北京时间到起始时间的天数
  78. uint16_t tempYear=1970, tempMonth=0;


  79. //1.年的天数
  80. while(tempYear < beijingTime->ui8Year)
  81. {
  82.   if(isLeapYear(tempYear)){
  83.    daynum += 366;
  84.   }
  85.   else{
  86.    daynum += 365;
  87.   }
  88.   tempYear++;
  89. }
  90. //2.月的天数
  91.   while(tempMonth < beijingTime->ui8Month-1)
  92.   {
  93.         if(isLeapYear(beijingTime->ui8Year)){ //闰年
  94.             daynum += Leap_month_day[tempMonth];
  95.         }
  96.         else{
  97.       daynum += month_day[tempMonth];
  98.         }
  99.   tempMonth++;
  100. }
  101.     //3.天数
  102. daynum += (beijingTime->ui8DayOfMonth-1);

  103.     //4.时分秒
  104.     SecNum = daynum*24*60*60; //s   
  105.     SecNum += beijingTime->ui8Hour*60*60;   
  106.     SecNum += beijingTime->ui8Minute*60;   
  107.     SecNum += beijingTime->ui8Second;

  108.     //5.时区调整
  109.     SecNum -= TIMEZONE*60*60;

  110.     return SecNum;
  111. }
复制代码
3.timeconv.h
  1. #ifndef __TIMECONV_H
  2. #define __TIMECONV_H

  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdint.h>
  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. }
复制代码
三、 编译烧录
演示结果
捕获.PNG
四、总结
显示的时间是没有进行对时的,下一篇讲写关于如何连接网络,并且如何获取网络时间。然后把显示屏上时间更新为网络时间。

相关文章

评论

您需要登录后才可以回帖 登录 | 注册

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
发文章

最新文章