The Museum of HP Calculators

HP Articles Forum

[Return to the Index ]
[ Previous | Next ]


RPN Sunrise / Sunset programs for HP41C/HP42S/HP33S/HP35S

Posted by M. Joury on 14 Dec 2011, 9:25 a.m.

Following are a set of sunrise/sunset programs that I have developed for the HP41/HP42/HP33S/HP35S calculators. Comments are suggestions are welcome.

HP-41C and HP42S (with modifications to date routines) program to display sunrise / sunset for a given date

Usage:

Using local labels:

A = sunrise sunset for today (not available on HP42S since this routine assumes that availability of a Time module) B = sunrise / sunset for tomorrow (not available on HP42S since this routine assumes that availability of a Time module) C = sunrise / sunset for date in X register. Date should be provided in current DMY / MDY format either as dd.mmyyyy or as mm.ddyyyy respectively. Correct handling for dates in either format is dependent on the DOY routine presented in this article. D = Flip daylight savings time mode. Flag 01 clear = standard time / Flag 01 set = daylight savings time. E = Flip display mode. Flag 00 clear = single line display / Flag 00 set = multiline display

After running the program the following results are on the stack.

Z: Sunset time Y: Sunrise time X: Length of day

=============================================================================================== Main HP41C program

01 LBL SUN 02 LBL A Local label execute for current date 03 DATE Get current date 04 GTO 03 Jump to main routine 05 LBL B Local label execute for current date + 1 day 06 DATE Get current date 07 1 08 DATE+ Add 1 day to current date 09 LBL C Local label execute for date in X register 10 LBL 03 Used for main routine for performance reasons 11 DEG Set degree mode 12 FIX 2 Set FIX with 2 decimal places (HH:MM) 13 CF 02 Clear sunrise / sunset setting (calc sunrise first) 14 XEQ 'DOY Get day of year. Separate routine 15 STO 05 Store day of year 16 -5 UTC offset 17 STO 03 18 90.83333333 Zenith value for sunrise. Change for twilight times 19 STO 06 20 42.24157 Latitude. Change to match location 21 STO 01 22 -83.75537 Longitude (west is negative). Change to match location 23 STO 02 24 15 25 / Longitude Hour Angle 26 STO 07 27 XEQ 01 Compute sunrise / sunset depending on flag 02 28 STO 15 Save first computation (sunrise / sunset) 29 SF 02 Set to compute sunset 30 XEQ 01 Compute sunrise / sunset depending on flag 02 31 RCL 15 Place first result in stack (Y: sunset / X: sunrise) 32 FS 00 Which display routine has been requested? 33 GTO 00 Go to multiline routine 34 CLA Clear ALPHA register 35 ATIME Take time from X and format into ALPHA (sunrise) 36 }-R:S- Append '-R:S-' to ALPHA 37 X<>Y Get second result (Y: sunrise / X: sunset) 38 ATIME Take time from X and format into ALPHA (sunset) 39 X<>Y Restore initial order (Y: sunset / X: sunrise) 40 RCL Y Recall sunset 41 RCL Y Recall sunrise 42 HMS- Compute day length (Z: sunset / Y: sunrise / X: length 43 BEEP Alert that we have an answer 44 AVIEW Display result as a single line (no day length) 45 RTN Backspace to see day length in the X register 46 LBL 00 Multiline display routine 47 SF 21 Set to pause on AVIEW when no printer present 48 'RISE: ' 49 ATIME Append sunrise time 50 AVIEW 51 'SET: ' 52 X<>Y Bring sunset into X 53 ATIME Append sunset time 54 X<>Y Restore original order 55 AVIEW 56 RCL Y Recall sunset 57 RCL Y Recall sunrise 58 HMS- 59 'LENGTH: ' 60 ATIME Append day length 61 AVIEW 62 RTN 63 LBL 01 Main sunrise / sunset routine 64 6 65 FS? 02 Clear = compute sunrise / set = compute sunset 66 18 67 RCL 07 68 • 69 24 70 / 71 RCL 05 72 + 73 STO 08 74 .9856 75 x 76 3.289 77 • 78 STO 09 79 SIN 80 1.916 81 x 82 RCL 09 83 + 84 2 85 RCL 09 86 x 87 SIN 88 .02 89 x 90 + 91 282.634 92 + 93 360 94 MOD 95 STO 10 96 TAN 97 .91764 98 x 99 ATAN 100 360 101 MOD 102 STO 11 103 RCL 10 104 90 105 / 106 XEQ 'FLOOR Floor function. External program 107 90 108 x 109 + 110 RCL 11 111 90 112 / 113 XEQ 'FLOOR Floor function. External program 114 90 115 x 116 • 117 15 118 / 119 STO 11 120 RCL 10 121 SIN 122 .39782 123 x 124 STO 12 125 ASIN 126 COS 127 STO 13 128 RCL 06 129 COS 130 RCL 01 131 SIN 132 RCL 12 133 x 134 • 135 RCL 01 136 COS 137 RCL 13 138 x 139 / 140 ACOS 141 FS?C 02 Clear = compute sunrise / set = compute sunset 142 GTO 02 143 360 144 X<>Y 145 • 146 LBL 02 147 15 148 / 149 RCL 11 150 + 151 .06571 152 RCL 08 153 x 154 • 155 6.622 156 • 157 24 158 MOD 159 RCL 07 160 • 161 RCL 03 162 + 163 FS? 01 Check for daylight savings 164 1 165 FS? 01 Check for daylight savings 166 + 167 HMS 168 RTN 169 LBL D Set daylight savings mode. Flag 01 set when in effect 170 FC?C 01 171 SF 01 172 RTN 173 LBL E Set display mode. 174 FC?C 00 F00 Clear = single line / F00 set = multiline 175 SF 00 176 END

