⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.asm

📁 digital clock arm sample code using for beginer
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;***********************************************************
;
;              Digital Clock with alarm using DS1307
;
;                   For Keil/ A51 Macro Assembler
;
;              Designed By: Ajay Bhargav
;
;              E-mail:      ajay_bhargav@hotmail.com
;
;              WWW:         www.rickeyworld.tk
;
;   PS: Use only A51 Macro assembler for linking this file
;       else you can directly use the hex file provided.
;
;       And if in anycase you want to edit the code then
;       you have to use an alternative to write the macros
;       defined in the file. In my opinion you dont need to
;       edit it.
;
;***********************************************************

hold	macro					;delay macro for I2C
	nop
	nop
	nop
	endm

disp_str macro string				;Macro for sending string to LCD
	irpc char, <string>
		if nul 'char'
			exitm
		endif
		mov a,#'char'
		lcall data_in
	endm
	endm

build_char macro P1,P2,P3,P4,P5,P6,P7,P8	;Macro for building a custom character
	irp arg, <P1,P2,P3,P4,P5,P6,P7,P8>
		mov a,#arg
		acall data_in
	endm
	endm
	
	sec equ 30h				;second register
	min equ 31h				;minutes register
	hour equ 32h				;hour register
	days equ 33h				;days register
	date equ 34h				;date register
	month equ 35h				;month register
	year equ 36h				;year register
	alarm equ 37h				;alarm register
	sig equ 38h				;signature for data checking in RTC RAM
	ahour equ 39h				;alarm hour register
	amin equ 3Ah				;alarm minute register
	sda equ P1.1				;Serial data pin
	scl equ P1.0				;Serial clock pin
	rw equ p3.6				;read write pin of LCD
	en equ p3.5				;enable pin of LCD
	rs equ p3.7				;register select pin of LCD
	alarm_key equ p3.3			;Alarm set key
	time_key equ p3.2			;Time ser key
	increment equ p1.3			;Increment value
	decrement equ p1.2			;Decrement Value
	alarm_port equ p1.4			;Alarm Buzzer port
	alarm_flag equ 20h			;flag for alarm
	time_flag equ 21h			;flag for time
	ampm_flag equ 22h			;am/pm flag
	alarm_on equ 23h			;Alarm on flag
	alarm_off equ 24h			;alarm off flag
	alarm_ring equ 25h			;alarm ring flag
	alarm_hrs equ 58h			;Alarm hour register
	alarm_min equ 59h			;Alarm Minutes register
	alarm_chk_hour equ 5Ah			;Alarm hour check register
	alarm_chk_min equ 5Bh			;Alarm min check register
	ampm equ 61h				;AM/PM register
	BELL equ 0h				;Bell icon
	SPEAKER_OFF equ 1h			;speaker off icon
	SPEAKER_ON equ 2h			;speaker on icon
	CLOCK equ 3h				;clock icon
	OK equ 4h				;ok icon
	HEART equ 5h				;heart icon
	MUSIC equ 6h				;music icon

	org 0000h				;Starting of main program
	ljmp start

	org 03h					;interrupt for set time
	setb time_flag
	reti

	org 13h					;Interrupt for set alarm
	setb alarm_flag				;and switch off the alarm if on
	jnb alarm_off,hmmm
	setb alarm_port
	mov a,#1h
	lcall command
	mov a,#81h
	lcall command
	mov a,#OK
	acall data_in
	mov r7,#35
	disp_str < Alarm Off!>
	setb tr0
loooop:	jnb tf0,$
	clr tf0
	djnz r7,loooop
	clr tr0
	mov a,#1h
	acall command
	acall disp_const
	setb alarm_off
	setb alarm_on
	clr alarm_flag
	clr alarm_flag
	clr alarm_on
	setb alarm_port
	clr alarm_off
	clr alarm_flag
	clr alarm_ring
hmmm:	reti

start:	mov tmod,#1h				;start routine
	mov sp,#10h
	acall initial
	acall build
	mov a,#80h
	acall command
	mov a,#CLOCK
	acall data_in
	disp_str < Digital Clock >		;Name display
	mov r7,#70
	setb tr0
