首页 > 代码库 > 如何获取CMOS内存的时间

如何获取CMOS内存的时间

参考Linux V1.0

定义CMOS_READ模块

cmos_read.h

--------------------------------------------------------------------------------------------------------------------------

#define CMOS_READ(addr) ({outb(addr|0x80,0x70);inb(0x71);})
#define outb(value,port)\
__asm__ ("outb %%al,%%dx"::"a"(value),"d"(port))

#define inb(port) ({\
        unsigned char _v;\
        __asm__ volatile("inb %%dx,%%al":"=a"(_v):"d"(port));\
        _v;\
        })

#define RTC_REG_A       10
#define RTC_REG_B  11
#define RTC_DM_BINARY 0x04
#define RTC_ALWAYS_BCD  1
#define RTC_FREQ_SELECT RTC_REG_A
#define RTC_UIP 0x80

#define RTC_SECONDS  0
#define RTC_MINUTES  2
#define RTC_HOURS    4
#define RTC_DAY_OF_WEEK  6
#define RTC_DAY_OF_MONTH 7
#define RTC_MONTH  8
#define RTC_YEAR  9
#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
#define RTC_CONTROL RTC_REG_B

#define MINUTE 60
#define HOUR (60*MINUTE)
#define DAY (24*HOUR)
#define YEAR (365*DAY)
     
struct mktime {
       int sec;
       int min;
       int hour;
       int day;
       int mon;
       int year;
       };

--------------------------------------------------------------------------------------------------------------------------

cmos_read.c

--------------------------------------------------------------------------------------------------------------------------

#include "stdio.h"
#include "stdlib.h"
#include "cmos_read.h"

void time_get(void)
{
     struct mktime time;
     int i;
     for(i = 0; i < 1000000 ; i++)
           if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
           break;
     for(i = 0; i < 1000000 ; i++)
           if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
           break;
     do{
           time.sec = CMOS_READ(RTC_SECONDS);
           time.min = CMOS_READ(RTC_MINUTES);
           time.hour = CMOS_READ(RTC_HOURS);
           time.day = CMOS_READ(RTC_DAY_OF_MONTH);
           time.mon = CMOS_READ(RTC_MONTH);
           time.year = CMOS_READ(RTC_YEAR);
     } while (time.sec != CMOS_READ(RTC_SECONDS));
     if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
   {
     BCD_TO_BIN(time.sec);
     BCD_TO_BIN(time.min);
     BCD_TO_BIN(time.hour);
     BCD_TO_BIN(time.day);
     BCD_TO_BIN(time.mon);
     BCD_TO_BIN(time.year);
   } 
}
      
main()
{

      printf("hello\n");
      time_get();
      system("pause");
}      

//End

 
2011-01-10

如何获取CMOS内存的时间