PIC单片机

单片机

PIC单片机 PIC单片机自学 PIC单片机编程 PIC单片机教材下载 PIC单片机学习源程序 PIC单片机仿真器|PIC单片机编程器 PIC单片机编程器实验板定价 PIC单片机学习开发板购买方式 PIC单片机学习论坛

PIC16C54单片机四位LED时钟显示程序

 

title"clock"
list p=16c54,n=66
PIC54 equ 1ffh
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;written by:PIC单片机www.pic16.com ;;
;;四位LED时钟显示程序 ;;
;;RB1-SW1秒设置 RB2-SW2分设置 RB3-SW3时设置 ;;
;;RB1--RB7接LED段码,RA0--RA3做选通 ;;
;;使用4M晶振TMR0滪分频为1:16 TMRO的循环时间为4.096MS 244次为一秒 ;;
;;用sec_nth计数 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pointer equ 00h
rtcc equ 01h
pc equ 02h
status equ 03h
fsr equ 04h
port_a equ 05h
port_b equ 06h
;---------------------------------
carry equ 0
c equ 0
dcarry equ 1
dc equ 1
z_bit equ 2
z equ 2
p_down equ 3
pd equ 3
t_out equ 4
to equ 4
pa0 equ 5
pa1 equ 6
pa2 equ 7
;---------------------------------
zero equ 0x7e
one equ 0x0c
two equ 0xb6
three equ 0x9e
four equ 0xcc
five equ 0xda
six equ 0xfa
seven equ 0x0e
eight equ 0xfe
nine equ 0xce
colon equ 0x01
t equ 0xf0
blank equ 0x00
;-----------------------------------------
maxnths equ 0x0c ; d12
maxsecs equ 0xc4 ; d196
maxmins equ 0xc4 ; d196
maxhrs equ 0xf4 ; d244
minhrs equ 0xf3 ; d243
adjmin equ 0x09 ; d9
adjhr equ 0x22 ; d34
adjday equ 0x03 ; d3
;-----------------------------------------------
disp1 equ b'11111110' ;pa0--pa3
disp2 equ b'11111101'
disp3 equ b'11111011'
disp4 equ b'11110111'
dispoff equ b'11111111'
switch equ b'00001110' ;key in portb
;---------------------------------------------
sec equ 00h ;秒 flags d0--d7
min equ 01h ;分
hrs equ 02h ;时
chg equ 03h ;有显示或按键按下
sw1 equ 04h ;
sw2 equ 05h ;
sw3 equ 06h ;
sw_on equ 07h ;有键按下
;------------------------------------
keys equ 08h ;哪个键按下?
flags equ 09h ;the bits for :sec d0. min d1 .hrs d2.chg ;d3.sw1.sw2.sw3 d6 .swon d7
display equ 0bh ;new display
digit1 equ 0ch ;a0
digit2 equ 0dh ;a1
digit3 equ 0eh ;a2
digit4 equ 0fh ;a3 led
;---------------------------------------
sec_nth equ 10h ;秒计数单元
seconds equ 11h ;秒
minutes equ 12h ;分
hours equ 13h ;时
var equ 14h ;运算变量
count equ 15h
count1 equ 16h

start
movlw 0x03
option
movlw 0
tris port_a
tris port_b
movlw blank
movwf port_b
bcf status,pa1
bcf status,pa0
;----------------------------------------
movlw 0x01
movwf rtcc
movlw 0xfe
movwf display
movlw blank
movwf digit1
movwf digit2
movwf digit3
movwf digit4
movlw maxnths ;d12
movwf sec_nth
movlw maxsecs ;d196
movwf seconds
movlw maxmins
movwf minutes
movlw 0xff
movwf hours
movlw 00h
movwf flags
main

rtcc_fill
movf rtcc,0
btfss status,z
goto rtcc_fill
incfsz sec_nth,1
goto time_done
movlw maxnths
movwf sec_nth
check_sw
btfss flags,sw_on
goto set_time
btfsc flags,sw1
goto set_time
movlw maxsecs ;d196
movwf seconds
movlw 0x7f
movwf sec_nth
btfss flags,sw2
goto hourset
movlw 0xaf
movwf sec_nth
incfsz minutes,1
goto hourset
movlw maxmins ;d196
movwf minutes
hourset
btfsc flags,sw2
goto check_time
incfsz hours,1
goto check_time
movlw maxhrs
movwf hours
goto check_time
set_time
bsf flags,sec
bsf flags,chg
incfsz seconds,1
goto time_done
movlw maxsecs
movwf seconds
bsf flags,min
bsf flags,chg
movlw adjmin
subwf sec_nth,1
incfsz minutes,1
goto time_done
movlw maxmins ;0xc4
movwf minutes
bsf flags,hrs
bsf flags,chg
movlw adjhr ;0x22
addwf sec_nth
incfsz hours,1
goto time_done
movlw maxhrs ;0xf4
movwf hours
movlw adjday ;0x03
subwf sec_nth,1
time_done
btfss flags,chg
goto cycle
check_seconds
btfss flags,sw1
goto check_time
movlw 0x00
movwf digit2
movwf digit3
movwf digit4
movlw maxsecs ;0xc4
subwf seconds,0
movwf digit1
goto split_hex
check_time
movlw 0x00
movwf digit2
movwf digit4
movlw minhrs ;0xf3
subwf hours,0
movwf digit3
movlw maxmins ;0xc4
subwf minutes,0
movwf digit1
split_hex
movlw 0x02
movwf count
movlw digit1
movwf fsr
goto loop
loop2
movlw digit3
movwf fsr
loop movlw 0x0a
subwf pointer,1
btfsc status,c
goto increment_10s
addwf pointer,1
goto next_digit
increment_10s
incf fsr,1
incf pointer,1
decf fsr,1
goto loop
next_digit
decfsz count,1
goto loop2
convert_hex_to_display
movlw digit1
movwf fsr
movlw 0x04
movwf count
next_hex
movf pointer,0
call return_code
movwf pointer
incf fsr,1
decfsz count,1
goto next_hex
fix_display
movlw zero
subwf digit4,0
btfss status,z
goto fix_sec
movlw blank
movwf digit4
fix_sec
btfss flags,sw1
goto clear_flags
movwf digit3
clear_flags
movlw 0xf0
andwf flags,1
cycle
movlw dispoff
movwf port_a
movlw switch
tris port_b
movlw 0x0f
andwf flags,1
nop
nop
nop
movf port_b,0
movwf var
btfss var,1
goto switch2
bsf flags,chg
bsf flags,sw1
bsf flags,sw_on
switch2
btfss var,2
goto switch3
bsf flags,chg
bsf flags,sw2
bsf flags,sw_on
switch3
btfss var,3
goto switch3
bsf flags,chg
bsf flags,sw3
bsf flags,sw_on
setport
movlw 0x00
tris port_b
movlw blank
movwf port_b
btfss display,0
movf digit4,0
btfss display,1
movf digit3,0
btfss display,2
movf digit2,0
btfss display,3
movf digit1,0
movwf port_b
btfsc sec_nth,7
bsf port_b,0
movf display,0
movwf port_a
movwf display
rlf display,1
bsf display,0
btfss display,4
bcf display,0
goto main
return_code
addwf pc,1
retlw zero ;0
retlw one
retlw two
retlw three
retlw four ;4
retlw five
retlw six
retlw seven
retlw eight
retlw nine ;9
org PIC54
goto start


end

-------------PIC单片机www.pic16.com-----------