AnsweredAssumed Answered

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

Question asked by toprak.can on Jul 9, 2013
Latest reply on Jul 9, 2013 by Clive One
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
 */
#include "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){
     }


     




}

Outcomes