Hello guys,
I am currently working with a mircocontroller for the first time. My goal is to achieve a PWM-signal (1kHz). While trying to generate the signal I encountered the problem that my signal seems to be moving horizontal due to inconsistancy in time up to 100 µs. The goal was to guarantee accurate values up to 50µs. I inserted 2 example pictures in which you can see the problem:




The left and right pictures each show one PWM-signal. No settings were changed and if it would be viewed in Realtime you could see the signal jumping right and left by 100 µs. Although you can hardly spot any differenz from those pictures there defintely is one.
My take on generating a PWM-signal includes a counting loop which counts up and activates high until the value is reached to swap to low. When the absolut counting value which takes roughly 1ms (1kHz) the loop resets and starts all over again. I attached my code for the PWM-Generation. I tried to describe my variables as good as I can (I am no native english speaker), if anything remains unclear feel free to ask!
I think the problem revolves around the controller beeing overwhelmed by the amount of tasks he has to forfill. Additionaly the prewritten code provided uses a statemachine and interrupt functions which could also potentally cause delays in prozess time. Also there are prewritten functions called SYST_Config and SYST_Handler. The former one configures a reload count which could set a time in which the latter function is called. Sadly i wasn't quite able to understand how they are working since changing the reload count didn't result in any changes as how often SYST_Handler is beeing called.
The solution to my problem should revolve around utilazing a proper time to vary the High/Low timings instead of counting loops since these loops depend on the speed at which the core resolves its tasks and therfore is not really precise.
Any Help is highly appreciated!
Regards,Paul
I am currently working with a mircocontroller for the first time. My goal is to achieve a PWM-signal (1kHz). While trying to generate the signal I encountered the problem that my signal seems to be moving horizontal due to inconsistancy in time up to 100 µs. The goal was to guarantee accurate values up to 50µs. I inserted 2 example pictures in which you can see the problem:




The left and right pictures each show one PWM-signal. No settings were changed and if it would be viewed in Realtime you could see the signal jumping right and left by 100 µs. Although you can hardly spot any differenz from those pictures there defintely is one.
My take on generating a PWM-signal includes a counting loop which counts up and activates high until the value is reached to swap to low. When the absolut counting value which takes roughly 1ms (1kHz) the loop resets and starts all over again. I attached my code for the PWM-Generation. I tried to describe my variables as good as I can (I am no native english speaker), if anything remains unclear feel free to ask!
PWM-signal generation:
ADC_Handler();
Test_DEMOD2 = (DEMOD2_DATA>>8); //fetches the measured values
Test_DEMOD1 = (DEMOD1_DATA>>8);
Counter=Counter+1; //Programmcounter
SYST_Handler(); //the described prewritten function
if (((Test_DEMOD1 == 32)&(Test_DEMOD2 == 83))|((Test_DEMOD1 == 32)&(Test_DEMOD2 == 84))) //genereates a pwm-signal while nothing is measured (signals failure)
{
while (Inaktiv<22)
{
GPIO1_LOW();
Inaktiv=Inaktiv+1;
}
while (Inaktiv>=22)
{
GPIO1_HIGH();
Inaktiv=Inaktiv+1;
if (Inaktiv>=258)
{
Inaktiv=0;
}
}
}
DIF=Test_DEMOD1-Test_DEMOD2+120; //shifting my numbers to positive values only
if(DIF>=150) //prevents programm from stopping due to a too high value when initialising the measure
{
DIF=149;
}
POS=DIF; //POS and NEG are defined as float values in order to calculate a proper ratio
NEG=150-DIF;
VERH=POS/(POS+NEG); //VERH stands for ratio
if (((Test_DEMOD1 != 32)&(Test_DEMOD2 != 83)))
{
TAN=12*VERH; //the loop has to happen 12 times in order to achive a frequency of 1kHz
TAUS=12*(1-VERH); //TAN is the Time while output his High and TAUS is the time when output is Low
while(aktiv<TAUS)
{
GPIO1_LOW();
aktiv=aktiv+1;
}
while(aktiv>=TAUS)
{
GPIO1_HIGH();
aktiv=aktiv+1;
if(aktiv>=12)
{
aktiv=0;
}
}
}
}
}
I think the problem revolves around the controller beeing overwhelmed by the amount of tasks he has to forfill. Additionaly the prewritten code provided uses a statemachine and interrupt functions which could also potentally cause delays in prozess time. Also there are prewritten functions called SYST_Config and SYST_Handler. The former one configures a reload count which could set a time in which the latter function is called. Sadly i wasn't quite able to understand how they are working since changing the reload count didn't result in any changes as how often SYST_Handler is beeing called.
The solution to my problem should revolve around utilazing a proper time to vary the High/Low timings instead of counting loops since these loops depend on the speed at which the core resolves its tasks and therfore is not really precise.
Any Help is highly appreciated!
Regards,Paul
Attachments
-
132.2 KB Views: 3