这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » 怎么取出实时时钟中的当前时间?

共4条 1/1 1 跳转至

怎么取出实时时钟中的当前时间?

菜鸟
2004-08-16 04:46:06     打赏
如题。还有 struct timespec tp; clock_gettime(CLOCK_REALTIME,&tp);
printf("current time : %x s %x ns\n",tp.tv_sec,tp.tv_nsec); 结果怎么会是“current time :0s,0ns”啊?



关键词: 怎么     取出     实时     时钟     中的     当前     时间    

菜鸟
2004-08-16 22:01:00     打赏
2楼
非常感谢~Sophia~ 我找到的一段代码: /* includes */
#include "vxWorks.h"
#include "time.h"
#include "intLib.h"
#include "C:\Tornado2.2\target\config\pcPentium4\pc.h" /* macro to check that <v> is between <l> and <h> inclusive range */
#define CHECK_RANGE(v,l,h) (((v)>=(l))&&((v)<=(h))) /* Macros to convert 2-digit BCD into binary and vice versa */
#define BIN2BCD(b) (((b)%10) | (((b)/10)<<4))
#define BCD2BIN(b) (((b)&0xf) + ((b)>>4)*10) /* RTC register access, macros should be defined for a particular BSP */
#if CPU_FAMILY==I80X86 #define RTC_REG_SET(reg,val) \
(sysOutByte(RTC_INDEX,(reg)),sysOutByte(RTC_DATA,(val)))
#define RTC_REG_GET(reg) (sysOutByte(RTC_INDEX,(reg)),sysInByte(RTC_DATA)) #else
#ifndef RTC_JMP
#define RTC_JMP 1
#endif #ifndef RTC_BASE
#define RTC_BASE 0x800000
#endif #define RTC_REG(o) ((volatile u_char *)(RTC_BASE+(o)*RTC_JMP))
#define RTC_REG_SET(reg,val) (*RTC_REG(reg) = (val))
#define RTC_REG_GET(reg) (*RTC_REG(reg))
#endif /******************************************************************************
*
* sysRtcGet - get the current date abd time from a Real Time Clock chip
*
* The values are returned in a ANSI time structure.
* During initialization phase, the POSIX clock of the system is
* set according to the Real Time Clock time and date, thus
* it is recommended to use the POSIX functions and avoid calling
* this function from the application software,
* to acheive similar results but with greater portability.
*
* NOTE: Interrupts are locked during reading of the values from the chip.
*
* RETURNS: OK or ERROR
*
* SEE ALSO
* clockLib(), ansiTime
*
*/ STATUS sysRtcGet
(
struct tm *tm
)
{
FAST int ipl ;
FAST count = 50; ipl = intLock(); /* wait until registers update is done, then we got 244 us
* to read the regs without loosing sanity
*/
while((count--) && (RTC_REG_GET(0x0a) & 0x80)); tm->tm_hour = RTC_REG_GET( 0x04); /* get BCD regs as is */
tm->tm_min = RTC_REG_GET( 0x02); /* while ints are off, */
tm->tm_sec = RTC_REG_GET( 0x00); /* and decode later */
tm->tm_mon = RTC_REG_GET( 0x08);
tm->tm_mday = RTC_REG_GET( 0x07);
tm->tm_year = RTC_REG_GET( 0x09);
tm->tm_wday = RTC_REG_GET( 0x06); intUnlock(ipl);
/* corrections - all registers are BCD, we need them in binary */
tm->tm_hour = BCD2BIN( tm->tm_hour );
tm->tm_min = BCD2BIN( tm->tm_min );
tm->tm_sec = BCD2BIN( tm->tm_sec );
tm->tm_mon = BCD2BIN( tm->tm_mon );
tm->tm_mday = BCD2BIN( tm->tm_mday );
tm->tm_year = BCD2BIN( tm->tm_year );
tm->tm_wday = BCD2BIN( tm->tm_wday ); /* corrections - some fields range is defined differently */
tm->tm_mon -- ; /* chip does 1-12, POSIX needs 0-11 */
tm->tm_wday -- ; /* chip does 1-7, POSIX needs 0-6 */ /* corrections - handle year after y2k */
if( tm->tm_year < 80 )
tm->tm_year += 100 ; /* These fields are unknown, filled with 0 */
tm->tm_yday = 0; /* days since January 1 - [0, 365] */
tm->tm_isdst= 0; /* Daylight Saving Time flag */ return OK ;
} /******************************************************************************
*
* sysRtcSet - Set the time and date into the RTC chip
*
* NOTE
* Setting the time is done with interrupts locked, but it is expected
* to be called rarely.
*
* RETURNS: OK or ERROR if values are out of range.
*/ STATUS sysRtcSet
(
const struct tm *timedate
)
{
struct tm t1 = * timedate ;
FAST struct tm *tm = &t1 ; /* make a local copy of the argument */
FAST count = 50;
FAST int ipl ; /* Check value ranges */ if( !CHECK_RANGE( tm->tm_sec, 0, 59)) return ERROR ;
if( !CHECK_RANGE( tm->tm_min, 0, 59)) return ERROR ;
if( !CHECK_RANGE( tm->tm_hour, 0, 23)) return ERROR ;
if( !CHECK_RANGE( tm->tm_mday, 1, 31)) return ERROR ;
if( !CHECK_RANGE( tm->tm_mon , 0, 11)) return ERROR ; /* correction - for y2k */
if( tm->tm_year > 99 )
tm->tm_year -= 100 ; if( !CHECK_RANGE( tm->tm_year, 0, 99)) return ERROR ; /* corrections - convert to BSD and add offset where needed. */
tm->tm_hour = BIN2BCD( tm->tm_hour );
tm->tm_min = BIN2BCD( tm->tm_min );
tm->tm_sec = BIN2BCD( tm->tm_sec );
tm->tm_mon = BIN2BCD( tm->tm_mon+1 );
tm->tm_mday = BIN2BCD( tm->tm_mday );
tm->tm_year = BIN2BCD( tm->tm_year );
tm->tm_wday = BIN2BCD( tm->tm_wday+1 ); ipl = intLock(); /* wait until registers update is done, then we got 244 us
* to read the regs without loosing sanity
*/
while((count--) && (RTC_REG_GET(0x0a) & 0x80));

RTC_REG_SET( 0x04, tm->tm_hour );
RTC_REG_SET( 0x02, tm->tm_min );
RTC_REG_SET( 0x00, tm->tm_sec );
RTC_REG_SET( 0x08, tm->tm_mon );
RTC_REG_SET( 0x07, tm->tm_mday);
RTC_REG_SET( 0x09, tm->tm_year);
RTC_REG_SET( 0x06, tm->tm_wday); intUnlock(ipl); return OK ;
} /******************************************************************************
*
* sysRtcInit - initialize the RTC chip
*
* This function should called from sysHwInit2(). With this particular
* device, there is nothing we need to do here.
*
*/
STATUS sysRtcInit ( void)
{
/* turn the oscilator on, just in case */
RTC_REG_SET( 0x0a, 0x20 );
RTC_REG_SET( 0x0b, 0x02 ); /* set 24-hr & BCD modes */
return OK ;
} /******************************************************************************
*
* sysRtcShutdown - put the RTC chip in to sleep mode
*
* The sleep mode is designed to save on battery life during inactive
* storage of the equipment. During this time the date & time do not
* progress.
*
*/ void sysRtcShutdown(void)
{
RTC_REG_SET( 0x0a, 0x00 );
} /* End Of File */

菜鸟
2004-08-16 22:03:00     打赏
3楼
利用以上的代码 ,加上如下的代码即可搞定。 struct tm tm ;
sysRtcGet( &tm ) ;

tv.tv_sec = mktime( &tm );
tv.tv_nsec = 0;
clock_settime( CLOCK_REALTIME, &tv );

菜鸟
2006-07-02 21:06:00     打赏
4楼
楼主,好象就调用几个接口函数就可以 ,time.h里面有定义

共4条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]