Сейчас пытаюсь добавить вольтметр переменного напряжения.
Соединил две программы , вольтметр для переменного напряжения работает правельно но для нпостаянного нет, не реагирует на подачу сигнала на PA или PA7.
первый скетч,
#include <STM32ADC.h>
STM32ADC myADC(ADC1);
uint8 pins[] = {PA6, PA7};
const int maxSamples = 2;
uint16_t dataPoints[maxSamples];
long sum1, sum2;
void setup() {
Serial.begin(115200);
myADC.calibrate();
rcc_set_prescaler(RCC_PRESCALER_ADC, RCC_ADCPRE_PCLK_DIV_8);
pinMode(PA6, INPUT_ANALOG);
pinMode(PA7, INPUT_ANALOG);
myADC.setSampleRate(ADC_SMPR_239_5);
myADC.setScanMode();
myADC.setPins(pins, 2);
myADC.setContinuous();
myADC.setDMA(dataPoints, 2, (DMA_MINC_MODE | DMA_CIRC_MODE), NULL);
myADC.startConversion();
}
void loop() {
for (int j = 0; j < 10; j++) {
sum1 = sum1 + dataPoints[0];
sum2 = sum2 + dataPoints[1];
delay(1);
}
//Serial.println(sum1/10);sum1=0;
//Serial.println(sum2/10);sum2=0;
///////////////
Serial.print(sum1 / 10); sum1 = 0;
Serial.print(" ");
Serial.println(sum2 / 10); sum2 = 0;
////////////////
};
второй,
#include "arduinoFFT.h"
#include <LiquidCrystal.h>
//LiquidCrystal lcd(PB15, PB5, PA2, PB6, PB8, PB9);
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <EEPROM.h>
#define SAMPLES1 128
#define SAMPLING_FREQUENCY 40000
///////////////
int led = PC13;
const int numReadings = 10;
const float THRESHOLD = 500;
int piezoPin = PB6;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
////////////////
int Tup = PB13;
int Tdown = PB15;
int k;
//int k<100;
///////////////
//int inputPin = A0;
////////////////
arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES1];
double vImag[SAMPLES1];
// no noise
//--------------------------------------
int currentaverage = average;
int previousaverage = 0;
//---------------------------------------
void setup() {
pinMode(led, OUTPUT);
Serial.begin(115200);
lcd.begin( 16, 2 );
//EEPROM.read(500, (uint16*)&k);
pinMode(Tup, INPUT_PULLDOWN);
pinMode(Tdown, INPUT_PULLDOWN);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
}
void loop() {
// no noise
//----------------------------------------
previousaverage = currentaverage;
//**************************************************************************
currentaverage = average;
//if (abs(currentaverage - previousaverage) < previousaverage / 40 ) // 100 = 1%
if (abs(currentaverage - previousaverage) < previousaverage / 25 ) // 100 = 1%
//********************************************************************************
{
currentaverage = previousaverage;
}
//----------------------------------------
/*SAMPLING*/
for (int t = 0; t < SAMPLES1; t++)
{
microseconds = micros(); //Overflows after around 70 minutes!
vReal[t] = analogRead(PB0);
// vReal[t] = analogRead(PA6);
vImag[t] = 0;
while (micros() < (microseconds + sampling_period_us)) {
}
}
/*FFT*/
FFT.Windowing(vReal, SAMPLES1, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES1, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES1);
double peak = FFT.MajorPeak(vReal, SAMPLES1, SAMPLING_FREQUENCY);
////////////////
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
//readings[readIndex] = analogRead(inputPin);
readings[readIndex] = vReal[34];
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
//Serial.println(average);
delay(1); // delay in between reads for stability
///////////////////
// if (currentaverage > THRESHOLD)
if (currentaverage > k)
{
digitalWrite(led, LOW);
tone(piezoPin, 4000);
Serial.print(1000.0);
}
else
{
digitalWrite(led, HIGH);
noTone(piezoPin);
Serial.print(5.0);
}
/////////////////////
if (digitalRead(Tup) == HIGH)
{
{
k++;
}
}
if (digitalRead(Tdown) == HIGH)
{
k--;
}
/////////////////
/*PRINT RESULTS*/
Serial.print(led);
Serial.print(" ");
Serial.print(average);
Serial.print(" ");
Serial.print(currentaverage);
Serial.print(" ");
//Serial.println(vReal[34], 1);//40=122.1kHz, 30=8.9, 35 = 10.6, 34 = 10.30kHz
Serial.println(vReal[35], 1);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("N ");
//lcd.print(vReal[12], 1);// 10 - 3kHz, 12 =
lcd.print(currentaverage);
lcd.setCursor(0, 1);
lcd.print("T ");
//lcd.print(THRESHOLD);
lcd.print(k);
EEPROM.write(500, k);
}
и два вместе.
#include "arduinoFFT.h"
#include <LiquidCrystal.h>
//LiquidCrystal lcd(PB15, PB5, PA2, PB6, PB8, PB9);
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#include <EEPROM.h>
#define SAMPLES1 128
#define SAMPLING_FREQUENCY 40000
//////////////////
#include <STM32ADC.h>
STM32ADC myADC(ADC1);
uint8 pins[] = {PA6, PA7};
const int maxSamples = 2;
uint16_t dataPoints[maxSamples];
long sum1, sum2;
///////////////
int led = PC13;
const int numReadings = 10;
const float THRESHOLD = 500;
int piezoPin = PB6;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
////////////////
int Tup = PB13;
int Tdown = PB15;
int k;
//int k<100;
///////////////
//int inputPin = A0;
////////////////
arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES1];
double vImag[SAMPLES1];
// no noise
//--------------------------------------
int currentaverage = average;
int previousaverage = 0;
//---------------------------------------
void setup() {
////////////////
myADC.calibrate();
rcc_set_prescaler(RCC_PRESCALER_ADC, RCC_ADCPRE_PCLK_DIV_8);
pinMode(PA6, INPUT_ANALOG);
pinMode(PA7, INPUT_ANALOG);
myADC.setSampleRate(ADC_SMPR_239_5);
myADC.setScanMode();
myADC.setPins(pins, 2);
myADC.setContinuous();
myADC.setDMA(dataPoints, 2, (DMA_MINC_MODE | DMA_CIRC_MODE), NULL);
myADC.startConversion();
///////////////
pinMode(led, OUTPUT);
Serial.begin(115200);
lcd.begin( 16, 2 );
//EEPROM.read(500, (uint16*)&k);
pinMode(Tup, INPUT_PULLDOWN);
pinMode(Tdown, INPUT_PULLDOWN);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
}
void loop() {
/////////////////
for (int j = 0; j < 10; j++) {
sum1 = sum1 + dataPoints[0];
sum2 = sum2 + dataPoints[1];
delay(1);
}
//Serial.println(sum1/10);sum1=0;
//Serial.println(sum2/10);sum2=0;
///////////////
//Serial.print(sum1 / 10); sum1 = 0;
// Serial.print(" ");
// Serial.println(sum2 / 10); sum2 = 0;
////////////////
////////////////
// no noise
//----------------------------------------
previousaverage = currentaverage;
//**************************************************************************
currentaverage = average;
//if (abs(currentaverage - previousaverage) < previousaverage / 40 ) // 100 = 1%
if (abs(currentaverage - previousaverage) < previousaverage / 25 ) // 100 = 1%
//********************************************************************************
{
currentaverage = previousaverage;
}
//----------------------------------------
/*SAMPLING*/
for (int t = 0; t< SAMPLES1; t++)
{
microseconds = micros(); //Overflows after around 70 minutes!
vReal[t] = analogRead(PB0);
// vReal[t] = analogRead(PA6);
vImag[t] = 0;
while (micros() < (microseconds + sampling_period_us)) {
}
}
/*FFT*/
FFT.Windowing(vReal, SAMPLES1, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES1, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES1);
double peak = FFT.MajorPeak(vReal, SAMPLES1, SAMPLING_FREQUENCY);
////////////////
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
//readings[readIndex] = analogRead(inputPin);
readings[readIndex] = vReal[34];
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
//Serial.println(average);
delay(1); // delay in between reads for stability
///////////////////
// if (currentaverage > THRESHOLD)
if (currentaverage > k)
{
digitalWrite(led, LOW);
tone(piezoPin, 4000);
Serial.print(1000.0);
}
else
{
digitalWrite(led, HIGH);
noTone(piezoPin);
Serial.print(5.0);
}
/////////////////////
if (digitalRead(Tup) == HIGH)
{
{
k++;
}
}
if (digitalRead(Tdown) == HIGH)
{
k--;
}
/////////////////
/*PRINT RESULTS*/
Serial.print(led);
Serial.print(" ");
Serial.print(average);
Serial.print(" ");
Serial.print(currentaverage);
Serial.print(" ");
//Serial.println(vReal[34], 1);//40=122.1kHz, 30=8.9, 35 = 10.6, 34 = 10.30kHz
Serial.print(vReal[35], 1);
/////////////////
Serial.print(sum1 / 10); sum1 = 0;
Serial.print(" ");
Serial.println(sum2 / 10); sum2 = 0;
/////////////////
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("N ");
//lcd.print(vReal[12], 1);// 10 - 3kHz, 12 =
lcd.print(currentaverage);
lcd.setCursor(0, 1);
lcd.print("T ");
//lcd.print(THRESHOLD);
lcd.print(k);
EEPROM.write(500, k);
}