Skip to main content
SANAN.1
Associate II
May 11, 2021
Question

How can I increase the amplitude of the sine wave generated from the pwm of the stm8s003f3 microcontroller ?? Is there any particular formula to calculate that??

  • May 11, 2021
  • 1 reply
  • 3369 views

I have a haptic button where the vibration has been set as soon as the pwm is genereated, I want to increase the vibration by changing the amplitude of the sine wave generated from the pwm, Can I know how can I do that??

    This topic has been closed for replies.

    1 reply

    Peter BENSCH
    Technical Moderator
    May 11, 2021

    Since you have not posted any code, we can only guess: I assume that the sine is output via PWM in your program with full amplitude, i.e. with a peak value of 100%.

    If you want to increase the resulting vibration, you have to increase the amplitude outside of the controller using an amplifier, e.g. with an opamp. However, the specific implementation depends on your setup, because you also did not mention how the vibration itself is generated and which signals can be processed there.

    Regards

    /Peter

    In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
    SANAN.1
    SANAN.1Author
    Associate II
    May 11, 2021

    Hello Peter here is the sine structure of the code and the pmw value, here i want to know the relation between the duty cycle and sine signal and change the amplitude to gain more vibration

    static void LoadValueIntoSinStruct(void)

    {

     uint8_t offset=0,diff=0;

      

     while(offset <= (nbr_of_samples_half/2))

     {

      /*First half of upper half samples of sine wave is loaded into "value" of sin structure.*/

       sin.Value[offset] = sample[offset]; 

       offset++;            

     }

      

     diff = (uint8_t)(offset - 1);

     while(offset < nbr_of_samples_half)

     {                   

       diff--;  

       /* Second half of upper half samples of sine wave is loaded into "value" of sin structure.*/

       sin.Value[offset] = sample[diff];   

       offset++;             

     }  

     /*Index is initialized to 0.Sin wave upper half is generated at start*/

     sin.Index = 0;            

     sin.Up = TRUE;

    }

    INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)

    {

     uint8_t PWM_Value = 0; 

     if (counter >= COUNTER)        

     { 

      /* Reset the counter value*/

      counter = 0;

      /* Upper half cycle of sine wave */

      if (sin.Up == TRUE)

      {

       /* Load the duty cycle register for upper half cycle of sine wave.*/ 

       /* This is achieved by adding sample values into AVERAGE_AMP.*/

       PWM_Value = (uint8_t)(AVERAGE_AMP + sin.Value[sin.Index]);

      }

      else 

      { 

       /* Lower half cycle of sine wave */ 

       /* Load the duty cycle register for lower half cycle of sine wave. */ 

       /* This is achieved by  subtracting sample values into AVERAGE_AMP. */   

       PWM_Value = (uint8_t)(AVERAGE_AMP - sin.Value[sin.Index]);

      }

      /* Set the Capture Compare1 Register value */

    //  TIM2_SetCompare1(PWM_Value);

      TIM2->CCR1H = (uint8_t)(PWM_Value >> 8);

      TIM2->CCR1L = (uint8_t)(PWM_Value);

      /*Increment the index for pointing to wardsnext sample value to be loaded */

      sin.Index++;     

      if (sin.Index == nbr_of_samples_half) 

      {

       /* Reset the index value */

       sin.Index = 0;

       /* Lower half sin wave is activated if previously it was upper half and vice-versa */

       sin.Up = (bool_t)(~sin.Up);

      } 

     }

     /* Increment the counter value at each overflow interrupt */

     counter++;

     //TIM2_ClearITPendingBit(TI21_IT_UPDATE);

     TIM2->SR1 = (uint8_t)(~(uint8_t)TIM2_IT_UPDATE);

    }

    SANAN.1
    SANAN.1Author
    Associate II
    May 11, 2021

    basically I want to know why sinusodial signal is genereated from the pwm, its realtion with pwm??