cancel
Showing results for 
Search instead for 
Did you mean: 

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??

SANAN.1
Associate II

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??

5 REPLIES 5
Peter BENSCH
ST Employee

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.

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);

}

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

also regarding the vibration its a haptic vibration controlled by the pwm!!

It looks like you are using the example TIM1_Sinewave_Generation from the Standard Peripheral Library.

Sine wave and PWM: well, there are lots of sources describing how to generate a sine using PWM, e.g. on Wikipedia. What other way would you get a sine out of the STM8S003?

A haptic vibration requires a module that converts the sinusoidal voltage into a physical movement: a coil, a motor, a piezo, or something similar. Each of them uses different drive technologies with individual input parameters. So if you want to increase the strength of the vibration, you have to increase the amplitude - provided that the following module can convert this higher amplitude into mechanical movement.

An increase in the amplitude within the microcontroller is only possible if the signal was not already at 100% beforehand - which is the case with the routine you are currently using.

Recommendation:

  • please check which input signal the module generating the haptic vibrations can handle
  • build an amplifier stage using e.g. an opamp between the PWM low-pass filter and the vibration module, which amplifies the signal by the desired factor and is at most so large that you do not overdrive the module

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.