#include "stm32f4xx.h" int i=0; int c=0; double freq = 50; double PrescalerValue = 30; double value = 0; void timer4config(void); void timer3config(void); void timer5config(void); int main(void) { // PC0 // PD12 // PA0 value = (uint16_t) (SystemCoreClock/freq/(PrescalerValue+1))-1;; timer3config(); for (i=0; i<10700; i++) { c++; } timer4config(); c =0 ; for (i=0; i<10700; i++) { c++; } timer5config(); } void timer3config(void) { // Structures TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; // Enable TIM3 Clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Enable GPIOC Pin RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); // Initialise pins 6 C GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; 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(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3); // Setup timer defaults TIM_TimeBaseStructure.TIM_Period = value; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Configure timer for PWM - channel 1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = value/2; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3, ENABLE); // Start timer TIM_Cmd(TIM3, ENABLE); } void timer4config(void) { // Structures TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; // Enable TIM4 Clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); // Enable GPIOD Pin RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // Initialise pins 12D GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; 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_PinSource12, GPIO_AF_TIM4); // Setup timer defaults TIM_TimeBaseStructure.TIM_Period = value; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Configure timer for PWM - channel 1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = value/2; TIM_OC1Init(TIM4, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM4, ENABLE); // Start timer TIM_Cmd(TIM4, ENABLE); } void timer5config(void) { // Structures TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; // Enable TIM4 Clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); // Enable GPIOD Pin RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // Initialise pins 12D GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; 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(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM5); // Setup timer defaults TIM_TimeBaseStructure.TIM_Period = value; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Configure timer for PWM - channel 1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = value/2; TIM_OC1Init(TIM5, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM5, ENABLE); // Start timer TIM_Cmd(TIM5, ENABLE); }