PDA

View Full Version : Urgent assistance rqd - RTC stripdown



Scampy
- 15th June 2015, 23:16
Guys

I've somehow managed to exceed the memory available in the PIC, which given the number of include files that are used It's quite possible.

Anyway, I'm trying to strip out a lot of the bloat in the code, the biggest part is the RTC routine, which was originally from DT when I was developing the original code way back when I was more of a green around the gills than I am now. But I'm not sure if I strip it down to just the time settings that I will screw up completely.



RTClock:

Data @150,74,97,110,70,101,98,77,97,114,65,112,114
' Jan Feb Mar Apr
Data 77,97,121,74,117,110,74,117,108,65,117,103
' May Jun Jul Aug
Data 83,101,112,79,99,116,78,111,118,68,101,99
' Sep Oct Nov Dec
Data 84,117,101,87,101,100,84,104,117,70,114,105
' Tue Wed Thu Fri
Data 83,97,116,83,117,110,77,111,110
' Sat Sun Mon

' Start Program
' =============
goto JumpStart

' Subroutine Converts back to BCD
' -------------------------------

ConvertBCD:
CounterB=CounterA DIG 1 ' CounterA is the entry variable
CounterB=CounterB<<4 ' CounterB holds the converted BCD result on exit
CounterB=CounterB+CounterA DIG 0
Return

' Subroutine Displays Month at current Cursor Position
' ----------------------------------------------------

DisplayMonth: ' CounterB holds Month (1-12) this value is destroyed by the routine, additionally uses CounterA and CounterD variables
CounterB=CounterB*3-3 ' Convert BCD Month to EEPROM Start Address
DisplaySequence:
For CounterA=CounterB to CounterB+2
Read (CounterA+150),CounterD ' Read and Display Month
LCDOut CounterD
Next CounterA
Return

' Subroutine works out Number of Days in the Month
' ------------------------------------------------
' SetYear - entry variable containing year (0-99)
' SetMonth - entry Variable Containing Month (1-12)
' CounterA - exits with number of days
FindDays:
LookUp SetMonth -1,[31,28,31,30,31,30,31,31,30,31,30,31],CounterA ' this line gives the 'usual' days of the Month
' Section below adjusts for Leap-Year
If SetMonth=2 then
If (SetYear&$03)=0 then CounterA=29
endif
Return

' Subroutine waits until user reseases Set Button
' -----------------------------------------------
SetButtonRelease:
LCDOut $FE,1
While SetButton=0:Wend
Pause 200 ' Small pause to kill any Key-Bounce
Return

' End of Subroutine Nest Area
' ---------------------------

' Actual Start of Real Program
' ============================
JumpStart:

Pause 200 ' Timeout for LCD to settle

' Time & Date Display Loop
' ========================
ReDisplay:
LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250 ' Initialize LCD
ReDisplayLoop:
' Read RTC
' --------
I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCY ear,RTCCtrl]

' Decide if Setup Required/Wanted
' -------------------------------
If RTCSec.7=1 then goto SetUpPreset

If SetButton=0 then ' If Set Button is pressed, User will be taken to the Setup Menu.
Gosub SetButtonRelease
goto Setup
endif
If DecButton=0 then
Gosub SetButtonRelease
goto mainmenu
endif '
' Display RTC
' -----------

' Display Time on Line 1
' ----------------------
LCDOut $FE,$80
If RTCHour.6=1 then ' Work-Out 12 or 24 hour Display for Hours
CounterA=(RTCHour>>4)&$01
else
CounterA=(RTCHour>>4)&$03
endif
CounterA=CounterA*10+(RTCHour&$0F)
If RTCHour.6=1 then ' Display Hours appropriately for 12 or 24 hour Mode
LCDOut #CounterA
else
LCDOut #CounterA Dig 1,#CounterA Dig 0
endif
LCDOut ":",#(RTCMin>>4)&$0F,#RTCMin&$0F,":"
LCDOut #(RTCSec>>4)&$0F,#RTCSec&$0F," "
IF RTCHour.6=1 then
If RTCHour.5=1 then
LCDOut "PM"
else
LCDOut "AM"
endif
endif
' Display Day of Week on Line 2
' -----------------------------
LCDOut " ",$FE,$C0
CounterB=RTCWDay*3+33 ' Convert BCD WeekDay to EEPROM Start Address
Gosub DisplaySequence ' Display 3 character sequence from EEPROM
' Display Date on Line 2
' ----------------------
LCDOut " ",#(RTCDay>>4)&$0F,#RTCDay&$0F," "
CounterB=((RTCMonth>>4)&$0F)*10+(RTCMonth&$0F)
Gosub DisplayMonth
LCDOut " 20",#(RTCYear>>4)&$0F,#RTCYear&$0F,$FE,$80
Pause 250
Goto ReDisplayLoop