wloop:	jnb tf0,$
	clr tf0
	djnz r7,wloop
	clr time_flag
	clr alarm_flag
	clr alarm_on
	clr ampm_flag
	setb alarm_port
	mov ie,#85h
	acall startc				;Reading signature byte if same then no need
	mov a,#0d0h				;of initialization else initialize the RTC
	acall send
	mov a,#08h
	acall send
	acall startc
	mov a,#0d1h
	acall send
	acall recv
	acall stop
	cjne a,#'~',done
	sjmp run
done:	acall initial
	acall disp_const
	mov sec,#00h				;For more information about the RTC DS1307
	mov min,#0h				;Please refer to the datasheet
	mov hour,#52h				;21h for 24 hour format
	mov days,#01h
	mov date,#1h
	mov month,#1h
	mov year,#05h
	mov alarm,#0
	mov sig,#'~'
	mov ahour,#0
	mov amin,#0
	mov r6,#0bh
	mov r0,#30h
	mov r1,#00h
	acall rtc_ini				;initializing RTC if required
run:	acall initial
	acall disp_const
run1:	acall startc				;Actual Starting
	mov a,#0d0h
	acall send
	mov a,#00h
	acall send
	mov r6,#3fh
	acall startc				;Reading the RCT content
	mov a,#0d1h
	acall send
	mov r0,#40h
here:	acall recv
	djnz r6,here
	acall stop
	acall display				;Display RTC
	jnb time_flag,noset			;check for time flag if set the set the clock
	clr time_flag
	clr ea
	acall time_set
	clr time_flag
	mov r6,#09h
	mov r0,#30h
	mov r1,#00h
	acall rtc_ini
	clr time_flag
	mov a,#1h
	acall command
	mov a,#81h
	acall command
	mov a,#OK
	acall data_in
	disp_str < Time Set!>
	mov r7,#35
	setb tr0
looooop:jnb tf0,$
	clr tf0
	djnz r7,looooop
	clr tr0
	mov a,#1h
	acall command
	acall disp_const
	setb ea
noset:
	jnb alarm_flag,noset1			;check for alarm flag if set then set alarm
	clr alarm_flag
	clr ea
	lcall alarm_set
	setb ea
	setb alarm_on
	clr alarm_flag
	setb alarm_on
noset1:
	jnb alarm_ring,noset2			;check if alarm ring flag is set
	lcall alarm_alarm			;if set then ring the alarm
noset2:	ljmp run1				;Do everything again

startc:						;I2C atart condition subroutine
	clr scl
	setb sda
	hold
	setb scl
	clr sda
	hold
	ret
send:						;Sending data to I2C bus
	mov r7,#08
back:
	clr scl
	hold
	rlc a
	mov sda,c
	setb scl
	hold
	clr scl
	hold
	djnz r7,back
	setb sda
	setb scl
	hold
	clr scl
	hold
	ret
	
stop:	hold					;I2C stop Condition
	clr sda
	setb scl
	nop
	setb sda
	hold
	clr scl
	ret

recv:						;Recieving data from I2C bus
	mov r7,#08
back2:
	setb sda
	setb scl
	hold
	mov c,sda
	rlc a
	clr scl
	hold
	djnz r7,back2
	setb sda
	clr scl
	hold
	clr sda
	setb scl
	hold
	clr scl
	hold
	mov @r0,a
	inc r0
	ret

rtc_ini:acall startc				;RTC initialization subroutine
	mov a,#0d0h
	acall send
	mov a,r1
	acall send
	mov a,@r0
	acall send
	inc r0
	inc r1
	acall stop
	djnz r6,rtc_ini
	acall stop
	ret

initial:mov a,#38h				;LCD initialization subroutine
	acall command
	mov a,#0ch
	acall command
	mov a,#01h
	acall command
	mov a,#06h
	acall command
	ret

build:	mov a,#40h				;Building custom character routine
	acall command
	build_char 4h,0eh,0eh,0eh,1fh,0h,4h,0h	 ;BELL
	build_char 1h,3h,0fh,0fh,0fh,3h,1h,0h	 ;SPEAKER OFF
	build_char 8h,10h,0h,18h,0h,10h,8h,0h	 ;SPEAKER ON
	build_char 0h,0eh,15h,17h,11h,0eh,0h,0h	 ;CLOCK
	build_char 0h,1h,3h,16h,1ch,8h,0h,0h	 ;OK
	build_char 0ah,1fh,1fh,1fh,0eh,4h,0h,0h	 ;HEART
	build_char 2h,3h,2h,0eh,1eh,0ch,0h,0h	 ;MUSIC
	ret

