This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heathaptics.ino
166 lines (151 loc) · 2.96 KB
/
heathaptics.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
#include "types.h"
#include "pins.h"
#include <i2cmaster.h>
ModeType mode = STOP;
boolean exitFlag = false;
short setPts[2] = {0, 0};
void setup()
{
// Setup pwm frequency of 7812.5 Hz
TCCR0B = TCCR0B & 0b11111000 | 0x02;
// Init serial module
Serial.begin(57600);
i2c_init(); //Initialise the i2c bus
PORTC = (1 << PORTC4) | (1 << PORTC5); //enable pullups
// Output pins
pinMode(PIN_PWMA, OUTPUT);
pinMode(PIN_PWMB, OUTPUT);
pinMode(PIN_DIRA, OUTPUT);
pinMode(PIN_DIRB, OUTPUT);
// For peltier devices at 6V, 1 count ~ 7.8125mA or 46.87mW
// Init Peltier outputs
analogWrite(PIN_PWMA, 0);
digitalWrite(PIN_DIRA, LOW);
analogWrite(PIN_PWMB, 0);
digitalWrite(PIN_DIRA, LOW);
}
void loop()
{
switch ( mode )
{
case STOP:
m_stop();
break;
case RUN:
m_run();
break;
case DEBUG:
m_debug();
break;
case I2CSETUP:
m_i2cSetup();
break;
}
if(mode > I2CSETUP)
{
mode = STOP;
}
}
void m_stop()
{
while(!exitFlag)
{
if(Serial.available() > 0)
{
char cmd = Serial.read();
if ( cmd == SETMODE)
{
mode = (ModeType) Serial.read();
exitFlag = true;
}
}
}
}
void m_run()
{
// Enable controller timer interrupt
while(!exitFlag)
{
if(Serial.available() > 0)
{
char cmd = Serial.read();
switch ( cmd )
{
case STOPB:
mode = STOP;
exitFlag = true;
break;
case SETMODE:
mode = (ModeType) Serial.read();
exitFlag = true;
break;
case SETA:
setPts[0] = Serial.read() << 8;
setPts[0] |= Serial.read();
break;
case SETB:
setPts[1] = Serial.read() << 8;
setPts[1] |= Serial.read();
break;
case SETBOTH:
setPts[0] = Serial.read() << 8;
setPts[0] |= Serial.read();
setPts[1] = Serial.read() << 8;
setPts[1] |= Serial.read();
break;
}
}
setPeltiers();
}
//Disable controller interrupt
}
void m_debug()
{
}
void m_i2cSetup()
{
while(!exitFlag)
{
if(Serial.available() > 0)
{
char cmd = Serial.read();
switch ( cmd )
{
case STOP:
mode = STOP;
exitFlag = true;
break;
case SETMODE:
mode = (ModeType) Serial.read();
exitFlag = true;
break;
case SETDEVID:
byte addr = Serial.read();
ChangeAddress(addr,0x00);
break;
//case LISTDEV:
}
}
}
}
void setPeltiers()
{
// Peltier A
analogWrite(PIN_PWMA, 0);
if(setPts[0] < 0)
{
digitalWrite(PIN_DIRA, LOW);
}else{
digitalWrite(PIN_DIRA, HIGH);
}
analogWrite(PIN_PWMA, abs(setPts[0]));
// Peltier B
analogWrite(PIN_PWMB, 0);
if(setPts[1] < 0)
{
digitalWrite(PIN_DIRB, LOW);
}else{
digitalWrite(PIN_DIRB, HIGH);
}
analogWrite(PIN_PWMB, abs(setPts[1]));
}