-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rodriguez.ino
97 lines (80 loc) · 2.56 KB
/
Rodriguez.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
#include <TimerOne.h>
#define BASE_OUT_PIN 9
#define COLLECTOR_OUT_PIN 10
#define SWITCHF 10000 //Generate PWM at 10kHz.
//Minimum pulse width is about 130 microseconds, so higher frequencies lose duty cycle steps at the low end.
//At 10kHz, pulse widths 0 and 1 generate no pulse, above 2 generates a pulse.
//trying for 4 extra bits from the ADC (equivalent of 14 bits instead of 10.)
//Oversample by 4^n to get n extra bits. 4^4=256
#define OVERSAMPLING 256
float fperiod=(1/((float)SWITCHF))*1000000;
unsigned long period=(int)fperiod;
String message_buffer;
unsigned long dutycycle=1; //Start at zero volts output
void setup() {
Serial.begin(1000000);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
// }
pinMode(BASE_OUT_PIN, OUTPUT);
Timer1.initialize(period);
Timer1.pwm(BASE_OUT_PIN,dutycycle);
Timer1.pwm(COLLECTOR_OUT_PIN,dutycycle);
}
void loop() {
if (Serial.available() > 0){
int numChar = Serial.available();
byte received_byte=0;
while (numChar--){
received_byte=Serial.read();
//discard line feeds
if (received_byte==10){
continue;
}
//Set Base output on "B" (66)
if (received_byte==66){
handle_message(BASE_OUT_PIN);
makeMeasurementsAndSendMessage();
continue;
}
//Set Collector output on "C" (67)
if (received_byte==67){
handle_message(COLLECTOR_OUT_PIN);
makeMeasurementsAndSendMessage();
continue;
}
if (received_byte!=10 && received_byte!=13 &&isDigit(received_byte)) {
message_buffer=message_buffer + char(received_byte);
continue;
}
}
}
}
void handle_message(int pin){
if (message_buffer==""){
return;
}
dutycycle=message_buffer.toInt();
message_buffer="";
if (dutycycle<0){
dutycycle=0;
}
if (dutycycle>1023){
dutycycle=1023;
}
Timer1.pwm(pin,dutycycle); //,period);
}
void makeMeasurementsAndSendMessage(){
long counter = OVERSAMPLING;
unsigned long VBias = 0;
unsigned long VBase = 0;
unsigned long VCollector = 0;
unsigned long VCollectorBias = 0;
while (counter--){
VBias = VBias + analogRead(A0);
VBase = VBase + analogRead(A1);
VCollector = VCollector + analogRead(A2);
VCollectorBias = VCollectorBias + analogRead(A3);
}
Serial.println(String(VBias) + "\t" + String(VBase) + "\t" + String(VCollector) + "\t" + String(VCollectorBias));
}