My VVVF Program Problem

Hello.
This is my VVVF programs, and I use an arduinoMEGA.
In the "wide 3 pulse" mode(in the following source code, the number "pulse" is 2 ), my program looks freezing―runs quite slowly.
What happens? And what do I have to solve this? Please teach me.

Source code is as follows:

#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>

bool pulseMode;

bool Uwave_w3,Vwave_w3,Wwave_w3;

volatile double Carrier_freqency, Vout, Vout_Target,Vout_dokimode, Hz, Hz_kasokudo, hz_mode, Hz_Target;
volatile int pulse;
volatile uint16_t U, V, W, Top;
volatile int32_t Doki_SIN_U, Doki_SIN_V, Doki_SIN_W;
int trainnumber;
volatile double Voltage_frequency,one_pulse_freqency,wide3_pulse_freqency,sync_3pulse_freqency,sync_9pulse_freqency;
float OCR5A_hidoki,OCR5B_hidoki,OCR5C_hidoki;

//asyncronous mode LUT
const volatile uint8_t U_table[256] PROGMEM = 
{127,130,134,137,140,143,146,149,152,155,158,161,164,167,170,173,176,179,182,185,187,190,193,195,198,201,203,206,208,210,213,215,
217,220,222,224,226,228,230,231,233,235,237,238,240,241,242,244,245,246,247,248,249,250,251,252,252,253,253,254,254,254,254,255,
255,255,254,254,254,254,253,253,252,252,251,250,249,248,247,246,245,244,242,241,240,238,237,235,233,231,230,228,226,224,222,220,
217,215,213,210,208,206,203,201,198,195,193,190,187,185,182,179,176,173,170,167,164,161,158,155,152,149,146,143,140,137,134,130,
127,124,121,118,115,112,109,106,102,99,96,93,90,87,84,82,79,76,73,70,67,65,62,59,57,54,51,49,47,44,42,40,
37,35,33,31,29,27,25,23,21,20,18,17,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,
0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,17,18,20,21,23,25,27,29,31,33,35,
37,40,42,44,47,49,51,54,57,59,62,65,67,70,73,76,79,82,84,87,90,93,96,99,102,106,109,112,115,118,121,124};
const volatile uint8_t V_table[256] PROGMEM = 
{17,16,14,13,11,10,9,8,7,6,5,4,3,3,2,2,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,
4,5,6,7,8,9,11,12,13,15,16,18,19,21,23,24,26,28,30,32,34,37,39,41,43,46,48,51,53,56,58,61,
64,66,69,72,75,78,81,83,86,89,92,95,98,101,105,108,111,114,117,120,123,126,129,133,136,139,142,145,148,151,154,157,
160,163,166,169,172,175,178,181,184,186,189,192,195,197,200,202,205,207,210,212,214,217,219,221,223,225,227,229,231,233,234,236,
238,239,241,242,243,245,246,247,248,249,250,251,251,252,253,253,254,254,254,254,255,255,255,255,254,254,254,253,253,252,252,251,
250,249,249,248,246,245,244,243,242,240,239,237,235,234,232,230,228,226,224,222,220,218,216,214,211,209,206,204,201,199,196,194,
191,188,185,183,180,177,174,171,168,165,162,159,156,153,150,147,144,141,138,135,131,128,125,122,119,116,113,110,107,104,100,97,
94,91,88,85,82,80,77,74,71,68,65,63,60,57,55,52,50,47,45,43,40,38,36,34,32,30,28,26,24,22,20,19};
const volatile uint8_t W_table[256] PROGMEM = 
{238,236,234,233,231,229,227,225,223,221,219,217,214,212,210,207,205,202,200,197,195,192,189,186,184,181,178,175,172,169,166,163,
160,157,154,151,148,145,142,139,136,133,129,126,123,120,117,114,111,108,105,101,98,95,92,89,86,83,81,78,75,72,69,66,
64,61,58,56,53,51,48,46,43,41,39,37,34,32,30,28,26,24,23,21,19,18,16,15,13,12,11,9,8,7,6,5,
4,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,2,2,3,3,4,5,6,7,8,9,10,11,13,14,16,
17,19,20,22,24,26,28,30,32,34,36,38,40,43,45,47,50,52,55,57,60,63,65,68,71,74,77,80,82,85,88,91,
94,97,100,104,107,110,113,116,119,122,125,128,131,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,185,188,
191,194,196,199,201,204,206,209,211,214,216,218,220,222,224,226,228,230,232,234,235,237,239,240,242,243,244,245,246,248,249,249,
250,251,252,252,253,253,254,254,254,255,255,255,255,254,254,254,254,253,253,252,251,251,250,249,248,247,246,245,243,242,241,239};

