-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·277 lines (210 loc) · 6.86 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
/**
* Capacitive-touch-sensor, A capacitive touch sensor for the AVR platform.
* Copyright (C) 2011 Skrupellos
*
* This file is part of Capacitive-touch-sensor.
*
* Capacitive-touch-sensor is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Capacitive-touch-sensor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Capacitive-touch-sensor.
* If not, see <http://www.gnu.org/licenses/>.
*
*
* @author Skrupellos
*/
#include "main.h"
// This interrupt gets fired if the TX buffer is empty. Hence the interrupt
// should only wake up the CPU, there is no need to implement an ISR.
EMPTY_INTERRUPT(USART0_UDRE_vect);
// This interrupt gets fired after 5 times \tau, when the capacitor is 99%
// discharged.
ISR(TIMER0_OVF_vect) {
discharging = false;
}
static int sendUart(char c, FILE *stream) {
// Automatically convert Unix into Win line endings
if (c == '\n') {
sendUart('\r', stream);
}
// Sleep until the TX buffer is empty
cli();
while( !(UCSR0A & _BV(UDRE0)) ) {
sei();
sleep();
}
// Put data into buffer, sends the data
UDR0 = c;
return 0;
}
static FILE uartOut = FDEV_SETUP_STREAM(sendUart, NULL, _FDEV_SETUP_WRITE);
uint16_t accuireCap(uint8_t samples) {
// ## INIT #################################################################
uint32_t sum = 0;
uint8_t swSamples = samples;
// Both: Disable pull-up / Set to GND
CBI(PORT_UP, PIN_UP);
CBI(PORT_DOWN, PIN_DOWN);
while(swSamples--) {
// ## RESET / EVALUATE #################################################
uint16_t hwSamples = 0;
// Both: Output (GND)
SBI(DDR_UP, PIN_UP);
SBI(DDR_DOWN, PIN_DOWN);
// Both are now connected to GND directly
discharging = true;
// Reset the timer counter
TCNT0 = 0;
enableTimer();
cli();
while(discharging) {
sei();
sleep();
}
disableTimer();
// ## SAMPLE ###########################################################
do {
// ==== Switch ports ===============================================
// Upper: Input
CBI(DDR_UP, PIN_UP);
// Upper is now open
// Bottom: Set to Vcc / Enable pull-up
SBI(PORT_DOWN, PIN_DOWN);
// First: Bottom is now connected to Vcc directly
// Further: Bottom is now connected to Vcc through a pull-up
// Bottom: Output (Vcc)
SBI(DDR_DOWN, PIN_DOWN);
// Bottom is now connected to Vcc directly
// ==== Wait =======================================================
if(experience) {
// http://www.avr-asm-tutorial.net/avr_de/zeitschleifen/
// delay8.html
// 21cc / 3 = 7
uint8_t cnt;
asm volatile(
"ldi %0, 7 ; 1cc \n"
"Loop: \n\t"
"dec %0 ; 1cc \n\t"
"brne Loop ; 2cc if ne 0, 1cc if eq 0 \n\t"
: "=&w" (cnt)
);
} else {
asm volatile("nop");
}
// ==== Switch ports ===============================================
// Bottom: Input (pull-up)
CBI(DDR_DOWN, PIN_DOWN);
// Bottom is now connected to Vcc through a pull-up
// Bottom: Disable pull-up / Set to GND
CBI(PORT_DOWN, PIN_DOWN);
// Bottom is now open
// Upper: Output (GND)
SBI(DDR_UP, PIN_UP);
// Upper is now connected to GND directly
// ==== Measure ====================================================
// The analog comparator result is available after 2cc. But
// incrementing hwSamples takes already longer.
} while(++hwSamples && (ACSR & _BV(ACO)));
// Add the HW sampled value to the software sampled treating an overflow
// as a maximum.
if(hwSamples == 0) {
sum += 0xFFFF;
} else {
sum += hwSamples;
}
}
// Return the averaged value
return sum / samples;
}
int main()
{
// ==== Timer ==============================================================
// TCCR0A – Timer/Counter Control Register A
// WGM01:0: Waveform Generation Mode (Fast PWM)
TCCR0A = _BV(WGM01) | _BV(WGM00);
// TCCR0B – Timer/Counter Control Register B
// WGM02: Waveform Generation Mode (Fast PWM)
TCCR0B = _BV(WGM02);
// OCR0A – Output Compare Register A
// 64 * 34 = 2176
OCR0A = 34;
// TIMSK0 – Timer/Counter Interrupt Mask Register
// TOIE0: Timer/Counter0 Overflow Interrupt Enable
TIMSK0 = _BV(TOIE0);
// ==== UART ===============================================================
// Set baud rate
UBRR0H = (unsigned char)(UBRR_VAL>>8);
UBRR0L = (unsigned char)UBRR_VAL;
// UCSRnB: USART Control and Status Register n B
// UDRIEn: USART Data Register Empty Interrupt Enable n
// TXENn: Transmitter Enable n
UCSR0B = _BV(UDRIE0) | _BV(TXEN0);
// ==== Disable input buffers (Analog/Powersave) ===========================
// DIDR0 – Digital Input Disable Register 0
// Bit 7:0 – ADC7D:ADC0D: ADC7:0 Digital Input Disable
DIDR0 = 0xFF;
// DIDR1 – Digital Input Disable Register 1
// Bit 1, 0 – AIN1D, AIN0D: AIN1, AIN0 Digital Input Disable
DIDR1 = 0x03;
// DIDR2 – Digital Input Disable Register 2
// Bit 7:0 – ADC15D:ADC8D: ADC15:8 Digital Input Disable
DIDR2 = 0xFF;
// ==== STDOUT =============================================================
// Set STDOUT to an UART sink
stdout = &uartOut;
// ==== Debug port =========================================================
DDRA = 0xFF;
PORTA = 0xFF;
// ==== Arm the interrupts =================================================
sei();
uint16_t history[HISTORY_LEN];
uint16_t *curHistoryItem = history;
uint8_t isInPeak = false;
while(true) {
uint16_t cap, minCap, maxCap;
uint16_t *historyItem;
uint8_t i;
// Append the current value to the history (ring buffer)
cap = accuireCap(SW_SAMPLES);
*curHistoryItem = cap;
if(curHistoryItem == (history + HISTORY_LEN - 1)) {
curHistoryItem = history;
} else {
curHistoryItem++;
}
// Get the min/max values from the history
historyItem = history;
minCap = 0xFFFF;
maxCap = 0x0000;
for(i = HISTORY_LEN; i; i--) {
if(minCap > *historyItem) {
minCap = *historyItem;
}
if(maxCap < *historyItem) {
maxCap = *historyItem;
}
historyItem++;
}
// Print a message if a touch is recognized
if(maxCap * THRESHOLD > cap) {
if(isInPeak == false) {
printf("Blubb\n");
isInPeak = true;
}
} else {
isInPeak = false;
}
// Print some debug messages (you can copy&paste the to Excel; CSV)
printf("%5d; %5d; %5d; %5d; %5d; %5d\n",
cap, minCap, maxCap, maxCap - minCap, cap - minCap, maxCap - cap
);
}
}