cancel
Showing results for 
Search instead for 
Did you mean: 

PWM with Buzzer

adnen_speder
Associate II
Posted on May 16, 2012 at 10:45

Salut a tous,

SVP quelqu'un peut m'aider, j'ai créer un application sur PWM de stm32f100rb pour générer une tonalité sur un buzzer, mais je sais pas comment générer les notes Do,Ré,Mi ... svp quelqu'un peut me donner une conseille : Code :


//#include ''stm32f10x.h''

//#include ''stm32f10x_gpio.h''

//#include ''stm32f10x_rcc.h''

#include ''stm32f10x_tim.h''




//Formule PWM

//tH : temps au niveau haut

//tB : temps au niveau bas

//T : tH + tB = période

//tH/T = rapport cyclique d'impulsion


void
PWM(
void
) {


GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

TIM_OCInitTypeDef TIM_OCInitStruct;


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
//APB2PeriphClockCmd

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//APB1PeriphClockCmd


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
//LED's

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//Alt Function Push Pull

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
//TIM3_CH3 vers GPIOC.Pin8, TIM3_CH4 vers GPIOC.Pin9 (voir datasheet)


//Laisser la freque. de PWM = 100Hz.

//Laisser la période = 1000. Par conséquent, la minuterie fonctionne à partir de zéro à 1000, pour donner la résolution 0.1Hz.

TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);

TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV4;

TIM_TimeBaseInitStruct.TIM_Period = 1000 - 1; 
//0..999

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);


TIM_OCStructInit(&TIM_OCInitStruct);

TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStruct.TIM_Pulse = 0; 
// 0 .. 1000 (0=Off, 1000=On) exemple 500 = 3.3v/2...


TIM_OC2Init(TIM3, &TIM_OCInitStruct); 
// Channel 2 PC7 BUZZZZZZ

TIM_OC3Init(TIM3, &TIM_OCInitStruct); 
// Channel 3 Blue LED

TIM_OC4Init(TIM3, &TIM_OCInitStruct); 
// Channel 4 Green LED


TIM_Cmd(TIM3, ENABLE);


}

int
variation(
int
brightness) {

PWM();

int
i;

// if (brightness == 0 || brightness > 1000) {

// brightness = 1;

// }

while
(1) {

brightness++;


//TIM3->CCR2 = 100;

TIM3->CCR3 = brightness;

TIM3->CCR4 = brightness;


for
(i = 0; i < 9000; i++)

;


if
(brightness == 1000) {

//TIM3->CCR2 = 100;

brightness = 0;

}



}

}

#pwm-adnen
5 REPLIES 5
Posted on May 16, 2012 at 16:47

You will need to pay attention to the prescaler to get the timebase working properly.
 For different tones you will presumably need to drive the buzzer with different frequencies.
 Do you have some details for the frequency and duty cycle you need for the different tones? 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
adnen_speder
Associate II
Posted on May 17, 2012 at 12:18

Thank you clive :)

Do you have some details for the frequency and duty cycle you need for the different tones?

No i dnt know :\ 

adnen_speder
Associate II
Posted on May 18, 2012 at 19:19

!!!!
adnen_speder
Associate II
Posted on May 23, 2012 at 18:43

Please any one can help me ...

Posted on May 23, 2012 at 22:41

I'm not sure you want help, but rather someone to code for you.

I wouldn't be mixing the LEDs and BUZZER on the same timer, as the LEDs are interested in the pulse width, and the buzzer the frequency. It might still be workable, but you'd need to recompute the duty cycle percentage as the frequency changes so the average voltage remains consistent. Something like the following should permit different frequencies to be set, but you'll have to delay between sending tones of different frequencies, and you will need to determine the frequency of the tones you want to use. I could figure them out, but I'm not that invested.

void TimerConfig(void)
{ 
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;// Buzzer
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
// TIM3 60 KHZ, ie 24 MHz / 400 or 72 MHz / 1200
// 60000 Hz chosen as a large, clean number fitting in 16-bit
TIM_TimeBaseInitStruct.TIM_Prescaler = 400 - 1; // 60 KHz timebase
TIM_TimeBaseInitStruct.TIM_Period = 1000 - 1; // Arbitary placeholder 60 Hz
TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse = (TIM_TimeBaseInitStruct.TIM_Period + 1) / 2; // 50% Duty
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStruct); // Channel 2 PC7 Buzzer
TIM_Cmd(TIM3, ENABLE);
} // sourcer32@gmail.com
int setfreq(int freq) // 1 to 3000 Hz
{
int period = 60000 / freq; // compute period as function of 60KHz ticks
TIM3->AAR = period - 1;
TIM3->CCR2 = period / 2; // Channel 2 50/50
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..