-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.jal
114 lines (90 loc) · 3.08 KB
/
clock.jal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
VAR BYTE*8 clock_absolute = 0 -- range 0 - 2**64
VAR BYTE*2 clock_millisecond = 0 -- range 0 - 999
VAR BYTE clock_second = 0 -- range 0 - 59
VAR BYTE clock_minute = 0 -- range 0 - 59
VAR BYTE clock_hour = 12 -- range 0 - 23
VAR BYTE clock_weekday = 3 -- range 0 - 6 sun - sat
VAR BYTE*2 clock_doy = 73 -- range 0 - 366
VAR BYTE*3 clock_mjd = 56000
VAR BYTE*2 clock_year = 2012 -- range 1800 - 2**16
-- (clock defaults to 2012-03-16 Tue, aka MJD 56000)
function is_leap_year(byte*2 in year) return bit is
if (year & 0b11) == 0b00 then -- divisible by 4.
if (year % 100) != 0 then -- unless divisible by 100
return true
else
if (year % 400) == 0 then -- but if divisible by 400
return true
end if
end if
end if
return false
end function
const byte*2 leapyears[] = {
2012, 2016, 2020, 2024,
2028, 2032, 2036, 2040,
2044, 2048, 2052, 2056,
2060, 2064, 2068, 2072,
2076, 2080, 2084, 2088,
2092, 2096, 2104, 2108
};
function clock_leap_year(byte*2 in year) return bit is
var byte idx
for count(leapyears) using idx loop
if year == leapyears[idx] then
return true
end if
end loop
return false
end function
function clock_year_len(byte*2 in year) return byte*2 is
if clock_leap_year(year) then
return 366
else
return 365
end if
end function
PROCEDURE timer0_isr_wedge() IS
pragma inline
-- This is called in the Timer0 ISR, yielding 1ms precision
clock_millisecond = clock_millisecond + 1
clock_absolute = clock_absolute + 1
IF clock_millisecond > 999 THEN
clock_millisecond = 0
clock_second = clock_second + 1
IF clock_second > 59 THEN
clock_second = 0
clock_minute = clock_minute + 1
IF clock_minute > 59 THEN
clock_minute = 0
clock_hour = clock_hour + 1
IF clock_hour > 23 THEN
clock_hour = 0
clock_mjd = clock_mjd + 1
clock_weekday = clock_weekday + 1
IF clock_weekday > 6 THEN
clock_weekday = 0
END IF
clock_doy = clock_doy + 1
if clock_doy > (clock_year_len(clock_year)) then
clock_doy = 1
clock_year = clock_year + 1
end if
END IF
END IF
END IF
END IF
END PROCEDURE
procedure print_time is
-- 2012-000 Wed 00:00:00.000
--print_string("MJD")
--format_dword_dec(usb_serial_data, clock_mjd, 6, 0)
--usb_serial_data = " "
format_word_dec(usb_serial_data, clock_year, 4, 0)
usb_serial_data = "-"
format_word_dec(usb_serial_data, clock_doy, 3, 0)
usb_serial_data = " "
format_time_hms(usb_serial_data, clock_hour, clock_minute, clock_second)
usb_serial_data = "."
format_word_dec(usb_serial_data, clock_millisecond, 3, 0)
end procedure