-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS1302.spin
116 lines (90 loc) · 3.24 KB
/
DS1302.spin
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
113
114
115
116
{{ DS1302.spin }}
'
' init : to be called first
' setDatetime( day, mth, year, dow, hour, min ) : set date/time
' readTime( hr, min, sec ) : read time
' readDate( day, mth, year ) : read day, month, year
CON
_1us = 1_000_000 ' Divisor for 1 us (not yet possible)
_10us = 1_000_000 / 10 ' Divisor for 10 us
VAR
long clkcycles, clkcycles_us
byte x, CycleFlag
byte clk
byte io
byte ce
byte datar
PUB init( inClk, inIo, inCe )
clkcycles_us := ( clkfreq / _1us ) #> 381 ' will work better with Propeller II
clk := inClk
io := inIo
ce := inCe
dira[ce]~~ 'set to output
outa[ce]~
delay_us(2)
dira[clk]~~
outa[clk]~
PUB config
write($90,$a6) ' Init chargeing register 1010 0110 charge activée une diode , R=4K
write($8e,0) ' Init write-protect bit
'x := read($85) ' read register
'x &= %01111111 ' reset bit 7 (12/24h cycle)
'write($84,x) ' write 12/24h cycle
write($84,0) ' write 12/24h cycle
x := read($81) ' read clock halt
if x & %10000000 ' If not clock halt
write($80,0) ' enabled clock halt
PRI writeByte( cmd ) | i
dira[io]~~ 'set to output
repeat i from 0 to 7
outa[io] := cmd
cmd >>= 1
outa[clk]~~
delay_us(2)
outa[clk]~
PRI write( cmd, data )
outa[ce]~~
writeByte( cmd )
writeByte( data )
outa[ce]~
PRI read( cmd ) | i
outa[ce]~~
writeByte( cmd )
dira[io]~ 'set to input
datar~
repeat i from 0 to 7
if ina[io] == 1
datar |= |< i ' set bit
outa[clk]~~
delay_us(2)
outa[clk]~
delay_us(2)
outa[ce]~
return(datar)
PUB setDatetime( mth, day, year, dow, hr, xmin, xsec )
write($8c, Convert_Bin_1302( year ) )
write($8a, dow )
write($88, Convert_Bin_1302( mth ) )
write($86, Convert_Bin_1302( day ) )
write($84, Convert_Bin_1302( hr ) )
write($82, Convert_Bin_1302( xmin ) )
write($80, Convert_Bin_1302( xsec ) )
PUB readDate( day, mth, year, dow )
byte[year] := Convert_1302_bin_Years( read($8d) )
byte[mth] := Convert_1302_bin( read($89) )
byte[day] := Convert_1302_bin( read($87) )
byte[dow] := Convert_1302_bin( read($8b) )
PUB readTime( hr, xmin, sec ) | tmp1, tmp2
byte[hr] := Convert_1302_bin( read($85) )
byte[xmin] := Convert_1302_bin( read($83) )
byte[sec] := Convert_1302_bin( read($81) )
pub Convert_1302_bin(dataIn)
result := (dataIn & %00001111) + ((dataIn/16) & %00000111) *10
pub Convert_Bin_1302(dataIn) | tmp
tmp:= dataIn /10
result := dataIn - ( tmp * 10 ) + ( tmp * 16 )
pub Convert_1302_bin_Years(dataIn)
result := (dataIn & %00001111) + ((dataIn/16) & %00001111) *10
PRI delay_us( period )
clkcycles := ( clkcycles_us * period ) #> 381
waitcnt(clkcycles + cnt) ' Wait for designated time