1 | '---------------------------------------
|
2 | ' project
|
3 | ' Library for reading TSIC digital temperature sensors like 305 and 206
|
4 | '---------------------------------------
|
5 | ' compiler:
|
6 | ' LunaAVR V2015 R1.6 (build 8413)
|
7 | '---------------------------------------
|
8 | ' Systemsettings
|
9 | '---------------------------------------
|
10 | const F_CPU = 14745600
|
11 | avr.Device = atmega32
|
12 | avr.Clock = F_CPU
|
13 | avr.Stack = 48
|
14 |
|
15 | '---------------------------------------
|
16 | ' Macros
|
17 | '---------------------------------------
|
18 | #macro BEGIN_ATOMIC
|
19 | PushByte(avr.SREG)
|
20 | cli
|
21 | #endmacro
|
22 |
|
23 | #macro END_ATOMIC
|
24 | avr.SREG = PopByte()
|
25 | #endmacro
|
26 |
|
27 | '---------------------------------------
|
28 | ' Defines
|
29 | '---------------------------------------
|
30 | #define M_VCC_PIN as portb.0
|
31 | #define M_SIGNAL_PIN as portb.1
|
32 |
|
33 | '---------------------------------------
|
34 | ' Library
|
35 | '---------------------------------------
|
36 |
|
37 | '---------------------------------------
|
38 | ' LCD Display
|
39 | '---------------------------------------
|
40 | #library "Library/Lcd4.interface"
|
41 | lcd4.PinDB4 = porta.3
|
42 | lcd4.PinDB5 = porta.4
|
43 | lcd4.PinDB6 = porta.5
|
44 | lcd4.PinDB7 = porta.6
|
45 |
|
46 | lcd4.PinEN = porta.2
|
47 | lcd4.PinRS = porta.0
|
48 | lcd4.PinRW = porta.1
|
49 |
|
50 | lcd4.init()
|
51 |
|
52 | '---------------------------------------
|
53 | ' Typedef
|
54 | '---------------------------------------
|
55 | #define bool_t as byte
|
56 |
|
57 | '---------------------------------------
|
58 | ' Variable
|
59 | '---------------------------------------
|
60 | dim l_temp as int32
|
61 | dim b_readtemp as bool_t = false
|
62 | dim error_code as byte = 0
|
63 |
|
64 | '---------------------------------------
|
65 | ' setup class
|
66 | '---------------------------------------
|
67 | tsiclib.Init()
|
68 | T0.Init()
|
69 |
|
70 | '---------------------------------------
|
71 | ' Welcome
|
72 | '---------------------------------------
|
73 | avr.Interrupts.enable
|
74 |
|
75 | lcd4.cursor.Set(1,1)
|
76 | ' "0123456789012345"
|
77 | lcd4.TextD "TSIC 206 Modul "
|
78 | lcd4.cursor.Set(2,1)
|
79 | lcd4.TextD " "
|
80 |
|
81 | '---------------------------------------
|
82 | ' main loop
|
83 | '---------------------------------------
|
84 | do
|
85 |
|
86 | if (b_readtemp) then
|
87 | b_readtemp = false
|
88 |
|
89 | l_temp = tsiclib.readTemperature()
|
90 |
|
91 | ' clear old lcddata
|
92 | lcd4.cursor.set(2,1)
|
93 | lcd4.TextD " "
|
94 | lcd4.cursor.set(2,1)
|
95 |
|
96 | ' war das Auslesen erfolgreich ?
|
97 | error_code = tsiclib.readError()
|
98 | if ( error_code = tsiclib.NO_ERROR ) then
|
99 | lcd4.Text format("-000.0", l_temp)
|
100 | Lcd4.Char(223) ' Char '°'
|
101 | lcd4.Char(asc("C"))
|
102 | else
|
103 | lcd4.TextD "Error "
|
104 | lcd4.Text Str(error_code)
|
105 | endif
|
106 |
|
107 | endif
|
108 |
|
109 | loop
|
110 |
|
111 | class T0
|
112 | '---------------------------------------
|
113 | ' Konstanten
|
114 | '---------------------------------------
|
115 | const T0_PRESCALER = 64
|
116 | const T0_FREQUENCY = 1000 ' Hz
|
117 | const T0_PRELOAD = word(1.0 * avr.F_CPU /T0_PRESCALER /T0_FREQUENCY +.5)
|
118 |
|
119 | '---------------------------------------
|
120 | ' public function
|
121 | '---------------------------------------
|
122 | procedure Init()
|
123 | timer0.pwm.mode = 2 ' ctc orc0
|
124 | timer0.value = 0
|
125 | timer0.cmp.isr = t0_cmp_isr
|
126 | avr.TIFR = (1<<OCF0) ' clear old interrupt flag
|
127 | timer0.cmp.enable
|
128 | timer0.cmp.value = T0_PRELOAD-1
|
129 | timer0.clock = T0_PRESCALER
|
130 | endproc
|
131 |
|
132 | '---------------------------------------
|
133 | ' private function
|
134 | '---------------------------------------
|
135 | const MAX_COUNTER = T0_FREQUENCY
|
136 |
|
137 | isr t0_cmp_isr fastauto
|
138 | dim cnt as static word
|
139 | cnt += 1
|
140 | if (cnt >= MAX_COUNTER) then
|
141 | cnt = 0
|
142 | avr.b_readtemp = true
|
143 | endif
|
144 | endisr
|
145 |
|
146 | endclass
|
147 |
|
148 | '---------------------------------------
|
149 | ' load extern class
|
150 | '---------------------------------------
|
151 | #include "class/tsiclib.class.luna"
|