cancel
Showing results for 
Search instead for 
Did you mean: 

STM8 how to change Duty Cycle during running

Yusuf Korkmaz
Associate II
Posted on January 04, 2018 at 13:40

Hello community,

i try to programm an code for my experimentation and i have a problem .. my code should change the duty cycle ie. the value of CCR2_V if the value of Convertion_Value_Current exceeds or is below a specific value .. as a result i want to see on the oscilloscope a changing pwm but it is still with the same duty cycle as defined in the main (CCR2_V=27)

the idea was to increment or decrement the value of the register, but it does not working. Have you any ideas where the problem can be?

Thanks

BG

&sharpinclude <stm8s.h>

&sharpinclude <stm8s_eval_lcd.h>

&sharpinclude <stdio.h>

uint8_t StrName[16]={0};

uint16_t Conversion_Value = 0;

float Stufe = 0.00488;

float Nominal_Min = 3.500;

float Nominal_Max = 4.500;

float Conversion_Value_Current  = 0;

uint16_t CCR2_Val = 27 ;

uint8_t a = 50;

// Parametrisierung des PI Reglers  !!!! MUSS NOCH IN MATLAB BESTIMMT WERDEN !!!! //

float kp = 0.001;

float tn = 0.04;

int main(void){

  // Systemtakt

  CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

 

 

 

  // Erzeuge PWM

  TIM3_DeInit();

  TIM3_TimeBaseInit(TIM3_PRESCALER_1, 45);

  TIM3_OC2Init(TIM3_OCMODE_PWM1, TIM3_OUTPUTSTATE_ENABLE,CCR2_Val, TIM3_OCPOLARITY_HIGH);

  TIM3_ARRPreloadConfig(ENABLE);

  TIM3_OC2PreloadConfig(ENABLE);

  TIM3_Cmd(ENABLE);

 

   

 

 

  // Initialisiere ADC und LCD

  STM8S_EVAL_LCD_Init();   

  GPIO_DeInit(GPIOB);

  ADC2_DeInit();

  GPIO_Init(GPIOB, GPIO_PIN_2, GPIO_MODE_IN_PU_NO_IT);

  ADC2_Init(ADC2_CONVERSIONMODE_CONTINUOUS, ADC2_CHANNEL_2, ADC2_PRESSEL_FCPU_D4, ADC2_EXTTRIG_TIM, DISABLE , ADC2_ALIGN_RIGHT, ADC2_SCHMITTTRIG_CHANNEL9, DISABLE);

  ADC2_Cmd(ENABLE);

 

  LCD_Clear();

  LCD_BacklightCmd(ENABLE);

 

  ADC2_StartConversion();

  LCD_SetCursorPos(LCD_LINE1, 0);

  LCD_Print('Convers.Value.:');

  LCD_Print('                ');

      

 

  while(1)

   {

    

    while(!ADC2_GetFlagStatus()){}

   

    Conversion_Value = ADC2_GetConversionValue();

   

    Conversion_Value_Current = Conversion_Value*Stufe;

  

   

    //Regelung der Duty Cycle f�r einen erw�nschten Ausgangsstrom

   

    if(Conversion_Value_Current > Nominal_Max)

    {

     

      CCR2_Val--;

        

    }

    else if (Conversion_Value_Current < Nominal_Min)

    {

    

     CCR2_Val++;

       

    }

 

    TIM3_OC2Init(TIM3_OCMODE_PWM1, TIM3_OUTPUTSTATE_ENABLE,CCR2_Val, TIM3_OCPOLARITY_HIGH);

 

       

    sprintf((char*)StrName, '%.3f    ', (Conversion_Value*Stufe));

    LCD_SetCursorPos(LCD_LINE2, 0);

    LCD_Print(StrName);

    ADC2_ClearFlag();  

   }

  

#stm8/128 #pwm-stm8 #stm8
2 REPLIES 2
Posted on January 04, 2018 at 14:51

Would probably avoid preloading the CCRx, but rather let the new value latch at the update event.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 04, 2018 at 15:25

Hello Clive One, thanks a lot for your fast reply and support.

But when I do not preload the CCR2, then i do not have the waveform .. slow on the uptake, sorry i do not quire get it

How the buffering the new value would be look like in the code?