-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameter_setup_interface_code.ino
More file actions
574 lines (495 loc) · 16.9 KB
/
parameter_setup_interface_code.ino
File metadata and controls
574 lines (495 loc) · 16.9 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET); /* Object of class Adafruit_SSD1306 */
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#define SSD1306_LCDHEIGHT 64
unsigned int selectioncounter = 0;
const int selectionselectpin = 12;
const int selectionpin = 11;
const int voltagepin = A1;
unsigned int freq;
unsigned int freq1;
unsigned int freq2;
unsigned int freq3;
unsigned int pulsed;
unsigned int pulsed1;
unsigned int pulsed2;
unsigned int pulsed3;
unsigned int voltagecounter;
unsigned int voltageinput;
unsigned int voltage;
unsigned int Top;
unsigned int bdelay;
unsigned int bdelay1;
unsigned int hpw;
unsigned int oft;
float clktime;
unsigned int pw;
bool selectionselectcounter = false;
bool irindicator2 = false;
int Pwmpin = 9; //Pwm Output Pin ;
//float EMA_a = 0.2; //initialization of EMA alpha
// int EMA_S = 0; //initialization of EMA S
//int Button=1;//Button to change the frequency
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); /* Initialize display with address 0x3C */
display.clearDisplay(); /* Clear display */
display.setCursor(15,40); /* Set x,y coordinates */
display.setTextSize(2); /* Select font size of text. Increases with size of argument. */
display.setTextColor(WHITE); /* Color of text*/
display.println("H.V.P.C."); /* Text to be displayed */
display.display();
delay(500);
pinMode(3,OUTPUT);//Pwm pin as Output
pinMode(selectionselectpin, INPUT);
pinMode(selectionpin, INPUT);
pinMode(voltagepin, INPUT);
pinMode(9,OUTPUT);//timer1 pwm
//pinMode(Button, INPUT);//Button as Input
parametersetup1();
TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS21);
OCR2A=142;
//for frequency range 0 to 4 setting prescale of clock to 256(due to comparatively large delay)
if (freq<=4)
{
Top = 16000000 / (256*freq);
clktime= 1/freq;
pw= (pulsed*Top)/(1000000*clktime);
bdelay= (16*Top)/(1000000*clktime);
bdelay1=bdelay*2;
hpw=pw/2-bdelay;
oft=Top-pw;
TCNT1=0;
OCR1A = hpw;
TCCR1A = _BV(COM1A0); //Non-inverted Mode
TCCR1B = _BV(WGM12) | _BV(CS12); //Prescalar 256
TCCR1C= _BV(FOC1A);
TIMSK1= _BV(OCIE1A);
sei();
}
//for frequency range 4 to 33 setting prescale of clock to 64
else if (freq>4 & freq<=33)
{
Top = 16000000 / (64 * freq);
clktime= 1/freq;
pw= (pulsed*Top)/(1000000*clktime);
bdelay= (5*Top)/(1000000*clktime);
bdelay1=bdelay*2;
hpw=pw/2-bdelay;
oft=Top-pw;
TCNT1=0;
OCR1A = hpw;
TCCR1A = _BV(COM1A0); //Non-inverted Mode
TCCR1B = _BV(WGM12) | _BV(CS11) | _BV(CS10); //Prescalar 64
TCCR1C= _BV(FOC1A);
TIMSK1= _BV(OCIE1A);
sei();
}
//for frequency range 33 to 250 setting prescale of clock to 8
else if(freq>33 & freq<=250)
{
Top = 16000000 / (8 * freq);
clktime= 1/freq;
pw= (pulsed*Top)/(1000000*clktime);
bdelay= (3*Top)/(1000000*clktime);
bdelay1=bdelay*2;
hpw=pw/2-bdelay;
oft=Top-pw;
TCNT1=0;
OCR1A = hpw;
TCCR1A = _BV(COM1A0); //Non-inverted Mode
TCCR1B = _BV(WGM12) | _BV(CS11); //Prescalar 8
TCCR1C= _BV(FOC1A);
TIMSK1= _BV(OCIE1A);
sei();
}
else
{
Top = 16000000 /freq;
clktime= 1/freq;
pw= (pulsed*Top)/(1000000*clktime);
bdelay= (3*Top)/(1000000*clktime);
bdelay1=bdelay*2;
hpw=pw/2-bdelay;
oft=Top-pw;
TCNT1=0;
OCR1A = hpw;
TCCR1A = _BV(COM1A0); //Non-inverted Mode
TCCR1B = _BV(WGM12) | _BV(CS10); //Prescalar 1
TCCR1C= _BV(FOC1A);
TIMSK1= _BV(OCIE1A);
sei();
}
}
void loop() {
//voltageinput=analogRead(voltagepin);
// voltagecounter = map(voltageinput,0,1024,0,65);
// EMA_S = (EMA_a*voltagecounter) + ((1-EMA_a)*EMA_S); //run the EMA
// voltage=map(EMA_S,0,50,0,160);
// voltage= int(voltagecounter/25);
if (digitalRead(selectionpin) == HIGH)
{
delay(5);
if (digitalRead(selectionpin) == HIGH)
{
while (digitalRead(selectionpin) == HIGH);
delay(5);
while (digitalRead(selectionpin) == HIGH);
voltagecounter = voltagecounter+1;
if (voltagecounter>=51)
{
voltagecounter=0;
}
}
}
if (digitalRead(selectionselectpin) == HIGH)
{
delay(5);
if (digitalRead(selectionselectpin) == HIGH)
{
while (digitalRead(selectionselectpin) == HIGH);
delay(5);
while (digitalRead(selectionselectpin) == HIGH);
voltagecounter = voltagecounter-1;
if (voltagecounter>=51)
{
voltagecounter= 50;
}
}
}
voltage = map(voltagecounter,0,55,0,200);
OCR2B = voltagecounter;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 5); // position to display
display.println("F: Hz p.w: us");
display.setCursor(25, 5); // position to display
display.println(freq); // text to display
display.setCursor(90, 5); // position to display
display.println(pulsed); // text to display
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 30); // position to display
display.println("V int : ");
display.setCursor(90, 30); // position to display
display.println(voltagecounter);
display.setCursor(90, 36); // position to display
display.println("__");
display.display();
}
ISR(TIMER1_COMPA_vect)
{
const unsigned int wavetable[] = {hpw,bdelay1,hpw,oft};
unsigned int index=0;
OCR1A = wavetable[index];
index++;
TCNT1=0;
if (index>=4)
{
index=0;
}
}
//implementing embedded interface code for setting frequency and pulseduration of device
void parametersetup1()
{
selectionselectcounter = true;
selectioncounter=0;
while (selectionselectcounter == true)
{
if (digitalRead(selectionpin) == HIGH)
{
delay(10);
if (digitalRead(selectionpin) == HIGH)
{
while (digitalRead(selectionpin) == HIGH);
delay(10);
while (digitalRead(selectionpin) == HIGH);
selectioncounter = selectioncounter + 1;
if (selectioncounter == 10)
{
selectioncounter = 0;
}
freq1 = selectioncounter;
}
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 10); // position to display
display.println("Frequency: HZ Pulse width: us");
display.setCursor(80, 13); // position to display
display.println("_"); // text to display
display.setCursor(80, 10); // position to display
display.println(freq1); // text to display
display.setCursor(86, 10); // position to display
display.println(freq2); // text to display
display.setCursor(92, 10); // position to display
display.println(freq3); // text to display
display.setCursor(80, 26); // position to display
display.println(pulsed); // text to display
display.display(); // show on OLED
if (digitalRead(selectionselectpin) == HIGH) //Detection button interface to=HIGH
{
delay(10); //Delay 10ms for the elimination of key leading-edge jitter
if (digitalRead(selectionselectpin) == HIGH) //Confirm button is pressed
{
while (digitalRead(selectionselectpin) == HIGH); //Wait for key
delay(10); //delay 10ms for the elimination of key trailing-edge jitter
while (digitalRead(selectionselectpin) == HIGH); //Confirm button release
selectionselectcounter = false;
}
}
}
selectionselectcounter = true;
selectioncounter=0;
while (selectionselectcounter == true)
{
if (digitalRead(selectionpin) == HIGH)
{
delay(5);
if (digitalRead(selectionpin) == HIGH)
{
while (digitalRead(selectionpin) == HIGH);
delay(5);
while (digitalRead(selectionpin) == HIGH);
selectioncounter = selectioncounter + 1;
if (selectioncounter == 10)
{
selectioncounter = 0;
}
freq2 = selectioncounter;
}
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 10); // position to display
display.println("Frequency: HZ Pulse width: us");
display.setCursor(80, 10); // position to display
display.println(freq1); // text to display
display.setCursor(86, 10); // position to display
display.println(freq2); // text to display
display.setCursor(86, 13); // position to display
display.println("_"); // text to display
display.setCursor(92, 10); // position to display
display.println(freq3); // text to display
display.setCursor(80, 26); // position to display
display.println(pulsed); // text to display
display.display(); // show on OLED
if (digitalRead(selectionselectpin) == HIGH) //Detection button interface to=HIGH
{
delay(5); //Delay 10ms for the elimination of key leading-edge jitter
if (digitalRead(selectionselectpin) == HIGH) //Confirm button is pressed
{
while (digitalRead(selectionselectpin) == HIGH); //Wait for key
delay(5); //delay 10ms for the elimination of key trailing-edge jitter
while (digitalRead(selectionselectpin) == HIGH); //Confirm button release
selectionselectcounter = false;
}
}
}
selectionselectcounter = true;
selectioncounter=0;
while (selectionselectcounter == true)
{
if (digitalRead(selectionpin) == HIGH)
{
delay(5);
if (digitalRead(selectionpin) == HIGH)
{
while (digitalRead(selectionpin) == HIGH);
delay(5);
while (digitalRead(selectionpin) == HIGH);
selectioncounter = selectioncounter + 1;
if (selectioncounter == 10)
{
selectioncounter = 0;
}
freq3 = selectioncounter;
}
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20,10); // position to display
display.println("Frequency: HZ Pulse width: us");
display.setCursor(80, 10); // position to display
display.println(freq1); // text to display
display.setCursor(86, 10); // position to display
display.println(freq2); // text to display
display.setCursor(92, 10); // position to display
display.println(freq3); // text to display
display.setCursor(92, 13); // position to display
display.println("_"); // text to display
display.setCursor(80, 26); // position to display
display.println(pulsed); // text to display
display.display(); // show on OLED
if (digitalRead(selectionselectpin) == HIGH) //Detection button interface to=HIGH
{
delay(5); //Delay 10ms for the elimination of key leading-edge jitter
if (digitalRead(selectionselectpin) == HIGH) //Confirm button is pressed
{
while (digitalRead(selectionselectpin) == HIGH); //Wait for key
delay(5); //delay 10ms for the elimination of key trailing-edge jitter
while (digitalRead(selectionselectpin) == HIGH); //Confirm button release
selectionselectcounter = false;
}
}
}
selectionselectcounter = true;
selectioncounter=0;
while (selectionselectcounter == true)
{
if (digitalRead(selectionpin) == HIGH)
{
delay(5);
if (digitalRead(selectionpin) == HIGH)
{
while (digitalRead(selectionpin) == HIGH);
delay(5);
while (digitalRead(selectionpin) == HIGH);
selectioncounter = selectioncounter + 1;
if (selectioncounter == 10)
{
selectioncounter = 0;
}
pulsed3 = selectioncounter;
}
}
freq = (100 * freq1) + (10 * freq2) + freq3;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 10); // position to display
display.println("Frequency: HZ Pulse width: us");
display.setCursor(80, 10); // position to display
display.println(freq); // text to display
display.setCursor(82, 26); // position to display
display.println(pulsed3); // text to display
display.setCursor(88, 26); // position to display
display.println(pulsed2); // text to display
display.setCursor(94, 26); // position to display
display.println(pulsed1); // text to display
display.setCursor(82, 29); // position to display
display.println("_"); // text to display
display.display(); // show on OLED
if (digitalRead(selectionselectpin) == HIGH) //Detection button interface to=HIGH
{
delay(5); //Delay 10ms for the elimination of key leading-edge jitter
if (digitalRead(selectionselectpin) == HIGH) //Confirm button is pressed
{
while (digitalRead(selectionselectpin) == HIGH); //Wait for key
delay(5); //delay 10ms for the elimination of key trailing-edge jitter
while (digitalRead(selectionselectpin) == HIGH); //Confirm button release
selectionselectcounter = false;
}
}
}
selectionselectcounter = true;
selectioncounter=0;
while (selectionselectcounter == true)
{
if (digitalRead(selectionpin) == HIGH)
{
delay(5);
if (digitalRead(selectionpin) == HIGH)
{
while (digitalRead(selectionpin) == HIGH);
delay(5);
while (digitalRead(selectionpin) == HIGH);
selectioncounter = selectioncounter + 1;
if (selectioncounter == 10)
{
selectioncounter = 0;
}
pulsed2 = selectioncounter;
}
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 10); // position to display
display.println("Frequency: HZ Pulse width: us");
display.setCursor(80, 10); // position to display
display.println(freq); // text to display
display.setCursor(82, 26); // position to display
display.println(pulsed3); // text to display
display.setCursor(88, 26); // position to display
display.println(pulsed2); // text to display
display.setCursor(88, 29); // position to display
display.println("_"); // text to display
display.setCursor(94, 26); // position to display
display.println(pulsed1); // text to display
display.display(); // show on OLED
if (digitalRead(selectionselectpin) == HIGH) //Detection button interface to=HIGH
{
delay(5); //Delay 10ms for the elimination of key leading-edge jitter
if (digitalRead(selectionselectpin) == HIGH) //Confirm button is pressed
{
while (digitalRead(selectionselectpin) == HIGH); //Wait for key
delay(5); //delay 10ms for the elimination of key trailing-edge jitter
while (digitalRead(selectionselectpin) == HIGH); //Confirm button release
selectionselectcounter = false;
}
}
}
selectionselectcounter = true;
selectioncounter=0;
while (selectionselectcounter == true)
{
if (digitalRead(selectionpin) == HIGH)
{
delay(5);
if (digitalRead(selectionpin) == HIGH)
{
while (digitalRead(selectionpin) == HIGH);
delay(5);
while (digitalRead(selectionpin) == HIGH);
selectioncounter = selectioncounter + 1;
if (selectioncounter == 10)
{
selectioncounter = 0;
}
pulsed1 = selectioncounter;
}
}
pulsed = (100 * pulsed3) + (10 * pulsed2) + pulsed1;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 10); // position to display
display.println("Frequency: HZ Pulse width: us");
display.setCursor(80, 10); // position to display
display.println(freq); // text to display
display.setCursor(82, 26); // position to display
display.println(pulsed3); // text to display
display.setCursor(88, 26); // position to display
display.println(pulsed2); // text to display
display.setCursor(94, 26); // position to display
display.println(pulsed1); // text to display
display.setCursor(94, 29); // position to display
display.println("_"); // text to display
display.display(); // show on OLED
if (digitalRead(selectionselectpin) == HIGH) //Detection button interface to=HIGH
{
delay(5); //Delay 10ms for the elimination of key leading-edge jitter
if (digitalRead(selectionselectpin) == HIGH) //Confirm button is pressed
{
while (digitalRead(selectionselectpin) == HIGH); //Wait for key
delay(5); //delay 10ms for the elimination of key trailing-edge jitter
while (digitalRead(selectionselectpin) == HIGH); //Confirm button release
selectionselectcounter = false;
}
}
}
freq = (100 * freq1) + (10 * freq2) + freq3;
pulsed = (100 * pulsed3) + (10 * pulsed2) + pulsed1;
}