1.目的

nrf51822用定时器模拟一个万年历

2.分析

3.平台:

协议栈版本:SDK10.0.0

编译软件:keil 5.14

硬件平台:nrf51822最小系统

例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4

4.步骤上传图片

/********************************************************************************* @file     calender.c* @author   Depth* @version  V1.0.0* @brief    calender module based on app timer
******************************************************************************/
#include "calender.h"/********************************************************************** MACROS*/#define  YearLength(yr)  (IsLeapYear(yr) ? 366 : 365)
//日历变量UTCTimeStruct Time_iOS = {8,59,15,9,6,2015};
UTCTimeStruct Time_Current;/********************************************************************** CONSTANTS*/// (MAXCALCTICKS * 5) + (max remainder) must be <= (uint16_t max),
// so: (13105 * 5) + 7 <= 65535
#define MAXCALCTICKS  ((uint16_t)(13105))#define    BEGYEAR         1970     // UTC started at 00:00:00 January 1, 2000#define  DAY             86400UL  // 24 hours * 60 minutes * 60 seconds/********************************************************************** EXTERNAL FUNCTIONS*/
extern uint16_t ll_McuPrecisionCount(void);/********************************************************************** LOCAL VARIABLES*/
//static uint16_t previousLLTimerTick = 0;
//static uint16_t remUsTicks = 0;
//static uint16_t timeMSec = 0;// number of seconds since   2010.1.1.  00:00:00
UTCTime TimeSeconds = 0;/********************************************************************** LOCAL FUNCTION PROTOTYPES*/
static uint8_t monthLength( uint8_t lpyr, uint8_t mon );/********************************************************************** FUNCTIONS*//********************************************************************** @fn      Set_Clock** @brief   Set the new time.  This will only set the seconds portion*          of time and doesn't change the factional second counter.** @param   newTime - number of seconds since 0 hrs, 0 minutes,*                    0 seconds, on the 1st of January 2000 UTC** @return  none*/
void Set_Clock( UTCTime newTime )
{TimeSeconds = newTime;
}/********************************************************************** @fn      Get_Clock** @brief   Gets the current time.  This will only return the seconds*          portion of time and doesn't include the factional second*          counter.** @param   none** @return  number of seconds since 0 hrs, 0 minutes, 0 seconds,*          on the 1st of January 2000 UTC*/
UTCTime Get_Clock( void )
{return ( TimeSeconds );
}/********************************************************************** @fn      ConvertUTCTime** @brief   Converts UTCTime to UTCTimeStruct** @param   输出 tm - pointer to breakdown struct** @param   输入 secTime - number of seconds since 0 hrs, 0 minutes,*          0 seconds, on the 1st of January 2000 UTC** @return  none*/
uint16_t numDays =0;
void ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime )
{// calculate the time less than a day - hours, minutes, seconds{uint32_t day = secTime % DAY;  // 求出最后一天的剩余秒数,(除去整数的天数之后的剩余秒数)tm->seconds = day % 60UL;      // 求出最后一分钟的剩余秒数 (最后一天的剩余秒数对60求余)tm->minutes = (day % 3600UL) / 60UL;  //求出 最后一小时剩余的整分钟数tm->hour = day / 3600UL;       //求最后一天的整小时数}// Fill in the calendar - day, month, year{numDays = secTime / DAY;tm->year = BEGYEAR;while ( numDays >= YearLength( tm->year ) ){numDays -= YearLength( tm->year );tm->year++;}tm->month = 0;while ( numDays > monthLength( IsLeapYear( tm->year ), tm->month ) ){numDays -= monthLength( IsLeapYear( tm->year ), tm->month );tm->month++;}tm->month++;tm->day = numDays;tm->day++;}
}/********************************************************************** @fn      monthLength** @param   lpyr - 1 for leap year, 0 if not** @param   mon - 0 - 11 (jan - dec)** @return  number of days in specified month*/
static uint8_t monthLength( uint8_t lpyr, uint8_t mon )
{uint8_t days = 31;if ( mon == 1 ) // feb{days = ( 28 + lpyr );}else{if ( mon > 6 ) // aug-dec{mon--;}if ( mon & 1 ){days = 30;}}return ( days );
}/********************************************************************** @fn      ConvertUTCSecs** @brief   Converts a UTCTimeStruct to UTCTime** @param   tm - pointer to provided struct** @return  number of seconds since 00:00:00 on 01/01/2000 (UTC)*/
UTCTime ConvertUTCSecs( UTCTimeStruct *tm )
{uint32_t seconds;/* Seconds for the partial day */seconds = (((tm->hour * 60UL) + tm->minutes) * 60UL) + tm->seconds;/* Account for previous complete days */{/* Start with complete days in current month */uint16_t days = tm->day;/* Next, complete months in current year */{int8_t month = tm->month - 1;//while ( --month >= 0 ){days += monthLength( IsLeapYear( tm->year ), month );}}/* Next, complete years before current year */{uint16_t year = tm->year;while ( --year >= BEGYEAR ){days += YearLength( year );}}/* Add total seconds before partial day */seconds += (days * DAY);}return ( seconds );
}
/********************************************************************** @fn      CaculateWeekday** @brief   Converts a UTCTimeStruct to UTCTime** @param   tm - pointer to provided struct** @return  number of seconds since 00:00:00 on 01/01/2000 (UTC)*/
uint8_t CaculateWeekday(UTCTimeStruct *ptime)
{ // ¸ù¾ÝÈÕÆÚÊý¼ÆËãÐÇÆÚÊýuint16_t year   = ptime->year ;uint8_t  mounth = ptime->month;uint8_t  day    = ptime->day + 1;uint8_t  week;if(mounth==1 || mounth==2) { mounth += 12;  year--; }  week = (day + 2*mounth + 3*(mounth+1)/5 + year + year/4 - year/100 + year/400)%7; if(week == 0)  week = 7;return (uint8_t)week;
}
#include "nrf51.h"
#include "nrf_gpio.h"
#include "rtc.h"
#include "app_scheduler.h"
#include "ble_gatts.h"
#include "app_error.h"
#include "nrf_gpio.h"
//#include "power_manger.h"
#include <stdbool.h>
#include "calender.h"
#include "app_timer.h"
#include <stdint.h>
#include <string.h>
#include "nordic_common.h"
#include "nrf.h"
#include "nrf_gpio.h"
#include "nrf51_bitfields.h"
#include "app_error.h"
#include "calender.h"/**********************************************************
注册一个1s的时间用于RTC
********************************************************/
#define APP_TIMER_PRESCALER 0
//wall clock id
app_timer_id_t                              SecondID;        //广播的时候闪烁蓝灯
#define ONESECOND_INTERVAL                   APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER) // 广播时间设置
static void SECONED_ADD_clock(void * p_context)  //采集皮肤数据灯闪烁指示
{(void)p_context;UTCTimeStruct utc;TimeSeconds++ ;//一天有86400s   一个小时 3600sConvertUTCTime( &utc, Get_Clock() );  // ??????if(utc.minutes == 0 && utc.seconds == 0 && utc.year !=0){ //每个小时if(utc.hour == 0){ //每一天if(utc.day == 0){//每一个月}}}          }
/**************************************************************************
* system clock init
***************************************************************************/
void SECOND_clock_init(void)
{uint32_t err_code;UTCTimeStruct utc;  utc.seconds = 0x00;utc.minutes = 0x00;utc.hour    = 0x00;utc.day     = 0x00; //CC2540 ???? 0 -30utc.month   = 0x00; //CC2540 ???? 0 -11  utc.year    = 2000;Set_Clock( ConvertUTCSecs( &utc ) );err_code = app_timer_create(&SecondID, APP_TIMER_MODE_REPEATED, SECONED_ADD_clock); //广播事件APP_ERROR_CHECK(err_code);err_code = app_timer_start(SecondID, ONESECOND_INTERVAL, NULL);APP_ERROR_CHECK(err_code);}

SECOND_clock_init();//初始化后,系统每1s,TimeSeconds加一

ConvertUTCTime( &utc, Get_Clock() );  // 获取当前时间

CaculateWeekday(); //可以获得当前的星期几

nrf51822 --- 软件模拟 万年历相关推荐

  1. 异常记录(CPU产生的异常和软件模拟产生的异常)

    文章目录 前奏 异常的分类 CPU产生的异常 软件模拟产生的异常 异常产生 CPU异常的产生 CommonDispatchException函数分析 总结: 软件模拟异常 填充ExceptionRec ...

  2. CPU和软件模拟异常的执行流程

    文章目录 CPU异常记录 异常的分类 CPU产生的异常 软件模拟产生的异常 CPU异常的处理流程 CommonDispatchException函数分析 总结 模拟异常记录 模拟异常的执行流程 Rai ...

  3. STM32 软件模拟 IIC 代码,标准库、HAL库可用

    1 #ifndef _IIC_H 2 #define _IIC_H 3 4 #include "stdio.h" 5 #include "stm32f1xx_hal.h& ...

  4. IIC软件模拟-读写EEPROM

    这里写目录标题 1.IIC简介 2. I2C 基本读写过程 2.1.主机写数据到从机 2.2.主机由从机中读数据 2.3.读和写数据 2.4.地址及数据方向 2.5.响应信号 3.软件模拟I2C 4. ...

  5. 单片机软件模拟SPI接口—加深理解SPI总线协议

    单片机软件模拟SPI接口-加深理解SPI总线协议   SPI(Serial Peripheral Interfacer 串行外设接口)是摩托罗拉公司推出的一种同步串行通讯接口,用于微处理器臌控制器和外 ...

  6. 软件模拟PWM——呼吸灯小程序的理解

    呼吸灯就是类似人的呼吸一样,一呼一吸,灯的变化是从亮到灭再到亮的过程,是一个循序渐进的过程,而不是一个跳变. 通过软件模拟PWM,可以达到呼吸灯的效果. PWM即脉冲宽度调制,简单地说,就是一段时间为 ...

  7. aspen变压吸附塔_ASPEN软件模拟在分离中的应用

    ASPEN软件模拟在分离中的应用 工艺092 刘峰030091054 当前化学研究已达到分子设计的水平,化工生产和管理也多采用计算机控制.计算机进入化学化工领域后,在帮助深入研究化学基础理论呵促进化工 ...

  8. 软件模拟中美gdp今后几年的变化情况

    软件模拟中美gdp 以2018年的实际增速为基准,推演今后几年的中美gpd总量对比. 2018年中美GDP总量: 美国 205130.00万亿美元 中国 134572.67万亿美元 2018年GDP增 ...

  9. STM32 Cube MX 之hal库软件模拟IIC 可直接移植使用

    此为软件模拟IIC,可以直接移植到HAL库使用..h文件需要自己做函数声明这里就不再放出,如有问题大家可以讨论. 使用的时候只需要更改SDA 和SCL引脚的宏定义就可以移植使用,当然IIC协议其实就是 ...

最新文章

  1. linux arm gcc 内联汇编参考手册
  2. 纲:散户炒股存两大弱势 我自己不炒也不建议小散炒
  3. centos7.3 docker安装grafana
  4. php html区别_php与html区别
  5. windows控制linux桌面图标,完全控制你的Windows桌面 (转)
  6. nacos 本地测试_本地调试和服务器调试都无法连通-问答-阿里云开发者社区-阿里云...
  7. boost/container/small_vector.hpp: No such file or directory on Ubuntu 14.04
  8. 多益网络 2016 春季实习校招笔试回顾(C++游戏后台)
  9. 关于C++标准库中的数据抽象
  10. Message no. C6015--No valuation variant found for valuation area xxxx
  11. c语言for语句用法和例子
  12. Windows xp sp3 补丁下载-cuyahoga
  13. react自定义鼠标右键菜单
  14. 笔记本取消fn +功能键
  15. 英语学习的几个实用网站
  16. 少年,请多一些开疆拓土的勇气——写给在C和C++间犹豫的学生
  17. vue项目对接pad端——混合开发总结
  18. 谷歌大脑提出VeLO优化器,无需调参,最高比Adam快16倍!
  19. 队列的应用——短信模拟
  20. 多媒体实验 Visual Studio 图像显示与处理 对图像进行二值化、求边缘、增强等处理

热门文章

  1. 单片机c语言右移指令,单片机C语言左移和右移漫谈
  2. 网络知识host(Host的含义和作用)
  3. 信息学奥赛一本通 1239:统计数字 | 1847:【07NOIP提高组】统计数字 | OpenJudge NOI 2.4 7909 | 洛谷 P1097 [NOIP2007 提高组] 统计数字
  4. 从幻想走向科学:人类操纵大脑的条条大路
  5. 锐龙r5 4600h性能怎么样
  6. 实景三维浪潮翻涌,新技术“席卷”石家庄!
  7. 基于eclipse的登录注册页面
  8. Java50道经典习题-程序23 求岁数
  9. 抖音快手最新数据采集全套接口
  10. 《C#妹妹和Objective-C阿姨对话录》(04)垃圾回收基础--拆迁队那点事