⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 time.c

📁 STM32 单片机例程
💻 C
📖 第 1 页 / 共 2 页
字号:
//    
//    /* RTC Configuration */
    RTC_Configuration(1);
    
    tm.tm_year=2008-1900;//年 从1900 年算起至今的年数,注意:DS1302,PCF8563的year范围都是(0~99),程序中读写year自动转换
    tm.tm_mon=1-1; //月 (0~11)注意:DS1302,PCF8563的mon范围都是(1~12),程序中读写mon自动转换(0~11)
    tm.tm_mday=1; //日 (1~31)
    tm.tm_hour=0;   //时 (0~23)
    tm.tm_min=0;   //分 (0~59)
    tm.tm_sec=0;   //秒 (0~59)
    
    /* Adjust time by values entred by the user on the hyperterminal */
    settime(mymktime(&tm));
    
    PWR_BackupAccessCmd(ENABLE);//// enable access to RTC, BDC registers
    BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
    PWR_BackupAccessCmd(DISABLE); //// disable access to RTC registers
    
    //cpyU8fU16(buf, BKP_ReadBackupRegister(BKP_DR1));
    //DebugSendBuf(buf, 4);
  }
//
   timebasic();//上电读一次时间
  
  NVIC->ISER[0] |= (1 << (RTC_IRQChannel & 0x1F));            // enable interrupt
}


/*

功能:返回系统UTC时间,此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数
说明:该时间不是本地时间,而是格林威治时间
*/
void time(uint32 *sec)
{
   *sec=gSec;
   
//   // Wait until last write operation on RTC registers has finished
//   RTC_WaitForLastTask();
//   
//   *sec=RTC_GetCounter();
//   
//   // Wait until last write operation on RTC registers has finished
//   RTC_WaitForLastTask();
}

//设置系统UTC时间
void settime(uint32 sec)
{
   gSec=sec;//更新全局时钟
   /* Allow access to BKP Domain */
   PWR_BackupAccessCmd(ENABLE);//// enable access to RTC, BDC registers
   
   /* Wait until last write operation on RTC registers has finished */
   RTC_WaitForLastTask();
   
   RTC_SetCounter(sec);
   
   /* Wait until last write operation on RTC registers has finished */
   RTC_WaitForLastTask();
   
   PWR_BackupAccessCmd(DISABLE); //// disable access to RTC registers
}

/*
//将经过的秒数转换成[时间结构数据]

返回结构tm代表本地时间
*/
struct tm * mylocaltime(const time_t * timep)
{time_t time;
   
   time=*timep+GTM_OFFSET_S;
   return( mygmtime(&time) );
}

/*
//将经过的秒数转换成[时间结构数据]

返回结构tm代表UTC 时间
*/
struct tm * mygmtime(const time_t *timep)
{uint32 day, sec;
 
 uint16 y;
 uint16 M,mi;//必须定义为16bit,否则数据溢出造成结果不正确
 int16 d;
 //const int gtm_s=8*60*60;//时区gtm+8
 //const long day1970=719468;//1970年1月1日00:00:00到公元前1年3月1日00:00:00的天数
 static struct tm tm_t;
 //uint16 yday;
   
    sec = *timep;//+GTM_OFFSET_S;
    
    day=sec/DAY_S;//(24*60*60);//DAY_S;//到1970年1月1日的天数
    sec -= day*DAY_S;//到当天00:00:00的秒数
    day+=DAY1970;//到公元前1年3月1日00:00:00的天数
    //y=((double)10000*day+147800)/3652425;
    y=( (float)day*10000+147800L ) / 3652425L;
    
    d=day - ( (uint32)y*365 + y/4 - y/100 + y/400 );
    if(d<0)
    {
      y--;
      d = day - (365*y + y/4 - y/100 + y/400);
    }
    mi = ( (uint16)d*100 + 52 )/3060;
    M = (mi + 2)%12 + 1;     
    d = d - ( mi*306 + 5 )/10 + 1;
    y = y + (mi + 2)/12;
    
    tm_t.tm_year=y-1900;
    tm_t.tm_mon=M-1;
    tm_t.tm_mday=d;
    tm_t.tm_hour=(sec%DAY_S) / HOUR_S;
    tm_t.tm_min=(sec%HOUR_S) / 60;
    tm_t.tm_sec=sec%60;
    
    /*
    //计算从今年1月1日算起至今的天数
    M=(M+9)%12;
    yday=(M*306 + 5)/10 + d - 1;
    if(yday>275)//1,2月
      yday=306-yday;
    else
      yday+=60-!(y % 4 == 0 && (y % 400 == 0 || y % 100 != 0) ) ;
    tm_t.yday=yday;//从今年1月1日算起至今的天数,范围为0-365
    */
    
    tm_t.tm_wday=(day-306+1)%7;//公元1年1月1日是星期1,公元1年1月1日到公元前1年3月1日是306天
    
