SoFunction
Updated on 2025-04-09

Detailed explanation of the idea of ​​implementing electronic alarm clocks in assembly language

2.1 Design ideas

First, use the three chips 8255, 8254, and 8259 to realize the function of the electronic clock so that the alarm clock can run normally; secondly, add the alarm function, the hourly time function, and set the current time function on the basis of the clock; complete the design.

2.2 Design plan

1. Electronic clock part: This design is to count the pulse by setting the count value of the counter 8254. In the program, 8254 works on counter 0 and method 3. The CLK connected is 1MHz, the initial value of the design is 10,000, and counts every 100 interrupts. The generated counting time is exactly the time passed by the clock every second. The interrupt is controlled by setting the initial value of the initialization command word for the interrupt controller 8259. The single bits of time, second, minute and ten bits are stored separately in the program through shift instructions, and the single bits of minutes and second are stored in the specified registers respectively. Compare transfer instructions to complete the accumulation of seconds and minutes. The seven-segment digital tube is selected by port A of the 8255 parallel interface, and the segment code to be displayed is output through port B. In this way, hours, minutes and seconds are displayed on the 6 seven-segment digital tubes respectively. When the 1S time arrives, modify the current digital tube value, clear the low position of the second at 10 seconds, add 1 to the high position, and the same is true for the minutes and sum; when it reaches 60 seconds, clear the second, add 1 to the minute, and when it reaches 60 minutes, clear the minute, add 1 to the hour, and when it arrives 24 hours, all clear the zero.

2. Alarm ringing part: For the alarm part, we must first complete the connection and design of the keys of the digital tube display unit, so that the 16 keys are 0-F respectively; then use the single pulse input of KK1 of the experimental platform to generate an interrupt, so that the time can be set. Store the set time and compare it with the current time. When the time is the same, counter 1 works, the buzzer beeps, and music is played, which completes the alarm ringing function.

3. Octopus time report: Octopus time report means that the ring ring is ringing at the full time. At the hour of the clock, no matter what time it is, the minute and second parts of the clock are zero. Therefore, as long as the minute and second parts of the clock are compared with zero, when they are all equal, it means that the hour point has arrived and the bell can be ringed. The hourly time report is completed, and the hourly time report time set by this alarm clock is 5 seconds.

4. Set the current time: This part also uses the key part of the digital tube display unit. Just use the previous design. This time, KK2 inputted with a single pulse will generate an interrupt setting time, and then store the set time in the time variable, and the time will be set.

A8254 EQU 06C0H ;Macro definition
B8254 EQU 06C2H 
C8254 EQU 06C4H 
CON8254 EQU 06C6H 
MY8255_A EQU 0600H
MY8255_B EQU 0602H
MY8255_C EQU 0604H
MY8255_CON EQU 0606H
PUBLIC SEC,MIN,HOUR     
DATA SEGMENT    ;Define data segments
TAB  DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H  ;Digital meter
  DB 7FH,6FH,77H,7CH,39H,5EH,79H,71H 
FREQ_LIST DW 371,495,495,495,624,556,495,556,624  ;Frequency table
   DW 495,495,624,742,833,833,833,742,624
   DW 624,495,556,495,556,624,495,416,416,371
   DW 495,833,742,624,624,495,556,495,556,833
   DW 742,624,624,742,833,990,742,624,624,495
   DW 556,495,556,624,495,416,416,371,495,0
TIME_LIST DB 4, 6, 2, 4, 4, 6, 2, 4, 4  ;schedule
   DB 6, 2, 4, 4, 12, 1, 3, 6, 2
   DB 4, 4, 6, 2, 4, 4, 6, 2, 4, 4
   DB 12, 4, 6, 2, 4, 4, 6, 2, 4, 4
   DB 6, 2, 4, 4, 12, 4, 6, 2, 4, 4
   DB 6, 2, 4, 4, 6, 2, 4, 4, 12 