=============================================================================================== CEIL and FLOOR functions used by the HP41C version. You only need the FLOOR code

01 LBL 'CEIL 02 INT 03 X<>Y 04 X<> L 05 X>Y? 06 ISG Y 07 LBL 00 08 X<> L 09 X<>Y 10 RTN 11 LBL 'FLOOR 12 INT 13 X<>Y 14 X<> L 15 X<Y? 16 DSE Y 17 LBL 00 18 X<> L 19 X<>Y 20 END

=============================================================================================== DOY (Day Of Year) routine used by the HP41C version. Note that as mentioned elsewhere the 41C version assumes that a Time module is available.

01 LBL 'DOY 02 ENTER^ 03 ENTER^ 04 1 E2 05 x 06 FRC 07 .0001 08 • 09 FC? 31 MDY mode 10 1231 11 FS? 01 DMY mode 12 3112 13 + 14 1 E2 15 / 16 X<>Y 17 DDAYS 18 END

=============================================================================================== Date calculation routines for HP42S. Can also be used for an HP41C or CV without a Time module.

A call to DOY with the date (MM.DDYYYY) in the X register returns the day of the year. A call to DDAYS with date in Y amd X registers returns the number of days between dates.

01 LBL "DtCalc" 02 LBL "DOY" 03 LBL A

The following two lines can be replaced with RCL ST X

04 ENTER Push date onto stack 05 ENTER 06 1E2 Get rid of MM.DD 07 x by shifting left and keeping 08 FP the floating portion (year) 09 0.0001 Year = year - 1 10 - 11 1E2 Shift year back to the right 12 / 13 12.31 Add 12.31 to make this the 14 + last day of the previous year 15 X<>Y 16 LBL "DDAYS" 17 LBL B 18 STO "DATE" 19 X<>Y 20 XEQ 90 Get day number for first date 21 STO 01 22 RCL "DATE" 23 XEQ 90 24 RCL- 01 25 RTN 26 LBL "DN" 27 LBL D 28 LBL 90 29 ENTER 30 IP 31 9 32 + 33 12 34 MOD 35 STO "MONTH" 36 X<>Y 37 ENTER 38 FP 39 1E2 40 x 41 IP 42 STO "DAY" 43 RDN 44 1E2 45 x 46 FP 47 1E4 48 x 49 RCL "MONTH" 50 10 51 / 52 IP 53 - 54 STO "YEAR" 55 365 56 x 57 RCL "YEAR" 58 4 59 / 60 IP 61 + 62 RCL "YEAR" 63 1E2 64 / 65 IP 66 - 67 RCL "YEAR" 68 400 69 / 70 IP 71 + 72 RCL "MONTH" 73 306 74 x 75 5 76 + 77 10 78 / 79 IP 80 + 81 RCL "DAY" 82 1 83 - 84 + 85 END

=============================================================================================== Flag and register usage and notes.

