// Automatically generated C++ file on Mon Jun 15 15:23:07 2026 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD -o dpwm_typ2.cpp kernel32.lib #include // Filter coëfficiënten (Type 2 Biquad) #define CA1 (1.2088255364823535f) #define CA2 (-0.20882553648235358f) #define CB0 (0.3981763441362136f) #define CB1 (0.005178224754781007f) #define CB2 (-0.3929981193814326f) #define spannings_deler (1000.0/151000.0) #define Vout_set (400.0) #define adcrange (4096.0) // DIGITALE INSTELLINGEN #define DIGITAL_REF (((Vout_set*spannings_deler) / (Vref)) * adcrange) // Gewenste Vout in ADC counts (bijv. 400V equivalent) #define MAX_TON (5.0e-6) // Harde bovengrens on-time (5 microseconden) #define MIN_TON (50.0e-9) // Minimale on-time (50 nanoseconden) 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 PWM #undef clk #undef Vref #undef Vin #undef adcout #undef zcd // Geheugen voor flankdetectie bool lastClk = false; bool lastZcd = false; // State variables voor het digitale filter (Type 2 compensator) float x1 = 0.0f; // x[n-1] float x2 = 0.0f; // x[n-2] float y1 = 0.0f; // y[n-1] float y2 = 0.0f; // y[n-2] // Variabelen voor de PWM-generatie double ton_target = 0.0; // De actuele gewenste ton (geüpdatet door het filter) double t_pulse_start = 0.0; // Tijdstip waarop de FET is aangezet bool pwm_active = false; // Status van de PWM puls extern "C" __declspec(dllexport) void dpwm_typ2(void **opaque, double t, union uData *data) { bool clk = data[0].b ; // input double Vref = data[1].d ; // input double Vin = data[2].d ; // input bool zcd = data[3].b ; // input double &PWM = data[4].d ; // output unsigned int &adcout = data[5].ui; // output // Implement module evaluation code here: if(lastClk == false && clk == true) { // A. Virtuele ADC conversie double vin_clamped = Vin; if (vin_clamped > Vref) vin_clamped = Vref; if (vin_clamped < 0.0) vin_clamped = 0.0; unsigned int adc_counts = (unsigned int)((vin_clamped / Vref) * (adcrange)); adcout = adc_counts; // Update debugger output pin // B. Genormaliseerde fout berekenen (omzetten naar bereik -1.0 tot 1.0) float v_out_digital = (float)adc_counts / (float)adcrange; float v_ref_digital = (float)DIGITAL_REF / (float)adcrange; float x0 = v_ref_digital - v_out_digital; // C. Bereken de Type 2 differentievergelijking (Uitgang is genormaliseerde sturing 0.0 tot 1.0) float filter_out = (CB0 * x0) + (CB1 * x1) + (CB2 * x2) + (CA1 * y1) + (CA2 * y2); // D. Anti-windup / Saturation op de genormaliseerde regeloutput if(filter_out > 1.0f) filter_out = 1.0f; else if(filter_out < 0.0f) filter_out = 0.0f; // E. Schaal de genormaliseerde output (0.0 - 1.0) naar de fysieke on-time (ton) ton_target = (double)filter_out * MAX_TON; if(ton_target < MIN_TON) ton_target = MIN_TON; // F. Update de geschiedenis van het filter met de genormaliseerde waarden x2 = x1; x1 = x0; y2 = y1; y1 = filter_out; } // -------------------------------------------------------------------------- // DEEL 2: SNELLE HARDWARE LUS (PWM Start/Stop op basis van ZCD en tijd) // -------------------------------------------------------------------------- // A. Start de PWM puls op de opgaande flank van het ZCD signaal (stroom is 0) if(lastZcd == false && zcd == true) { if(!pwm_active) { t_pulse_start = t; pwm_active = true; } } // B. Beëindig de PWM puls zodra de berekende ton_target is verstreken if(pwm_active && (t - t_pulse_start >= ton_target)) { pwm_active = false; } // Stuur de fysieke PWM output pin aan (0V of 5V voor de driver) PWM = pwm_active ? 5.0 : -1.0; // Sla de statussen op voor de volgende simulatiestap lastClk = clk; lastZcd = zcd; }