-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSousvide2016.c
More file actions
366 lines (306 loc) · 7.93 KB
/
Sousvide2016.c
File metadata and controls
366 lines (306 loc) · 7.93 KB
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
#define F_CPU 12000000UL
#define TIMER_COUNTER 46786
// <16 bit timer max value> - F_CPU/(<äåëèòåëü>*<äîëè ñåêóíäû>) 65536 - 12000000/(64*10)
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <util/delay.h>
#include <u8g.h>
#include <avr/eeprom.h>
u8g_t u8g;
#define BTN_COUNT 3 // 0 .. 2
#define BTN_PIN PINA
#define BTN_PORT PORTA
#define BTN_RIGHT PORTA5
#define BTN_CENTER PORTA6
#define BTN_LEFT PORTA7
#define SPI_PORTX PORTB
#define SPI_DDRX DDRB
#define SPI_MOSI 5
#define SPI_MISO 6
#define SPI_SS 4
#define SPI_SCK 7
#define USART_BAUDRATE 9600
#define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#define USART_CAN_READ (UCSRA & (1<<RXC))
void USART_Init(void);
unsigned char USART_Receive(void);
void USART_Transmit( unsigned char data);
char timerHours = 1;
char timerMinutes = 0;
char timerSeconds = 0;
char timerDSeconds = 0;
char power;
uint16_t deltaTemp;
uint16_t temperature;
uint16_t EEMEM destTemperatureStore = 35 << 2;
uint16_t destTemperature;
uint8_t button_counter[BTN_COUNT];
/*èíèöèàëèçàöèÿ SPI ìîäóëÿ â ðåæèìå master*/
void SPI_Init(void)
{
/*íàñòðîéêà ïîðòîâ ââîäà-âûâîäà
âñå âûâîäû, êðîìå MISO âûõîäû*/
SPI_DDRX |= (1<<SPI_MOSI)|(1<<SPI_SCK)|(1<<SPI_SS);
SPI_DDRX &= ~(0<<SPI_MISO);
SPI_PORTX |= (1<<SPI_MOSI)|(1<<SPI_SCK)|(1<<SPI_SS)|(1<<SPI_MISO);
/*ðàçðåøåíèå spi,ñòàðøèé áèò âïåðåä,ìàñòåð, ðåæèì 0*/
SPCR = (1<<SPE)|(0<<DORD)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(0<<SPR0);
SPSR = (0<<SPI2X);
// SPI_PORTX &= ~(1<<SPI_SS);
// _delay_ms(100);
// SPI_PORTX |= (1<<SPI_SS);
}
void SPI_WriteByte(uint8_t data)
{
SPI_PORTX &= ~(1<<SPI_SS);
SPDR = data;
while(!(SPSR & (1<<SPIF)));
SPI_PORTX |= (1<<SPI_SS);
}
uint8_t SPI_ReadByte(uint8_t data)
{
uint8_t report;
SPI_PORTX &= ~(1<<SPI_SS);
SPDR = data;
while(!(SPSR & (1<<SPIF)));
report = SPDR;
SPI_PORTX |= (1<<SPI_SS);
return report;
}
void SPI_WriteArray(uint8_t num, uint8_t *data)
{
SPI_PORTX &= ~(1<<SPI_SS);
while(num--){
SPDR = *data++;
while(!(SPSR & (1<<SPIF)));
}
SPI_PORTX |= (1<<SPI_SS);
}
void SPI_ReadArray(uint8_t num, uint8_t *data)
{
SPI_PORTX &= ~(1<<SPI_SS);
_delay_ms(10);
while(num--){
SPDR = *data;
while(!(SPSR & (1<<SPIF)));
*data++ = SPDR;
}
SPI_PORTX |= (1<<SPI_SS);
_delay_ms(170);
}
void USART_Init(void) //Ôóíêöèÿ èíèöèàëèçàöèè USART
{
UBRRH = (uint8_t) (UBRR_VALUE>>8); // Set baudrate
UBRRL = (uint8_t) UBRR_VALUE;
UCSRB = (1<<RXEN) | (1<<TXEN); //Ðàçðåøåíèå íà ïðèåì è í àïåðåäà÷ó ÷åðåç USART
UCSRC = (1<<URSEL) | (1<<USBS) | (3<<UCSZ0); // Set frame format to 8 data bits, no parity, 1 stop bit
}
unsigned char USART_Receive( void ) //Ôóíêöèÿ ïðèåìà äàííûõ
{
while ( !USART_CAN_READ ); //Îæèäàíèå ïðèåìà ñèìâîëà
return UDR; //Âîçâðàò ñèìâîëà
}
void USART_Transmit( unsigned char data ) //Ôóíêöèÿ îòïðàâêè äàííûõ
{
while ( !(UCSRA & (1<<UDRE)) ); //Îæèäàíèå îïóñòîøåíèÿ áóôåðà ïðèåìà
UDR = data; //Íà÷àëî ïåðåäà÷è äàííûõ
}
void fixPointToStr(uint16_t j, char * str)
{
itoa(j>>2, str, 10);
str[2] = '.';
if( j&1 )
{
str[3] = j&2 ? '7' : '2';
str[4] = '5';
} else {
str[3] = j&2 ? '5' : '0';
str[4] = '0';
}
str[5] = '\0';
}
void draw(void)
{
char str[8] = "\0\0\0\0\0\0\0\0";
PORTA &= ~(1<<PORTA2);
u8g_FirstPage(&u8g);
do
{
u8g_SetColorIndex(&u8g, 1);
u8g_SetFont(&u8g, u8g_font_courR14);
str[0] = '0';
itoa(timerHours, timerHours < 9 ? &str[1] : str, 10);
str[2] = timerSeconds & 1 ? ':' : ' ';
str[3] = '0';
itoa(timerMinutes, timerMinutes < 9 ? &str[4] : &str[3], 10);
str[5] = '\0';
u8g_DrawStr(&u8g, 0, 62, "TIME");
u8g_DrawStr(&u8g, 60, 62, str);
fixPointToStr(temperature, str);
u8g_DrawStr(&u8g, 60, 16, str);
u8g_DrawStr(&u8g, 115, 16, "C");
itoa(power*20, str, 10);
u8g_DrawStr(&u8g, 60, 40, str);
u8g_DrawStr(&u8g, 115, 40, "%");
u8g_SetFont(&u8g, u8g_font_fub35n);
//u8g_DrawFrame(&u8g, 0, 0, 127, 21);
//u8g_SetColorIndex(&u8g, 0);
//itoa(destTemperature, str, 10);
itoa(destTemperature>>2, str, 10);
//fixPointToStr(destTemperature, str);
u8g_DrawStr(&u8g, 0, 40, str);
} while ( u8g_NextPage(&u8g) );
//u8g_Delay(100);
PORTA |= 1<<PORTA2;
}
ISR( TIMER1_OVF_vect )
{
TCNT1 = TIMER_COUNTER; //âûñòàâëÿåì íà÷àëüíîå çíà÷åíèå TCNT1
// 000 001 010 011 100 101 110 111 1000 1001
if( button_counter[0] )
{
destTemperature -=
button_counter[0] == 1 ? 1
: button_counter[0] < 8 ? 0
: button_counter[0] < 16 ? ( (timerDSeconds & 3) == 0?1:0)
: button_counter[0] < 32 ? ( (timerDSeconds & 1) == 0?1:0)
: button_counter[0] >> 5;
eeprom_write_word(&destTemperatureStore, destTemperature);
}
if( button_counter[2] )
{
destTemperature +=
button_counter[2] == 1 ? 1
: button_counter[2] < 8 ? 0
: button_counter[2] < 16 ? ( (timerDSeconds & 3) == 0?1:0)
: button_counter[2] < 32 ? ( (timerDSeconds & 1) == 0?1:0)
: button_counter[2] >> 5;
eeprom_write_word(&destTemperatureStore, destTemperature);
}
if( destTemperature < 40 )
{
destTemperature = 40;
} else if( destTemperature > 396 )
{
destTemperature = 396;
}
if( timerDSeconds > 5 ){
PORTC |= 1<<PORTC0;
}else{
PORTC &= ~(1<<PORTC0);
}
/* if( power == 5 ){
// PORTC |= 1<<PORTC1;
PORTC |= 1<<PORTC0;
// PORTC &= ~(1<<PORTC0);
}else if( power > (timerDSeconds % 5) ){
PORTC |= 1<<PORTC0;
// PORTC &= ~(1<<PORTC1);
}else{
PORTC &= ~(1<<PORTC0);
// PORTC &= ~(1<<PORTC1);
}*/
if ( timerDSeconds > 0 )
{
timerDSeconds--;
for( char i = 0; i < BTN_COUNT ; i++ )
{
if( button_counter[i] > 0 && button_counter[i] < 255 )
{
button_counter[i]++;
}
}
} else {
timerDSeconds = 9;
if( destTemperature <= temperature ){
power = 0;
} else {
deltaTemp = destTemperature - temperature;
if( deltaTemp > 4 ){
power = 5;
} else if( deltaTemp > 8 ) {
power = 3;
}
}
if ( timerSeconds > 0 ){
timerSeconds--;
} else {
timerSeconds = 59;
if ( timerMinutes > 0 )
{
timerMinutes--;
} else {
timerMinutes = 59;
if ( timerHours > 0 ){
timerHours--;
} else {
timerHours = 1; //TODO
}
}
}
}
if( (timerDSeconds & 1) == 0 ){ draw(); }
}
int main(void) {
//DDRA = 0;
//BTN_PORT = (1<<BTN_RIGHT)|(1<<BTN_LEFT)|(1<<BTN_CENTER);
DDRC |= (1<<PORTC0)|(1<<PORTC1);
PORTC |= (1<<PORTC0)|(1<<PORTC1);
destTemperature = eeprom_read_word(&destTemperatureStore);
TCCR1B = (0<<CS12)|(1<<CS11)|(1<<CS10); // äåëèòåëü 64
TIMSK |= (1<<TOIE1); // ðàçðåøàåì ïðåðûâàíèå ïî ïåðåïîëíåíèþ òàéìåðà
TCNT1 = TIMER_COUNTER; // âûñòàâëÿåì íà÷àëüíîå çíà÷åíèå TCNT1
sei(); // âûñòàâëÿåì áèò îáùåãî ðàçðåøåíèÿ ïðåðûâàíèé
//SPI_Init();
//u8g_InitSPI(&u8g, &u8g_dev_ssd1306_128x64_sw_spi, PN(0, 3), PN(0, 4), PN(0, 0), PN(0, 1), PN(0, 2));
//u8g_InitHWSPI(&u8g, &u8g_dev_ssd1306_128x64_hw_spi, PN(0, 0), PN(0, 1), PN(0, 2));
// USART_Init();// Ñêîðîñòü ñîåäèíåíèÿ 9600 áèò/ñ
uint8_t temperature_buf[2];
for ( ; ; ) {
temperature_buf[1] = temperature_buf[0] = 0xff;
//SPI_ReadArray(2, temperature_buf);
//temperature = (temperature_buf[0]<<8 | temperature_buf[1]) >> 3;
temperature = 50;
char buttons = BTN_PIN;
if( buttons & (1<<BTN_LEFT) )
{
button_counter[0] = 0;
} else if( !button_counter[0] ) {
button_counter[0] = 1;
}
if( buttons & (1<<BTN_CENTER) )
{
button_counter[1] = 0;
} else if( !button_counter[1] ) {
button_counter[1] = 1;
}
if( buttons & (1<<BTN_RIGHT) )
{
button_counter[2] = 0;
} else if( !button_counter[2] ) {
button_counter[2] = 1;
}
//strcat(str, "t");
/*
USART_Transmit(temperature_buf[0]);
USART_Transmit(temperature_buf[1]);
*/
//USART_Transmit(str[0]);
//USART_Transmit(str[1]);
//USART_Transmit(10); //Îòïðàâêà ñèìâîëà
//USART_Transmit(13); //Îòïðàâêà ñèìâîëà
/*_delay_ms(1000);
if(USART_CAN_READ){
act = USART_Receive();
if(act == 49){
PORTC |= 1<<PORTC0;
}else if(act == 48){
PORTC &= ~(1<<PORTC0);
}
}
*/
}
}