/
pP_function.h
144 lines (120 loc) · 3.54 KB
/
pP_function.h
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
/*
pyr0-piezo functions library
Created by Alan "pyr0ball" Weinstock 6/26/2019
*/
void digitalWriteFast(uint8_t pin, uint8_t x) {
if (pin / 8) { // pin >= 8
PORTB ^= (-x ^ PORTB) & (1 << (pin % 8));
}
else {
PORTD ^= (-x ^ PORTD) & (1 << (pin % 8));
}
}
/*------------------------------------------------*/
void pulse() {
digitalWriteFast(TRG_OUT, LOW);
sensorHReading = 1;
delay(TRG_DUR);
digitalWriteFast(TRG_OUT, HIGH);
}
/*------------------------------------------------*/
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high<<8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
/*------------------------------------------------*/
void adjustVin() {
VOld = Vin;
Vin = readVcc(), DEC;
senseLong = senseThrs * 1024L;
compLong = compThrs * 1024L;
senseInt = (long long) senseLong / Vin;
compInt = (long long) compLong / Vin;
senseInt = (int) senseInt;
compInt = (int) compInt;
}
/*------------------------------------------------*/
void adjustFollow() {
/* Compares diffs of threshold vs read value
if positive, adjusts the follower to within
the range set above*/
ADJ_FOLLOW = (senseInt / 4);
// Analog output (PWM) of duty cycle
analogWrite(V_FOL_PWM, ADJ_FOLLOW);
}
/*------------------------------------------------*/
void adjustComp() {
ADJ_COMP = (compInt / 4);
analogWrite(VCOMP_PWM, ADJ_COMP);
}
/*------------------------------------------------*/
void calibrateAlert() {
VLast = VOld - Vin;
if (VLast > Hyst || VLast < -Hyst ) {
ERR_STATE = 1;
}
}
/*------------------------------------------------*/
void adjustGain() {
if (GAIN_FACTOR == 0) {
pinMode(GADJ_R3, INPUT);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 0) {
pinMode(GADJ_R3, OUTPUT);
digitalWrite(GADJ_R3, LOW);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 1) {
pinMode(GADJ_R2, OUTPUT);
digitalWrite(GADJ_R2, LOW);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 2) {
pinMode(GADJ_R1, OUTPUT);
digitalWrite(GADJ_R1, LOW);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 3) {
pinMode(GADJ_R0, OUTPUT);
digitalWrite(GADJ_R0, LOW);
ERR_STATE = 0;
}
}
/*------------------------------------------------*/
void checkError () {
if (ERR_STATE == 1) {
digitalWrite(ERR_LED, BlinkState);
BlinkState = !BlinkState;
}
else if (ERR_STATE == 0) {
BlinkState = LOW;
digitalWrite(ERR_LED, BlinkState);
}
}