Arduino Polska Forum
Voltomierz + generator - Wersja do druku

+- Arduino Polska Forum (https://forum.arduinopolska.pl)
+-- Dział: Reszta producentów (https://forum.arduinopolska.pl/dzial-reszta-producent%C3%B3w)
+--- Dział: Oprogramowanie (https://forum.arduinopolska.pl/dzial-oprogramowanie)
+--- Wątek: Voltomierz + generator (/watek-voltomierz-generator)



Voltomierz + generator - zak - 15-06-2022

Woltomierz przestaje działać po dodaniu ostatniej linijki kodu generatora w void setup

Kod:
timer_setup(15000); // Hz
Co z tym fantem zrobić ?
Kod:
/*
  Measuring AC Current Using ACS712
  www.circuits4you.com
*/
/////////////////
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//int sensorValue = PA4;
int sensorValue = A3;
/////////////////
const int sensorIn = A3;
int mVperAmp = 355; // kalibracja

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
/////////////////////////////
volatile bool sempl_flag = true;
uint16_t sempl_count;

void timer_setup(int16_t f)
{
  int16_t fr = 4000000 / f;

  cli();  //
  TIMSK0 &= ~(1 << TOIE0);
  TCCR0A = (1 << WGM01);
  TCCR0B = (1 << CS00) | (1 << CS02);
  OCR0A = 255;
  TIMSK0 |= (1 << OCIE0A);
  DDRB |= B00000110; //9 10  OUTPUT
  PORTB &= ~ B00000110; //
  TCNT1 = 0;
  ICR1 = (fr * 2) - 1; // 500 8kHz //(Mem.ocr1a[Mem.Prof]*2)-1;
  TCCR1A = 0x50;
  TCCR1B = 0x19;
  OCR1A = 1;
  OCR1B = 765; 
  // OCR1B = 765; на OCR1B = fr+(fr/2);
  OCR1B = fr + (fr / 2);

  TCCR2A = (TCCR2A & 0x3F) | 0x40;     
  TCCR2A = (TCCR2A & 0xF0) | (1 << WGM21); // CTC - mode
  sei();
}

ISR(TIMER0_COMPA_vect)
{
  sempl_flag = false;
}
//======================================================================================
void setup() {

  //Serial.begin(9600);
  Serial.begin(115200);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize with the I2C addr 0x3D
                                            // if not working use 0x3C (for the 128x64)
  display.setTextColor(WHITE);

  /////////
  //timer_setup(15000); // Hz             ++++++++++++++++++++++++++++++++++++++++++++
  /////////
}
//===========================
void loop() {

delay(1);
  display.clearDisplay();
  Voltage = getVPP();
  VRMS = (Voltage / 2.0) * 0.707; //root 2 is 0.707
  AmpsRMS = (VRMS * 1000) / mVperAmp;
  Serial.print(AmpsRMS);
  //Serial.print(AmpsRMS*1000);// mV
  Serial.println(" Amps RMS");
  ///////
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(80, 0);
  //display.print(" RMS");
  display.println(AmpsRMS);
   // display.println(AmpsRMS*1000);
  display.display();
  /////////////
}

float getVPP()
{
  float result;
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here

  uint32_t start_time = millis();
  while ((millis() - start_time) < 10) //sample for 1 Sec
  {
    readValue = analogRead(sensorIn);
    if (readValue > maxValue)
    {
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      minValue = readValue;
    }
  }

  result = ((maxValue - minValue) * 5.0) / 1024.0;

  return result;
}



RE: Voltomierz + generator - Jarewa0606 - 15-06-2022

Wyrzuć cała konfigurację timera setup, i ten isr simpl_flag ie jest nigdzie wykorzystywane więc nie
Potrzebne to jest


RE: Voltomierz + generator - Robson Kerman - 18-06-2022

Przerwanie musi zostać, bo timer musi wejść w wektor aby się zresetować, inaczej na pinach 9 i 10 nie będzie pwm.
Ta flaga owszem nic nie robi i można ją wywalić. W ogóle można dużo rzeczy z tego kodu wywalić.

Po drugie, jeśli uruchomisz Timer1 w taki sposób jak masz napisane w funkcji void timer_setup(int16_t f), to program nigdy nie wyjdzie z while ((millis() - start_time) < 10), więc tak jak napisałeś, woltomierz przestaje działać.
Jak już masz ten zegar ustawiony, to czemu by jego nie wykorzystać do liczenia czasu próbkowania?