' Clock and Calandar Settings
' ===========================
' Default setting preset to the moment this program first born
'
SetupPreset:
RTCSec=$00 ' Seconds preset to 00
RTCMin=$00 ' Minutes preset to 00
RTCHour=$14 ' Hours preset to 14'00
RTCWDay=$01 ' Weekday preset to 01
RTCDay=$12 ' Day preset 12
RTCMonth=$06 ' Months preset to June
RTCYear=$15 ' Year preset to 2015
RTCCtrl=$10 ' Control preset to output 1 second 'Tick' on SQWpin
'
' Convert the Current BCD Data to Something easier to Edit
' --------------------------------------------------------
Setup:
SetTime=RTCHour.6 ' Determines 12/24 Hour mode
If SetTime=1 then
SetHour=(RTCHour>>4)&$01
' if 12-hour mode
else
SetHour=(RTCHour>>4)&$03
' if 24 hour mode
endif
SetHour=SetHour*10+(RTCHour&$0F)
If SetTime=1 then
If RTCHour.5=1 then
If SetHour<12 then SetHour=SetHour+12
' Add-in the AM/PM indicator
else
If SetHour=12 then SetHour=0
endif
endif
SetMin=((RTCMin>>4)&$0F)*10+(RTCMin&$0F)
SetSec=((RTCSec>>4)&$0F)*10+(RTCSec&$0F)
SetYear=((RTCYear>>4)&$0F)*10+(RTCYear&$0F)
SetMonth=((RTCMonth>>4)&$0F)*10+(RTCMonth&$0F)
SetDay=((RTCDay>>4)&$0F)*10+(RTCDay&$0F)

' Setup Menu Loop
' ---------------
CounterC=0 ' Set to Start of Setup (seven steps from 0 to 6)
TimeOut=0 ' Zero the Time-Out counter
SetupLoop:
LCDOut $FE,1,"Set "

' First Display the Menu Prompts
' ------------------------------

If CounterC=0 then
LCDOut "Mode"
endif
If CounterC=1 then
LCDOut "Hours"
endif
If CounterC=2 then
LCDOut "Minutes"
endif
If CounterC=3 then
LCDOut "Seconds"
endif
If CounterC=4 then
LCDOut "Year : 20"
endif
If CounterC=5 then
LCDOut "Month"
endif
If CounterC=6 then
LCDOut "Day"
endif
If CounterC<>4 then LCDOut " :"
' Now Display the Data
' --------------------
SetupDisplayLoop:
' 12/24 Hour Option
' -----------------
If CounterC=0 then
LCDOut $FE,$8B
If SetTime=0 then
LCDOut "24HR"
else
LCDOut "12HR"
endif
LCDOut $FE,$8B
endif
' Hours
' -----
' Hours are tricky because of the 12/24 & AM/PM bit
' so we have to reconvert 1-24 back to 1-12 plus AM/PM
If CounterC=1 then
CounterA=SetHour
If SetTime=1 then
LCDOut $FE,$8E
' Display AM/PM bit
If CounterA<12 then
LCDOut "AM"
else
LCDOut "PM"
endif
If CounterA=0 then CounterA=12
If CounterA>12 then CounterA=CounterA-12
endif
LCDOut $FE,$8C,#CounterA
If CounterA<10 then LCDOut " "
LCDOut $FE,$8C
endif
' Minutes
' -------
If CounterC=2 then
LCDOut $FE,$8E,#SetMin
If SetMin<10 then LCDOut " "
LCDOut $FE,$8E
endif
' Seconds
' -------
If CounterC=3 then
LCDOut $FE,$8E,#SetSec
If SetSec<10 then LCDOut " "
LCDOut $FE,$8E
endif
' Year
' ----
If CounterC=4 then
LCDOut $FE,$8D,#SetYear DIG 1,#SetYear Dig 0
LCDOut $FE,$8D
endif
' Month
' -----

If CounterC=5 then
LCDOut $FE,$8C
CounterB=SetMonth
Gosub DisplayMonth
LCDOut $FE,$8C
endif

' Day
' ---
If CounterC=6 then
LCDOut $FE,$8A,#SetDay
If SetDay<10 then LCDOut " "
LCDOut $FE,$8A
endif
' The Data-Entry Bit
' ------------------
SetupEntryLoop:
' Decrement Button Pressed
' ------------------------
If DecButton=0 then
' 12/24 Clock Selection
' ---------------------
If CounterC=0 then
If SetTime=0 then
SetTime=1
else
SetTime=0
endif
endif
' Decrement Hours
' ---------------
If CounterC=1 then
If SetHour=0 then
SetHour=23
else
SetHour=SetHour-1
endif
endif
' Decrement Minutes
' -----------------
If CounterC=2 then
If SetMin=0 then
SetMin=59
else
SetMin=SetMin-1
endif
endif
' Decrement Seconds
' -----------------
' Kinda overkill to include setting seconds,
' but if you got codespace to burn...
If CounterC=3 then
If SetSec=0 then
SetSec=59
else
SetSec=SetSec-1
endif
endif
' Decrement Years
' ---------------
If CounterC=4 then
If SetYear=0 then
SetYear=99
else
SetYear=SetYear-1
endif
endif
' Decrement Month
' ---------------
If CounterC=5 then
If SetMonth=1 then
SetMonth=12
else
SetMonth=SetMonth-1
endif

