Real Time Clock programming

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
kerim

Real Time Clock programming

Post by kerim »

I made this small Pascal program that works with RTC on low level. It should display the date on the left upper corner, and on the screen it should write the current time. But, I get very silly numbers (i.e. 75 seconds.)
Why ?

-- Here's the source code of my program --

uses Crt;
var Sahat : Byte;

function ReadByte(Addr : Byte) : Byte;
var tmpB : Byte;
begin
asm
mov al, [Addr]
out 70h, al
in al, 71h
mov [tmpB], al
end;
ReadByte := tmpB;
end;

begin
ClrScr;
Write(ReadByte($07), '.', ReadByte($08), '.200', ReadByte($09));
repeat
Gotoxy(10, 10);
Write(ReadByte($04), ':', ReadByte($02), ':', ReadByte($00));
until keypressed;
end.
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:Real Time Clock programming

Post by Pype.Clicker »

did you check the internal format of CMOS time ? i wouldn't be surprised if it was bits-packed (like MS-DOS time structure) or binary-coded-decimal (so that n is encoded into n%10 + (n/10)*16)...
Schol-R-LEA

Re:Real Time Clock programming

Post by Schol-R-LEA »

According to the older edition of IPHB I have here, the RTC information accessed through the CMOS is in packed BCD (binary coded decimal) format by default. In order to get the value in this format, you need to read each nybble separately, as a single decimal digit (that is to say, each nybble holds a value in the range 0..9, with the high nybble being interpreted as value*10, giving a total range of 0..99 for the byte).

You can change this to a binary format by clearing the Data Mode flag (bit 2 in Status Register B, byte 0Bh of the CMOS). However, doing this will probably mangle most other time/date utilities, especially under Windows. Note also that when the Time Mode flag (Bit 0 of the same register) is cleared (that is, when using 12-hour time instead of 24-hour), the high bit of the hour byte is the AM/PM flag, and you need to mask it before reading the hour. You will want to check the DM and TM flags before reading the data to ensure that you have the correct format.

Users in most of the USA will also want to read the the DST flag (bit 0 of status register B) which indicates Daylight Savings Time when set.

BTW, there is also century field in byte 31h; using that instead of assuming the '200' part (which will break in 2010, of course) would be wise, though probably redundant in this case.

Finally, if you are writing an OS (which I assume is your eventual goal), you will want to test that the Valid flag (bit 7 of status register D) is set during start up, to make sure that the CMOS battery is still good. While this is probably redundant - the BIOS should have checked it during the POST - it may be worth it as a sanity check in case of some odd circumstances.
PIPO

Re:Real Time Clock programming

Post by PIPO »

Where can i get a list of ports ( most completly possible )???
Therx

Re:Real Time Clock programming

Post by Therx »

I have a doc on my website http://xinit.port5.com/. It's just a neater version of something I found somewhere. It's under motherboard chips and its the CMOS tutorial
Slasher

Re:Real Time Clock programming

Post by Slasher »

Schol-R-LEA is right. The values are in BCD.(if you do not change them to binary format through one of the CMOS status registers) You can print them by simply printing the values you read in HEXADECIMAL with no conversion needed!!!!
ie
seconds=CMOS_READ(SEC);
minutes=CMOS_READ(MIN);
hours=CMOS_READ(HOURS);
/*print time in hh:mm:ss format*/
printf("%x:%x:%x,hours,minutes,seconds);
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:Real Time Clock programming

Post by Pype.Clicker »

Code Slasher wrote: You can print them by simply printing the values you read in HEXADECIMAL with no conversion needed!!!!
lol :D that's a stuff i would never have thought about :)
Scalpel

Re:Real Time Clock programming

Post by Scalpel »

I also have a problem reading info from the CMOS.

I first use:

Code: Select all

outb(0x70, 0x10);
diskinfo = inb(0x71);
to get information about the floppy-drives. This works as intended.

Later I use:

Code: Select all

outb(0x70, 0x00);
seconds = inb(0x71);
to get info from the clock. But I only get the same response as from the floppy-drives. Do I need to reset something in between?
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:Real Time Clock programming

Post by Pype.Clicker »

not to my knowledge. Where do your 'outb' code come from ?
Scalpel

Re:Real Time Clock programming

Post by Scalpel »

It's the same as used in Linux v0.01. It works fine with the floppy-detection...
Post Reply