AnsweredAssumed Answered

STM8  how to change Duty Cycle during running

Question asked by Yusuf Korkmaz on Jan 4, 2018
Latest reply on Jan 4, 2018 by Yusuf Korkmaz

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

 

#include <stm8s.h>
#include <stm8s_eval_lcd.h>
#include <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();  
   }
  

Outcomes