More separation of declaration and definition

This commit is contained in:
loredan13 2020-02-19 19:25:24 +03:00
parent bf800e9940
commit 60a26ad3f7
6 changed files with 258 additions and 193 deletions

View file

@ -2,6 +2,17 @@
#include "pP_function.h" #include "pP_function.h"
#include <EEPROM.h> #include <EEPROM.h>
int GAIN_FACTOR = GAIN_FACTOR_DEFAULT;
int followerThrs = FOLLOWER_THRESHOLD_DEFAULT;
int compThrs = COMP_THRESHOLD_DEFAULT;
int LOOP_DUR = LOOP_DUR_DEFAULT;
int TRG_DUR = TRG_DUR_DEFAULT;
int Hyst = HYST_DEFAULT;
int LOGIC = LOGIC_DEFAULT;
int PZDET = PZDET_DEFAULT;
int Debug = 0;
long voltMeterConstant = VM_CONST_DEFAULT;
/*------------------------------------------------*/ /*------------------------------------------------*/
void eraseEEPROM() { void eraseEEPROM() {

View file

@ -5,15 +5,15 @@
#define GAIN_FACTOR_DEFAULT 2 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x #define GAIN_FACTOR_DEFAULT 2 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
#define GAIN_FACTOR_ADDRESS 0 #define GAIN_FACTOR_ADDRESS 0
int GAIN_FACTOR = GAIN_FACTOR_DEFAULT; extern int GAIN_FACTOR;
#define FOLLOWER_THRESHOLD_DEFAULT 1450 // Voltage follower default voltage in mV #define FOLLOWER_THRESHOLD_DEFAULT 1450 // Voltage follower default voltage in mV
#define FOLLOWER_THRESHOLD_ADDRESS 4 #define FOLLOWER_THRESHOLD_ADDRESS 4
int followerThrs = 1450; extern int followerThrs;
#define COMP_THRESHOLD_DEFAULT 2850 // Comparatore Vref default voltage in mV #define COMP_THRESHOLD_DEFAULT 2850 // Comparatore Vref default voltage in mV
#define COMP_THRESHOLD_ADDRESS 8 #define COMP_THRESHOLD_ADDRESS 8
int compThrs = COMP_THRESHOLD_DEFAULT; extern int compThrs;
#ifndef InitCount #ifndef InitCount
#define InitCount 6 // Number of times to blink the LED on start #define InitCount 6 // Number of times to blink the LED on start
@ -21,29 +21,29 @@ int compThrs = COMP_THRESHOLD_DEFAULT;
#define LOOP_DUR_DEFAULT 50 // duration of time between ADC checks and other loop functions #define LOOP_DUR_DEFAULT 50 // duration of time between ADC checks and other loop functions
#define LOOP_DUR_ADDRESS 12 #define LOOP_DUR_ADDRESS 12
int LOOP_DUR = LOOP_DUR_DEFAULT; extern int LOOP_DUR;
#define TRG_DUR_DEFAULT 20 // duration of the Z-axis pulse sent, in ms #define TRG_DUR_DEFAULT 20 // duration of the Z-axis pulse sent, in ms
#define TRG_DUR_ADDRESS 16 #define TRG_DUR_ADDRESS 16
int TRG_DUR = TRG_DUR_DEFAULT; extern int TRG_DUR;
#define HYST_DEFAULT 20 #define HYST_DEFAULT 20
#define HYST_ADDRESS 20 #define HYST_ADDRESS 20
int Hyst = HYST_DEFAULT; // Hysteresis value for ADC measurements extern int Hyst; // Hysteresis value for ADC measurements
#define LOGIC_DEFAULT 1 #define LOGIC_DEFAULT 1
#define LOGIC_ADDRESS 32 #define LOGIC_ADDRESS 32
int LOGIC = LOGIC_DEFAULT; // Trigger logic scheme, Active LOW is default extern int LOGIC; // Trigger logic scheme, Active LOW is default
#define PZDET_DEFAULT 0 #define PZDET_DEFAULT 0
#define PZDET_ADDRESS 26 #define PZDET_ADDRESS 26
int PZDET = PZDET_DEFAULT; // Enable or disable piezo connection detection, default is off extern int PZDET; // Enable or disable piezo connection detection, default is off
int Debug = 0; extern int Debug;
#define VM_CONST_ADDRESS 28 #define VM_CONST_ADDRESS 28
#define VM_CONST_DEFAULT 1125300L #define VM_CONST_DEFAULT 1125300L
long voltMeterConstant = VM_CONST_DEFAULT; // For fine tuning input voltage sense extern long voltMeterConstant; // For fine tuning input voltage sense
#ifdef I2C_INPUT #ifdef I2C_INPUT
#define I2C_SLAVE_ADDRESS 24 #define I2C_SLAVE_ADDRESS 24

View file

@ -0,0 +1,216 @@
/*
pyr0-piezo functions library
Created by Alan "pyr0ball" Weinstock 6/26/2019
*/
//#pragma once
//#include "pP_function.h"
#include "pP_function.h"
#include "Arduino.h"
#include "pP_config.h"
#include "pP_volatile.h"
#include "pP_pins.h"
#include "stdint.h"
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));
}
}
int analogReadFast(uint8_t ADCpin)
{
byte ADCSRAoriginal = ADCSRA;
ADCSRA = (ADCSRA & B11111000) | 4;
int adc = analogRead(ADCpin);
ADCSRA = ADCSRAoriginal;
return adc;
}
/*------------------------------------------------*/
void doubleFlash()
{
BlinkCount = 4;
}
/*------------------------------------------------*/
void pulse()
{
digitalWriteFast(TRG_OUT, LOGIC);
sensorHReading = 1;
delay(TRG_DUR);
digitalWriteFast(TRG_OUT, !LOGIC);
Serial.println("Trig'd!");
doubleFlash();
}
/*------------------------------------------------*/
long readVcc()
{
// Read 1.1V reference against AVcc
// Atmega's Secret Voltmeter setup:
// 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 = voltMeterConstant / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
/*-------------------------------------------------
The above function assumes an "ideal" multiplier constant.
Each Atmega chip is slightly different, so it won't be completely accurate
without tuning. Most of the time this won't be necessary, so don't mess
with this if you don't know what you're doing!
The reading can be fine-tuned by using a multimeter, and this equation:
scale_constant = internal1.1Ref * 1023 * 1000
where
internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function)
If the scale_constant calculated is different from the default 1125300,
update the voltMeterConstant variable in pP_config.h with the correct value
--------------------------------------------------*/
void readVin()
{
VOld = Vin;
Vin = readVcc();
followerLong = followerThrs * 1023L;
compLong = compThrs * 1023L;
followerInt = (long long)followerLong / Vin;
compInt = (long long)compLong / Vin;
followerInt = (int)followerInt;
compInt = (int)compInt;
}
/*------------------------------------------------*/
void adjustFollow()
{
/* Compares diffs of threshold vs read value
if positive, adjusts the follower to within
the range set above*/
followerLong = followerThrs * 1023L;
followerInt = (long long)followerLong / Vin;
followerInt = (int)followerInt;
ADJ_FOLLOW = (followerInt / 4);
// Analog output (PWM) of duty cycle
OCR2B = ADJ_FOLLOW;
}
/*------------------------------------------------*/
void adjustComp()
{
compLong = compThrs * 1023L;
compInt = (long long)compLong / Vin;
compInt = (int)compInt;
OCR1A = compInt;
}
/*------------------------------------------------*/
void calibrateAlert()
{
VLast = VOld - Vin;
if (VLast > Hyst || VLast < -Hyst)
{
ERR_STATE = 1;
}
}
/*------------------------------------------------*/
void adjustGain()
{
switch (GAIN_FACTOR)
{
case 4:
pinMode(GADJ_R0, OUTPUT);
digitalWriteFast(GADJ_R0, LOW);
break;
case 3:
pinMode(GADJ_R1, OUTPUT);
digitalWriteFast(GADJ_R1, LOW);
pinMode(GADJ_R0, INPUT);
break;
case 2:
pinMode(GADJ_R2, OUTPUT);
digitalWriteFast(GADJ_R2, LOW);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
break;
case 1:
pinMode(GADJ_R3, OUTPUT);
digitalWriteFast(GADJ_R3, LOW);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
break;
case 0:
default:
pinMode(GADJ_R3, INPUT);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
break;
}
}
/*------------------------------------------------*/
//void checkError () {
// if (ERR_STATE == 1) {
// digitalWriteFast(ERR_LED, BlinkState);
// BlinkState = !BlinkState;
// }
// else if (ERR_STATE == 0) {
// BlinkState = LOW;
// digitalWriteFast(ERR_LED, BlinkState);
// }
//}
/*------------------------------------------------*/
void pzConCheck()
{
PZ_STATE = digitalRead(PZDET_PIN);
if (PZ_STATE == PZDET)
{
//digitalWriteFast(TRG_OUT, LOGIC);
ERR_STATE = 1;
}
}

View file

@ -9,84 +9,13 @@
#ifndef PP_FUNCTION_H #ifndef PP_FUNCTION_H
#define PP_FUNCTION_H #define PP_FUNCTION_H
#include "Arduino.h"
#include "pP_config.h"
#include "pP_volatile.h"
#include "pP_pins.h"
#include "stdint.h" #include "stdint.h"
void digitalWriteFast(uint8_t pin, uint8_t x) void digitalWriteFast(uint8_t pin, uint8_t x);
{ int analogReadFast(uint8_t ADCpin);
if (pin / 8) void doubleFlash();
{ // pin >= 8 void pulse();
PORTB ^= (-x ^ PORTB) & (1 << (pin % 8)); long readVcc();
}
else
{
PORTD ^= (-x ^ PORTD) & (1 << (pin % 8));
}
}
int inline analogReadFast(byte ADCpin)
{
byte ADCSRAoriginal = ADCSRA;
ADCSRA = (ADCSRA & B11111000) | 4;
int adc = analogRead(ADCpin);
ADCSRA = ADCSRAoriginal;
return adc;
}
/*------------------------------------------------*/
void doubleFlash()
{
BlinkCount = 4;
}
/*------------------------------------------------*/
void pulse()
{
digitalWriteFast(TRG_OUT, LOGIC);
sensorHReading = 1;
delay(TRG_DUR);
digitalWriteFast(TRG_OUT, !LOGIC);
Serial.println("Trig'd!");
doubleFlash();
}
/*------------------------------------------------*/
long readVcc()
{
// Read 1.1V reference against AVcc
// Atmega's Secret Voltmeter setup:
// 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 = voltMeterConstant / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
/*------------------------------------------------- /*-------------------------------------------------
The above function assumes an "ideal" multiplier constant. The above function assumes an "ideal" multiplier constant.
@ -104,96 +33,11 @@ internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function)
If the scale_constant calculated is different from the default 1125300, If the scale_constant calculated is different from the default 1125300,
update the voltMeterConstant variable in pP_config.h with the correct value update the voltMeterConstant variable in pP_config.h with the correct value
--------------------------------------------------*/ --------------------------------------------------*/
void readVin();
void readVin() void adjustFollow();
{ void adjustComp();
VOld = Vin; void calibrateAlert();
Vin = readVcc(); void adjustGain();
followerLong = followerThrs * 1023L;
compLong = compThrs * 1023L;
followerInt = (long long)followerLong / Vin;
compInt = (long long)compLong / Vin;
followerInt = (int)followerInt;
compInt = (int)compInt;
}
/*------------------------------------------------*/
void adjustFollow()
{
/* Compares diffs of threshold vs read value
if positive, adjusts the follower to within
the range set above*/
followerLong = followerThrs * 1023L;
followerInt = (long long)followerLong / Vin;
followerInt = (int)followerInt;
ADJ_FOLLOW = (followerInt / 4);
// Analog output (PWM) of duty cycle
OCR2B = ADJ_FOLLOW;
}
/*------------------------------------------------*/
void adjustComp()
{
compLong = compThrs * 1023L;
compInt = (long long)compLong / Vin;
compInt = (int)compInt;
OCR1A = compInt;
}
/*------------------------------------------------*/
void calibrateAlert()
{
VLast = VOld - Vin;
if (VLast > Hyst || VLast < -Hyst)
{
ERR_STATE = 1;
}
}
/*------------------------------------------------*/
void adjustGain()
{
switch (GAIN_FACTOR)
{
case 4:
pinMode(GADJ_R0, OUTPUT);
digitalWriteFast(GADJ_R0, LOW);
break;
case 3:
pinMode(GADJ_R1, OUTPUT);
digitalWriteFast(GADJ_R1, LOW);
pinMode(GADJ_R0, INPUT);
break;
case 2:
pinMode(GADJ_R2, OUTPUT);
digitalWriteFast(GADJ_R2, LOW);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
break;
case 1:
pinMode(GADJ_R3, OUTPUT);
digitalWriteFast(GADJ_R3, LOW);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
break;
case 0:
default:
pinMode(GADJ_R3, INPUT);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
break;
}
}
/*------------------------------------------------*/
//void checkError () { //void checkError () {
// if (ERR_STATE == 1) { // if (ERR_STATE == 1) {
// digitalWriteFast(ERR_LED, BlinkState); // digitalWriteFast(ERR_LED, BlinkState);
@ -204,17 +48,6 @@ void adjustGain()
// digitalWriteFast(ERR_LED, BlinkState); // digitalWriteFast(ERR_LED, BlinkState);
// } // }
//} //}
void pzConCheck();
/*------------------------------------------------*/
void pzConCheck()
{
PZ_STATE = digitalRead(PZDET_PIN);
if (PZ_STATE == PZDET)
{
//digitalWriteFast(TRG_OUT, LOGIC);
ERR_STATE = 1;
}
}
#endif //PP_FUNCTION_H #endif //PP_FUNCTION_H

View file

@ -17,6 +17,8 @@ int VLast = 0;
long followerLong = followerThrs * 1023L; long followerLong = followerThrs * 1023L;
long compLong = compThrs * 1023L; long compLong = compThrs * 1023L;
long followerInt = 0;
long compInt = 0;
// Voltage Comparator Adjustment parameters // Voltage Comparator Adjustment parameters
int VComp = 0; int VComp = 0;
@ -30,7 +32,10 @@ int BlinkState = 0;
int BlinkCount = (InitCount * 2) + 1; // Multiply Blink count by 2 to handle toggle state, add one extra to make sure light is on after int BlinkCount = (InitCount * 2) + 1; // Multiply Blink count by 2 to handle toggle state, add one extra to make sure light is on after
// Serial Input Parsing Variables // Serial Input Parsing Variables
char inputBuffer[buffSize];
uint8_t bytesRecvd = 0; uint8_t bytesRecvd = 0;
bool serialIncoming = false; bool serialIncoming = false;
char serialMessageIn[buffSize] = {0}; char serialMessageIn[buffSize] = {0};
long serialLong = 0; long serialLong = 0;
LightChrono mainLoop;

View file

@ -19,8 +19,8 @@ extern int VLast;
extern long followerLong; extern long followerLong;
extern long compLong; extern long compLong;
long followerInt; extern long followerInt;
long compInt; extern long compInt;
// Voltage Comparator Adjustment parameters // Voltage Comparator Adjustment parameters
extern int VComp; extern int VComp;
@ -35,7 +35,7 @@ extern int BlinkCount; // Multiply Blink count by 2 to handle toggle state, ad
// Serial Input Parsing Variables // Serial Input Parsing Variables
#define buffSize 40 #define buffSize 40
char inputBuffer[buffSize]; extern char inputBuffer[buffSize];
#define endMarker '\n' #define endMarker '\n'
extern uint8_t bytesRecvd; extern uint8_t bytesRecvd;
extern bool serialIncoming; extern bool serialIncoming;
@ -46,6 +46,6 @@ extern long serialLong;
//#define HIGH 1 //#define HIGH 1
// Task scheduler instances // Task scheduler instances
LightChrono mainLoop; extern LightChrono mainLoop;
#endif //PP_VOLATILE_H #endif //PP_VOLATILE_H