How can i read the System-Time

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Wacky

How can i read the System-Time

Post 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
slacker

Re:How can i read the System-Time

Post by slacker »

i used a numtoascii function to translate the number retrieved from the port. you also need portin/portout functions.
Wacky

Re:How can i read the System-Time

Post by Wacky »

I'm really sorry, but i have no idea... ;-(
i already have 'outportb' and 'inportb'.. could you post an example, please?!

...
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:How can i read the System-Time

Post 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 ...
gtsphere

Re:How can i read the System-Time

Post 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
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:How can i read the System-Time

Post 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;
}

-- Stu --
Post Reply