CMOS芯片特徵:

①包含一個實時鐘和一個有128個存儲單元的RAM存儲器

②128個字節的RAM中,內部實時鐘佔用0~0dh單元來保存時間信息,其餘大部份單元用於保存系統配置信息,供系統啟動時BIOS程序讀取。

③該芯片內部有兩個端口,端口地址為70h和71h。CPU通過這兩個端口來讀寫CMOS RAM。

在CMOS RAM中,存放著當前的時間:年、月、日、時、分、秒。這6個信息的長度都為1個字節,存放單元為:

秒:0 分:2 時:4 日:7 月:8 年:9

這些數據以BCD碼的方式存放。(高4位的BCD碼表示十位,低4位的BCD碼表示個位。)

BCD码值+30h = 十进制数对应的ASCII码

C语言实现(Win-TC编译通过):

#include

void main(){

while(!kbhit()){//按任意键退出循环

int

i,m;

printf("date:");

for

(i = 9;i >= 7;i--){

//输出日期

outportb(0x70,i);//向port地址70h输出value

m

= inportb(0x71);//返回从指定的I/O端口71h读入一个字节(8位数)

printf("%2x

",m);

}

printf("\n");

printf("time:");

for

(i = 4;i >= 0;i = i-2){

//输出时间

outportb(0x70,i);

m

= inportb(0x71);

printf("%2x

",m);

}

delay(1000);

//延时1S

clrscr();//清屏

}

}

汇编实现(masm615编译通过):

assume cs:code

INCLUDE Irvine16.inc

.data

byte1

BYTE ?

byte2

BYTE ?

string1

BYTE "Date:"

string2

BYTE 0ah,"Time:"

code segment

start:

mov

ax,@data;initialize DS

mov

ds,ax

;用INT

21h中断功能40h在屏幕上显示“Date;”

mov

ah,40h

mov

bx,1

mov

cx,SIZEOF string1

mov

dx,OFFSET string1

int

21h

;从CMOS

RAM中获取Date信息

mov

bl,08D

.WHILE bl>6

mov

al,bl

out

70h,al ;向地址端口70h写入要访问的单元的地址

in

al,71h ;从数据端口71h中取得指定单元中的数据

mov

ah,al ;al中为从CMOS

RAM的相应单元读出的数据

mov

cl,4

shr

ah,cl ;ah中为date中十位数码值

and

al,00001111b ;al中为date中个位数码值

add

ah,30h ;BCD码值+30h = 十进制数对应的ASCII码

add

al,30h

;用INT

21h中断功能2在屏幕上显示date

mov

byte1,ah

mov

byte2,al

mov

ah,02h

mov

dl,byte1

int

21h

mov

ah,02h

mov

dl,byte2

int

21h

dec

bx

.ENDW

;用INT

21h中断功能40h在屏幕上显示“Time;”

mov

ah,40h

mov

bx,1

mov

cx,SIZEOF string2

mov

dx,OFFSET string2

int

21h

;从CMOS

RAM中获取Time信息

mov

bl,04D

.WHILE

bl>0

mov

al,bl

out

70h,al

in

al,71h

mov

ah,al

mov

cl,4

shr

ah,cl

and

al,00001111b

add

ah,30h

add

al,30h

;用INT

21h中断功能2在屏幕上显示date

mov

byte1,ah

mov

byte2,al

mov

ah,02h

mov

dl,byte1

int

21h

mov

ah,02h

mov

dl,byte2

int

21h

dec

bx

dec

bx

.ENDW

mov ah,4ch

int 21h

code ends

end start

更多推荐