SEC  DB 0   ;initialization      
HOUR DB 0         
MIN  DB 0
N_TIME DB 20 DUP(0)
Z_TIME DB 20 DUP(0)
N_S  DB 30
N_M  DB 0
N_H  DB 0
TIMER DB 10 DUP(0)
DUAN DB 0
BUF  DB 3 DUP(0) 
DATA ENDS  
SSTACK SEGMENT STACK  ;Stack segment 
  DW 256 DUP(?)
SSTACK ENDS
CODE SEGMENT    ;Code segment
  ASSUME CS:CODE, SS:SSTACK,DS:DATA
START: ;8254initialization
  MOV DX, CON8254   ;8254
  MOV AL, 36H    ;counter0,Way3
  OUT DX, AL
  ;Dividing frequency
  MOV DX, A8254
  MOV AL, 010H
  OUT DX, AL
  MOV AL, 27H
  OUT DX, AL
  ;8255initialization
  MOV AL,81H
  MOV DX,MY8255_CON
  OUT DX,AL
  ;MIR7Interrupt
  MOV AX, 0000H
  MOV DS, AX
  MOV AX, OFFSET MIR7   ;PickInterrupt入口地址
  MOV SI, 003CH      ;Interrupt矢量地址
  MOV [SI], AX      ;fillIRQ7offset vector
  MOV AX, SEG MIR7    ;Section address
  MOV SI, 003EH
  MOV [SI], AX      ;fillIRQ7的Section address矢量
  ;MIR6Interrupt
  MOV AX, OFFSET MIR6   ;PickInterrupt入口地址
  MOV SI, 0038H      ;Interrupt矢量地址
  MOV [SI], AX      ;fillIRQ6offset vector
  MOV AX, SEG MIR6    ;Section address
  MOV SI, 003AH
  MOV [SI], AX      ;fillIRQ6的Section address矢量
  ;MIR2Interrupt
  MOV AX, OFFSET SIR1   ;PickInterrupt入口地址
  MOV SI, 00C4H      ;Interrupt矢量地址
  MOV [SI], AX      ;fillSIR1offset vector
  MOV AX, SEG SIR1    ;Section address
  MOV SI, 00C6H
  MOV [SI], AX      ;fillSIR1的Section address矢量
  ;8259initialization
  MOV AL, 11H
  OUT 20H, AL      ;ICW1
  MOV AL, 08H
  OUT 21H, AL 
  MOV AL,04H
  OUT 21H,AL     ;ICW2
  MOV AL, 03H
  OUT 21H, AL      ;ICW4
  ;initialization从片8259
   MOV AL, 11H
  OUT 0A0H, AL   ;ICW1
  MOV AL, 30H
  OUT 0A1H, AL   ;ICW2
  MOV AL, 02H    
  OUT 0A1H, AL   ;ICW3
  MOV AL, 01H
  OUT 0A1H, AL   ;ICW4
  MOV AL, 0FDH
  OUT 0A1H,AL    ;OCW1 = 1111 1101
   MOV AX,SEG N_TIME  ;PickN_TIME的Section address
  MOV DS,AX    
  MOV SI,OFFSET N_TIME  ;PickN_TIMEAddress offset
  MOV [SI+1],1
  STI
AA1: 
  CALL DISP    ;Call the display subroutine(disp)
  CALL BJ    ;Call the alarm clock(bj)
  CALL ZDBS   ;Call the full time(bj)
   JMP AA1
MIR6 PROC
  CLI
  PUSH AX
  PUSH SI
  MOV AX,DATA
  MOV DS,AX
   MOV SI,3000H
  MOV AL,00H
  MOV [SI],AL    ;Clear display buffer
  MOV [SI+1],AL
  MOV [SI+2],AL
  MOV [SI+3],AL
  MOV [SI+4],AL
  MOV [SI+5],AL
  MOV DI,3005H
  MOV DX,MY8255_CON  ;Write8255Control word
  MOV AL,81H
  OUT DX,AL
BEGIN: CALL DIS    ;Call the display subroutine
  CALL CLEAR    ;Clear the screen
  CALL CCSCAN    ;scanning
  JNZ INK1
  JMP BEGIN
