#include "stm32f4xx.h" #include "stm32f4_discovery.h" int x; int main(void) { /*Structures used in the configuration*/ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable TIM4 Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); /* Enable GPIOD Pins that are used for on board LED's */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //Enabled GPIOB we are going to use PB6 which is linked to TIM4_CH1 according to the //documentation /* Initialise pins 13, 14 and 15 D - relating to on board LED's*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4); /* Setup timer defaults */ TIM_TimeBaseStructure.TIM_Period = 840;//665; TIM_TimeBaseStructure.TIM_Prescaler = 100;//PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /* Configure timer for PWM - channel 2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC2Init(TIM4, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); /* Configure timer for PWM - channel 3*/ /* Configure timer for PWM - channel 4 */ TIM_ARRPreloadConfig(TIM4, ENABLE); /* Start timer*/ TIM_Cmd(TIM4, ENABLE); int power = 0, j=0; while(1) //Loop forever { TIM4->CCR2 = 100;// x=GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_13); for(j=0; j<80000; j++); power++; if(power > 699){ power = 0; for(j=0; j<80000; j++); } } }