MitkoDyakov would you please share kicad project files including schematic and PCB into the project repo, also for easier access please mention the GitHub repo in here too.
This is a working Zero crossing code that I have implemented before on some industrial equipment, it uses 64 samples per sine wave cycle to calculate the Zero crossing
#define ADC_samples 64
//we need two cycles to calculate the Frequency, ok!
short oldSamples[128];
const int BLOCK_SIZE =ADC_samples;
const int NUM_TAPS =32;
arm_fir_instance_q15 S;
q15_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1];
q15_t firCoeffs32[NUM_TAPS] = {
-1, -2, 8, 39, 83, 97, 14, -213, -535,
-762, -596, 232, 1778, 3773, 5659, 6810, 6810, 5659,
3773, 1778, 232, -596, -762, -535, -213, 14, 97,
83, 39, 8, -2, -1
};
void init_myFilter(void){
/* Call FIR init function to initialize the instance structure. */
arm_fir_init_q15(&S, NUM_TAPS, firCoeffs32, firStateF32, BLOCK_SIZE);
}
void dsp_zero_cross(short* input,int size,float* zero_1st,float* zero_2st, float* zero_3st)
{
int i;
float first_zero = 0,second_zero = 0,third_zero = 0;
int index[4] = {0};
int index_index = 0;
int x1 = 0;
int x2 = 0;
int x3 = 0;
short temp[ADC_samples];
//first copy from the last portion to the first portion
for(i=64;i<128;i++)
oldSamples[i-64] = oldSamples[i];
//now copy the input to the last portion
for(i=64;i<128;i++)
oldSamples[i]= input[i-64];
arm_fir_fast_q15(&S,oldSamples,temp,BLOCK_SIZE);
arm_fir_fast_q15(&S,oldSamples+64,temp,BLOCK_SIZE);
for (i = 1; i < size; i++)
{
if((temp[i]<=0 && temp[i-1]>0) || (temp[i]>=0 && temp[i-1]<0))
{
index[index_index] = i;
index_index++;
if(index_index == 4)
{
break;
}
}
}
if(index_index == 0)
{
*zero_2st=-1;
*zero_1st=-1;
*zero_3st=-1;
return;
}
else if(index_index == 1)
{
*zero_2st=-1;
*zero_1st=-1;
*zero_3st=-1;
return;
}
else if (index_index == 2)
{
x1 = index[0];
x2 = index[1];
if((temp[x1 - 1] - temp[x1]) == 0 || (temp[x1 + 1] - temp[x1]) == 0 || (temp[x2 - 1] - temp[x2]) == 0 || (temp[x2 + 1] - temp[x2]) == 0 || x2 == (size - 1) || x1 == (size - 1))
{
*zero_2st=-1;
*zero_1st=-1;
*zero_3st=-1;
return;
}
else
{
first_zero = ((float)(temp[x1] * temp[x1 + 1])/((float)(temp[x1 - 1] - temp[x1])*(float)(temp[x1 - 1] - temp[x1 + 1]))) * (x1 - 1);
first_zero += ((float)(temp[x1 - 1] * temp[x1 + 1])/((float)(temp[x1] - temp[x1 - 1])*(float)(temp[x1] - temp[x1+ 1]))) * (x1);
first_zero += ((float)(temp[x1 - 1] * temp[x1])/((float)(temp[x1 + 1] - temp[x1 - 1])*(float)(temp[x1 + 1] - temp[x1]))) * (x1 + 1);
////////////////////
second_zero = ((float)(temp[x2] * temp[x2 + 1])/((float)(temp[x2 - 1] - temp[x2])*(float)(temp[x2 - 1] - temp[x2 + 1]))) * (x2 - 1);
second_zero += ((float)(temp[x2 - 1] * temp[x2 + 1])/((float)(temp[x2] - temp[x2- 1])*(float)(temp[x2] - temp[x2 + 1]))) * (x2);
second_zero += ((float)(temp[x2 - 1] * temp[x2])/((float)(temp[x2 + 1] - temp[x2 - 1])*(float)(temp[x2 + 1] - temp[x2]))) * (x2 + 1);
*zero_1st = first_zero;
*zero_2st = second_zero;
*zero_3st=-1;
return;
}
}
else if (index_index == 3)
{
x1 = index[0];
x2 = index[1];
x3 = index[2];
if((temp[x1 - 1] - temp[x1]) == 0 || (temp[x1 + 1] - temp[x1]) == 0 || (temp[x2 - 1] - temp[x2]) == 0 || (temp[x2 + 1] - temp[x2]) == 0|| (temp[x3 - 1] - temp[x3]) == 0 || (temp[x3 + 1] - temp[x3]) == 0 || x3 == (size - 1) || x2 == (size - 1) || x1 == (size - 1))
{
*zero_2st=-1;
*zero_1st=-1;
*zero_3st=-1;
return;
}
else
{
first_zero = ((float)(temp[x1] * temp[x1 + 1])/((float)(temp[x1 - 1] - temp[x1])*(float)(temp[x1 - 1] - temp[x1 + 1]))) * (x1 - 1);
first_zero += ((float)(temp[x1 - 1] * temp[x1 + 1])/((float)(temp[x1] - temp[x1 - 1])*(float)(temp[x1] - temp[x1+ 1]))) * (x1);
first_zero += ((float)(temp[x1 - 1] * temp[x1])/((float)(temp[x1 + 1] - temp[x1 - 1])*(float)(temp[x1 + 1] - temp[x1]))) * (x1 + 1);
////////////////////
second_zero = ((float)(temp[x2] * temp[x2 + 1])/((float)(temp[x2 - 1] - temp[x2])*(float)(temp[x2 - 1] - temp[x2 + 1]))) * (x2 - 1);
second_zero += ((float)(temp[x2 - 1] * temp[x2 + 1])/((float)(temp[x2] - temp[x2- 1])*(float)(temp[x2] - temp[x2 + 1]))) * (x2);
second_zero += ((float)(temp[x2 - 1] * temp[x2])/((float)(temp[x2 + 1] - temp[x2 - 1])*(float)(temp[x2 + 1] - temp[x2]))) * (x2 + 1);
///////////////////
third_zero = ((float)(temp[x3] * temp[x3 + 1])/((float)(temp[x3 - 1] - temp[x3])*(float)(temp[x3 - 1] - temp[x3 + 1]))) * (x3 - 1);
third_zero += ((float)(temp[x3 - 1] * temp[x3 + 1])/((float)(temp[x3] - temp[x3 - 1])*(float)(temp[x3] - temp[x3+ 1]))) * (x3);
third_zero += ((float)(temp[x3 - 1] * temp[x3])/((float)(temp[x3 + 1] - temp[x3 - 1])*(float)(temp[x3 + 1] - temp[x3]))) * (x3 + 1);
*zero_1st = first_zero;
*zero_2st = second_zero;
*zero_3st= third_zero;
}
}
}