// MakeIdea [Hemant]
#include <avr/io.h>
#include <avr/interrupt.h>
#include <math.h>
#include <SoftwareSerial.h>
#include <PZEM004Tv30.h> // PZEM
PZEM004Tv30 pzem(&Serial3);
#define SinDivisions (200)
volatile long counterX;
volatile int num;
const int interruptPin = 3;
volatile int phs;
int tstInt=0;
volatile int newICR3;
volatile int lockTest;
volatile int lockDet;
static int microMHz = 16;
static int freq = 50;
static long int period;
static unsigned int lookUp[SinDivisions];
static char theTCCR3A = 0b10000010;
void setup()
{
// Serial.begin(115200); //PZEM
double temp;
period = microMHz*1e6/freq/SinDivisions;
for(int i = 0; i < SinDivisions/2; i++)
{
temp = sin(i*2*M_PI/SinDivisions)*period;
lookUp[i] = (int)(temp+0.5);
}
TCCR3A = theTCCR3A; // Konfigurasi register kontrol Timer 3
TCCR3B = 0b00011001; // Konfigurasi register kontrol Timer 3
TIMSK3 = 0b00000001; // Mengaktifkan interrupt overflow Timer 3
ICR3 = period; // Set nilai Input Capture Register 3
pinMode(interruptPin, INPUT_PULLUP); // Pin 3 untuk zero crossing detector
attachInterrupt(digitalPinToInterrupt(interruptPin), phaseDet, FALLING); // untuk pin 3
sei(); // Mengaktifkan global interrupts
// Mengatur pin sebagai output (PE3 atau PE4)
DDRE = 0b00011000; // PE3 sebagai output, sesuaikan dengan kebutuhan proyek
}
void loop(){;
}
void phaseDet() {
phs = digitalRead(interruptPin);
if (num > 0 && phs == 0) { // Adjust this value according to your specific needs
num--;
ICR3 = ICR3 + 1;
}
if (num < 99 && phs == 1) { // Adjust this value according to your specific needs
num++;
ICR3 = ICR3 - 1;
}
}
ISR(TIMER3_OVF_vect) {
static int num;
static int ph;
static char trig;
if(ph == 1)
{
theTCCR3A ^= 0b10100000; // Misalnya, XOR dengan nilai 0b11110000
TCCR3A = theTCCR3A; // Menetapkan nilai kembali ke register TCCR3A
ph = 0;
// phs = 1;
}
if(num >= SinDivisions/2)
{
num = 0;
ph++;
}
OCR3A = OCR3B = lookUp[num];
num++;
}
here my code to synchronize with grid and this waveform in osiloskop
but when i try to grid voltage drop and current so high, any solution?