//1 pulse
const volatile bool pulse1_U_table[] PROGMEM ={1,1,1,0,0,0};//U phase
const volatile bool pulse1_V_table[] PROGMEM ={1,0,0,0,1,1};//V phase
const volatile bool pulse1_W_table[] PROGMEM ={0,0,1,1,1,0};//W phase

//wide 3 pulse
const volatile uint8_t Wide_pulse3_U_table[] PROGMEM =
{254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,
254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
/*U phase*/
const volatile uint8_t Wide_pulse3_V_table[] PROGMEM =
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,
254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,
254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
/*V phase*/
const volatile uint8_t Wide_pulse3_W_table[] PROGMEM =
{254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,
254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254};
/*W phase*/

//wide 3 pulse carrier
const volatile uint8_t Wide_pulse3_Carrier_U_table[] PROGMEM =
{254,252,250,248,246,244,242,240,238,236,234,232,230,228,226,224,222,220,218,216,214,212,210,208,206,204,202,200,198,196,194,192,
190,188,186,184,182,180,178,176,174,172,170,168,166,164,162,160,158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,128,
128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,
192,194,196,198,200,202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,238,240,242,244,246,248,250,252,254,
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,
64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,
126,124,122,120,118,116,114,112,110,108,106,104,102,100,98,96,94,92,90,88,86,84,82,80,78,76,74,72,70,68,66,64,
62,60,58,56,54,52,50,48,46,44,42,40,38,36,34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0};
const volatile uint8_t Wide_pulse3_Carrier_V_table[] PROGMEM = 
{86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,126,124,122,120,118,116,114,112,110,108,106,
104,102,100,98,96,94,92,90,88,86,84,82,80,78,76,74,72,70,68,66,64,62,60,58,56,54,52,50,48,46,44,42,
40,38,36,34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0,254,252,250,248,246,244,242,240,238,236,234,
232,230,228,226,224,222,220,218,216,214,212,210,208,206,204,202,200,198,196,194,192,190,188,186,184,182,180,178,176,174,172,170,
168,166,164,162,160,158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,128,128,130,132,134,136,138,140,142,144,146,148,
150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,202,204,206,208,210,212,
214,216,218,220,222,224,226,228,230,232,234,236,238,240,242,244,246,248,250,252,254,0,2,4,6,8,10,12,14,16,18,20,
22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84};
const volatile uint8_t Wide_pulse3_Carrier_W_table[] PROGMEM =
{170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,
234,236,238,240,242,244,246,248,250,252,254,0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,
42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,
106,108,110,112,114,116,118,120,122,124,126,126,124,122,120,118,116,114,112,110,108,106,104,102,100,98,96,94,92,90,88,86,
84,82,80,78,76,74,72,70,68,66,64,62,60,58,56,54,52,50,48,46,44,42,40,38,36,34,32,30,28,26,24,22,
20,18,16,14,12,10,8,6,4,2,0,254,252,250,248,246,244,242,240,238,236,234,232,230,228,226,224,222,220,218,216,214,
212,210,208,206,204,202,200,198,196,194,192,190,188,186,184,182,180,178,176,174,172,170,168,166,164,162,160,158,156,154,152,150,
148,146,144,142,140,138,136,134,132,130,128,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168};


uint16_t number;
int pre_pulse;

const uint8_t Syncro_Pulse_table_U[3][108] PROGMEM = {
//3 pulse
{127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,
 127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0},
//9 pulse
{170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,
 170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127},
//27 pulse
{127,142,156,170,184,197,209,219,229,237,244,249,252,254,254,252,249,244,237,229,219,209,197,184,170,156,142,127,112,98,84,70,57,45,35,25,17,10,5,2,0,0,2,5,10,17,25,35,45,57,70,84,98,112,
 127,142,156,170,184,197,209,219,229,237,244,249,252,254,254,252,249,244,237,229,219,209,197,184,170,156,142,127,112,98,84,70,57,45,35,25,17,10,5,2,0,0,2,5,10,17,25,35,45,57,70,84,98,112}
};

const uint8_t Syncro_Pulse_table_V[3][108] PROGMEM = {
//3 pulse
{0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,
 0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127},
//9 pulse
{2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,
 2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17},
//27 pulse
{17,10,5,2,0,0,2,5,10,17,25,35,45,57,70,84,98,112,127,142,156,170,184,197,209,219,229,237,244,249,252,254,254,252,249,244,237,229,219,209,197,184,170,156,142,127,112,98,84,70,57,45,35,25,
 17,10,5,2,0,0,2,5,10,17,25,35,45,57,70,84,98,112,127,142,156,170,184,197,209,219,229,237,244,249,252,254,254,252,249,244,237,229,219,209,197,184,170,156,142,127,112,98,84,70,57,45,35,25}
};

const uint8_t Syncro_Pulse_table_W[3][108] PROGMEM = {
//3 pulse
{254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,
 254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254,254,127,0,0,127,254},
//9 pulse
{209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,
 209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237,209,170,127,84,45,17,2,2,17,45,84,127,170,209,237,252,252,237},
//27 pulse
{237,229,219,209,197,184,170,156,142,127,112,98,84,70,57,45,35,25,17,10,5,2,0,0,2,5,10,17,25,35,45,57,70,84,98,112,127,142,156,170,184,197,209,219,229,237,244,249,252,254,254,252,249,244,
 237,229,219,209,197,184,170,156,142,127,112,98,84,70,57,45,35,25,17,10,5,2,0,0,2,5,10,17,25,35,45,57,70,84,98,112,127,142,156,170,184,197,209,219,229,237,244,249,252,254,254,252,249,244}
};

#define Uwave pgm_read_byte_near(&pulse1_U_table[number])
#define Vwave pgm_read_byte_near(&pulse1_V_table[number])
#define Wwave pgm_read_byte_near(&pulse1_W_table[number])

#define Uwave_W3_S pgm_read_byte_near(&Wide_pulse3_U_table[number])
#define Vwave_W3_S pgm_read_byte_near(&Wide_pulse3_V_table[number])
#define Wwave_W3_S pgm_read_byte_near(&Wide_pulse3_W_table[number])

#define Uwave_W3_C pgm_read_byte_near(&Wide_pulse3_Carrier_U_table[number])
#define Vwave_W3_C pgm_read_byte_near(&Wide_pulse3_Carrier_V_table[number])
#define Wwave_W3_C pgm_read_byte_near(&Wide_pulse3_Carrier_W_table[number])

void train(){
       if(Hz > one_pulse_freqency){
        //1 pulse
        pulseMode=1;
        pulse = 1;
        }
        else if(Hz > wide3_pulse_freqency && Hz <= one_pulse_freqency){
        //wide 3 pulse
        pulseMode=1;
        pulse = 2;
        }
        else if(Hz > sync_9pulse_freqency && Hz <= wide3_pulse_freqency){
        //9 pulse
        pulseMode=1;
        pulse = 9;
        }
        else{
        //asyncronous mode
        pulseMode=0;
        pulse = 0;
        if(Hz <= 18.7)Carrier_freqency = 525;
        else if(Hz > 18.7 && Hz <= sync_9pulse_freqency){
          Carrier_freqency = 17.045455*Hz+275.272727;
        }
        }
        if(pulse != pre_pulse){
          number=0;
          TCNT5 = 0;
          pre_pulse = pulse;
        }
}

void Kasoku(void) {//accel
    if (Vout < Vout_Target && Hz <= one_pulse_freqency) {

    Vout =  Hz * Voltage_frequency;

    if(Hz > wide3_pulse_freqency && Hz <= one_pulse_freqency)Vout_dokimode = Vout;//1-0.954929658551372*acos((1+Vout)*0.5);//3/M_PI=0.954929658551372
    else if(Hz > sync_9pulse_freqency && Hz <= wide3_pulse_freqency)Vout_dokimode = 1-1.9098593171*asin((1-Vout)*0.5);//6/M_PI=1.9098593171
    else Vout_dokimode =0;

    if(Vout > Vout_Target)Vout = Vout_Target;
    if(Vout > 1.0)Vout = 1.0;
  } 
  if(Hz_kasokudo <= 0)Hz_kasokudo = hz_mode;
}

void Gensoku(void) {//brake
  if(Vout > Vout_Target && Hz <= one_pulse_freqency) {
    Vout =  Hz * Voltage_frequency;
    if(Hz > wide3_pulse_freqency && Hz <= one_pulse_freqency)Vout_dokimode = 1-3/M_PI*acos((1+Vout)/2);
    else if(Hz > sync_9pulse_freqency && Hz <= wide3_pulse_freqency)Vout_dokimode = 1-6/M_PI*asin((1-Vout)/2);
    else Vout_dokimode =0;
  if(Vout < 0.1) Vout = 0.0;
  }
    if(Hz_kasokudo >= 0)Hz_kasokudo = hz_mode * (-1);
}

ISR(TIMER2_COMPA_vect) {
   number += 1; 
   if(pulseMode)number %= 6;
   else number %= 256;
}

ISR(TIMER3_COMPA_vect) {
if(pulse == 1){
            number++;
            number %= 6;
  PORTL =  (Uwave << 3) | (Vwave << 4) | (Wwave << 5);
}
else if(pulse == 2){

              number++;
            number &= 0xff;
   Doki_SIN_U= 127 + (Uwave_W3_S - 127)*Vout_dokimode;
   Doki_SIN_V= 127 + (Vwave_W3_S - 127)*Vout_dokimode;
   Doki_SIN_W= 127 + (Wwave_W3_S - 127)*Vout_dokimode;
   if(Doki_SIN_U >= Uwave_W3_C)Uwave_w3=1;
else Uwave_w3=0;
if(Doki_SIN_V >= Vwave_W3_C)Vwave_w3=1;
else Vwave_w3=0;
if(Doki_SIN_W >= Wwave_W3_C)Wwave_w3=1;
else Wwave_w3=0;
  PORTL =  (Uwave_w3 << 3) | (Vwave_w3 << 4) | (Wwave_w3 << 5);
}
else{
}
}

ISR(TIMER4_COMPA_vect) {//speed controller
  Hz += Hz_kasokudo * 5;
  if(fabs(Hz- Hz_Target) <= 0.5 && Hz != 0) Hz=Hz_Target;
  if(Hz < 0) Hz=0;
}

void sin_nyuryoku_9pulse(){
  Doki_SIN_U = (Top / 2 * (((pgm_read_byte_near(&Syncro_Pulse_table_U[(int)((pulse - 3) / 6)][number]) - 127) / 99.745567 * Vout_dokimode + 1)));
  Doki_SIN_V = (Top / 2 * (((pgm_read_byte_near(&Syncro_Pulse_table_V[(int)((pulse - 3) / 6)][number]) - 127) / 99.745567 * Vout_dokimode + 1)));
  Doki_SIN_W = (Top / 2 * (((pgm_read_byte_near(&Syncro_Pulse_table_W[(int)((pulse - 3) / 6)][number]) - 127) / 99.745567 * Vout_dokimode + 1)));
}

void ocr5(){
  OCR5A = (Doki_SIN_U < Top && Doki_SIN_U > 0) * Doki_SIN_U + (Doki_SIN_U > Top || Doki_SIN_U == Top) * Top;
  OCR5B = (Doki_SIN_V < Top && Doki_SIN_V > 0) * Doki_SIN_V + (Doki_SIN_V > Top || Doki_SIN_V == Top) * Top;
  OCR5C = (Doki_SIN_W < Top && Doki_SIN_W > 0) * Doki_SIN_W + (Doki_SIN_W > Top || Doki_SIN_W == Top) * Top;
}

ISR(TIMER5_CAPT_vect) {
  if(number % 2 == 0)number++;  
  number %= 108;
  sin_nyuryoku_9pulse();
  ocr5();
  TCCR5A = 0b10101000;
  number++;
}

ISR(TIMER5_OVF_vect){
  if(number % 2 == 1)number++;  
  number %= 108;
  sin_nyuryoku_9pulse();
  ocr5();
  TCCR5A = 0b10101010;
number++;
}

ISR(TIMER5_COMPA_vect) { 
  if(pulseMode==1){
    if(pulse==2){
     Carrier_freqency = Hz * 3; 
     Top = (uint16_t)(1000000 / Carrier_freqency);
     }
     else{
     Carrier_freqency = Hz * pulse;
     Top = (uint16_t)(1000000 / Carrier_freqency);
     }
  }
  else Top = (uint16_t)(1000000 / Carrier_freqency);  //16*10^6/2/Carrier_freqency/8
  ICR5 = Top;
}

int main() {

  cli();
  init();

  Vout=0.0;
  hz_mode=0.0005;
  Hz = 0;
  Hz_kasokudo = 0;
  
  pinMode (44, OUTPUT);
  pinMode (45, OUTPUT);
  pinMode (46, OUTPUT);
  analogReference(DEFAULT);
  Serial.begin(230400);

  TCCR4A = 0b00000011;
  TCCR4B = 0b00011001;
  TIMSK4 = 0b00000010;
  sei();

    one_pulse_freqency = 51;
    wide3_pulse_freqency = 47;
    sync_9pulse_freqency = 39;
    Voltage_frequency = 1 / one_pulse_freqency;

  while (true) {//LOOP
  train();

if (Vout > 0.1 && Hz > 0 && Carrier_freqency > 0) {
  if (pulseMode) {//syncronous mode
        if (pulse == 1) {
          TIMSK5 = 0;
          TCCR5A = 0;
          TCCR5B = 0;
          if (Vout >= 0.01) {
            TCCR3A = 0b00000011;
            TCCR3B = 0b00011001;
            OCR3A = 16000000/Hz/6-1;
            TIMSK3 = 0b00000010;
          } 
          else {
            TIMSK3 = 0;
          }
        }
    else {
          TIMSK2 = 0;
        if(pulse == 2){
            TIMSK5 = 0;
            TCCR5A = 0;
            TCCR5B = 0;
            TCCR3A = 0b00000011;
            TCCR3B = 0b00011001;
            OCR3A = 16000000/Hz/256-1;
            TIMSK3 = 0b00000010;
         }
         else{
          TIMSK5 = 0b00100011;
          TCCR5B = 0b00010010;
         }
    }
  }
  else{//asynchronous mode
        TIMSK3 = 0;
        TCCR2A = 0b10000010;
        TCCR2B = 0b00000110;
        OCR2A = (8000000 / Hz / 256 / 128) - 1;
        TIMSK2 = 0b00010010;
        TCCR5A = 0b10101000;
        TCCR5B = 0b00010010;
        TIMSK5 = 0b00000010;
        OCR5A_hidoki = (Top / 2 * ((pgm_read_byte_near(&U_table[number]) - 127) * Vout / 99.745567 + 1));
        OCR5B_hidoki = (Top / 2 * ((pgm_read_byte_near(&V_table[number]) - 127) * Vout / 99.745567 + 1));
        OCR5C_hidoki = (Top / 2 * ((pgm_read_byte_near(&W_table[number]) - 127) * Vout / 99.745567 + 1));
   
       if(OCR5A_hidoki > Top) OCR5A_hidoki=Top;
        if(OCR5A_hidoki < 0) OCR5A_hidoki=0.0;
        if(OCR5B_hidoki > Top) OCR5B_hidoki=Top;
        if(OCR5B_hidoki < 0) OCR5B_hidoki=0.0;
        if(OCR5C_hidoki > Top) OCR5C_hidoki=Top;
        if(OCR5C_hidoki < 0) OCR5C_hidoki=0.0;
        OCR5A = OCR5A_hidoki;
        OCR5B = OCR5B_hidoki;
        OCR5C = OCR5C_hidoki;
 }
}
else {
      TIMSK3 = 0;
      TCCR5A = 0;
      TCCR5B = 0; 
      PORTL = 0;
}
    TCCR4A = 0b00000011;
    TCCR4B = 0b00011001;
    TIMSK4 = 0b00000010;
    OCR4A = (16000000 / 2000) - 1;
  char inkey;
  inkey = Serial.read();
 if(inkey=='a')Hz_Target = 0;
  else if(inkey=='b')Hz_Target =15;
  else if(inkey=='c')Hz_Target =38;
    else if(inkey=='d')Hz_Target = 40;
        else if(inkey=='e')Hz_Target =46;
        else if(inkey=='f')Hz_Target =48;
            else if(inkey=='g')Hz_Target =100;

    Vout_Target = Hz_Target * Voltage_frequency;
    if(Vout_Target < 0)Vout_Target = 0;
    if(Vout_Target > 1.0)Vout_Target= 1.0;
    if (round(Hz) == round(Hz_Target)) {      //do nothing
    } 
    else {
      if (Hz_Target > Hz) Kasoku();
      else if (Hz_Target < Hz) Gensoku();
      else Hz_kasokudo = 0;
    }
  }
  return 0;
} 

Add temporary Serial.print telling, at strategic points, where in the code it is and also millis();
Use serial monitor to watch.

please reformat your code and post it again.

Also, what will be the value Target if no char is not in the range 'a'…'g'?

This is running test code:
スクリーンショット 2024-07-03 225428
スクリーンショット 2024-07-03 225310

At frequency = 46Hz (inkey=e) mode, micros time is as follows:
スクリーンショット 2024-07-03 224910
But at frequency = 48Hz(inkey=f) mode, time is as follows:
スクリーンショット 2024-07-03 225521
Quite long time―around 0.2s.

Once I used timer counter for carrier wave―includling PWM outputs' inverting and non-inverting mode, but I failed to make wide 3 pulse mode.
Like this:

And for testing, I changed source code as follows:

Sorry. That mess doesn't help.
For the slow execution, use reply #2 to figure out where time is spent.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.