-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeating.cpp
78 lines (68 loc) · 1.39 KB
/
Heating.cpp
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
/*
* Project: ReflowOven
* File: Heating.cpp
* Author: Sandro Lutz
*/
#include "Heating.h"
void Heating::attach(int pin)
{
counter = 0;
cc = 0;
power = 0;
this->pin = pin;
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
// Timer2
// Prescaler: 256, OCR2 = 156.25
// => Overflow frequency = 100Hz
TCCR2 = (1<<CS22) | (1<<CS21);
TIMSK |= (1<<OCIE2);
OCR2 = 156;
sei();
}
void Heating::increasePower() {
if(this->power < this->MAX_POWER) {
++this->power;
}
}
void Heating::decreasePower() {
if(this->power > 0) {
--this->power;
}
}
void Heating::setPower(uint8_t power)
{
if(power > this->MAX_POWER)
power = this->MAX_POWER;
if(this->power != power) {
this->power = power;
}
}
uint8_t Heating::getPower()
{
return power;
}
ISR(TIMER2_COMP_vect)
{
// correct OCR2 register to to eliminate the
// deviation produced through the last 3 cycles.
if(heating.cc == 3) {
OCR2 = 156;
heating.cc = 0;
} else {
if(heating.cc == 2) {
OCR2 = 157;
}
++heating.cc;
}
if(heating.counter < heating.power) {
digitalWrite(heating.pin, LOW);
} else {
digitalWrite(heating.pin, HIGH);
}
if(heating.counter == Heating::MAX_POWER-1) {
heating.counter = 0;
} else {
++heating.counter;
}
}