cancel
Showing results for 
Search instead for 
Did you mean: 

Generate beep on STMT/8L-EV1

connor
Associate II
Posted on May 15, 2014 at 20:15

I have the STMT/8L-EV1 and I'd like to generate a 4KHz beep sound when one of the directional buttons is pressed.  Seems straight forward but I'm not sure how to implement it.  I've tried just using and turning the GPIO pin on and off to no avail.  I've tried to use the PWM for the TIM1_Channel2 the buzzer is on but that didn't work either.  I don't want the beep tied to a timer or interrupt.  All I need is a function to turn it on and one to turn it off.

Any ideas? Thanks.

#stm8 #beep #stm8l
5 REPLIES 5
chen
Associate II
Posted on May 16, 2014 at 11:05

Hi

''I have the STMT/8L-EV1 and I'd like to generate a 4KHz beep sound''

''I've tried to use the PWM for the TIM1_Channel2 the buzzer is on but that didn't work either.''

The IO pin probably does not have enough drive to drive the buzzer directly. You will needs a transistor or some kind of driver amp.

'' I don't want the beep tied to a timer''

Very difficult to get a regular on/off period just using code. It is easier to use the timer (in PWM mode) to do this.

If you do not mind the STM8 being tied up just toggling the IO port then it can be done.

connor
Associate II
Posted on May 16, 2014 at 20:15

I see.

I've looked through the demo code from the eval board and found this, two functions to turn the PWM on and off.  However I go and call these functions in my own code, they do not work.  Any thoughts?

void PlayPWM(u16 freq)

{

  u16 dutycycle;

  if (freq != 0)

  {

    /* Set frequency */

    TIM1->ARRH = (u8)(freq >> 8);

    TIM1->ARRL = (u8)(freq);

    /* Set duty cycle = freq/2 = 50% */

    dutycycle = (u16)(freq >> 4);

    TIM1->CCR2H = (u8)(dutycycle >> 8);

    TIM1->CCR2L = (u8)(dutycycle);

    /* Enable OCN2 */

    TIM1->CCER1 |= 0x40;

  }

}

void StopPWM(void)

{

  /* Disable OCN2 */

  TIM1->CCER1 &= (u8)(~0x40);

}

chen
Associate II
Posted on May 19, 2014 at 10:52

Hi

I am no familiar with the STM8 because I work with STM32F4 series.

Have you configured the IO pin to output the timer output instead of

IO ?

connor
Associate II
Posted on May 19, 2014 at 20:01

The function to initialize and configure the pin is 

GPIO_Init(GPIOE, GPIO_Pin_1, GPIO_Mode_Out_PP_Low_Fast);

I don't know, or haven't found a way to configure the actual pin to the timer.

chen
Associate II
Posted on May 20, 2014 at 10:35

Have you checked the reference manual for the device you are using?

''Alternate function outputs provide a direct path from a peripheral to an output or to an I/O pad, taking precedence over the port bit in the data output latch register (Px_ODR) and forcing the Px_DDR corresponding bit to 1. An alternate function output can be push-pull or pseudo-open drain depending on the peripheral and Control register 1 (Px_CR1) and slope can be controlled depending on the Control register 2 (Px_CR2) values.''