INK1: CALL DIS
  CALL KEYDALLY
  CALL KEYDALLY
  CALL CLEAR
  CALL CCSCAN
  MOV AX,SEG DUAN  ;PickDUAN的Section address
  MOV DS,AX    
  MOV SI,OFFSET DUAN  ;PickDUANAddress offset,For technical counting
  MOV AL,[SI] 
  INC AL 
  MOV [SI],AL
  CMP AL,7    ;ALand6Comparison
  JE B2
  JNZ INK2    ;Press with key,Go toINK2
  JMP BEGIN
;========================================
;Determine the position of pressing the key
;========================================
B2:  MOV AL,0
  MOV [SI],AL
  POP AX
  POP SI
  IRET
INK2: MOV CH,0FEH
  MOV CL,00H
COLUM: MOV AL,CH
  MOV DX,MY8255_A 
  OUT DX,AL
  MOV DX,MY8255_C 
  IN AL,DX
L1:  TEST AL,01H   ;is L1?
  JNZ L2
  MOV AL,00H   ;L1
  JMP KCODE
L2:  TEST AL,02H   ;is L2?
  JNZ L3
  MOV AL,04H   ;L2
  JMP KCODE
L3:  TEST AL,04H   ;is L3?
  JNZ L4
  MOV AL,08H   ;L3
  JMP KCODE
L4:  TEST AL,08H   ;is L4?
  JNZ NEXT
  MOV AL,0CH   ;L4
KCODE: ADD AL,CL
  CALL PUTBUF
  PUSH AX
KON: CALL DIS
  CALL CLEAR
  CALL CCSCAN
  JNZ KON
  POP AX
NEXT: INC CL
  MOV AL,CH
  TEST AL,08H
  JZ KERR
  ROL AL,1
  MOV CH,AL
  JMP COLUM
KERR: JMP BEGIN
;========================================
;键盘scanning子程序
;========================================
CCSCAN: MOV AL,00H
  MOV DX,MY8255_A 
  OUT DX,AL
  MOV DX,MY8255_C 
  IN AL,DX
  NOT AL
  AND AL,0FH
  RET
;========================================
;Clear the screen子程序
;========================================
CLEAR: MOV DX,MY8255_B 
  MOV AL,00H
  OUT DX,AL
  RET
;========================================
;Show subroutines
;========================================
DIS: PUSH AX
  MOV SI,3000H
  MOV DL,0DFH
  MOV AL,DL
AGAIN: PUSH DX
  MOV DX,MY8255_A 
  OUT DX,AL
  MOV AL,[SI]
  MOV BX,OFFSET TAB
  AND AX,00FFH
  ADD BX,AX
  MOV AL,[BX]
  MOV DX,MY8255_B 
  OUT DX,AL
  CALL KEYDALLY
  INC SI
  POP DX
  MOV AL,DL
  TEST AL,01H
  JZ OUT1
  ROR AL,1
  MOV DL,AL
  JMP AGAIN
OUT1: POP AX
  RET
;====== Delay subroutine ======  
KEYDALLY: PUSH CX
  MOV CX,0006H
D1:  MOV AX,009FH
D2:  DEC AX
  JNZ D2
  LOOP D1
  POP CX
  RET
;========================================
;Save the keyboard value into the buffer of the corresponding bit
;========================================
PUTBUF: MOV SI,DI
  MOV [SI],AL
  MOV CX,SEG DUAN  ;PickDUAN的Section address
  MOV DS,CX    
  MOV SI,OFFSET DUAN  ;PickDUANAddress offset
  MOV BX,[SI]
  MOV CX,SEG N_TIME  ;PickDUAN的Section address
  MOV DS,CX    
  MOV SI,OFFSET N_TIME  ;PickDUANAddress offset
  MOV [SI+BX],AL
  MOV DX,[SI+BX]
  DEC DI
  CMP DI,2FFFH
  JNZ GOBACK
  MOV DI,3005H