Flag Purpose Comments 00 Choose display mode Clear = single line / Set = multi line 01 Daylight savings time Clear = standard time / Set = DST 02 Sunrise or sunset Clear = compute sunrise / Set = sunset

Register Usage 33S / 35S / 39gs 41C / 42S A 01 Latitude O 02 Longitude U 03 Offset from UTC (local time) D 04 Date (not really required) N 05 Days from the beginning of the year. Z 06 Zenith G 07 Longitude -> Hour Angle (lngHour) T 08 Approximate time (t) M 09 Sun's mean anomaly (M) L 10 Sun's true longitude (L) R 11 Right Ascension (RA) S 12 Sun's declination (sinDec) C 13 Sun's declination (cosDec) H 14 Sun's local hour angle (H) B 15 Stores first result (rise or set)

Zenith for Sunrise Values Official Sunrise 90 degrees 50' 90.833333333333 Civil Twilight 96 degrees 96.0000 Nautical Twilight 102 degrees 102.0000 Astronomical Twilight 108 degrees 108.0000

Some Lat / Long Values City Latitude Longitude Albuquerque, NM 35.18066 -106.49527 Ann Arbor, MI 42.24157 -83.75537

Notes:

The HP-41C version depends on having the Time Module available for full functionality.

This version has a few tweaks that are not available in the 33S / 35S version. These are as follows:

1. Sunrise / sunset for today (LBL A) 2. Sunrise / sunset for tomorrow (LBL B) 3. Two different display options 4. Day length 5. Daylight savings time without a code (UTC offset) change

If desired item 4 can be easily retrofitted to the 33S and 35S versions. However, it is probably easier to simply have the user manually set a flag rather than have a short routine to do so.

If desired this program can be cut back to the functionality of the 33S and 35S versions. This might be desirable if memory space is at a premium. The features above can all be removed relatively easily.

1. Sunrise / sunset for today: Remove LBL A. Code between lines 02 and 04 inclusive. 2. Sunrise / sunset for tomorrow: Remove LBL B. Code between lines between 05 and 08 inclusive. 3. If both changes above are made LBL 03 (line 10) can be removed. With these changes it might also make sense to replace LBL C with LBL A or remove local labels completely. 4. The logic controlling the display of results is contained in lines 32-61 inclusive. Choose either approach or neither and just leave the results on the stack. The logic that calculates the day length is also included in this code. This is calculated in both display routines. If this logic is to be retained keep either the set of lines 40-42 or the lines 56-58. 5. If one or both display options are removed also remove the logic that switches between them. This is LBL E and can be found at lines 173-175. It is also possible to keep the display options but remove this code and set flag 00 manually. 6. If the logic that handles Daylight Savings Time is not needed it can also be removed. The code can be found at lines 163-166. 7. If the DST logic is removed also remove the logic that switches between standard time and DST. This is LBL D and can be found at lines 169-172. As with the display option selection code this selection code can be removed and flag 01 can be set manually.

A further optional improvement may be added. If you want to see the date for which sunrise / sunset times are being calculated add the following code right after LBL 03:

11 CF 21 Don't stop on AVIEW when printer is absent 12 CLA Clear ALPHA register 13 ADATE Format and append date from X into ALPHA 14 AVIEW View date during execution

=============================================================================================== Following are versions for the HP33S and HP35S. Note that both version require the date routines at the bottom of this article.

=============================================================================================== Programs for HP33S single label followed by short form

Single label

