// Automatically generated C++ file on Tue Apr 30 11:34:58 2024 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD digital_pwm_gen.cpp kernel32.lib union uData { bool b; char c; unsigned char uc; short s; unsigned short us; int i; unsigned int ui; float f; double d; long long int i64; unsigned long long int ui64; char *str; unsigned char *bytes; }; // int DllMain() must exist and return 1 for a process to load the .DLL // See https://docs.microsoft.com/en-us/windows/win32/dlls/dllmain for more information. int __stdcall DllMain(void *module, unsigned int reason, void *reserved) { return 1; } // #undef pin names lest they collide with names in any header file(s) you might include. #undef Tick #undef PWM #undef nPWM #undef Reset #undef PWM_Test #undef Max_Duty //--------My added variables------------ int Prev_Tick_State=0; //Holds previouse tick state int Time_Base_Counter =0; //This will hold the duty cycle generating counter int Dead_Time_Ticks =2; //Put here the amount ticks to form the required dead time. int Dead_Time_Counter =1; //This counter will count the dead time ticks int Dead_Time2_Counter=0; int Full_Cycle_TimeBase_Count =99; //This represents a full switching periode of 100 ticks (0-99) double Reset_Threshold =1; //This acts as the current sense (Reset pin) threshold level (Usually 1V). int Min_Duty =1; //1% is the minimum duty cycle allowed int Required_Max_Duty; //This will hold a limit set externally on the Max allowed duty cycle pin. bool State1_Completed =1; //Marks if all output transitions associated with (Time_Base_Counter = Required_Duty_Cycle)state were completed. bool State2_Completed =1; //Marks if all output transitions associated with (Time_Base_Counter =0) state were completed. bool Reset_State_Completed =1; //Marks that all output transitions associated with Reset were completed. bool Reset_Event=0; //This will act as a comparator marking "1" when the (Reset >= Reset_Threshold) bool Prev_Reset_Event=0; extern "C" __declspec(dllexport) void digital_pwm_gen(void **opaque, double t, union uData *data) { bool Tick = data[0].b; // input double Reset = data[1].d; // input int Max_Duty = data[2].i; // input bool &PWM = data[3].b; // output bool &nPWM = data[4].b; // output double &PWM_Test = data[5].d; // output // ----------------Implementation starts here------------------------------------ if (Prev_Tick_State==0 && Tick==1) //Actions on rising Edge of Tick { Required_Max_Duty = int(Max_Duty); //Sample the required Max duty cycle limit pin if (Required_Max_Duty > 99) Required_Max_Duty=99; //Make sure we limit the maximum required duty cycle to a valid value. if (Required_Max_Duty <= Min_Duty ) Required_Max_Duty = Min_Duty; //Make sure we limit the minimum duty cycle too. // ---Sample Reset Pin------------ if ((Reset >= Reset_Threshold)) //Sample the reset pin voltage once per switching cycle and act according to its value compared to a threshold { Reset_Event=1; } else if ((Reset < Reset_Threshold)) { Reset_Event=0; } PWM_Test = Time_Base_Counter; //----------------------------- if ((Time_Base_Counter == 0)&&(Reset_State_Completed)||(!State2_Completed)) { nPWM=0; //Start new switching cycle by first holding the complimentary PWM output low. State2_Completed=0; //Mark that the whole transition associated with this state as yet to be done if (Dead_Time_Counter == Dead_Time_Ticks) { PWM=1; //Output high on PWM output as Dead time elapse Dead_Time_Counter=0; State2_Completed=1; //Mark that we concluded this state associated outputs transitions } else { Dead_Time_Counter++; } } //--------------------------- if (((Time_Base_Counter == Required_Max_Duty)&&(Reset_State_Completed)||(!State1_Completed))) { PWM=0; //Set PWM output low. State1_Completed=0; //Mark that the whole transition associated with this state as yet to be done if (Dead_Time_Counter == Dead_Time_Ticks) { nPWM=1; //Output high on PWM output as Dead time elapse Dead_Time_Counter=0; State1_Completed=1; //Mark that we concluded this state associated outputs transitions } else { Dead_Time_Counter++; //Increment the dead time counter } } //-------------------------- if ((Time_Base_Counter < Full_Cycle_TimeBase_Count))//We counted a full switching cycle { Time_Base_Counter++; } else if ((Time_Base_Counter == Full_Cycle_TimeBase_Count)) { Time_Base_Counter=0; } //------------------------- if ((Reset_Event)&&((!Reset_State_Completed)||(Time_Base_Counter <= Full_Cycle_TimeBase_Count))) //We got a Reset but didn't complete associated actions { PWM=0; //Hold PWM output at low. if (Dead_Time_Counter == Dead_Time_Ticks) //We concluded the dead time period { nPWM=1; //Set nPWM output Dead_Time_Counter=0; //Mark that Dead time action was concluded if ((Time_Base_Counter <= Full_Cycle_TimeBase_Count)&& (Dead_Time_Counter=0)) //We finished the output transitions but need to wait for full switching cycle completion { Reset_State_Completed=0; //Mark that we didn't complete the reset state action Time_Base_Counter++; //Keep increasing the time base counter } else { Time_Base_Counter=0; //Restart a new counting cycle Reset_State_Completed=1; } } else if (Dead_Time_Counter