display:mov r1,#40h				;Displaying RTC content
	mov a,#0Cah
	acall command
	mov a,@r1
	mov 50h,@r1
	acall disp_val
PASS:	inc r1
	mov a,#0C7h
	acall command
	mov a,@r1
	mov 51h,@r1
	mov alarm_chk_min,@r1
	acall disp_val
PASS1:	inc r1
	mov a,#0c4h
	acall command
	mov a,@r1
	mov 52h,@r1
	mov alarm_chk_hour,@r1
	mov r4,a
	anl a,#1fh
	acall disp_val
	mov a,r4
	anl a,#20h
	cjne a,#00h,pm
	mov a,#0cdh
	acall command
	disp_str <am>
	sjmp pass2
pm:	mov a,#0cdh
	acall command
	disp_str <pm>
PASS2:	inc r1
	mov a,#80h
	acall command
	mov a,@r1
	mov 53h,@r1
	acall day
PASS3:	inc r1
	mov a,#88h
	acall command
	mov a,@r1
	mov 54h,@r1
	acall disp_val
PASS4:	inc r1
	mov a,#8bh
	acall command
	mov a,@r1
	mov 55h,@r1
	acall disp_val
PASS5:	inc r1
	mov a,#8eh
	acall command
	mov a,@r1
	mov 56h,@r1
	acall disp_val
	jnb alarm_on,PASS6
	inc r1
	inc r1
	inc r1
	mov a,@r1
	cjne a,52h,PASS6
	inc r1
	mov a,@r1
	cjne a,51h,PASS6
	setb alarm_ring
	setb alarm_ring
PASS6:	ret

disp_val:					;Display 8-bit Decimal Value
	push acc
	mov r5,a
	anl a,#0f0h
	swap a
	mov dptr,#ascii
	movc a,@a+dptr
	acall data_in
	mov a,r5
	anl a,#0fh
	movc a,@a+dptr
	acall data_in
	pop acc
	ret

disp_const:					;Display constant characters
	mov a,#8ah
	acall command
	mov a,#'/'
	acall data_in
	mov a,#8dh
	acall command
	mov a,#'/'
	acall data_in
	mov a,#0c6h
	acall command
	mov a,#':'
	acall data_in
	mov a,#0c9h
	acall command
	mov a,#':'
	acall data_in
	mov a,#0c0h
	acall command
	mov a,#BELL
	acall data_in
	mov a,#SPEAKER_OFF
	acall data_in
	ret

command:acall busy				;Sending command to LCD
	MOV P2,A
	CLR rs
	CLR rw
	SETB en
	CLR en
	RET

data_in:acall busy				;Sending data to LCD
	SETB rs
	MOV P2,A
	CLR rw
	SETB en
	CLR en
	RET

busy:	SETB P2.7				;Checking Busy flag
	CLR rs
	SETB rw
wait:	CLR en
	SETB en
	JB P2.7,wait
	RET

day:	push acc				;Displaying day subroutine for RTC
	cjne a,#01,skip
	disp_str < *Sun*>
	ljmp exit
skip:	cjne a,#02,skip2
	disp_str < *Mon*>
	ljmp exit
skip2:	cjne a,#03,skip3
	disp_str < *Tue*>
	ljmp exit
skip3:	cjne a,#04,skip4
	disp_str < *Wed*>
	sjmp exit
skip4:	cjne a,#05,skip5
	disp_str< *Thu*>
	sjmp exit
skip5:	cjne a,#06,skip6
	disp_str < *Fri*>
	sjmp exit
skip6:	disp_str < *Sat*>
exit:	pop acc
	ret

time_set:					;Setting time subroutine
	jnb time_key,$
	mov a,#1
	acall command
	mov a,#80h
	acall command
	mov a,#CLOCK
	acall data_in
	disp_str < set minutes:>
	mov a,#0c5h
	acall command
	mov a,51h
	acall disp_val
	lcall hexdec
key:	push acc
	mov a,#0c5h
	acall command
	pop acc
	jb increment,chk1
	inc a
	cjne a,#60,ok1
	mov a,#0
ok1:	jnb increment,$
	lcall dechex
	acall disp_val
	lcall hexdec
	sjmp key
chk1:	jb decrement,chk2
	dec a
	cjne a,#0ffh,ok2
	mov a,#59
ok2:	jnb decrement,$
	lcall dechex

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -