cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32f4 Discovery-Change the duty cycle with user button-Help

can
Associate
Posted on July 09, 2013 at 10:52

Hello everyone,I am the new user and I need your help about an example Stm32f4- Discovery.I use TIM10 register for to get pwm.And I want to be increase duty ratio with user button.I try to change TIM10->CCR1 but its not working.What should I do for this problem??;Here is my code:

/* TIM10_PWM

 * main.c

 *

 *  Created on: 12 Agu 2012

 *      Author: m2k

 */

&sharpinclude ''stm32f4xx.h''

GPIO_InitTypeDef  GPIO_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

uint16_t PrescalerValue = 0;

 

void GPIO_Conf(void) {

 

/* GPIOB ve TIM10 CLK Kaynagi etkinlestiriliyor */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10,ENABLE);

 

/* PB8 Ek �zellikler i�in ayarlaniyor */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

 

/*Pin Y�nlendirme Islemi*/

GPIO_PinAFConfig(GPIOB,GPIO_PinSource8,GPIO_AF_TIM10);

}

 

void TIM_Conf(void) {

int val=0;

PrescalerValue = 140;

/* TIM10 Ayarlari Yapiliyor */

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

/* TIM10 I�in CounterMode_UP Gereksizdir. Fakat Kodu degistirip

* diger sayicilar i�in kullanma ihtimaline karsi silmiyorum */

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = 10;

TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;

TIM_TimeBaseInit(TIM10, &TIM_TimeBaseStructure);

 

/* PWM Ayarlari yapiliyor. 32KHz @ %50 */

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM10->CCR1=val; 

TIM_OC1Init(TIM10, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM10, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM10, ENABLE);

 

}

int main(void) {

int val=0;

TIM10->CCR1=val;

if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)) {

val++;

}

GPIO_Conf();

TIM_Conf();

/* TIM10 Baslatiliyor */

TIM_Cmd(TIM10, ENABLE);

while(1){

}

}

#stm32f4-discovery-timer10-duty
2 REPLIES 2
Posted on July 09, 2013 at 11:20

First, put the button checking and subsequent change of TIM10->CCR1 into the final while(1) loop.

Later, you will probably need more processing around the button press checking (debouncing etc.)

JW

Posted on July 09, 2013 at 13:10

You'd need to configure the GPIO and clocks for the button pin, and spin in a loop testing the pin so it doesn't rush by the test once when it starts.

Zero is OFF

The values in Prescaler/Period are N-1, the value in Pulse are N, when N/2 is 50%, 0 always off, N always on
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..