Hi everyone, as a final addition to my 20M2 digital clock project, i would like the ability to set the time manually using push button switches in case power is lost, eliminating the need to reprogram the chip. I have posted my code below but cannot seem to figure out where the problems are. The two problems I am experiencing are: 1) Whenever I click on the set time button, I am able to toggle between the minutes (+ and -) and set them with no problem, however when the minute advances with the natural course of time, the 7 segments are not updated until I press the set time switch again. 2) after I have set the minutes, the plus and minus switches should be disabled until I press the set time switch again however they are still functioning.
I am pretty sure the problem lies somewhere within the logic of my loops and subs, but after looking over it again and again, I need a second opinion. Any help would be appreciated. Here is my code:
I have also attached my VSM sim file
I am pretty sure the problem lies somewhere within the logic of my loops and subs, but after looking over it again and again, I need a second opinion. Any help would be appreciated. Here is my code:
Code:
init: pause 500
'#########Create symbols for DS1307 Data, Clock and Latch pins
symbol sclk=c.1
symbol latch=c.3
symbol sdata=c.2
'#########Initialize Variables
symbol bitcounter = b1
symbol outbit = b2
symbol outbyte = b3
symbol counter = b4
symbol seconds = b5
symbol mins = b6
symbol hour = b7
symbol hour12=b8
symbol ampm_bit=b9
symbol digout = b10
symbol bcdmin=b11
symbol bcdmax=b12
symbol bcdnum=b13
symbol work=b14
'#######Create symbols for time setting buttons
symbol settimebutton=pinc.0
symbol incbutton=pinb.4
symbol decbutton=pinb.3
symbol up=0
symbol down=1
'##########Set MR LOW and OE HIGH on 74HC595 Shift Register
symbol MR=c.5
symbol OE=c.4
high MR
low OE
'#############Ensure Data CLK and Data-In are initially low
low sclk
low sdata
i2cslave %11010000, i2cslow, i2cbyte 'Set slave address for DS1307 module
'#################Write initial time to DS1307
let seconds = $55
let mins = $59
let hour=%01110001
writei2c 0,(seconds,mins,hour)
'#############Main Loop
do
if settimebutton=down then
gosub SETTIME
else
gosub DISPLAYTIME
endif
loop
SETTIME:
gosub displaytime
setminute:
do while settimebutton=down loop
bcdmax=$59:bcdmin=$00
do
if incbutton=down then
bcdnum=mins
gosub bcdinc
mins=bcdnum
gosub writemin
do while incbutton=down
loop
end if
if decbutton = down then
bcdnum=mins
gosub bcddec
mins = bcdnum
gosub writemin
do while decbutton=down
loop
endif
if settimebutton=down then exit
loop
gosub displaytime
return
bcdinc:
bcdnum=bcdnum+1
work=bcdnum & $0F
if work=10 then
bcdnum=bcdnum+6
endif
if bcdnum > bcdmax then
bcdnum=bcdmin
endif
return
bcddec:
if bcdnum=bcdmin then
bcdnum=bcdmax
else
bcdnum=bcdnum -1
work=bcdnum & $0F
if work >=$0F then
bcdnum=bcdnum - 6
endif
end if
return
writemin:
writei2c 1,(mins)
return
DISPLAYTIME:
'do
readi2c 0,(seconds, mins, hour)
hour12 = b7 and $1F 'Extract 12 hour time from hour byte
ampm_bit=b7 and $20 'Extract AM/PM bit from hour byte
if ampm_bit=32 then 'Test AM/PM bit and turn on associated LED
low b.1
else
high b.1
endif
'###########Convert BCD values to ASCII
bcdtoascii seconds,b14,b15 'SECONDS
bcdtoascii mins,b16,b17 'MINS
bcdtoascii hour12,b18,b19 'HOURS
'###########Convert ASCII values to Decimal
b14=b14 - $30
b15=b15 - $30
b16=b16 - $30
b17=b17 - $30
b18=b18 - $30
b19=b19 - $30
digout = b17
gosub shift595
digout=b16
gosub shift595
pulsout latch,1
pause 50
return
'loop
'#########Subroutine to push data bits to shift registers
shift595:
lookup digout, ($FC,$60,$DA,$F2,$66,$B6,$BE,$E0,$FE,$E6), outbyte
for bitcounter = 0 to 7
outbit = outbyte & 128
if outbit = 128 then
high sdata
else
low sdata
endif
pulsout sclk, 1
outbyte=outbyte * 2
next bitcounter
return
Attachments
-
133.2 KB Views: 13