switched to faster digitalWrite function for interrupt function
This commit is contained in:
parent
69c9405e0f
commit
c223240210
6 changed files with 575 additions and 564 deletions
|
|
@ -1,154 +1,154 @@
|
||||||
/*
|
/*
|
||||||
Piezoelectric Z-Axis sensor using AtMega88/168/328 (AtMega 48 doesnt have enough memory for this version)
|
Piezoelectric Z-Axis sensor using AtMega88/168/328 (AtMega 48 doesnt have enough memory for this version)
|
||||||
|
|
||||||
This sketch reads a piezo element to detect a touch of the printer's nozzle to the bed.
|
This sketch reads a piezo element to detect a touch of the printer's nozzle to the bed.
|
||||||
The sense pin is tied to an interrupt, which is pulled high by internal pullup resistor.
|
The sense pin is tied to an interrupt, which is pulled high by internal pullup resistor.
|
||||||
When the piezo touches the bed, the amplification circuit will draw the interrupt pin low
|
When the piezo touches the bed, the amplification circuit will draw the interrupt pin low
|
||||||
and the atmega will output a pulse based on the programmed trigger duration
|
and the atmega will output a pulse based on the programmed trigger duration
|
||||||
|
|
||||||
* PD2 INT0 (Piezo In 'D2')
|
* PD2 INT0 (Piezo In 'D2')
|
||||||
* D7 PCINT23 (Trigger OUT 'D7')
|
* D7 PCINT23 (Trigger OUT 'D7')
|
||||||
* PC0 ADC0 (Voltage Reference Check 'A0')
|
* PC0 ADC0 (Voltage Reference Check 'A0')
|
||||||
* PC1 ADC1 (Sensitivity Adjustment Check 'A1')
|
* PC1 ADC1 (Sensitivity Adjustment Check 'A1')
|
||||||
* PD4 PCINT20 (Error feedback LED 'D4')
|
* PD4 PCINT20 (Error feedback LED 'D4')
|
||||||
* PB6 PCINT6 (Voltage Adjustment Resistor 0 'D20')
|
* PB6 PCINT6 (Voltage Adjustment Resistor 0 'D20')
|
||||||
* PB7 PCINT7 (Voltage Adjustment Resistor 1 'D21')
|
* PB7 PCINT7 (Voltage Adjustment Resistor 1 'D21')
|
||||||
* PD5 T1 (Voltage Adjustment Resistor 2 'D5')
|
* PD5 T1 (Voltage Adjustment Resistor 2 'D5')
|
||||||
* PD6 PCINT22 (Voltage Adjustment Resistor 3 'D6')
|
* PD6 PCINT22 (Voltage Adjustment Resistor 3 'D6')
|
||||||
* PB1 OC1A (Comparator VRef PWM Out 'D9')
|
* PB1 OC1A (Comparator VRef PWM Out 'D9')
|
||||||
* PD3 OC2B (Voltage Follower VRef PWM Out 'D3')
|
* PD3 OC2B (Voltage Follower VRef PWM Out 'D3')
|
||||||
|
|
||||||
Schematics for this project can be found here: https://github.com/pyr0ball/pyr0piezo/tree/master/docs/Schematics
|
Schematics for this project can be found here: https://github.com/pyr0ball/pyr0piezo/tree/master/docs/Schematics
|
||||||
|
|
||||||
For Arduino IDE use MCUdude MiniCore: https://mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json
|
For Arduino IDE use MCUdude MiniCore: https://mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json
|
||||||
|
|
||||||
|
|
||||||
created 2/18/2019
|
created 2/18/2019
|
||||||
by Alan "pyr0ball" Weinstock
|
by Alan "pyr0ball" Weinstock
|
||||||
|
|
||||||
This code is in the public domain.
|
This code is in the public domain.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* To set the below parameters using serial input, use the following:
|
/* To set the below parameters using serial input, use the following:
|
||||||
|
|
||||||
To change trigger active duration: TRG_D [integer for milliseconds]
|
To change trigger active duration: TRG_D [integer for milliseconds]
|
||||||
To change gain factor: GAIN_F [integer for gain state - see note*]
|
To change gain factor: GAIN_F [integer for gain state - see note*]
|
||||||
To change ADC hysteresis value: HYST [integer]
|
To change ADC hysteresis value: HYST [integer]
|
||||||
To change sensor input pullup vRef low threshold: VADJ [float value]
|
To change sensor input pullup vRef low threshold: VADJ [float value]
|
||||||
To change comparator trigger high threshold: VCOMP [float value]
|
To change comparator trigger high threshold: VCOMP [float value]
|
||||||
|
|
||||||
|
|
||||||
These commands should be wrapped in this format:
|
These commands should be wrapped in this format:
|
||||||
<CMD, INT>
|
<CMD, INT>
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
<GAIN_F, 3> <~ set gain factor to index 3 (6x)
|
<GAIN_F, 3> <~ set gain factor to index 3 (6x)
|
||||||
<VADJ, 2350> <~ set the vref floor to 2.35V
|
<VADJ, 2350> <~ set the vref floor to 2.35V
|
||||||
|
|
||||||
*Note for Gain Factor:
|
*Note for Gain Factor:
|
||||||
The gain STATE is representative of these values:
|
The gain STATE is representative of these values:
|
||||||
0 = 3x
|
0 = 3x
|
||||||
1 = 3.5x
|
1 = 3.5x
|
||||||
2 = 4.33x
|
2 = 4.33x
|
||||||
3 = 6x
|
3 = 6x
|
||||||
4 = 11x
|
4 = 11x
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Configurable settings:
|
// Configurable settings:
|
||||||
int GAIN_FACTOR = 2; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
|
int GAIN_FACTOR = 2; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
|
||||||
#define InitCount 6 // Number of times to blink the LED on start
|
#define InitCount 6 // Number of times to blink the LED on start
|
||||||
int LOOP_DUR = 50; // duration of time between ADC checks and other loop functions
|
int LOOP_DUR = 50; // duration of time between ADC checks and other loop functions
|
||||||
int TRG_DUR = 20; // duration of the Z-axis pulse sent, in ms
|
int TRG_DUR = 20; // duration of the Z-axis pulse sent, in ms
|
||||||
#define senseThrs 1450
|
#define senseThrs 1450
|
||||||
#define compThrs 2850
|
#define compThrs 2850
|
||||||
int Hyst = 20; // Hysteresis value for ADC measurements
|
int Hyst = 20; // Hysteresis value for ADC measurements
|
||||||
|
|
||||||
/*------------------------------------------------------------*/
|
/*------------------------------------------------------------*/
|
||||||
|
|
||||||
// Debug output toggle. Uncomment to enable
|
// Debug output toggle. Uncomment to enable
|
||||||
#define DEBUG true
|
#define DEBUG true
|
||||||
|
|
||||||
/* Debug output verbose mode will continuously output sensor readings
|
/* Debug output verbose mode will continuously output sensor readings
|
||||||
rather than waiting for user input */
|
rather than waiting for user input */
|
||||||
//#define VERBOSE true
|
//#define VERBOSE true
|
||||||
|
|
||||||
// Headers, variables, and functions
|
// Headers, variables, and functions
|
||||||
#include "pP_pins.h"
|
#include "pP_pins.h"
|
||||||
#include "pP_volatile.h"
|
#include "pP_volatile.h"
|
||||||
#include "pP_function.h"
|
#include "pP_function.h"
|
||||||
#include "pP_serial.h"
|
#include "pP_serial.h"
|
||||||
|
|
||||||
// i2c input toggle. Uncomment to enable
|
// i2c input toggle. Uncomment to enable
|
||||||
#define I2C true
|
#define I2C true
|
||||||
#ifdef I2C
|
#ifdef I2C
|
||||||
#include "pP_i2c.h"
|
#include "pP_i2c.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(TRG_OUT, OUTPUT); // declare the Trigger as as OUTPUT
|
pinMode(TRG_OUT, OUTPUT); // declare the Trigger as as OUTPUT
|
||||||
pinMode(ERR_LED, OUTPUT);
|
pinMode(ERR_LED, OUTPUT);
|
||||||
pinMode(Z_TRG, INPUT_PULLUP); // declare z-sense input with pullup
|
pinMode(Z_TRG, INPUT_PULLUP); // declare z-sense input with pullup
|
||||||
pinMode(V_FOLLOW_PIN, INPUT);
|
pinMode(V_FOLLOW_PIN, INPUT);
|
||||||
pinMode(VCOMP_SENSE_PIN, INPUT);
|
pinMode(VCOMP_SENSE_PIN, INPUT);
|
||||||
pinMode(GADJ_R0, INPUT); // declare input to set high impedance
|
pinMode(GADJ_R0, INPUT); // declare input to set high impedance
|
||||||
pinMode(GADJ_R1, INPUT); // declare input to set high impedance
|
pinMode(GADJ_R1, INPUT); // declare input to set high impedance
|
||||||
pinMode(GADJ_R2, INPUT); // declare input to set high impedance
|
pinMode(GADJ_R2, INPUT); // declare input to set high impedance
|
||||||
pinMode(GADJ_R3, INPUT); // declare input to set high impedance
|
pinMode(GADJ_R3, INPUT); // declare input to set high impedance
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING);
|
attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING);
|
||||||
|
|
||||||
Serial.println("Initializing Pyr0-Piezo Sensor...");
|
Serial.println("Initializing Pyr0-Piezo Sensor...");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
// Blink LED's on init
|
// Blink LED's on init
|
||||||
if (BlinkCount > 0) {
|
if (BlinkCount > 0) {
|
||||||
BlinkState = !BlinkState;
|
BlinkState = !BlinkState;
|
||||||
digitalWrite(ERR_LED, BlinkState);
|
digitalWrite(ERR_LED, BlinkState);
|
||||||
digitalWrite(TRG_OUT, BlinkState);
|
digitalWrite(TRG_OUT, BlinkState);
|
||||||
delay(LOOP_DUR);
|
delay(LOOP_DUR);
|
||||||
--BlinkCount;
|
--BlinkCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Serial Input
|
// Get Serial Input
|
||||||
serialInput();
|
serialInput();
|
||||||
|
|
||||||
// Set any new parameters from serial input
|
// Set any new parameters from serial input
|
||||||
updateParams();
|
updateParams();
|
||||||
|
|
||||||
// Set the amplification gain factor
|
// Set the amplification gain factor
|
||||||
adjustGain();
|
adjustGain();
|
||||||
|
|
||||||
// Check voltage of first and second stages and compare against thresholds
|
// Check voltage of first and second stages and compare against thresholds
|
||||||
adjustVin();
|
adjustVin();
|
||||||
VComp = analogRead(VCOMP_SENSE_PIN);
|
VComp = analogRead(VCOMP_SENSE_PIN);
|
||||||
VAdj = analogRead(V_FOLLOW_PIN);
|
VAdj = analogRead(V_FOLLOW_PIN);
|
||||||
|
|
||||||
// Voltage Follower adjustment
|
// Voltage Follower adjustment
|
||||||
if (VLast > Hyst || VLast < -Hyst) {
|
if (VLast > Hyst || VLast < -Hyst) {
|
||||||
adjustFollow();
|
adjustFollow();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Voltage Comparator adjustment
|
// Voltage Comparator adjustment
|
||||||
if (VLast > Hyst || VLast < -Hyst) {
|
if (VLast > Hyst || VLast < -Hyst) {
|
||||||
adjustComp();
|
adjustComp();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alert the user that auto-calibration is ongoing
|
// Alert the user that auto-calibration is ongoing
|
||||||
calibrateAlert();
|
calibrateAlert();
|
||||||
|
|
||||||
// Check for error state
|
// Check for error state
|
||||||
checkError();
|
checkError();
|
||||||
|
|
||||||
// Reply with status
|
// Reply with status
|
||||||
serialReply();
|
serialReply();
|
||||||
|
|
||||||
// Sets trigger output state to false after completing loop
|
// Sets trigger output state to false after completing loop
|
||||||
delay(LOOP_DUR);
|
delay(LOOP_DUR);
|
||||||
digitalWrite(TRG_OUT, HIGH);
|
digitalWrite(TRG_OUT, HIGH);
|
||||||
sensorHReading = 0;
|
sensorHReading = 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,141 +1,149 @@
|
||||||
/*
|
/*
|
||||||
pyr0-piezo functions library
|
pyr0-piezo functions library
|
||||||
Created by Alan "pyr0ball" Weinstock 6/26/2019
|
Created by Alan "pyr0ball" Weinstock 6/26/2019
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*------------------------------------------------*/
|
void digitalWriteFast(uint8_t pin, uint8_t x) {
|
||||||
|
if (pin / 8) { // pin >= 8
|
||||||
void pulse() {
|
PORTB ^= (-x ^ PORTB) & (1 << (pin % 8));
|
||||||
digitalWrite(TRG_OUT, LOW);
|
}
|
||||||
sensorHReading = 1;
|
else {
|
||||||
delay(TRG_DUR);
|
PORTD ^= (-x ^ PORTD) & (1 << (pin % 8));
|
||||||
digitalWrite(TRG_OUT, HIGH);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*------------------------------------------------*/
|
||||||
/*------------------------------------------------*/
|
|
||||||
|
void pulse() {
|
||||||
long readVcc() {
|
digitalWriteFast(TRG_OUT, LOW);
|
||||||
// Read 1.1V reference against AVcc
|
sensorHReading = 1;
|
||||||
// set the reference to Vcc and the measurement to the internal 1.1V reference
|
delay(TRG_DUR);
|
||||||
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
digitalWriteFast(TRG_OUT, HIGH);
|
||||||
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);
|
long readVcc() {
|
||||||
#else
|
// Read 1.1V reference against AVcc
|
||||||
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
|
// set the reference to Vcc and the measurement to the internal 1.1V reference
|
||||||
#endif
|
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
||||||
|
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
|
||||||
delay(2); // Wait for Vref to settle
|
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||||||
ADCSRA |= _BV(ADSC); // Start conversion
|
ADMUX = _BV(MUX5) | _BV(MUX0);
|
||||||
while (bit_is_set(ADCSRA,ADSC)); // measuring
|
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
||||||
|
ADMUX = _BV(MUX3) | _BV(MUX2);
|
||||||
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
|
#else
|
||||||
uint8_t high = ADCH; // unlocks both
|
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
|
||||||
|
#endif
|
||||||
long result = (high<<8) | low;
|
|
||||||
|
delay(2); // Wait for Vref to settle
|
||||||
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
|
ADCSRA |= _BV(ADSC); // Start conversion
|
||||||
return result; // Vcc in millivolts
|
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
|
||||||
|
|
||||||
void adjustVin() {
|
long result = (high<<8) | low;
|
||||||
VOld = Vin;
|
|
||||||
Vin = readVcc(), DEC;
|
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
|
||||||
senseLong = senseThrs * 1024L;
|
return result; // Vcc in millivolts
|
||||||
compLong = compThrs * 1024L;
|
}
|
||||||
senseInt = (long long) senseLong / Vin;
|
|
||||||
compInt = (long long) compLong / Vin;
|
/*------------------------------------------------*/
|
||||||
senseInt = (int) senseInt;
|
|
||||||
compInt = (int) compInt;
|
void adjustVin() {
|
||||||
}
|
VOld = Vin;
|
||||||
|
Vin = readVcc(), DEC;
|
||||||
/*------------------------------------------------*/
|
senseLong = senseThrs * 1024L;
|
||||||
|
compLong = compThrs * 1024L;
|
||||||
void adjustFollow() {
|
senseInt = (long long) senseLong / Vin;
|
||||||
/* Compares diffs of threshold vs read value
|
compInt = (long long) compLong / Vin;
|
||||||
if positive, adjusts the follower to within
|
senseInt = (int) senseInt;
|
||||||
the range set above*/
|
compInt = (int) compInt;
|
||||||
ADJ_FOLLOW = (senseInt / 4);
|
}
|
||||||
|
|
||||||
// Analog output (PWM) of duty cycle
|
/*------------------------------------------------*/
|
||||||
analogWrite(V_FOL_PWM, ADJ_FOLLOW);
|
|
||||||
}
|
void adjustFollow() {
|
||||||
|
/* Compares diffs of threshold vs read value
|
||||||
/*------------------------------------------------*/
|
if positive, adjusts the follower to within
|
||||||
|
the range set above*/
|
||||||
void adjustComp() {
|
ADJ_FOLLOW = (senseInt / 4);
|
||||||
ADJ_COMP = (compInt / 4);
|
|
||||||
|
// Analog output (PWM) of duty cycle
|
||||||
analogWrite(VCOMP_PWM, ADJ_COMP);
|
analogWrite(V_FOL_PWM, ADJ_FOLLOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void calibrateAlert() {
|
void adjustComp() {
|
||||||
VLast = VOld - Vin;
|
ADJ_COMP = (compInt / 4);
|
||||||
if (VLast > Hyst || VLast < -Hyst ) {
|
|
||||||
ERR_STATE = 1;
|
analogWrite(VCOMP_PWM, ADJ_COMP);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/*------------------------------------------------*/
|
||||||
/*------------------------------------------------*/
|
|
||||||
|
void calibrateAlert() {
|
||||||
void adjustGain() {
|
VLast = VOld - Vin;
|
||||||
|
if (VLast > Hyst || VLast < -Hyst ) {
|
||||||
if (GAIN_FACTOR == 0) {
|
ERR_STATE = 1;
|
||||||
pinMode(GADJ_R3, INPUT);
|
}
|
||||||
pinMode(GADJ_R2, INPUT);
|
}
|
||||||
pinMode(GADJ_R1, INPUT);
|
|
||||||
pinMode(GADJ_R0, INPUT);
|
/*------------------------------------------------*/
|
||||||
ERR_STATE = 0;
|
|
||||||
}
|
void adjustGain() {
|
||||||
else if (GAIN_FACTOR > 0) {
|
|
||||||
pinMode(GADJ_R3, OUTPUT);
|
if (GAIN_FACTOR == 0) {
|
||||||
digitalWrite(GADJ_R3, LOW);
|
pinMode(GADJ_R3, INPUT);
|
||||||
pinMode(GADJ_R2, INPUT);
|
pinMode(GADJ_R2, INPUT);
|
||||||
pinMode(GADJ_R1, INPUT);
|
pinMode(GADJ_R1, INPUT);
|
||||||
pinMode(GADJ_R0, INPUT);
|
pinMode(GADJ_R0, INPUT);
|
||||||
ERR_STATE = 0;
|
ERR_STATE = 0;
|
||||||
}
|
}
|
||||||
else if (GAIN_FACTOR > 1) {
|
else if (GAIN_FACTOR > 0) {
|
||||||
pinMode(GADJ_R2, OUTPUT);
|
pinMode(GADJ_R3, OUTPUT);
|
||||||
digitalWrite(GADJ_R2, LOW);
|
digitalWrite(GADJ_R3, LOW);
|
||||||
pinMode(GADJ_R1, INPUT);
|
pinMode(GADJ_R2, INPUT);
|
||||||
pinMode(GADJ_R0, INPUT);
|
pinMode(GADJ_R1, INPUT);
|
||||||
ERR_STATE = 0;
|
pinMode(GADJ_R0, INPUT);
|
||||||
}
|
ERR_STATE = 0;
|
||||||
else if (GAIN_FACTOR > 2) {
|
}
|
||||||
pinMode(GADJ_R1, OUTPUT);
|
else if (GAIN_FACTOR > 1) {
|
||||||
digitalWrite(GADJ_R1, LOW);
|
pinMode(GADJ_R2, OUTPUT);
|
||||||
pinMode(GADJ_R0, INPUT);
|
digitalWrite(GADJ_R2, LOW);
|
||||||
ERR_STATE = 0;
|
pinMode(GADJ_R1, INPUT);
|
||||||
}
|
pinMode(GADJ_R0, INPUT);
|
||||||
else if (GAIN_FACTOR > 3) {
|
ERR_STATE = 0;
|
||||||
pinMode(GADJ_R0, OUTPUT);
|
}
|
||||||
digitalWrite(GADJ_R0, LOW);
|
else if (GAIN_FACTOR > 2) {
|
||||||
ERR_STATE = 0;
|
pinMode(GADJ_R1, OUTPUT);
|
||||||
}
|
digitalWrite(GADJ_R1, LOW);
|
||||||
}
|
pinMode(GADJ_R0, INPUT);
|
||||||
|
ERR_STATE = 0;
|
||||||
/*------------------------------------------------*/
|
}
|
||||||
|
else if (GAIN_FACTOR > 3) {
|
||||||
void checkError () {
|
pinMode(GADJ_R0, OUTPUT);
|
||||||
if (ERR_STATE == 1) {
|
digitalWrite(GADJ_R0, LOW);
|
||||||
digitalWrite(ERR_LED, BlinkState);
|
ERR_STATE = 0;
|
||||||
BlinkState = !BlinkState;
|
}
|
||||||
}
|
}
|
||||||
else if (ERR_STATE == 0) {
|
|
||||||
BlinkState = LOW;
|
/*------------------------------------------------*/
|
||||||
digitalWrite(ERR_LED, BlinkState);
|
|
||||||
}
|
void checkError () {
|
||||||
}
|
if (ERR_STATE == 1) {
|
||||||
|
digitalWrite(ERR_LED, BlinkState);
|
||||||
/*------------------------------------------------*/
|
BlinkState = !BlinkState;
|
||||||
|
}
|
||||||
|
else if (ERR_STATE == 0) {
|
||||||
// #endif
|
BlinkState = LOW;
|
||||||
|
digitalWrite(ERR_LED, BlinkState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
|
|
||||||
|
// #endif
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,25 @@
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
#ifdef I2C
|
#ifdef I2C
|
||||||
void i2cReply() {
|
void i2cReply() {
|
||||||
if (serialIncoming) {
|
if (serialIncoming) {
|
||||||
Wire.write("OK");
|
Wire.write("OK");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
#ifdef I2C
|
#ifdef I2C
|
||||||
void i2cInput() {
|
void i2cInput() {
|
||||||
|
|
||||||
// receive data from Serial and save it into inputBuffer
|
// receive data from Serial and save it into inputBuffer
|
||||||
|
|
||||||
while(Wire.available()) {
|
while(Wire.available()) {
|
||||||
identifyMarkers();
|
identifyMarkers();
|
||||||
updateParams();
|
updateParams();
|
||||||
i2cReply();
|
i2cReply();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,32 @@
|
||||||
/* pyr0-piezo pins configuration file
|
/* pyr0-piezo pins configuration file
|
||||||
|
|
||||||
Default pins (based on Rev.2.x.xPCB layout)
|
Default pins (based on Rev.2.x.xPCB layout)
|
||||||
|
|
||||||
* PD2 INT0 (Piezo In 'D2')
|
* PD2 INT0 (Piezo In 'D2')
|
||||||
* D7 PCINT23 (Trigger OUT 'D7')
|
* D7 PCINT23 (Trigger OUT 'D7')
|
||||||
* PC0 ADC0 (Voltage Reference Check 'A0')
|
* PC0 ADC0 (Voltage Reference Check 'A0')
|
||||||
* PC1 ADC1 (Sensitivity Adjustment Check 'A1')
|
* PC1 ADC1 (Sensitivity Adjustment Check 'A1')
|
||||||
* PD4 PCINT20 (Error feedback LED 'D4')
|
* PD4 PCINT20 (Error feedback LED 'D4')
|
||||||
* PB6 PCINT6 (Voltage Adjustment Resistor 0 'D20')
|
* PB6 PCINT6 (Voltage Adjustment Resistor 0 'D20')
|
||||||
* PB7 PCINT7 (Voltage Adjustment Resistor 1 'D21')
|
* PB7 PCINT7 (Voltage Adjustment Resistor 1 'D21')
|
||||||
* PD5 T1 (Voltage Adjustment Resistor 2 'D5')
|
* PD5 T1 (Voltage Adjustment Resistor 2 'D5')
|
||||||
* PD6 PCINT22 (Voltage Adjustment Resistor 3 'D6')
|
* PD6 PCINT22 (Voltage Adjustment Resistor 3 'D6')
|
||||||
* PB1 OC1A (Comparator VRef PWM Out 'D9')
|
* PB1 OC1A (Comparator VRef PWM Out 'D9')
|
||||||
* PD3 OC2B (Voltage Follower VRef PWM Out 'D3')
|
* PD3 OC2B (Voltage Follower VRef PWM Out 'D3')
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Analog Pin Assignments
|
// Analog Pin Assignments
|
||||||
#define V_FOLLOW_PIN A0 // Sense pin to check Voltage Follower stage
|
#define V_FOLLOW_PIN A0 // Sense pin to check Voltage Follower stage
|
||||||
#define VCOMP_SENSE_PIN A1 // Sense pin to check comparator stage voltage
|
#define VCOMP_SENSE_PIN A1 // Sense pin to check comparator stage voltage
|
||||||
|
|
||||||
// Digital Pin Assignments
|
// Digital Pin Assignments
|
||||||
#define TRG_OUT 7 // LED and Z-Min trigger output connected to digital pin 7
|
#define TRG_OUT 7 // LED and Z-Min trigger output connected to digital pin 7
|
||||||
//#define TRG_OUT 13 // For testing on Atmega328/2560, Output is moved to onboard LED pin
|
//#define TRG_OUT 13 // For testing on Atmega328/2560, Output is moved to onboard LED pin
|
||||||
#define Z_TRG 2 // the piezo is connected to INT0 / digital pin 2
|
#define Z_TRG 2 // the piezo is connected to INT0 / digital pin 2
|
||||||
#define ERR_LED 4 // LED will blink if optimal voltage range cannot be achieved
|
#define ERR_LED 4 // LED will blink if optimal voltage range cannot be achieved
|
||||||
#define GADJ_R0 20 // Auto-adjust ladder pin assignments
|
#define GADJ_R0 20 // Auto-adjust ladder pin assignments
|
||||||
#define GADJ_R1 21 // "
|
#define GADJ_R1 21 // "
|
||||||
#define GADJ_R2 5 // "
|
#define GADJ_R2 5 // "
|
||||||
#define GADJ_R3 6 // "
|
#define GADJ_R3 6 // "
|
||||||
#define V_FOL_PWM 3 // PWM analog output pin for voltage follower adjustment
|
#define V_FOL_PWM 3 // PWM analog output pin for voltage follower adjustment
|
||||||
#define VCOMP_PWM 9 // PWM analog output pin for comparator adjustment
|
#define VCOMP_PWM 9 // PWM analog output pin for comparator adjustment
|
||||||
|
|
@ -1,173 +1,173 @@
|
||||||
|
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void parseData() {
|
void parseData() {
|
||||||
|
|
||||||
// split the data into its parts
|
// split the data into its parts
|
||||||
|
|
||||||
char * strtokIndx; // this is used by strtok() as an index
|
char * strtokIndx; // this is used by strtok() as an index
|
||||||
|
|
||||||
strtokIndx = strtok(inputBuffer,","); // get the first part - the string
|
strtokIndx = strtok(inputBuffer,","); // get the first part - the string
|
||||||
strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn
|
strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn
|
||||||
|
|
||||||
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
|
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
|
||||||
serialInt = atoi(strtokIndx); // convert this part to an integer
|
serialInt = atoi(strtokIndx); // convert this part to an integer
|
||||||
|
|
||||||
}
|
}
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void identifyMarkers() {
|
void identifyMarkers() {
|
||||||
|
|
||||||
char x = Serial.read();
|
char x = Serial.read();
|
||||||
// char y = Wire.read();
|
// char y = Wire.read();
|
||||||
|
|
||||||
if (x == endMarker) {
|
if (x == endMarker) {
|
||||||
readInProgress = false;
|
readInProgress = false;
|
||||||
serialIncoming = true;
|
serialIncoming = true;
|
||||||
inputBuffer[bytesRecvd] = 0;
|
inputBuffer[bytesRecvd] = 0;
|
||||||
parseData();
|
parseData();
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(readInProgress) {
|
else if(readInProgress) {
|
||||||
inputBuffer[bytesRecvd] = x;
|
inputBuffer[bytesRecvd] = x;
|
||||||
bytesRecvd ++;
|
bytesRecvd ++;
|
||||||
if (bytesRecvd == buffSize) {
|
if (bytesRecvd == buffSize) {
|
||||||
bytesRecvd = buffSize - 1;
|
bytesRecvd = buffSize - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (x == startMarker) {
|
else if (x == startMarker) {
|
||||||
bytesRecvd = 0;
|
bytesRecvd = 0;
|
||||||
readInProgress = true;
|
readInProgress = true;
|
||||||
}
|
}
|
||||||
#ifdef I2C
|
#ifdef I2C
|
||||||
if (y == endMarker) {
|
if (y == endMarker) {
|
||||||
readInProgress = false;
|
readInProgress = false;
|
||||||
serialIncoming = true;
|
serialIncoming = true;
|
||||||
inputBuffer[bytesRecvd] = 0;
|
inputBuffer[bytesRecvd] = 0;
|
||||||
parseData();
|
parseData();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(readInProgress) {
|
if(readInProgress) {
|
||||||
inputBuffer[bytesRecvd] = y;
|
inputBuffer[bytesRecvd] = y;
|
||||||
bytesRecvd ++;
|
bytesRecvd ++;
|
||||||
if (bytesRecvd == buffSize) {
|
if (bytesRecvd == buffSize) {
|
||||||
bytesRecvd = buffSize - 1;
|
bytesRecvd = buffSize - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (y == startMarker) {
|
if (y == startMarker) {
|
||||||
bytesRecvd = 0;
|
bytesRecvd = 0;
|
||||||
readInProgress = true;
|
readInProgress = true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void updateTrigDuration() {
|
void updateTrigDuration() {
|
||||||
if (serialInt >= 0) {
|
if (serialInt >= 0) {
|
||||||
TRG_DUR = serialInt;
|
TRG_DUR = serialInt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void updateGainFactor() {
|
void updateGainFactor() {
|
||||||
if (serialInt >= 0) {
|
if (serialInt >= 0) {
|
||||||
GAIN_FACTOR = serialInt;
|
GAIN_FACTOR = serialInt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void updateVComp() {
|
void updateVComp() {
|
||||||
if (serialInt >= 0) {
|
if (serialInt >= 0) {
|
||||||
compInt = serialInt;
|
compInt = serialInt;
|
||||||
//senseInt = compInt; // syncing these params til #24 is fixed
|
//senseInt = compInt; // syncing these params til #24 is fixed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void updateVAdj() {
|
void updateVAdj() {
|
||||||
if (serialInt >= 0) {
|
if (serialInt >= 0) {
|
||||||
senseInt = serialInt;
|
senseInt = serialInt;
|
||||||
//compInt = senseInt; // syncing these params til #24 is fixed
|
//compInt = senseInt; // syncing these params til #24 is fixed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void updateHysteresis() {
|
void updateHysteresis() {
|
||||||
if (serialInt >= 0) {
|
if (serialInt >= 0) {
|
||||||
Hyst = serialInt;
|
Hyst = serialInt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void updateParams() {
|
void updateParams() {
|
||||||
if (strcmp(serialMessageIn, "TRG_D") == 0) {
|
if (strcmp(serialMessageIn, "TRG_D") == 0) {
|
||||||
updateTrigDuration();
|
updateTrigDuration();
|
||||||
}
|
}
|
||||||
else if (strcmp(serialMessageIn, "GAIN_F") == 0) {
|
else if (strcmp(serialMessageIn, "GAIN_F") == 0) {
|
||||||
updateGainFactor();
|
updateGainFactor();
|
||||||
}
|
}
|
||||||
else if (strcmp(serialMessageIn, "VCOMP") == 0) {
|
else if (strcmp(serialMessageIn, "VCOMP") == 0) {
|
||||||
updateVComp();
|
updateVComp();
|
||||||
}
|
}
|
||||||
else if (strcmp(serialMessageIn, "VADJ") == 0) {
|
else if (strcmp(serialMessageIn, "VADJ") == 0) {
|
||||||
updateVAdj();
|
updateVAdj();
|
||||||
}
|
}
|
||||||
else if (strcmp(serialMessageIn, "HYST") == 0) {
|
else if (strcmp(serialMessageIn, "HYST") == 0) {
|
||||||
updateHysteresis();
|
updateHysteresis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void serialInput() {
|
void serialInput() {
|
||||||
|
|
||||||
// receive data from Serial and save it into inputBuffer
|
// receive data from Serial and save it into inputBuffer
|
||||||
|
|
||||||
if(Serial.available() > 0) {
|
if(Serial.available() > 0) {
|
||||||
|
|
||||||
// the order of these IF clauses is significant
|
// the order of these IF clauses is significant
|
||||||
identifyMarkers();
|
identifyMarkers();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------*/
|
/*------------------------------------------------*/
|
||||||
|
|
||||||
void serialReply() {
|
void serialReply() {
|
||||||
#ifndef VERBOSE
|
#ifndef VERBOSE
|
||||||
if (serialIncoming) {
|
if (serialIncoming) {
|
||||||
serialIncoming = false;
|
serialIncoming = false;
|
||||||
#endif
|
#endif
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.print("Vcc:");
|
Serial.print("Vcc:");
|
||||||
Serial.println(Vin);
|
Serial.println(Vin);
|
||||||
Serial.print("Comp Sense:");
|
Serial.print("Comp Sense:");
|
||||||
Serial.print(VComp);
|
Serial.print(VComp);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
Serial.print("Comparator State:");
|
Serial.print("Comparator State:");
|
||||||
Serial.print(ADJ_COMP);
|
Serial.print(ADJ_COMP);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
Serial.println(compInt);
|
Serial.println(compInt);
|
||||||
|
|
||||||
Serial.print("Amp Sense:");
|
Serial.print("Amp Sense:");
|
||||||
Serial.print(VAdj);
|
Serial.print(VAdj);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
Serial.print("Follower State:");
|
Serial.print("Follower State:");
|
||||||
Serial.print(ADJ_FOLLOW);
|
Serial.print(ADJ_FOLLOW);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
Serial.println(senseInt);
|
Serial.println(senseInt);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Serial.print("Delay:");
|
Serial.print("Delay:");
|
||||||
Serial.println(TRG_DUR);
|
Serial.println(TRG_DUR);
|
||||||
Serial.print("Error State:");
|
Serial.print("Error State:");
|
||||||
Serial.println(ERR_STATE);
|
Serial.println(ERR_STATE);
|
||||||
Serial.println("------------------");
|
Serial.println("------------------");
|
||||||
#ifndef VERBOSE
|
#ifndef VERBOSE
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,43 @@
|
||||||
// these variables will change on their own. Do not edit ANYTHING below this line
|
// these variables will change on their own. Do not edit ANYTHING below this line
|
||||||
volatile int sensorHReading = 0; // variable to store the value read from the sensor pin
|
volatile int sensorHReading = 0; // variable to store the value read from the sensor pin
|
||||||
volatile int ADJ_FOLLOW = 0; // Variable for Follower adjustment
|
volatile int ADJ_FOLLOW = 0; // Variable for Follower adjustment
|
||||||
volatile int ADJ_COMP = 0; // Variable for Comparator adjustment
|
volatile int ADJ_COMP = 0; // Variable for Comparator adjustment
|
||||||
volatile int ERR_STATE = 0;
|
volatile int ERR_STATE = 0;
|
||||||
|
|
||||||
int Vin = 5000; // input reference voltage in millivolts (multiply V by 1000)
|
int Vin = 5000; // input reference voltage in millivolts (multiply V by 1000)
|
||||||
int VOld = 5000; // Variable to store previous cycle's Vin
|
int VOld = 5000; // Variable to store previous cycle's Vin
|
||||||
int VLast = 0;
|
int VLast = 0;
|
||||||
|
|
||||||
// Convert threshold values based on the input voltage
|
// Convert threshold values based on the input voltage
|
||||||
long senseLong = senseThrs * 1024L;
|
long senseLong = senseThrs * 1024L;
|
||||||
long compLong = compThrs * 1024L;
|
long compLong = compThrs * 1024L;
|
||||||
long senseInt = senseLong / Vin;
|
long senseInt = senseLong / Vin;
|
||||||
long compInt = compLong / Vin;
|
long compInt = compLong / Vin;
|
||||||
|
|
||||||
// Voltage Comparator Adjustment parameters
|
// Voltage Comparator Adjustment parameters
|
||||||
int VComp = 0;
|
int VComp = 0;
|
||||||
int diffCompL = VComp - compInt;
|
int diffCompL = VComp - compInt;
|
||||||
int diffCompH = compInt - VComp;
|
int diffCompH = compInt - VComp;
|
||||||
|
|
||||||
// Voltage Follower Adjustment parameters
|
// Voltage Follower Adjustment parameters
|
||||||
int VAdj = 0;
|
int VAdj = 0;
|
||||||
int diffAdjL = VAdj - senseInt;
|
int diffAdjL = VAdj - senseInt;
|
||||||
int diffAdjH = senseInt - VAdj;
|
int diffAdjH = senseInt - VAdj;
|
||||||
|
|
||||||
// Error blink parameters
|
// Error blink parameters
|
||||||
int BlinkState = LOW;
|
int BlinkState = LOW;
|
||||||
int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle toggle state
|
int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle toggle state
|
||||||
|
|
||||||
// Serial Input Parsing Variables
|
// Serial Input Parsing Variables
|
||||||
#define buffSize 40
|
#define buffSize 40
|
||||||
char inputBuffer[buffSize];
|
char inputBuffer[buffSize];
|
||||||
#define startMarker '<'
|
#define startMarker '<'
|
||||||
#define endMarker '>'
|
#define endMarker '>'
|
||||||
byte bytesRecvd = 0;
|
byte bytesRecvd = 0;
|
||||||
bool readInProgress = false;
|
bool readInProgress = false;
|
||||||
bool serialIncoming = false;
|
bool serialIncoming = false;
|
||||||
char serialMessageIn[buffSize] = {0};
|
char serialMessageIn[buffSize] = {0};
|
||||||
int serialInt = 0;
|
int serialInt = 0;
|
||||||
|
|
||||||
|
#define LOW 0
|
||||||
|
#define HIGH 1
|
||||||
Loading…
Reference in a new issue