C0001 LBL C C0002 DEG C0003 CF 0 C0004 XEQ X C0005 STO N C0006 -5 C0007 STO U C0008 90.83333333 C0009 STO Z C0010 42.24157 C0011 STO A C0012 -83.75537 C0013 STO O C0014 15 C0015 / C0016 STO G C0017 SF 0 If flag 0 is set we are calculating sunset C0018 XEQ C028 Calculate sunset first C0019 STO B Save sunset time C0020 CF 0 C0021 XEQ C028 C0022 RCL B C0023 SF 10 C0024 SUNRISE-SUNSET C0025 CF 10 C0026 CF 0 C0027 RTN C0028 6 C0029 FS? 0 If set use 18 for sunset calculation C0030 18 C0031 RCL- G C0032 24 C0033 / C0034 RCL+ N C0035 STO T C0036 0.9856 C0037 x C0038 3.289 C0039 1.   C0040 STO M C0041 SIN C0042 1.916 C0043 x C0044 RCL+ M C0045 2 C0046 RCLx M C0047 SIN C0048 0.02 C0049 x C0050 + C0051 3 These lines can be removed from both C0052 RCLx M this program and the single label version C0053 SIN they have an extremely small influence on C0054 0.0003 the result. C0055 x C0056 + C0057 282.634 C0058 + C0059 360 C0060 RMDR C0061 STO L C0062 TAN C0063 0.91764 C0064 x C0065 ATAN C0066 360 C0067 RMDR C0068 STO R C0069 RCL L C0070 90 C0071 / C0072 INTG C0073 90 C0074 x C0075 + C0076 RCL R C0077 90 C0078 / C0079 INTG C0080 90 C0081 x C0082 1.   C0083 15 C0084 / C0085 STO R C0086 RCL L C0087 SIN C0088 0.39782 C0089 x C0090 STO S C0091 ASIN C0092 COS C0093 STO C C0094 RCL Z C0095 COS C0096 RCL A C0097 SIN C0098 RCLx S C0099 1.   C0100 RCL A C0101 COS C0102 RCLx C C0103 / C0104 ACOS C0105 FS? 0 C0106 GTO C110 C0107 360 C0108 X<>Y C0109 1.   C0110 15 C0111 / C0112 RCL+ R C0113 0.06571 C0114 RCLx T C0115 1.   C0116 6.622 C0117 1.   C0118 24 C0119 RMDR C0120 RCL- G C0121 RCL+ U C0122 ->HMS C0123 RTN

Short form

C0001 LBL C C0002 DEG C0003 CF 0 C0004 XEQ X C0005 STO N C0006 -5 C0007 STO U C0008 90.83333333 C0009 STO Z C0010 42.24157 C0011 STO A C0012 -83.75537 C0013 STO O C0014 15 C0015 / C0016 STO G C0017 SF 0 If flag 0 is set we are calculating sunset C0018 XEQ V Calculate sunset first C0019 STO B Save sunset time C0020 CF 0 C0021 XEQ V C0022 RCL B C0023 SF 10 C0024 SUNRISE-SUNSET C0025 CF 10 C0026 CF 0 C0027 RTN V0001 LBL V V0002 6 V0003 FS? 0 If set use 18 for sunset calculation V0004 18 V0005 RCL- G V0006 24 V0007 / V0008 RCL+ N V0009 STO T V0010 0.9856 V0011 x V0012 3.289 V0013 1.   V0014 STO M V0015 SIN V0016 1.916 V0017 x V0018 RCL+ M V0019 2 V0020 RCLx M V0021 SIN V0022 0.02 V0023 x V0024 + V0025 3 These lines can be removed from both V0026 RCLx M this program and the single label version V0027 SIN they have an extremely small influence on V0028 0.0003 the result. V0029 x V0030 + V0031 282.634 V0032 + V0033 360 V0034 RMDR V0035 STO L V0036 TAN V0037 0.91764 V0038 x V0039 ATAN V0040 360 V0041 RMDR V0042 STO R V0043 RCL L V0044 90 V0045 / V0046 INTG V0047 90 V0048 x V0049 + V0050 RCL R V0051 90 V0052 / V0053 INTG V0054 90 V0055 x V0056 1.   V0057 15 V0058 / V0059 STO R V0060 RCL L V0061 SIN V0062 0.39782 V0063 x V0064 STO S V0065 ASIN V0066 COS V0067 STO C V0068 RCL Z V0069 COS V0070 RCL A V0071 SIN V0072 RCLx S V0073 1.   V0074 RCL A V0075 COS V0076 RCLx C V0077 / V0078 ACOS V0079 FS? 0 V0080 GTO W V0081 360 V0082 X<>Y V0083 1.   W0001 LBL W W0002 15 W0003 / W0004 RCL+ R W0005 0.06571 W0006 RCLx T W0007 1.   W0008 6.622 W0009 1.   W0010 24 W0011 RMDR W0012 RCL- G W0013 RCL+ U W0014 ->HMS W0015 RTN

=============================================================================================== Programs for HP35S short form. Single label version is identical to HP33S single label