GOBACK: RET
MIR6 ENDP
SIR1 PROC
  CLI
  PUSH AX
  PUSH SI
  MOV AX,DATA
  MOV DS,AX
   MOV SI,3000H
  MOV AL,00H
  MOV [SI],AL    ;Clear display buffer
  MOV [SI+1],AL
  MOV [SI+2],AL
  MOV [SI+3],AL
  MOV [SI+4],AL
  MOV [SI+5],AL
  MOV DI,3005H
  MOV DX,MY8255_CON  ;Write8255Control word
  MOV AL,81H
  OUT DX,AL
BEGIN_U: CALL DIS1    ;Call the display subroutine
  CALL CLEAR1    ;Clear the screen
  CALL CCSCAN1    ;scanning
  JNZ INK3
  JMP BEGIN_U
INK3: CALL DIS1
  CALL U_DALLY
  CALL U_DALLY
  CALL CLEAR1
  CALL CCSCAN1
  MOV AX,SEG DUAN  ;PickDUAN的Section address
  MOV DS,AX    
  MOV SI,OFFSET DUAN  ;PickDUANAddress offset
  MOV AL,[SI] 
  INC AL 
  MOV [SI],AL
  CMP AL,7    ;ALand7Comparison
  JE B3
  JNZ INK4    ;Press with key,Go toINK2
  JMP BEGIN_U
;========================================
;Determine the position of pressing the key
;========================================
B3:  MOV AL,0
  MOV [SI],AL
  POP AX
  POP SI
  IRET
INK4: MOV CH,0FEH
  MOV CL,00H
COLUM2: MOV AL,CH
  MOV DX,MY8255_A 
  OUT DX,AL
  MOV DX,MY8255_C 
  IN AL,DX
U1:  TEST AL,01H   ;is L1?
  JNZ U2
  MOV AL,00H   ;L1
  JMP KCODE1
U2:  TEST AL,02H   ;is L2?
  JNZ U3
  MOV AL,04H   ;L2
  JMP KCODE1
U3:  TEST AL,04H   ;is L3?
  JNZ U4
  MOV AL,08H   ;L3
  JMP KCODE1
U4:  TEST AL,08H   ;is L4?
  JNZ NEXT1
  MOV AL,0CH   ;L4
KCODE1: ADD AL,CL
  CALL PUTBUF1
  PUSH AX
KON1: CALL DIS1
  CALL CLEAR1
  CALL CCSCAN1
  JNZ KON1
  POP AX
NEXT1: INC CL
  MOV AL,CH
  TEST AL,08H
  JZ KERR1
  ROL AL,1
  MOV CH,AL
  JMP COLUM2
KERR1: JMP BEGIN_U
;========================================
;键盘scanning子程序
;========================================
CCSCAN1: MOV AL,00H
  MOV DX,MY8255_A 
  OUT DX,AL
  MOV DX,MY8255_C 
  IN AL,DX
  NOT AL
  AND AL,0FH
  RET
;========================================
;Clear the screen子程序
;========================================
CLEAR1: MOV DX,MY8255_B 
  MOV AL,00H
  OUT DX,AL
  RET
;========================================
;Show subroutines
;========================================
DIS1: PUSH AX
  MOV SI,3000H
  MOV DL,0DFH
  MOV AL,DL
AGAIN1: PUSH DX
  MOV DX,MY8255_A 
  OUT DX,AL
  MOV AL,[SI]
  MOV BX,OFFSET TAB
  AND AX,00FFH
  ADD BX,AX
  MOV AL,[BX]
  MOV DX,MY8255_B 
  OUT DX,AL
  CALL U_DALLY
  INC SI
  POP DX
  MOV AL,DL
  TEST AL,01H
  JZ OUT2
  ROR AL,1
  MOV DL,AL
  JMP AGAIN1
OUT2: POP AX
  RET
;====== Delay subroutine ======  
U_DALLY: PUSH CX
  MOV CX,0006H
W1:  MOV AX,009FH
W2:  DEC AX
  JNZ W2
  LOOP W1
  POP CX
  RET
