Page 1 of 1

How can i read the System-Time

Posted: Mon May 19, 2003 1:58 pm
by Wacky
Hello, everybody.
Has anybody of you an idea how to read out the System-Time in C ?
I had a look on http://osdev.neopages.net/docs/cmos.php, but i have no idea how to write this in C :(

Do some of you know some samples, or a good tutorial? Please help.. and Thank you

cheers WaCkY

Re:How can i read the System-Time

Posted: Mon May 19, 2003 2:24 pm
by slacker
i used a numtoascii function to translate the number retrieved from the port. you also need portin/portout functions.

Re:How can i read the System-Time

Posted: Mon May 19, 2003 3:08 pm
by Wacky
I'm really sorry, but i have no idea... ;-(
i already have 'outportb' and 'inportb'.. could you post an example, please?!

...

Re:How can i read the System-Time

Posted: Mon May 19, 2003 3:27 pm
by Pype.Clicker
then it should be pretty straightforward!

Code: Select all

{
   outb(0x70,0);
   seconds=inb(0x71);
   outb(0x70,2);
   minutes = inb(0x71);
...
   char dayofweek[8]={"?","Sun","Mon", ... , "Sat"};
   
   printf("%s %2x/%2x/%2x %2x:%2x %2x",dayofweek[day],
             day,month,year,hour,minute,seconds);
}
of course, this expects you have a printf (or kprint or whatever) that can handle hexa display. As BCD is storing each digit on a separate nybble (i.e. bcd = hi-digit*16 + lo-digit), displaying it as an HEX will show the correct value.

Note that the above code only "prints" the current system time ... if you want other operations like comparing it, or keeping it up to date, you should do it yourself ...

Re:How can i read the System-Time

Posted: Mon May 19, 2003 8:11 pm
by gtsphere
http://www.clipx.net/ng/bios/index.php


that site was very helpful when i had to read the system date, so the time should be very similar. As for the BCD, i think as long as you are outputting them, there shouldn't be a reason to try to decrypt them, just make it the Ascii equivlant, by bit masking some value (i'm not on the computer where i have the exact value) So if someone knows more, please post!

Hope this helps
-GT

Re:How can i read the System-Time

Posted: Wed May 21, 2003 1:11 pm
by df
this is what I use...

Code: Select all

typedef struct osDATETIME
{
   UINT32   oDate;
   UINT32   oTime;
} osDATETIME;

#define BCD(a)   ((a/16)*10)+(a%16)

void RTC_GetDateTime(osDATETIME *oDT)
{
   UINT32 a, b;
   UINT8 c;
   
   out_byte(0x70, 0x9);
   c=in_byte(0x71);
   c=BCD(c);
   
   if(c<50)
   {
      a=(2000+c)*10000;
   }
   else
   {
      a=(1900+c)*10000;
   }   

   out_byte(0x70, 0x08);
   c=in_byte(0x71);
   c=BCD(c);
   a+=(c*100);
   
   out_byte(0x70, 0x07);
   c=in_byte(0x71);
   c=BCD(c);
   a+=c;
      
   // a==20030101

   out_byte(0x70, 0x04);
   c=in_byte(0x71);
   c=BCD(c);
   b=c*10000;
   
   out_byte(0x70, 0x02);
   c=in_byte(0x71);
   c=BCD(c);
   b+=c*100;
   
   out_byte(0x70, 0x0);
   c=in_byte(0x71);
   c=BCD(c);
   b+=c;
   
   oDT->oDate=a;
   oDT->oTime=b;
}