汇编语言,精简时钟
;**************************************************************************
;简易时钟,汇编程序(汇编精度高)
;作者:黄新建(从事机器的销售,参与控制板的开发与生产)
;2010年1月分
;**************************************************************************
;--------------------ACC-------------------
A7 BIT 0E7H
A6 BIT 0E6H
A5 BIT 0E5H
A4 BIT 0E4H
A3 BIT 0E3H
A2 BIT 0E2H
A1 BIT 0E1H
A0 BIT 0E0H
;--------------------ACC-------------------
Buffer EQU R7
;-----AT24C02----------------------------
WriteAddress equ 0A0H
ReadAddress equ 0A1H
IIC_SCK EQU P2.1
IIC_SDA EQU P2.0
;----------------------------------------
;-----LEDs-------------------------------
S1 EQU P0.2
S2 EQU P0.3
S3 EQU P0.4
S4 EQU P0.5
;----------------------------------------
;------------------LED-------------------
LED EQU P1.0
LED2 EQU P1.1
;----------------------------------------
;-----74HC164----------------------------
SCK_164 EQU P0.1
DAT_164 EQU P0.0
;----------------------------------------
;-----KEY--------------------------------
KeyHour EQU P2.4
KeyMin EQU P2.5
KeyEnter EQU P2.2
KeyExit EQU P2.3
;--------------Time---------------------
D1 EQU 30H;----------Time
Hour EQU 31H
Min EQU 32H
D2 EQU 33H
timer EQU 34H
ClockHour EQU 35H;----------Clock
ClockMin EQU 36H
HourAddr EQU 0
MinAddr EQU 1
;----------------------------------------
ORG 0000H
LJMP Main
ORG 0003H
RETI
ORG 000BH
LJMP Timer0Interrupt
;======================MAIN==================================
Main:
MOV SP,#60H
acall INIT
acall InitTimer0
MOV timer,#0
MOV D1,#0
MOV Min,#0
MOV Hour,#12
MOV Buffer,#HourAddr;Read Hour Data
ACALL AT24C02_Read
MOV ClockHour,Buffer
MOV Buffer,#MinAddr;Read Min Data
ACALL AT24C02_Read
MOV ClockMin,Buffer
while:
MainL1:
ACALL Display;display(Hour,Min)
ACALL KeyDispose
MOV a,timer
CJNE a,#10,MainL2;if timer>=500us D1++;
MainL2:
JC MainL3;如果C=1 说明a<10 如果a>=10 c=0
MOV timer,#0
CPL LED
INC D1
mov a,D1
cjne a,#120,MainL3;if D1>60s Min++;
MOV D1,#0
inc Min
mov a,Min
CJNE a,#60,MainL3 ;if Min>=60 Min=0; Hour++
mov Min,#0 ;
inc Hour; ;
mov a,Hour
cjne a,#24,MainL3 ;if Hour>=24 Hour=0;
mov Hour,#0 ;
;
MainL3: ;
LJMP while
;=====================MAINEnd=================================
;=====================KeySet==================================
KeyDispose:
SETB KeyHour
SETB KeyMin
jb KeyHour,KeyDisposeL1
ACALL KeyDelay
jb KeyHour,KeyDisposeL1
INC Hour
MOV a,Hour
CJNE a,#24,KeyDisposeL1
MOV Hour,#0
KeyDisposeL1:
JB KeyMin,KeyDisposeL2
ACALL KeyDelay
JB KeyMin,KeyDisposeL2
INC Min
MOV a,Min
CJNE a,#60,KeyDisposeL2
MOV Min,#0
KeyDisposeL2:
RET
KeyDelay:
MOV 50H,#20
KeyDelayL1:
ACALL Display
DJNZ 50H,KeyDelayL1
RET
;=====================KeyEnd==================================
;=====================TimeDisplay==============================
Display:
mov DPTR,#LED_TAB;装入数据
mov a,Hour;1
mov b,#10
div ab
mov r1,b
movc a,@a+DPTR
mov r7,a
acall HC164_Send
clr S4
mov r7,#1
acall Delay_nMs
setb S4;------------------
mov a,r1
movc a,@a+DPTR
xrl a,#40H
mov r7,a
acall HC164_Send
clr S3
mov r7,#1
acall Delay_nMs
setb S3;==================
mov a,Min;2
mov b,#10
div ab
mov r1,b
movc a,@a+DPTR
mov r7,a
acall HC164_Send
clr S2
mov r7,#1
acall Delay_nMs
setb S2;-----------------
mov a,r1
movc a,@a+DPTR
mov r7,a
acall HC164_Send
clr S1
mov r7,#1
acall Delay_nMs
setb S1
RET
;========================DisplayEnd===========================
;========================AT24C02==============================
AT24C02_Write:
mov a,r7 ;addr
mov 50H,a
mov a,r6 ;data
mov 51H,a
acall IIC_Start ;开始
mov r7,#WriteAddress
acall IIC_Write ;写指令
mov r7,50H
acall IIC_Write ;写地址
mov r7,51H
acall IIC_Write ;写数据
acall IIC_Stop ;停止
mov r7,#10
acall Delay_nMs ;delay10ms
RET
;--------------------
AT24C02_Read:
mov a,r7
mov 50H,a
acall IIC_Start ;开始
mov r7,#WriteAddress ;写指令
acall IIC_Write
mov r7,50H ;写地址
acall IIC_Write
acall IIC_Start ;重新开始
mov r7,#ReadAddress
acall IIC_Write ;读指令
acall IIC_Read ;读出数据(R7)
acall IIC_NoAck ;读完成
acall IIC_Stop ;停止
RET
;--------------------
IIC_Write:
mov A,r7
mov r4,#8
IIC_WriteL1:
CLR IIC_SDA
clr C;c->A0
RLC A;A7->C
jnc IIC_WriteL2 ;if(A7==1) sda=1
setb IIC_SDA
IIC_WriteL2:
SETB IIC_SCK
acall Delay5Us
clr IIC_SCK
djnz r4,IIC_WriteL1
setb IIC_SDA;test Ack
acall Delay5Us
acall Delay5Us
setb IIC_SCK
acall Delay5Us
clr IIC_SCK
RET
;--------------------
IIC_Read:
mov r4,#8
SETB IIC_SDA
IIC_ReadL1:
SETB IIC_SCK;clk=1
nop
nop
clr c
jnb IIC_SDA,IIC_ReadL2;if(sda==1) c=1
setb c
IIC_ReadL2:
rlc a
clr IIC_SCK;clk=0
djnz r4,IIC_ReadL1
mov r7,a;return r7
RET
;--------------------
IIC_Start:
setb IIC_SDA;sda=1
acall Delay5Us
setb IIC_SCK;clk=1
acall Delay5Us
clr IIC_SDA;sda=0
acall Delay5Us
clr IIC_SCK;clk=0
acall Delay5Us
RET
;--------------------
IIC_Stop:
clr IIC_SDA;sda=0
acall Delay5Us
setb IIC_SCK;clk=1
acall Delay5Us
setb IIC_SDA;sda=1
acall Delay5Us
clr IIC_SCK;clk=0
acall Delay5Us
RET
;--------------------
IIC_NoAck:
setb IIC_SDA;sda=1
acall Delay5Us
setb IIC_SCK;clk=1
acall Delay5Us;
clr IIC_SCK;clk=0
acall Delay5Us
RET
;--------------------
Delay5Us:
nop;1us+call(2us)+ret(2us)=5us
RET
;========================AT24C02End===========================
;========================74HC164===============================
HC164_Send:
MOV R4,#8
HC164L1:
mov a,#80H
ANL a,r7
JB A7,HC164L2
CLR DAT_164
AJMP HC164L3
HC164L2:
SETB DAT_164
HC164L3:
SETB SCK_164
MOV A,R7
CLR C
RLC A
MOV R7,A
CLR SCK_164
DJNZ R4,HC164L1
RET
;======================74HC164End================================
;=========================Delay================================
Delay_nUs:
djnz r7,Delay_nUs
RET
Delay_nMs:
mov r4,#250
djnz r4,$
mov r4,#250
djnz r4,$
djnz r7,Delay_nMs
RET
Delay_nS:
mov r4,#8
DelayNsL2:
mov r3,#250
DelayNsL1:
mov r2,#250
djnz r2,$
djnz r3,DelayNsL1
djnz r4,DelayNsL2
djnz r7,Delay_nS
RET
;=========================DelayEnd=============================
INIT:
mov P0,#0FFH
mov P1,#0FFH
mov P2,#0FFH
mov P3,#0FFH
SETB EA ;打开CPU总中断请求
SETB IT0 ;设定INT0的触发方式为脉冲负边沿触发
SETB EX0 ;打开INT0中断请求
RET
InitTimer0:
MOV TMOD,#01H
MOV TH0,#3CH
MOV TL0,#0C5H
SETB EA
SETB ET0
SETB TR0
RET
Timer0Interrupt:;2us
PUSH DPH ;2us
PUSH DPL ;2us
PUSH ACC ;2us
MOV TH0,#3CH;2us
MOV TL0,#0BCH;2us -12us
;========================
INC timer ;
;========================
POP ACC ;
POP DPL ;
POP DPH ;
RETI ;
LED_TAB:
DB 0B7H;0
DB 084H;1
DB 03DH;2
DB 0ADH;3
DB 08EH;4
DB 0ABH;5
DB 0BBH;6
DB 085H;7
DB 0BFH;8
DB 0AFH;9
END
汇编语言,精简时钟.rar
(2.24 KB)
|
|