cancel
Showing results for 
Search instead for 
Did you mean: 

Help to configure TIM1(URGENT)

aloui
Associate II
Posted on April 10, 2009 at 22:12

Help to configure TIM1(URGENT)

5 REPLIES 5
aloui
Associate II
Posted on May 17, 2011 at 13:09

Hello,

I am a student and we have a project to drive a Buzzer using an STM32. The professor will send frequencies to every one via Zigbee, we catch them and we drive the Buzzer, in order to have a complete symphony in the classroom composed of different instruments, each one is one of our boards that we have already developed.

We are not allowed to use the STM32 firmware (only the STM32103XX_map.h file is allowed). Until now, I have configured the internal clocks, 2 buttons with interrupts, 3 leds, 1 LCD, the zigbee on UART3, but I'm blocked now on the buzzer part.

I have to use a PWM on PA8 via TIM1_CH1 with a duty cicle of 50%.

I have configured the registers as i think well, i mean enabling the clock, configuring the pins to work on alternative functions output, the prescalar, the autoreload, the mode (PWM2) the channel 1, and the capture compare, but still nothing is out of the buzzer.

May someone tell me what are and in which order shall i configure those registers, in order to have a PWM signal with 50% duty cycle and a frequency range from 1K to 100khz, im changing ARR and TIM1->CCR each time so that CCR would be the half of TIM1->ARR, (i think also that i can play on the other way using TIM1->PSC in stead)

I appreciate your help

smart
Associate II
Posted on May 17, 2011 at 13:09

you can use this function though it uses STmm32 firmware library.

but still you can get some idea where to start

/*******************************************************************************

* Function Name : Hardware_PWM_PM5_PULSE

* Description : Initialize PWM channels.

* TIM1 Configuration: PWM signals with 50% duty cycle.

* TIM1CLK = 72 MHz, Prescaler = 0,Period = 1799, TIM1 counter clock = 36 Mhz

* TIM1 frequency = TIM1CLK/(TIM1_Period + 1) = 20 KHz

* TIM1 Channel1 duty cycle = TIM1->CCR1 / (TIM1_Period + 1)

* = 900/(1800)*100 = 50 %

********************************************************************************/

VOID Hardware_PWM_PM5_PULSE(WORD DutycycleValue)

{

GPIO_InitTypeDef GPIO_InitStructure;

TIM1_TimeBaseInitTypeDef TIM1_TimeBaseStructure; // Timer 1

TIM1_OCInitTypeDef TIM1_OCInitStructure;

/* GPIOA Configuration: Timer1 Channel 1,2,3 Output */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Time Base configuration */

TIM1_TimeBaseStructure.TIM1_Prescaler = 1;

TIM1_TimeBaseStructure.TIM1_CounterMode = TIM1_CounterMode_Up;

TIM1_TimeBaseStructure.TIM1_Period = 1799;

TIM1_TimeBaseStructure.TIM1_ClockDivision = 0;

TIM1_TimeBaseStructure.TIM1_RepetitionCounter = 0;

TIM1_TimeBaseInit(&TIM1_TimeBaseStructure);

/*Initialize the timer 1 PWM */

TIM1_OCInitStructure.TIM1_OCMode = TIM1_OCMode_PWM2;

TIM1_OCInitStructure.TIM1_OutputState = TIM1_OutputState_Enable;

TIM1_OCInitStructure.TIM1_OutputNState = TIM1_OutputNState_Disable ;

TIM1_OCInitStructure.TIM1_Pulse = DutycycleValue;

TIM1_OCInitStructure.TIM1_OCPolarity = TIM1_OCPolarity_Low;

TIM1_OCInitStructure.TIM1_OCIdleState = TIM1_OCIdleState_Set;

TIM1_OC1Init(&TIM1_OCInitStructure);

/* TIM1 counter enable */

TIM1_Cmd(ENABLE);

/* TIM1 Main Output Enable */

TIM1_CtrlPWMOutputs(ENABLE);

}

aloui
Associate II
Posted on May 17, 2011 at 13:09

Thanks a lot,

I would appreciate if i find how to do it directly using registers because this is what I'm supposed to do tomorrow. But it is helpful, at least it gives me ideas about what to do before the other.

Thank you so much again

aloui
Associate II
Posted on May 17, 2011 at 13:09

Still can't make it work.

Here is my code:

RCC->APB2ENR |=0x00000800;//activate TIM1 clock

//Initialize GPIO PA8 output AF 50Mhz

GPIOA->CRH &=~0x0000000f;

GPIOA->CRH |=0x0000000B;//mode alternate function

//================================

TIM1->PSC=1;//le prescalar 63Mhz (72/(psc+1))=36Mhz

TIM1->ARR=1799;//auto-reload (20 MHZ)

TIM1->CCR1=900;//capture compare 50% duty cycle

TIM1->CR1=0x0080;//Auto-reload preload enable

TIM1->CCMR1=0x0078;//configuration of Capture/Compare PWM2

//Output Compare 1 preload enable

TIM1->EGR |= 0x0001;//Update the registers

TIM1->CR1 |=0x0001;//On activate timer 1

TIM1->CCER |=0x0001;//On activate chanel 1

If any one has an idea what might be the problem please tell me

thanks

aloui
Associate II
Posted on May 17, 2011 at 13:09

Well, I've found how to do it:

here is my new source

RCC->APB2ENR |=0x00000004;//activate portA clock

RCC->APB2ENR |=0x00000800;//activate TIM1

//préparation des GPIO ===========

GPIOA->CRH &=~0x0000000f;

GPIOA->CRH |= 0x0000000B;//mode alternate function

//================================

TIM1->CCER &=~0x0001;//Disactivate CH 1 for Configuration

TIM1->CCMR1 = 0x0078;//configuration of Capture/Compare

TIM1->CR1 = 0x0080;//On configuration of Timer

TIM1->CCER |= 0x0001;//activate le CH 1

TIM1->CR1 |= 0x0001;//activate timer 1

TIM1->PSC = 1635;//1;//prescalar (36Mhz de frequance MAX)

TIM1->ARR = 100;//1799;//auto-reload 20 kHZ

TIM1->CCR1 = 50;//900;//capture compare 50% raport cyclique

TIM1->BDTR = 0x8000;//Activate the output MO

TIM1->EGR |= 0x0001;//Update registres

This works perfectly