    return(&tm_t);
}

/*
功能:将时间结构数据转换成经过的秒数
输入:tm时间结构
struct tm
{
  uint16 tm_year;//年 从1900 年算起至今的年数,注意:DS1302,PCF8563的year范围都是(0~99),程序中读写year自动转换
  uint8  tm_mon; //月 (0~11)注意:DS1302,PCF8563的mon范围都是(1~12),程序中读写mon自动转换(0~11)
  uint8  tm_mday; //日 (1~31)
  uint8  tm_hour;   //时 (0~23)
  uint8  tm_min;   //分 (0~59)
  uint8  tm_sec;   //秒 (0~59)
  uint8  tm_wday;//周 (0~6) 0=周日。注意:DS1302 的week范围是(1~7),程序中读写时间自动转换为(0~6),PCF8563的week范围是(0~6)
};
输出:从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数
说明:
1、公元前1年之后就是公元1年,没有公元0年
公元元年,也就是公元一年,公元一年一月一日是星期一
2、时间计算以公元前1年3月1日为基准
*/
time_t  mymktime(struct tm * p_tm)
{time_t seconds;
 //ulong sec;
 uint16 y;
 uint8 M,d,h,min,s;  //keil C51 bug  如果把min换成m的话,调试时M和m显示是一样的,但实际不同,结果也是正确的。说明只是显示的bug
 //const uint16 gtm_s=(8*60*60);//时区gtm+8
 //const uint16 yo=1970;
 //const uint32 day1970=719468; //1970年1月1日00:00:00到公元1年1月1日00:00:00的天数
   
    y=1900+p_tm->tm_year;    //从1900 年算起至今的年数
    M=1+p_tm->tm_mon;        //0~11
    d=p_tm->tm_mday;         //1~31
    h=p_tm->tm_hour;         //0~23
    min=p_tm->tm_min;        //0~59
    s=p_tm->tm_sec;          //0~59
    
    M=(M+9)%12;
    y=y-M/10;
    
    seconds=(uint32)y*365+y/4-y/100+y/400+(M*306 + 5)/10 + d - 1; //计算到公元1年1月1日00:00:00的天数
    seconds -= DAY1970; //计算到1970年1月1日的天数
    
    seconds=seconds*DAY_S+h*HOUR_S+min*60+s;  //-gtm_s;////计算到1970年1月1日00:00:00的秒数
    
    return ( seconds );
}

/**************************************************************************************
* 函数原型:GetSysTime
* 函数功能:取得系统时间的值
* 输入参数:
* 输出参数:
* 函数说明:
**************************************************************************************/
void GetSysTime(struct tm *rtime)
{uint32 sec;
 struct tm * tm;
  
  time(&sec);
  tm=mylocaltime(&sec);
  
  (*rtime).tm_year=(*tm).tm_year;
  (*rtime).tm_mon=(*tm).tm_mon;
  (*rtime).tm_mday=(*tm).tm_mday;
  (*rtime).tm_wday=(*tm).tm_wday;
  (*rtime).tm_hour=(*tm).tm_hour;
  (*rtime).tm_min=(*tm).tm_min;
  (*rtime).tm_sec=(*tm).tm_sec;
}


//将一个hex时间结构转换为Ascii字符串
void GetSysDateAscii(uint8 *buf)
{uint16 temp;
 //struct tm *gTm_t;  
   
   gTm_t=mylocaltime(&gSec);
   
   //sprintf(buf, "%s", "日期:");
   //buf+=5;
   
   sprintf((char*)buf, "%04u-", gTm_t->tm_year+1900);
   buf+=5;
   
   temp=gTm_t->tm_mon+1;
   sprintf((char*)buf, "%02u-", temp );
   buf+=3;
   
   temp=gTm_t->tm_mday;
   sprintf((char*)buf, "%02u ", temp );
}


void GetSysTimeAscii(uint8 *buf)
{uint16 temp;
   
   //gTm_t=mylocaltime(&gSec);
   
   //sprintf(buf, "%s", "时间:");
   //buf+=5;
   
   temp=gTm_t->tm_hour;
   sprintf((char*)buf, "%02u:",  temp);
   buf+=3;
   
   temp=gTm_t->tm_min;
   sprintf((char*)buf, "%02u:",  temp);
   buf+=3;
   
   temp=gTm_t->tm_sec;
   sprintf((char*)buf, "%02u ",  temp );
   buf+=3;
   
   temp=gTm_t->tm_wday;
   if(temp==0)
     temp=7;
   sprintf((char*)buf, "%1u ",   temp );
   
}

//取秒
void GetSysTimeSAscii(uint8 *buf)
{uint16 temp;
   
   gTm_t=mylocaltime(&gSec);  
   temp=gTm_t->tm_sec;
   sprintf((char*)buf, "%02u",  temp );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -