Timer Interrupt

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.
ich_will

Timer Interrupt

Post by ich_will »

Hi I set up a function for the timer interrupt (IRQ 0) and read every time a interrupt occurs the time with:

outportb(0x70, 0x04); // get the current hour
hour = inportb(0x71);

outportb(0x70, 0x02); // get the current minute
min = inportb(0x71);

outportb(0x70, 0x00); // get the current second
sec = inportb(0x71);

I know this is a slow solution but it's only for tests.

If we have for example 18 H the kernel outs 24 H, if we have 23 H it outs 35 H why?? The same thing with the minutes and seconds (not the same numbers i think) the seconds can be for example 62s.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Timer Interrupt

Post by Neo »

the BIOS clock counts in BCD not binary.
Only Human
DennisCGc

Re:Timer Interrupt

Post by DennisCGc »

Uhm, because the CMOS is BCD based.
DennisCGc

Re:Timer Interrupt

Post by DennisCGc »

Neo wrote: the BIOS clock counts in BCD not binary.
It is not the BIOS but the CMOS ;)
ich_will

Re:Timer Interrupt

Post by ich_will »

What does BCD mean and how can I convert it in real time? (Not set the bit for Binary Bit in CMOS Register B)
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Timer Interrupt

Post by Neo »

BCD representation is quite easy to convert to decimal. i not sure but i think this formula of mine is right.

Code: Select all

dec= bcd-((bcd>>4)*6);
IIRC BCD basically uses a nibble for each digit. i.e the decimal no.23 is represented as 0010_0011 binary etc. HTH
<edit>
BCD=Binary Coded Decimal.
and i think my explanation was for packed BCD. BCD uses a byte i think for each digit. (again check a number system book to be sure).
</edit>
Only Human
DennisCGc

Re:Timer Interrupt

Post by DennisCGc »

Neo wrote: BCD representation is quite easy to convert to decimal. i not sure but i think this formula of mine is right.

Code: Select all

dec= bcd-((bcd>>4)*6);
IIRC BCD basically uses a nibble for each digit. i.e the decimal no.23 is represented as 0010_0011 binary etc. HTH
I don't have my OS source on this computer, but I think it should work. Now I don't have to get the sources from the other computer :P
Therx

Re:Timer Interrupt

Post by Therx »

if you have a printf function support HEX then output it as that.

Pete
ASHLEY4

Re:Timer Interrupt

Post by ASHLEY4 »

This is the code i use to convert it:

Code: Select all

 ;----------------------------------------------------;
 ; bcd_to_ASCII         ;converts bcd number to ASCII ;
 ;----------------------------------------------------;
 ;                                                    ;
 ;   Input:                                           ;
 ;         Bcd number in al.                          ;
 ;  Output:                                            ;
 ;         ASCII number in val.                       ;
 ;                                             (100%) ;
 ;....................................................;

bcd_to_ASCII:
       pusha                  ;start of convert
       mov  ah,al            ;copy AL to AH
       and  ax,0f00fh      ;mask bits
       mov  cl,4              ;CL=04 for shift
       shr  ah,cl       ;shift right AH to get unpacked BCD
       or   ax, 3030h       ;combine with 30 to get ASCII
       xchg ah,al            ;swap for ASCII storage convention
       mov  [val],ax         ;store the ASCII value in VAL
       popa

       ret
ASHLEY4.
Dragon88

Re:Timer Interrupt

Post by Dragon88 »

<edit>
BCD=Binary Coded Decimal.
and i think my explanation was for packed BCD. BCD uses a byte i think for each digit. (again check a number system book to be sure).
</edit>
Depends on whether it is packed or not. Packed BCD stores 2 digits per byte, unpacked only stores 1 in the low nybble.
DennisCGc

Re:Timer Interrupt

Post by DennisCGc »

ASHLEY4 wrote: This is the code i use to convert it:

Code: Select all

 ;----------------------------------------------------;
 ; bcd_to_ASCII         ;converts bcd number to ASCII ;
 ;----------------------------------------------------;
 ;                                                    ;
 ;   Input:                                           ;
 ;         Bcd number in al.                          ;
 ;  Output:                                            ;
 ;         ASCII number in val.                       ;
 ;                                             (100%) ;
 ;....................................................;

bcd_to_ASCII:
       pusha                  ;start of convert
       mov  ah,al            ;copy AL to AH
       and  ax,0f00fh      ;mask bits
       mov  cl,4              ;CL=04 for shift
       shr  ah,cl       ;shift right AH to get unpacked BCD
       or   ax, 3030h       ;combine with 30 to get ASCII
       xchg ah,al            ;swap for ASCII storage convention
       mov  [val],ax         ;store the ASCII value in VAL
       popa

       ret
ASHLEY4.
I have another code with DAA and AAD, but I don't have it now :-[
I'll send it on this forum, when I have a floppy drive in my new computer ;)
ich_will

Re:Timer Interrupt

Post by ich_will »

DennisCGcs dec= bcd-((bcd>>4)*6); work fine.
But what shoud I do if an timer interrupt occurs? Should I increment a counter? but than i have to convert bcd every time i would get the time.
DennisCGc

Re:Timer Interrupt

Post by DennisCGc »

ich_will wrote: DennisCGcs dec= bcd-((bcd>>4)*6); work fine.
Btw, it was from neo ;)
But what shoud I do if an timer interrupt occurs? Should I increment a counter? but than i have to convert bcd every time i would get the time.
No, in my OS, I call a time_init.
This reads the time from the CMOS, and the time, which has been read, is converted to decimals.
And this initializes the PIT, it will be set that every 1/100 sec a timer interrupt will be called and when the interrupt occured 100 times, a second will be added, and so the clock is updated.
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:Timer Interrupt

Post by Pype.Clicker »

iirc, the time in the CMOS is automagically incremented by some external circuit (the realtime clock)
DennisCGc

Re:Timer Interrupt

Post by DennisCGc »

Pype.Clicker wrote: iirc, the time in the CMOS is automagically incremented by some external circuit (the realtime clock)
But then he have to convert it each time.
Well, it's up to him to choose a method, although my method may be faster, it may be more difficult than Pype's method.
Post Reply