-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
372 lines (294 loc) · 7.89 KB
/
main.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* Termometr na LM35 od 0 do 100st z obsluga wyswietlania na module TM1637
*/
#include <stdint.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#define LM35_DATA_PIN PB4
#define TM1637_DIO_HIGH() (PORTB |= _BV(TM1637_DIO_PIN))
#define TM1637_DIO_LOW() (PORTB &= ~_BV(TM1637_DIO_PIN))
#define TM1637_DIO_OUTPUT() (DDRB |= _BV(TM1637_DIO_PIN))
#define TM1637_DIO_INPUT() (DDRB &= ~_BV(TM1637_DIO_PIN))
#define TM1637_DIO_READ() (((PINB & _BV(TM1637_DIO_PIN)) > 0) ? 1 : 0)
#define TM1637_CLK_HIGH() (PORTB |= _BV(TM1637_CLK_PIN))
#define TM1637_CLK_LOW() (PORTB &= ~_BV(TM1637_CLK_PIN))
// Main Settings
#define TM1637_DIO_PIN PB0
#define TM1637_CLK_PIN PB1
#define TM1637_DELAY_US (5)
#define TM1637_BRIGHTNESS_MAX (7)
#define TM1637_POSITION_MAX (4)
// TM1637 commands
#define TM1637_CMD_SET_DATA 0x40
#define TM1637_CMD_SET_ADDR 0xC0
#define TM1637_CMD_SET_DSIPLAY 0x80
// TM1637 data settings (use bitwise OR to contruct complete command)
#define TM1637_SET_DATA_WRITE 0x00 // write data to the display register
#define TM1637_SET_DATA_READ 0x02 // read the key scan data
#define TM1637_SET_DATA_A_ADDR 0x00 // automatic address increment
#define TM1637_SET_DATA_F_ADDR 0x04 // fixed address
#define TM1637_SET_DATA_M_NORM 0x00 // normal mode
#define TM1637_SET_DATA_M_TEST 0x10 // test mode
// TM1637 display control command set (use bitwise OR to consruct complete command)
#define TM1637_SET_DISPLAY_OFF 0x00 // off
#define TM1637_SET_DISPLAY_ON 0x08 // on
static void TM1637_send_config(const uint8_t enable, const uint8_t brightness);
static void TM1637_send_command(const uint8_t value);
static void TM1637_start(void);
static void TM1637_stop(void);
static uint8_t TM1637_write_byte(uint8_t value);
static uint8_t _config = TM1637_SET_DISPLAY_ON | TM1637_BRIGHTNESS_MAX;
static uint8_t _segments = 0xff;
static void LM35_init(void);
static int LM35_read(void);
static int analog_read(uint8_t pin);
/**
* Initialize TM1637 display driver.
* Clock pin (TM1637_CLK_PIN) and data pin (TM1637_DIO_PIN)
* are defined at the top of this file.
*/
void TM1637_init(const uint8_t enable, const uint8_t brightness);
/**
* Turn display on/off.
* value: 1 - on, 0 - off
*/
void TM1637_enable(const uint8_t value);
/**
* Set display brightness.
* Min value: 0
* Max value: 7
*/
void TM1637_set_brightness(const uint8_t value);
/**
* Display raw segments at position (0x00..0x03)
*
* bits:
* -- 0 --
* | |
* 5 1
* | |
* -- 6 --
* | |
* 4 2
* | |
* -- 3 --
*
* Example segment configurations:
* - for character 'H', segments=0b01110110
* - for character '-', segments=0b01000000
* - etc.
*/
void TM1637_display_segments(const uint8_t position, const uint8_t segments);
/**
* Display digit ('0'..'9') at position (0x00..0x03)
*/
void TM1637_display_digit(const uint8_t position, const uint8_t digit);
/**
* Display colon on/off.
* value: 1 - on, 0 - off
*/
void TM1637_display_colon(const uint8_t value);
/**
* Clear all segments (including colon).
*/
void TM1637_clear(void);
PROGMEM const uint8_t _digit2segments[] =
{
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};
int main(void)
{
int temp;
uint8_t firstdig;
uint8_t seconddig;
uint8_t thirddig;
/* setup */
TM1637_init(1/*enable*/, 5/*brightness*/);
/* setup */
LM35_init();
/* loop */
while (1) {
// read ADC value 16 times and compute mean for accuracy
temp = 0;
for(uint8_t counts = 0; counts < 16; counts++) temp = temp + LM35_read();
temp = temp >> 4;
// wyswietlenie znaku stopni NA OSTATNIEJ POZYCJI
TM1637_display_segments(3, 99);
//przeliczenie cyfr na wszystkich pozycjach
firstdig = temp / 100;
seconddig = (temp - (firstdig * 100))/ 10;
thirddig = temp - (firstdig * 100) - (seconddig*10);
// wyswietlamy od pozycji 1giej na wyswietlaczu
// ostatnia pozycja to znak minus lub stopnie
TM1637_display_digit(0, firstdig);
TM1637_display_digit(1, seconddig);
TM1637_display_digit(2, thirddig);
// pomiar co pol sekundy
_delay_ms(500);
}
/* loop */
}
void
LM35_init(void)
{
DDRB &= ~_BV(LM35_DATA_PIN); // set data pin as INPUT
}
int
LM35_read(void)
{
int temp;
temp = analog_read(LM35_DATA_PIN); // read analog value from sensor
temp = ((((uint32_t)temp * 1000UL) >> 10) * 5); // convert value using euqation temp = Vin * 1000 / 1024 * Vref [milivolts]
return temp;
}
int
analog_read(uint8_t pin)
{
uint8_t low, high;
switch(pin) {
case PB2: ADMUX = _BV(MUX0); break; // ADC1
case PB4: ADMUX = _BV(MUX1); break; // ADC2
case PB3: ADMUX = _BV(MUX0)|_BV(MUX1); break; // ADC3
case PB5: // ADC0
default: ADMUX = 0; break;
}
ADMUX &= ~_BV(REFS0); // explicit set VCC as reference voltage (5V)
ADCSRA |= _BV(ADEN); // Enable ADC
ADCSRA |= _BV(ADSC); // Run single conversion
while(bit_is_set(ADCSRA, ADSC)); // Wait conversion is done
// Read values
low = ADCL;
high = ADCH;
// combine two bytes
return (high << 8) | low;
}
void
TM1637_init(const uint8_t enable, const uint8_t brightness)
{
DDRB |= (_BV(TM1637_DIO_PIN)|_BV(TM1637_CLK_PIN));
PORTB &= ~(_BV(TM1637_DIO_PIN)|_BV(TM1637_CLK_PIN));
TM1637_send_config(enable, brightness);
}
void
TM1637_enable(const uint8_t value)
{
TM1637_send_config(value, _config & TM1637_BRIGHTNESS_MAX);
}
void
TM1637_set_brightness(const uint8_t value)
{
TM1637_send_config(_config & TM1637_SET_DISPLAY_ON,
value & TM1637_BRIGHTNESS_MAX);
}
void
TM1637_display_segments(const uint8_t position, const uint8_t segments)
{
TM1637_send_command(TM1637_CMD_SET_DATA | TM1637_SET_DATA_F_ADDR);
TM1637_start();
TM1637_write_byte(TM1637_CMD_SET_ADDR | (position & (TM1637_POSITION_MAX - 1)));
TM1637_write_byte(segments);
TM1637_stop();
}
void
TM1637_display_digit(const uint8_t position, const uint8_t digit)
{
uint8_t segments = (digit < 10 ? pgm_read_byte_near((uint8_t *)&_digit2segments + digit) : 0x00);
if (position == 0x01) {
segments = segments | (_segments & 0x80);
_segments = segments;
}
TM1637_display_segments(position, segments);
}
void
TM1637_display_colon(const uint8_t value)
{
if (value) {
_segments |= 0x80;
} else {
_segments &= ~0x80;
}
TM1637_display_segments(0x01, _segments);
}
void
TM1637_clear(void)
{
uint8_t i;
for (i = 0; i < TM1637_POSITION_MAX; ++i) {
TM1637_display_segments(i, 0x00);
}
}
void
TM1637_send_config(const uint8_t enable, const uint8_t brightness)
{
_config = (enable ? TM1637_SET_DISPLAY_ON : TM1637_SET_DISPLAY_OFF) |
(brightness > TM1637_BRIGHTNESS_MAX ? TM1637_BRIGHTNESS_MAX : brightness);
TM1637_send_command(TM1637_CMD_SET_DSIPLAY | _config);
}
void
TM1637_send_command(const uint8_t value)
{
TM1637_start();
TM1637_write_byte(value);
TM1637_stop();
}
void
TM1637_start(void)
{
TM1637_DIO_HIGH();
TM1637_CLK_HIGH();
_delay_us(TM1637_DELAY_US);
TM1637_DIO_LOW();
}
void
TM1637_stop(void)
{
TM1637_CLK_LOW();
_delay_us(TM1637_DELAY_US);
TM1637_DIO_LOW();
_delay_us(TM1637_DELAY_US);
TM1637_CLK_HIGH();
_delay_us(TM1637_DELAY_US);
TM1637_DIO_HIGH();
}
uint8_t
TM1637_write_byte(uint8_t value)
{
uint8_t i, ack;
for (i = 0; i < 8; ++i, value >>= 1) {
TM1637_CLK_LOW();
_delay_us(TM1637_DELAY_US);
if (value & 0x01) {
TM1637_DIO_HIGH();
} else {
TM1637_DIO_LOW();
}
TM1637_CLK_HIGH();
_delay_us(TM1637_DELAY_US);
}
TM1637_CLK_LOW();
TM1637_DIO_INPUT();
TM1637_DIO_HIGH();
_delay_us(TM1637_DELAY_US);
ack = TM1637_DIO_READ();
if (ack) {
TM1637_DIO_OUTPUT();
TM1637_DIO_LOW();
}
_delay_us(TM1637_DELAY_US);
TM1637_CLK_HIGH();
_delay_us(TM1637_DELAY_US);
TM1637_CLK_LOW();
_delay_us(TM1637_DELAY_US);
TM1637_DIO_OUTPUT();
return ack;
}