C0001 LBL C C0002 DEG C0003 CF 0 C0004 XEQ X C0005 STO N C0006 -5 C0007 STO U C0008 90.83333333 C0009 STO Z C0010 42.24157 C0011 STO A C0012 -83.75537 C0013 STO O C0014 15 C0015 / C0016 STO G C0017 SF 0 If flag 0 is set we are calculating sunset C0018 XEQ C028 Calculate sunset first C0019 STO B Save sunset time C0020 CF 0 C0021 XEQ C028 C0022 RCL B C0023 SF 10 C0024 SUNRISE-SUNSET C0025 CF 10 C0026 CF 0 C0027 RTN C0028 6 C0029 FS? 0 If set use 18 for sunset calculation C0030 18 C0031 RCL- G C0032 24 C0033 / C0034 RCL+ N C0035 STO T C0036 0.9856 C0037 x C0038 3.289 C0039 1.   C0040 STO M C0041 SIN C0042 1.916 C0043 x C0044 RCL+ M C0045 2 C0046 RCLx M C0047 SIN C0048 0.02 C0049 x C0050 + C0051 3 These lines can be removed from both C0052 RCLx M this program and the single label version C0053 SIN they have an extremely small influence on C0054 0.0003 the result. C0055 x C0056 + C0057 282.634 C0058 + C0059 360 C0060 RMDR C0061 STO L C0062 TAN C0063 0.91764 C0064 x C0065 ATAN C0066 360 C0067 RMDR C0068 STO R C0069 RCL L C0070 90 C0071 / C0072 INTG C0073 90 C0074 x C0075 + C0076 RCL R C0077 90 C0078 / C0079 INTG C0080 90 C0081 x C0082 1.   C0083 15 C0084 / C0085 STO R C0086 RCL L C0087 SIN C0088 0.39782 C0089 x C0090 STO S C0091 ASIN C0092 COS C0093 STO C C0094 RCL Z C0095 COS C0096 RCL A C0097 SIN C0098 RCLx S C0099 1.   C0100 RCL A C0101 COS C0102 RCLx C C0103 / C0104 ACOS C0105 FS? 0 C0106 GTO C110 C0107 360 C0108 X<>Y C0109 1.   C0110 15 C0111 / C0112 RCL+ R C0113 0.06571 C0114 RCLx T C0115 1.   C0116 6.622 C0117 1.   C0118 24 C0119 RMDR C0120 RCL- G C0121 RCL+ U C0122 ->HMS C0123 RTN

=============================================================================================== Date routines for HP33S and HP35S

There are two routines included here. LBL X takes a date in the form of MM.DDYYYY and returns the day number for the year. LBL Y takes two dates (MM.DDYYYY in X and Y and returns the number of days between them.

X0001 LBL X Calculate day of year given date X0002 ENTER X0003 ENTER X0004 100 Get first date by year - 1 day X0005 x X0006 FP X0007 0.0001 X0008 1.   X0009 100 X0010 / X0011 12.31 X0012 + X0013 X<>Y Y0001 LBL Y Given two dates (on stack) get days between Y0002 STO Z Save second date Y0003 X<>Y Y0004 XEQ Z Get day number for first date Y0005 STO A Save day number for first date Y0006 RCL Z Get second date Y0007 XEQ Z Get day number for second date Y0008 RCL- A Subtract day number 1 from day number 2 Y0009 RTN Z0001 LBL Z Calculate day number Z0002 ENTER Z0003 IP Z0004 9 Z0005 + Z0006 12 Z0007 RMDR (MOD) Z0008 STO M Save month value Z0009 X<>Y Z0010 ENTER Z0011 FP Z0012 100 Z0013 x Z0014 IP Z0015 STO D Save day value Z0016 RDN Z0017 100 Z0018 x Z0019 FP Z0020 10,000 Z0021 x Z0022 RCL M Get month Z0023 10 Z0024 INT/ Z0025 1.   Z0026 STO Y Save year value Z0027 365 Z0028 x Z0029 RCL Y Get year value Z0030 4 Z0031 INT/ Z0032 + Z0033 RCL Y Z0034 100 Z0035 INT/ Z0036 1.   Z0037 RCL Y Z0038 400 Z0039 INT/ Z0040 + Z0041 RCL M Get month value Z0042 306 Z0043 x Z0044 5 Z0045 + Z0046 10 Z0047 INT/ Z0048 + Z0049 RCL D Get day value Z0050 1 Z0051 1.   Z0052 + Z0053 RTN

Password:

[ Return to the Message Index ]

Go back to the main exhibit hall