Woltomierz przestaje działać po dodaniu ostatniej linijki kodu generatora w void setup
Co z tym fantem zrobić ?
Kod:
timer_setup(15000); // Hz
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;
}