Gosub FindDays
If SetDay>CounterA then SetDay=CounterA
endif
' Decrement Days
' --------------
If CounterC=6 then
Gosub FindDays
If SetDay=1 then
SetDay=CounterA
else
SetDay=SetDay-1
endif
endif
Pause ButtonRepeat
TimeOut=0
Goto SetUpDisplayLoop
endif
' Increment Button Pressed
' ------------------------

If IncButton=0 then
' 12/24 Clock Selection
' ---------------------
If CounterC=0 then
If SetTime=1 then
SetTime=0
else
SetTime=1
endif
endif
' Increment Hours
' ---------------
If CounterC=1 then
If SetHour=23 then
SetHour=0
else
SetHour=SetHour+1
endif
endif
' Increment Minutes
' -----------------
If CounterC=2 then
If SetMin=59 then
SetMin=0
else
SetMin=SetMin+1
endif
endif
' Increment Seconds
' -----------------
If CounterC=3 then
If SetSec=59 then
SetSec=0
else
SetSec=SetSec+1
endif
endif
'
' Increment Years
' ---------------
If CounterC=4 then
If SetYear=99 then
SetYear=0
else
SetYear=SetYear+1
endif
endif
' Increment Month
' ---------------
If CounterC=5 then
If SetMonth=12 then
SetMonth=1
else
SetMonth=SetMonth+1
endif
Gosub FindDays
If SetDay>CounterA then SetDay=CounterA
endif

If CounterC=6 then
Gosub FindDays
If SetDay=>CounterA then
SetDay=1
else
SetDay=SetDay+1
endif
endif
Pause ButtonRepeat
TimeOut=0
Goto SetupDisplayLoop
endif
' Set Button Pressed
' ------------------
If SetButton=0 then
CounterC=CounterC+1
' Increment Menu Item
TimeOut=0
If CounterC>6 then
' Save Data if all edit items exhaused
LCDOut $FE,1,"Memorised"

' Save 12/24 Hours to BCD DS1307's Format
' ---------------------------------------
CounterA=SetHour
If SetTime=1 then
If CounterA>12 then CounterA=CounterA-12
If CounterA=0 then CounterA=12
endif
Gosub ConvertBCD
RTCHour=CounterB
' Save the Hours Value
If SetTime=1 then
RTCHour.6=1
' Save the 12 Hour Mode Flag
If SetHour=>12 then RTCHour.5=1
' Save the 'PM' Flag
endif
'
' Save Minutes
' ------------
CounterA=SetMin
Gosub ConvertBCD
RTCMin=CounterB
'
' Save Seconds
' ------------
CounterA=SetSec
Gosub ConvertBCD
RTCSec=CounterB
'
' Save Year
' ---------
CounterA=SetYear
Gosub ConvertBCD
RTCYear=CounterB
'
' Save Month
' ----------
CounterA=SetMonth
Gosub ConvertBCD
RTCMonth=CounterB
'
' Save Day
' --------
CounterA=SetDay
Gosub ConvertBCD
RTCDay=CounterB

CounterA=SetYear+4
CounterB=SetMonth
If SetMonth<3 then
CounterA=CounterA-1
CounterB=CounterB+12
endif
CounterD=(SetDay+(153*CounterB-457)/5+365*CounterA+CounterA/4-CounterA/100+CounterA/400+2) MOD 7
RTCWDay=CounterD+1
'
' Do the Business
' ---------------

I2CWrite SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCY ear,RTCCtrl]
Pause 1000
Gosub SetButtonRelease
Goto ReDisplay
endif
Gosub SetButtonRelease
Goto SetupLoop ' Loop for Next Menu Item
endif




All I'm after is the ability to set the hours and minutes for the current time... I don't need seconds, days, weeks months years etc....

Can someone either strip this code down, for me, or come up with a simple version. Do you have to write all the RTC variables in the string to the DS1307 chip ???

Art
- 18th June 2015, 20:24
Are these all the same variable type? You’re not showing variable declarations in the code you posed.


RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCY ear,RTCCtrl

If so, you can make it an array and step through them in the I2C Read and Write
hopefully you can use array elements in PBP I2C commands and it doesn’t stuff up:


for i = 0 to 6
I2CRead SDApin,SCLpin,$D0+i,$00,[RTCvar[i]]
next i



It’s been a while since I used one of the RTCs, but I think you send zero values to fill the command.

EDIT,,
I think this will be slightly cheaper, maybe only a word:


CounterA=(RTCHour>>4)&$03
If RTCHour.6=1 then ' Work-Out 12 or 24 hour Display for Hours
CounterA=(RTCHour>>4)&$01
endif