;========================================
;Save the keyboard value into the buffer of the corresponding bit
;========================================
PUTBUF1: MOV SI,DI
  MOV [SI],AL
  MOV CX,SEG DUAN  ;PickDUAN的Section address
  MOV DS,CX    
  MOV SI,OFFSET DUAN  ;PickDUANAddress offset
  MOV BX,[SI]
  MOV CX,SEG TIMER  ;PickDUAN的Section address
  MOV DS,CX    
  MOV SI,OFFSET TIMER  ;PickDUANAddress offset
  MOV [SI+BX],AL
  MOV DX,[SI+BX]
  DEC DI
  CMP DI,2FFFH
  JNZ GOBACK1
  MOV DI,3005H
GOBACK1: RET
 
SIR1 ENDP
MIR7 PROC     
  STI      ;置Interrupt标志位IF 
  PUSH AX     ;Press the stack
  PUSH SI
  MOV AX,SEG TIMER  ;PickTIMER的Section address
  MOV DS,AX    
  MOV SI,OFFSET TIMER  ;PickTIMERAddress offset
  MOV AL,[SI] 
  INC AL 
  MOV [SI],AL
  CMP AL,100    ;ALand100Comparison
  JNE TRNED    ;If equal,Jump
  MOV AL,0    ;If not, execute downward
  MOV [SI],AL
  MOV AL,[SI+1]
  ADD AL,1
  DAA       ;Decimal adjustment,Used for addition
  MOV [SI+1],AL
  CMP AL,60H
  JNE TRNED
  MOV AL,0
  MOV [SI+1],AL
  MOV AL,[SI+2]
  ADD AL,1
  DAA
  MOV [SI+2],AL
  CMP AL,60H
  JNE TRNED
  MOV AL,0
  MOV [SI+2],AL
  MOV AL,[SI+3]
  ADD AL,1
  DAA 
  MOV [SI+3],AL
  CMP AL,24H
  JNE TRNED
  MOV AL,0
  MOV [SI+3],AL
TRNED: 
  MOV AL,[SI+1] 
  MOV SEC,AL
  MOV AL,[SI+2] 
  MOV MIN,AL  
  MOV AL,[SI+3] 
  MOV HOUR,AL
  POP SI         ;Out the stack
  POP AX
  STI          ;置Interrupt标志位IF
  IRET         ;Interrupt返回
  ENDP
DISP: 
  PUSH BX
  PUSH DI
  PUSH AX
  PUSH SI
  MOV DX,MY8255_B
  MOV AX,SEG TAB
  MOV DS,AX
  MOV BX,OFFSET TAB 

  MOV AL,SEC 
  AND AL,0FH        ;and低四位相and
  MOV AH,00H    
  MOV SI,AX
  MOV AL,[BX+SI]
  OUT DX,AL
  MOV DX,MY8255_A
  MOV AL,0DFH
  OUT DX,AL
  CALL DALLY   
  MOV AL,SEC
  AND AL,0F0H
  ror al,04
  MOV AH,00H
  MOV SI,AX
  MOV AL,[BX+SI]
  MOV DX,MY8255_B
  OUT DX,AL
  MOV DX,MY8255_A
  MOV AL,0EFH
  OUT DX,AL
  CALL DALLY
  MOV AL,MIN 
  AND AL,0FH    
  MOV AH,00H    
  MOV SI,AX
  MOV AL,[BX+SI]
  MOV DX,MY8255_B
  OUT DX,AL
  MOV DX,MY8255_A
  MOV AL,0F7H
  OUT DX,AL
  CALL DALLY
  MOV AL,MIN
  AND AL,0F0H
  ror al,04
  MOV AH,00H
  MOV SI,AX
  MOV AL,[BX+SI]
  MOV DX,MY8255_B
  OUT DX,AL
  MOV DX,MY8255_A
  MOV AL,0FBH
  OUT DX,AL
  CALL DALLY
  MOV AL,HOUR 
  AND AL,0FH    
  MOV AH,00H    
  MOV SI,AX
  MOV AL,[BX+SI]
  MOV DX,MY8255_B
  OUT DX,AL
  MOV DX,MY8255_A
  MOV AL,0FDH
  OUT DX,AL
  CALL DALLY
  MOV AL,HOUR
  AND AL,0F0H
  ror al,04
  MOV AH,00H
  MOV SI,AX
  MOV AL,[BX+SI]
  MOV DX,MY8255_B
  OUT DX,AL
  MOV DX,MY8255_A
  MOV AL,0FEH
  OUT DX,AL
  CALL DALLY
  POP SI
  POP AX
  POP DI
  POP BX
  RET
