TA的每日心情 | 奋斗 2020-5-27 15:41 |
---|
签到天数: 868 天 连续签到: 1 天 [LV.10]以坛为家III
|
本帖最后由 wambob 于 2016-12-9 20:18 编辑
看了wu工的RTC测试,我也忍不住了,到官网看了下资料。
Fireduino RTC 使用外接设备为I2C 接口的HYM5863,挂接在Wire1 接口。
RTC使用了一个结构体- typedef struct
- {
- unsigned short year;//year (1900 ~ 2099)
- unsigned char mon;//month,begin from 1 to 12
- unsigned char week;//week (0:Sunday 1:Monday ... 6:Saturday)
- unsigned char day;//day,begin from 1 to 31
- unsigned char hour;//hour,24-hour
- unsigned char min;//minute
- unsigned char sec;//second
- }RTCTime;
复制代码 给了两个API用来设置和读取结构体值。getTime(RTCTime *time)和setTime(RTCTime *time)官网给出了一个串口打印的例子- #include "Arduino.h"
- #include "Wire.h"
- #include "RTC.h"
- RTCTime time;
- void setup(void)
- {
- Serial.begin(115200);
- Serial.print("\r\n FireDuino Setup ...\r\n");
- time.year = 2016;//year (1900 ~ 2099)
- time.mon = 3;//month,begin from 1 to 12
- time.week = 1 ;//week (0:Sunday 1:Monday ... 6:Saturday)
- time.day = 14;//day,begin from 1 to 31
- time.hour = 23;//hour,24-hour
- time.min = 59;//minute
- time.sec = 50;//second
- Wire1.begin();
- RTC.setTime(&time);
- }
- void loop(void)
- {
- delay(100);
- RTC.getTime(&time);
- Serial.println();
- Serial.print("Y:");
- Serial.print(time.year,DEC);
- Serial.print(" M:");
- Serial.print(time.mon,DEC);
- Serial.print(" W:");
- Serial.print(time.week,DEC);
- Serial.print(" d:");
- Serial.print(time.day,DEC);
- Serial.print(" h:");
- Serial.print(time.hour,DEC);
- Serial.print(" m:");
- Serial.print(time.min,DEC);
- Serial.print(" s:");
- Serial.print(time.sec,DEC);
- Serial.println();
- }
复制代码 打开从官网下载的IDE1.6.8版本,因为使用6.1.13版本配置的问题,读不出数,oled也不显示。
编译下载代码到板子上打开串口助手,可以看到读出了例程里的时间。
这个RTC例程在IDE目录下D:\arduino-1.6.8\portable\packages\Firefly\hardware\arm\1.3.0\libraries\RTC
红色框中是自己准备的oled和字库。第三方的库不支持,什么都要从来,挺费力啊。
其中的I2C协议文件在D:\arduino-1.6.8\portable\packages\Firefly\hardware\arm\1.3.0\libraries\Wire下,里面有数字加速度传感器ADXL345例程。
添加头文件和代码到例程里。编译的时候会提示只读文件,需要另存个路径,点OK吧。
我的主代码如下- /**
- * @file RTC_set_get.ino
- * @brief Definitions RTC_set_get Demo for Fireduino
- * @author jiang<jdz@t-chip.com.cn>
- * @version V1.0
- * @date 2016.02
- *
- * @par Copyright:
- * Copyright (c) 2016 T-CHIP INTELLIGENCE TECHNOLOGY CO.,LTD. \n\n
- *
- * For more information, please visit website <http://www.t-firefly.com/>, \n\n
- * or email to <service@t-firefly.com>.
- */
- #include "Arduino.h"
- #include "Wire.h"
- #include "RTC.h"
- #include "ssd1306.h"
- RTCTime time;
- char year1[100];
- char month1[100];
- char day1[100];
- char hour1[100];
- char minute1[100];
- char second1[100];
- char week1[100];
- void setup(void)
- {
- Serial.begin(115200);
- time.year = 2016;//year (1900 ~ 2099)
- time.mon = 12;//month,begin from 1 to 12
- time.week = 5 ;//week (0:Sunday 1:Monday ... 6:Saturday)
- time.day = 9;//day,begin from 1 to 31
- time.hour = 19;//hour,24-hour
- time.min = 35;//minute
- time.sec = 50;//second
- Wire1.begin();
- RTC.setTime(&time);
- OLED_pin_Init();
- oled_init();
- }
- void loop(void)
- {
- OLED_Clear();
- //delay(1000);
- OLED_ShowString(10,0,"RTC test!",12);
- RTC.getTime(&time);
-
- sprintf(year1,"%d" ,time.year);
- sprintf(month1,"%d" ,time.mon);
- sprintf(day1,"%d" ,time.day);
- sprintf(hour1,"%d" ,time.hour);
- sprintf(minute1,"%d" ,time.min);
- sprintf(second1,"%d" ,time.sec);
- sprintf(week1,"%d" ,time.week);
-
- OLED_ShowString(10,16,year1,12);
- OLED_ShowChar(34,16,'-',12,1);
- OLED_ShowString(40,16,month1,12);
- OLED_ShowChar(52,16,'-',12,1);
- OLED_ShowString(58,16,day1,12);
- OLED_ShowString(10,32,hour1,12);
- OLED_ShowChar(22,32,':',12,1);
- OLED_ShowString(28,32,minute1,12);
- OLED_ShowChar(40,32,':',12,1);
- OLED_ShowString(46,32,second1,12);
-
- OLED_ShowString(10,48,"week:",12);
- OLED_ShowString(40,48,week1,12);
- OLED_Refresh_Gram();//
- delay(3000);
- }
复制代码 编译后下载到板子上,显示出读取的时间,+上延迟,刷新一屏要用5秒看来需要修改下显示API,部分刷新了。
|
评分
-
查看全部评分
|