nrf51822 ---时间换成时间戳



单片机中时期如何转换成时间戳
 Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从 格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至 现在的总秒数。Unix时间戳不仅被使用在Unix系统、类Unix系统中(比如 Linux系统),也在许多其他操作系统中被广泛采用。
 转换程序如下:

//**************************************************************************
/********************************************************************************
 * @file     calender.h
 * @author   Depth
    * @version  V1.0.0
 * @brief    head file for calender.c
 ******************************************************************************/
#ifndef CALENER_H
#define CALENER_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>
 
/*********************************************************************
 * MACROS
 */
 
#define IsLeapYear(yr) (!((yr) % 400) || (((yr) % 100) && !((yr) % 4)))
/*********************************************************************
 * TYPEDEFS
 */
 // To be used with
typedef struct
{
  uint8_t seconds;  // 0-59
  uint8_t minutes;  // 0-59
  uint8_t hour;     // 0-23
  uint8_t day;      // 0-30
  uint8_t month;    // 0-11
  uint16_t year;    // 2000+
} UTCTimeStruct;

// number of seconds since 0 hrs, 0 minutes, 0 seconds, on the
// 1st of January 2000 UTC
typedef uint32_t UTCTime;
extern UTCTime TimeSeconds;
extern UTCTimeStruct Time_iOS;
 
/*********************************************************************
 * FUNCTIONS
 */
  /*
   * Updates the OSAL clock and Timers from the MAC 320us timer tick.
   */
  extern void TimeUpdate( void );
  /*
   * Set the new time.  This will only set the seconds portion
   * of time and doesn't change the factional second counter.
   *     newTime - number of seconds since 0 hrs, 0 minutes,
   *               0 seconds, on the 1st of January 2000 UTC
   */
  extern void Set_Clock( UTCTime newTime );
  /*
   * Gets the current time.  This will only return the seconds
   * portion of time and doesn't include the factional second counter.
   *     returns: number of seconds since 0 hrs, 0 minutes,
   *              0 seconds, on the 1st of January 2000 UTC
   */
  extern UTCTime Get_Clock( void );
  /*
   * Converts UTCTime to UTCTimeStruct
   *
   * secTime - number of seconds since 0 hrs, 0 minutes,
   *          0 seconds, on the 1st of January 2000 UTC
   * tm - pointer to breakdown struct
   */
  extern void ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime );
  /*
   * Converts UTCTimeStruct to UTCTime (seconds since 00:00:00 01/01/2000)
   *
   * tm - pointer to UTC time struct
   */
  extern UTCTime ConvertUTCSecs( UTCTimeStruct *tm );
  /*
   * Update/Adjust the osal clock and timers
   * Msec - elapsed time in milli seconds 
   */
  extern void AdjustTimer( uint32_t Msec );
/*********************************************************************
*********************************************************************/
#ifdef __cplusplus
}
#endif
#endif /* OSAL_CLOCK_H */

/********************************************************************************
 * @file     calender.c
 * @author   Depth
    * @version  V1.0.0
    * @date     12-May-2014  
 * @brief    calender module based on app timer
******************************************************************************/
#include "calender.h"

/*********************************************************************
 * MACROS
 */
#define YearLength(yr) (IsLeapYear(yr) ? 366 : 365)
//ÈÕÀú±äÁ¿
UTCTimeStruct Time_iOS = {14,13,12,11,1,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
 */
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
  {
    uint16_t 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 );
}

/***************************************************************************
***************************************************************************/ 
 void   main()
{

  UTCTimeStruct   Time = {14,30,16,18,8,2015};
  Secondflag  = ConvertUTCSecs(&Time) - 8*3600;  //可以得到多少s,但是因为时间戳是从8:0:0开始计数的 所以应该减去8*3600s
  ConvertUTCTime(&Time,1439885936+8*3600);  //为什么要加8*3600 因为是从1970:1:1:8:0:0开始计数的
} 



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: nRF52840-DK开发板是一款由Nordic Semiconductor推出的功能强大的开发板,适用于无线应用的开发和原型设计。该开发板基于nRF52840芯片,具有蓝牙5、蓝牙Mesh、Thread、Zigbee等多种无线通信协议的支持,是开发智能穿戴设备、物联网设备以及传感器网络等应用的理想选择。 nRF52840-DK开发板具有丰富的硬件接口,包括USB、UART、SPI、I2C、GPIO等,使得开发者可以方便地连接、控制各种外部设备。同时,开发板上还集成了按钮、LED指示灯、天线等常用的外设,便于用户进行基础的应用开发和调试。 nRF52840-DK开发板不仅提供了开发所需的硬件资源,还配备了丰富的软件开发工具和库函数。开发者可以使用Nordic Semiconductor提供的nRF5 SDK进行应用开发,还可以通过Keil、IAR和GCC等常用的集成开发环境进行编程。 除此之外,nRF52840-DK开发板还支持在线固件更新(OTA),提供了方便快捷的开发和调试环境。开发者可以使用Segger J-link等调试工具进行调试和烧录。 总之,nRF52840-DK开发板是一款强大而灵活的开发工具,适用于广泛的无线应用开发。无论是初学者还是有经验的开发者,都能够利用该开发板快速构建各种创新的无线应用,并实现功能丰富、稳定的产品。 ### 回答2: nrf52840-dk是一款常用的开发板,广泛用于物联网(IoT)和蓝牙应用的开发。以下是关于该开发板的一些重要信息: 1. 芯片:nrf52840是Nordic Semiconductor公司推出的一款高性能低功耗蓝牙LE SoC(System-on-a-Chip)芯片。这款芯片集成了一个Arm Cortex-M4F处理器和2.4GHz无线收发器,可支持蓝牙5.0和其他无线通信协议。 2. 开发板:nrf52840-dk是基于nrf52840芯片的开发板,具有丰富的外围设备和接口,方便开发者进行软件编程和硬件原型设计。该开发板包含了带有按钮、指示灯和天线的nRF52840 SoC模块,以及一系列GPIO引脚、USB接口、电源管理电路等。 3. 软件开发工具:使用nrf52840-dk进行开发时,可以使用Nordic Semiconductor提供的nRF5软件开发工具套件。这个工具套件包含了一系列用于编写和调试nRF52840软件的开发工具、库文件和示例代码。开发者可以使用该工具套件进行代码编辑、编译、下载和调试等操作。 4. 功能和应用:nrf52840-dk开发板支持蓝牙LE和其他无线通信协议,可以广泛应用于物联网设备、可穿戴设备、智能家居、健康监测、无线传感器网络等领域的开发。开发者可以利用该开发板进行传感器数据采集、通信协议开发和物联网应用的原型验证。 总之,nrf52840-dk开发板是一款功能强大、易于使用的开发工具,适用于物联网和蓝牙应用的开发。它提供了丰富的外围设备和接口,配合Nordic Semiconductor的软件开发工具套件,开发者可以快速进行硬件原型设计和软件编程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值