-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
426 lines (351 loc) · 9.38 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#ifndef BAUD
#define BAUD 9600
#endif
#include <util/setbaud.h>
#include <util/delay.h>
#include "lcd.h"
/*
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 6
* LCD D5 pin to digital pin 7
* LCD D6 pin to digital pin 4
* LCD D7 pin to digital pin 5
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*
* Encoder : 6,7
*/
#define temp A0
#define CLK 3
#define DT 2
#define SW 6
void adc_init(void){
ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)); //16Mhz/128 = 125Khz the ADC reference clock
ADMUX |= (1<<REFS0); //Voltage reference from Avcc (5v)
ADCSRA |= (1<<ADEN); //Turn on ADC
ADCSRA |= (1<<ADSC); //Do an initial conversion because this one is the slowest and to ensure that everything is up and running
}
uint16_t adc_read(void){
ADMUX &= 0xF0; //Clear the older channel that was read
ADMUX |= 0; //Defines the new ADC channel to be read
ADCSRA |= (1<<ADSC); //Starts a new conversion
while(ADCSRA & (1<<ADSC)); //Wait until the conversion is done
return ADCW; //Returns the ADC value of the chosen channel
}
void uart_init(void) {
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= _BV(U2X0);
#else
UCSR0A &= ~(_BV(U2X0));
#endif
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
}
int uart_putchar(char c, FILE *stream) {
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
char uart_getchar(FILE *stream) {
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
}
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar,
NULL,
_FDEV_SETUP_WRITE
);
#define TIMER_FREQ_HZ 1
void timer1_init(void){
// Init Pin PB2 Output, set to 0
DDRB |= (1<<DDB2);
PORTB &= ~(1<<PORTB2);
// initialize timer1
cli();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
// Set FastPWM with OCR1A on TOP, CLEAR on Compare + Set on BOTTOM
// Prescaler 256
// Here 64 !
TCCR1A = _BV(COM1A1)
| _BV(COM1B1)
| _BV(WGM10)
| _BV(WGM11);
TCCR1B = _BV(WGM12)
| _BV(WGM13)
| _BV(CS11)
| _BV(CS10);
OCR1B = 0;
OCR1A = 3125;
sei(); // enable all interrupts
}
long int sec;
void timer0_init(void){
cli();
TCCR0A = 0;
TCCR0B = 0;
TCNT0 = 0;
// Presc 1024
TCCR0B |= _BV(CS02) | _BV(CS00);
TIMSK0 |= _BV(TOIE0);
sei();
}
ISR(TIMER0_OVF_vect)
{
sec+=1;//(1024.f/16E6);
}
float seconds(void){
float result;
cli();
result = sec*(255.f*1024.f/16E6);
sei();
return result;
}
float millis(void){// OVERFLOW in 3,4E38 ms
float result;
cli();
result = sec*(255.f*1024.f/(16E6*10E-3));
sei();
return result;
}
#define TEMP_MAX 480
#define DEFAULT_TEMP 280
#define SLEEP_TEMP 0
float dt,new_error;
float THRESHOLD;
long new_time,old_time;
void set_temp(float temp){
uint16_t duty_cycle;
int duty_cycle_int = ((int)temp)*25;
duty_cycle = (uint16_t)(duty_cycle_int);
if(duty_cycle>=2500) duty_cycle = 2500;
if(duty_cycle<0) duty_cycle = 0;
cli();
OCR1B = duty_cycle;
sei();
}
float get_temp(void){
int i;
static float lpf_temp;
float avg_temp = 0.0;
#define AVG_SAMPLE 300
if(adc_read()*(5.0f/1024.0f) > 4){
return -1.0f;
}
for(i=0;i<AVG_SAMPLE;i++){
avg_temp += 600*(5.0f/1024.0f)*adc_read() - 100; //0.5V => 200°C
}
avg_temp = avg_temp/AVG_SAMPLE;
#define alpha 0.99f
lpf_temp = lpf_temp*alpha + (1.0-alpha)*avg_temp;
return ((float)avg_temp);
}
enum e_state {UpdateConsigne,
ProcessCommand,
SleepMode,
NoIron,
};
enum e_state state = ProcessCommand;
int counts = 0;
void encoder_init(void){
// DT 2
// CK 3
// PUSH 6
DDRD &= ~(1<<DDD2);
DDRD &= ~(1<<DDD3);
DDRD &= ~(1<<DDD6);
PORTD |= (1<<PORTD2);
PORTD |= (1<<PORTD3);
PORTD |= (1<<PORTD6);
counts = 0;
// Set Exti
PCMSK2 = 0;
PCICR = 0;
PCICR |= (1 << PCIE0);
PCMSK2 |= (1<<PCINT18);
PCMSK2 |= (1<<PCINT19);
PCICR |= (1 << PCIE2); // set PCIE2 to enable PCMSK0 scan
PCMSK2 |= (1 << PCINT22); // set PCINT0 to trigger an interrupt on state change
}
// Return 1 if pressed
uint8_t enc_switch_state(void){
return !(PIND&(1<<PIND6));
}
volatile uint8_t portdhistory = 0xFF; // default is high because the pull-up
volatile uint8_t cycle_press = 0;
volatile float awake_time = 0.f;
void increment_counts(void){
if((DEFAULT_TEMP+2*counts)<TEMP_MAX){
counts++;
}
}
void decrement_counts(void){
if((DEFAULT_TEMP+2*counts)>0){
counts--;
}
}
float time_last_press = 0.f;
ISR (PCINT2_vect){
uint8_t changedbits;
changedbits = PIND ^ portdhistory;
portdhistory = PIND;
// User Interation : Reset awake time
awake_time = millis();
// State machine for Encoder Press Unbouncing
if(changedbits & (1<<PIND6)){
if(!(PIND&(1<<PIND6))){// Button is pressed
cycle_press = 1;
time_last_press = millis();
}
else{//Button is unpressed
if(cycle_press==1){
cycle_press = 0;
state=(state==UpdateConsigne)?ProcessCommand:UpdateConsigne;
if((millis()-time_last_press)>20.f){
counts=0;
}
}
}
}
// Encoder Counting
if(state==UpdateConsigne){
if(changedbits & (1<<PIND2)){// Edge on D2
if(PIND&(1<<PIND2))//Rising
{
if(PIND&(1<<PIND3)){
increment_counts();
}
else{
decrement_counts();
}
}
else//Falling
{
if(!(PIND&(1<<PIND3))){
increment_counts();
}
else{
decrement_counts();
}
}
}
}
}
int get_counts(void){
int result;
cli();
result = counts;
sei();
return result;
}
uint8_t line1[] = "Starting up ...";
uint8_t line2[] = " ";
void update_screen(void){
lcd_write_instruction_4d(lcd_Home);
_delay_us(80); // 40 uS delay (min)
lcd_write_string_4d(line1);
new_line(2);
lcd_write_string_4d(line2);
new_line(1);
}
uint16_t Consigne = DEFAULT_TEMP;
long time;
int main(void)
{
uart_init();
sei();
stdout = &uart_output;
timer1_init();
timer0_init();
encoder_init();
lcd_init_4d();
adc_init();
float temp;
Consigne = DEFAULT_TEMP;
float blinky = 0.f;
uint8_t onoff = 0;
float freq_display = 0.f;
for (;;) {
// Measure Temperature
temp = get_temp();
if(temp<0.){
state=NoIron;
}
// Handle Sleep Mode Transition
if((millis()-awake_time)>20.f*60.f*100.f){
state = SleepMode;
awake_time = millis();
}
switch(state){
case UpdateConsigne:
set_temp(0.f);
Consigne = DEFAULT_TEMP + 2*get_counts();
snprintf((char*)line2,16," %i C ",(int)temp);
if((millis()-blinky)>50.f){
blinky =millis();
onoff = !onoff;
}
if(onoff){
snprintf((char*)line1,16," : %i C ",Consigne);
update_screen();
}
else{
snprintf((char*)line1,16,"Set : %i C ",Consigne);
update_screen();
}
break;
case NoIron:
// Update display content
snprintf((char*)line2,16,"No Iron ");
update_screen();
if(temp>=0.0){
state=ProcessCommand;
}
set_temp(0.f);
break;
case SleepMode:
// Update display content
snprintf((char*)line2,16," %i C %s ",(int)temp,
"SLEEP");
update_screen();
set_temp(SLEEP_TEMP);
// Out of sleep mode on ISR Encoder Pressed
break;
case ProcessCommand:
// Update display content
if((millis()-freq_display)>20.f){
snprintf((char*)line1,16,"Set : %i C ",Consigne);
snprintf((char*)line2,16," %i C ",(int)temp);
update_screen();
freq_display = millis();
}
new_error = Consigne - temp; // Process new error
THRESHOLD = 0.f;
if(new_error<THRESHOLD){// Cool down
set_temp(0.0f);
}
else{
set_temp(TEMP_MAX);// Full noise
}
break;
}
}
return 0; /* never reached */
}