forked from technoblogy/two-digit-thermometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoDigitThermometer.ino
279 lines (241 loc) · 7.3 KB
/
TwoDigitThermometer.ino
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* Two-Digit Thermometer
David Johnson-Davies - www.technoblogy.com - 9th April 2019
ATtiny84 @ 8 MHz (internal oscillator; BOD disabled)
CC BY 4.0
Licensed under a Creative Commons Attribution 4.0 International license:
http://creativecommons.org/licenses/by/4.0/
*/
#include <avr/sleep.h>
#include <avr/power.h>
// Comment out for a common anode display
#define commoncathode
// Seven-segment definitions
uint8_t charArray[] = {
// ABCDEFG Segments
0b1111110, // 0
0b0110000, // 1
0b1101101, // 2
0b1111001, // 3
0b0110011, // 4
0b1011011, // 5
0b1011111, // 6
0b1110000, // 7
0b1111111, // 8
0b1111011, // 9
0b0000000, // 10 Space
0b0000001, // 11 '-'
0b0110001, // 12 '-1'
0b1001111, // 13 'E'
0b0001110, // 14 'L'
0b0011101, // 15 'o'
0b0110111, // 16 'H'
0b0010000, // 17 'i'
0x80, // 18 '.'
};
const int Blank = 10;
const int Minus = 11;
const int Minus1 = 12;
const int Error = 13;
const int Lo = 14;
const int Hi = 16;
const int DP = 18;
volatile char Buffer[] = {2, 2};
// One Wire Protocol **********************************************
// Buffer to read data or ROM code
static union {
uint8_t DataBytes[9];
unsigned int DataWords[4];
};
const int Digit0Pin = 0; // PB0
const int Digit1Pin = 1; // PB1
const int OneWirePin = 2; // PB2
const int ReadROM = 0x33;
const int MatchROM = 0x55;
const int SkipROM = 0xCC;
const int ConvertT = 0x44;
const int ReadScratchpad = 0xBE;
inline void PinLow () {
DDRB = DDRB | 1<<OneWirePin;
}
inline void PinRelease () {
DDRB = DDRB & ~(1<<OneWirePin);
}
// Returns 0 or 1
inline uint8_t PinRead () {
return PINB>>OneWirePin & 1;
}
void DelayMicros (unsigned int micro) {
TCNT1 = 0; TIFR1 = 1<<OCF1A;
OCR1A = micro;
while ((TIFR1 & 1<<OCF1A) == 0);
}
void LowRelease (int low, int high) {
PinLow();
DelayMicros(low);
PinRelease();
DelayMicros(high);
}
void OneWireSetup () {
TCCR1A = 0<<WGM10; // Normal mode
TCCR1B = 0<<WGM12 | 2<<CS10; // Normal mode, divide clock by 8
}
uint8_t OneWireReset () {
uint8_t data = 1;
LowRelease(480, 70);
data = PinRead();
DelayMicros(410);
return data; // 0 = device present
}
void OneWireWrite (uint8_t data) {
int del;
for (int i = 0; i<8; i++) {
if ((data & 1) == 1) del = 6; else del = 60;
LowRelease(del, 70 - del);
data = data >> 1;
}
}
uint8_t OneWireRead () {
uint8_t data = 0;
for (int i = 0; i<8; i++) {
LowRelease(6, 9);
data = data | PinRead()<<i;
DelayMicros(55);
}
return data;
}
// Read bytes into array, least significant byte first
void OneWireReadBytes (int bytes) {
for (int i=0; i<bytes; i++) {
DataBytes[i] = OneWireRead();
}
}
// Calculate CRC over buffer - 0x00 is correct
uint8_t OneWireCRC (int bytes) {
uint8_t crc = 0;
for (int j=0; j<bytes; j++) {
crc = crc ^ DataBytes[j];
for (int i=0; i<8; i++) crc = crc>>1 ^ ((crc & 1) ? 0x8c : 0);
}
return crc;
}
// Display multiplexer **********************************************
uint8_t digit = 0;
void DisplayNextDigit () {
#if defined(commoncathode)
PORTB = PORTB | 1<<digit; // Turn old digit off
digit = digit ^ 1; // Toggle between 0 and 1
char segs = charArray[Buffer[digit]];
PORTA = segs; // Lit segments high
PORTB = PORTB & ~(1<<digit); // Turn new digit on
#else
PORTB = PORTB & ~(1<<digit); // Turn old digit off
digit = digit ^ 1; // Toggle between 0 and 1
char segs = charArray[Buffer[digit]];
PORTA = ~segs; // Lit segments low
PORTB = PORTB | 1<<digit; // Turn new digit on
#endif
}
// Display a two-digit number
void Display (int n) {
int units = n % 10;
int tens = n / 10;
int temp0 = tens;
int temp1 = abs(units);
if (tens < -1) {temp0 = Lo; temp1 = Lo+1; }
else if (tens > 9) {temp0 = Hi; temp1 = Hi+1; }
else if (tens == -1) temp0 = Minus1;
else if ((tens == 0) && (units >= 0)) temp0 = Blank;
else if ((tens == 0) && (units < 0)) temp0 = Minus;
Buffer[0] = temp0;
Buffer[1] = temp1;
}
// Display Error
void DisplayError (int no) {
Buffer[0] = Error;
Buffer[1] = no;
}
volatile uint8_t Ticks = 0;
// Flash the display on for n ticks
void DisplayOn (uint8_t n) {
Ticks = n;
TIMSK0 = 1<<OCIE0A; // Compare match interrupt
while (Ticks > 0);
TIMSK0 = 0; // Interrupts off
#if defined(commoncathode)
PORTB = PORTB | 1<<Digit1Pin | 1<<Digit0Pin; // Both digits off
#else
PORTB = PORTB & ~(1<<Digit1Pin | 1<<Digit0Pin); // Both digits off
#endif
}
// Timer/Counter0 interrupt - multiplexes display - 125Hz
ISR(TIMER0_COMPA_vect) {
DisplayNextDigit();
Ticks--;
}
// Temperature sensor **********************************************
// Display temperature of a single DS18B20 on the bus in 1/16ths of a degree
void DisplayTemperature () {
cli(); // No interrupts
if (OneWireReset() != 0) {
sei();
DisplayError(0); // Device not found
} else {
OneWireWrite(SkipROM);
OneWireWrite(ConvertT);
while (OneWireRead() != 0xFF);
OneWireReset();
OneWireWrite(SkipROM);
OneWireWrite(ReadScratchpad);
OneWireReadBytes(9);
sei(); // Interrupts
if (OneWireCRC(9) == 0) {
int temp = DataWords[0];
Display((temp+8)>>4); // Round to nearest degree
} else DisplayError(1); // CRC error
}
}
// Watchdog timer **********************************************
// Use Watchdog for time delay; n=0 is 16ms; n=6 is 1sec ; n=9 is 8secs,
void WDDelay(int n) {
CCP = 0xD8;
WDTCSR = 1<<WDIE | (n & 0x8)<<2 | (n & 0x7);
sleep_enable();
sleep_cpu();
}
ISR(WDT_vect) {
CCP = 0xD8;
WDTCSR = 0<<WDIE;
}
// Setup **********************************************
void setup () {
DDRA = 0xFF; // All outputs
DDRB = 1<<Digit1Pin | 1<<Digit0Pin; // PB0 & PB1 outputs
#if defined(commoncathode)
PORTB = 0<<OneWirePin | 1<<Digit1Pin | 1<<Digit0Pin; // Both digits off
#else
PORTB = 0<<OneWirePin | 0<<Digit1Pin | 0<<Digit0Pin; // Both digits off
#endif
OneWireSetup();
//
// Set up Timer/Counter0 to multiplex the display
TCCR0A = 2<<WGM00; // CTC mode; count up to OCR0A
TCCR0B = 1<<FOC0A | 1<<FOC0B | 0<<WGM02 | 4<<CS00; // Divide by 256
OCR0A = 250-1; // Compare match at 125Hz
TIMSK0 |= 1<<OCIE0A; // Interrupts initially off
ADCSRA &= ~(1<<ADEN); // Disable ADC to save power
PRR = 1<<PRUSART1 | 1<<PRUSART0 | 1<<PRADC; // Turn off clocks to USI & ADC to save power
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}
void loop () {
Buffer[0] = DP; Buffer[1] = Blank;
DisplayOn(12);
WDDelay(6); // Sleep for 1 second
Buffer[0] = Blank; Buffer[1] = DP;
DisplayOn(12);
WDDelay(6); // Sleep for 1 second
DisplayTemperature();
DisplayOn(12);
WDDelay(9); // Sleep for 8 seconds
WDDelay(9); // Sleep for 16 seconds
WDDelay(9); // Sleep for 24 seconds
}