
012/023 03 Nov 91 15:41:24
From:   Robert Mashlan of 114/7.0
To:     Cornel Huth of 114/35.0
Subj:   ACCESSING THE RTC
Attr:   
------------------------------------------------
AREA:80XXX
 >  RM> There is a small problem with this code.  To set the time
 >   for a MC146818 chip, you need to set bit 7 in status
 >   register B during the update, otherwise your changes are
 >   ignored.
 >   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 >  You're wrong! Try it sometime.

No Cornel, you are wrong.  Give this code a try:

===========================================================

.MODEL small

.STACK 100h

.CODE

rtcaddr         equ     byte ptr 70h
rtcdata         equ     byte ptr 71h

minute          =       02h
statusB         =       0bh

setclock        =       80h

.DATA

msg1            db      'The clock was updated$'
msg2            db      'The clock was not updated$'

.CODE

iodelay         macro
                local @@1,@@2

        jmp     @@1
@@1:    jmp     @@2
@@2:
                endm

start:
        mov     ax,@data                ; set up DS
        mov     ds,ax

        cli                             ; disable interrupts
        mov     al,statusB
        out     rtcaddr,al
        iodelay
        in      al,rtcdata              ; get status B register
        push    ax                      ; save it
        and     al,NOT setclock         ; clear set clock bit
                                        ; so disable time updates
        out     rtcdata,al              ; tell RTC

        iodelay
        mov     al,minute
        out     rtcaddr,al
        in      al,rtcdata              ; get current minute
        push    ax                      ; save value
        add     al,1
        daa                             ; add 1 to minute  (BCD)
        iodelay
        out     rtcdata,al              ; and store it

        iodelay
        pop     ax                      ; get previous value
        mov     ah,al                   ; and place in al
        in      al,rtcdata              ; get value from clock
        sti                             ; reenable interrupts
        cmp     al,ah                   ; compare values
        jne     @@nochange

        mov     dx,offset msg1          ; print updated message
        jmp     @@printmessage

@@nochange:
        mov     dx,offset msg2          ; print not updated message
@@printmessage:
        mov     ah,09h
        int     21h
@@exit:
        cli                             ; disable interrupts
        mov     al,statusB
        out     rtcaddr,al
        pop     ax
        iodelay
        out     rtcdata,al              ; restore status b reg
        sti                             ; reenable interrupts

        mov     ax,4c00h                ; terminate program
        int     21h

        ENDS

        END     start

---
 * Origin: RWare Software HQ - Boulder, CO (1:104/122)