ZDBS PROC      ;Time report at the hour
  MOV AL,MIN
  CMP AL,00H
  JNZ LP2

  MOV AL,SEC
  CMP AL,00H
  JNZ LP2

  MOV DX, CON8254   ;8254
  MOV AL, 76H    ;counter1,Way3
  OUT DX, AL
  
 ZD1:
 MOV DX, B8254
  MOV AL, 00H
  OUT DX, AL
  MOV AL, 09H
  OUT DX, AL  
  MOV AL,SEC
  CMP AL,05H
  JE OUTZD
  CALL DISP
  JMP ZD1
  
 OUTZD:  
  MOV AX,00H
  OUT DX,AL
  MOV AL,AH
  OUT DX,AL
  RET

  LP2: RET
ZDBS ENDP
  
BJ PROC       ;Alarm program
 MOV AL,HOUR
  MOV CX,SEG N_TIME  ;PickDUAN的Section address
  MOV DS,CX    
  MOV SI,OFFSET N_TIME  ;PickDUANAddress offset
   MOV BL,[SI+1]
  ROL BL,04
  MOV AL,0
  MOV AL,[SI+2]
  OR BL,AL
  CMP AL,BL
  JNZ LP 
  MOV CX,SEG N_TIME  ;PickDUAN的Section address
  MOV DS,CX    
  MOV SI,OFFSET N_TIME  ;PickDUANAddress offset
   MOV BL,[SI+3]
  ROL BL,04
  MOV AL,0
  MOV AL,[SI+4]
  OR BL,AL
  MOV AL,MIN
  CMP AL,BL
  JNZ LP
  MOV CX,SEG N_TIME  ;PickDUAN的Section address
  MOV DS,CX    
  MOV SI,OFFSET N_TIME  ;PickDUANAddress offset
  MOV BL,[SI+5]
  ROL BL,04
  MOV AL,0
  MOV AL,[SI+6]
  OR BL,AL
  MOV AL,SEC
  CMP AL,BL
  JNZ LP 
  MOV DX, CON8254   ;8254
  MOV AL, 76H    ;counter1,Way3
  OUT DX, AL
BEGIN_M: MOV BX,OFFSET FREQ_LIST
  MOV DI,OFFSET TIME_LIST  ;装入schedule起始地址

PLAY: MOV DX,0FH     ;The input clock is1MHz,1M = 0F4240H 
  MOV AX,4240H    
  DIV WORD PTR [BX]   ;Pick出频率值计算计数初值,0F4240H / Output frequency 
  MOV DX,B8254
  OUT DX,AL     ;Load count initial value
  MOV AL,AH
  OUT DX,AL
  MOV DL,[DI]     ;Pick出演奏相对时间,调用Delay subroutine 
  CALL DALLY2
  ADD BX,2
  INC DI
  CMP WORD PTR [BX],0   ;Determine whether it is at the end of the song?
  JE OUTPLAY
  CALL DISP
  JMP PLAY
OUTPLAY:MOV DX,B8254
  MOV AX,00H
  OUT DX,AL
  MOV AL,AH
  OUT DX,AL
  RET
 LP: RET
DALLY2 PROC      ;Delay subroutine
H0:  MOV CX,0010H
H1:  MOV AX,0FF0H
H2:  DEC AX
  JNZ H2
  LOOP H1
  DEC DL
  JNZ H0
  RET
DALLY2 ENDP 
BJ ENDP 

DALLY: PUSH CX
  MOV CX,0006H
T1:  MOV AX,009FH
T2:  DEC AX
  JNZ T2
  LOOP T1
  POP CX
  RET  
CODE ENDS

Summarize

The above is a detailed explanation of the assembly language implementation of electronic alarm clocks introduced to you by the editor. I hope it will be helpful to you!