1. 程式人生 > >x86平臺讀取CMOS中RTC時間例程

x86平臺讀取CMOS中RTC時間例程

#include "vxWorks.h"  
#include "time.h"  
#include "stdio.h"  
#include "memLib.h" 
#include "sysLib.h" 

STATUS timeTest (void) 
{ 
    int i; 
    time_t myTime1; 
    struct tm *myTime2; 
    unsigned int month_array[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    unsigned char cmd[8]={0x0,0x2,0x4,0x6,0x7,0x8,0x9,0x32};
    unsigned int currTimeBcd[8];
    unsigned int currTime[8];
     unsigned int yday2; 
    /* displays VxWorks uninitialized time = THU JAN 01 00:00:01 1970*/ 
    myTime1 = time (0); 
    myTime2 = localtime (&myTime1); 

    /* reads PC real time clock maintained in CMOS RAM */ 
    /* RTC CMOS RAM contents are accessed by writes and reads from */ 
    /* I/O addresses 0x70 and 0x71 respectively */ 
    /*    second = ((current_second_bcd & 0xF0) >> 4) * 10; 
    second = second + (current_second_bcd & 0x0F); 
    */
    for(i=0;i<8;i++)
    {
    	sysOutByte (0x70, cmd[i]); 
    	currTimeBcd[i]=sysInByte (0x71); 
    	currTime[i]=((currTimeBcd[i]&0xF0)>>4)*10;
    	currTime[i]=currTime[i]+(currTimeBcd[i]&0x0F);
    }
    
    printf ("\nsecond = %02X \nminute = %02X \nhour = %02X \nday = %02X \ndate = %02X ", 
    		currTimeBcd[0], currTimeBcd[1], currTimeBcd[2], currTimeBcd[3], 
			currTimeBcd[4]); 
    printf ("\nmonth = %02X \nyear = %02X \ncentury = %02X", currTimeBcd[5], 
    		currTimeBcd[6], currTimeBcd[7]); 
    
    currTime[7]=currTime[7]*100;/*century*/
    
    currTime[6]=currTime[7]+currTime[6]-1900; /*year*/
    
    
    for (i = 0; i < currTime[5]; i++)   /*month*/
        yday2 = yday2 + month_array[i]; 
    yday2 = yday2 + currTime[4];       /*date*/
    


    printf ("second = %d minute = %d hour = %d ", 
    		currTime[0], currTime[1], currTime[2]); 
    printf ("\ndate = %d ,month = %d, year = %d, day = %d, yday2 = %d\n", currTime[4], currTime[5], currTime[6], currTime[3], yday2); 

    

    myTime2->tm_sec = currTime[0];  
    myTime2->tm_min = currTime[1]; 
    myTime2->tm_hour = currTime[2]; 
    myTime2->tm_mday = currTime[4]; 
    myTime2->tm_mon = currTime[5]-1; /* months are 0-11 */ 
    myTime2->tm_year = currTime[6]; 
    myTime2->tm_wday = currTime[3]; 
    myTime2->tm_yday = yday2; 
    myTime2->tm_isdst = 1;       /* daylight savings time in effect (does nothing)*/ 

    /* convert tm structure to seconds */ 
    /*  
       myTime1 = mktime (myTime2); 

      

       mytimespec.tv_sec = myTime1; 
       mytimespec.tv_nsec = 0; 
       clock_settime(CLOCK_REALTIME, &mytimespec); 

     

       myTime1 = time(0); 
       myTime2 = localtime(&myTime1); 
       */

    printf ("\ncurrent vxWorks Time = %s", asctime (myTime2)); 
   
    return (OK); 
} 

執行結果
在這裡插入圖片描述