/** * @brief Configure the TIMER peripheral. * @param None * @retval None */ void TIMER_Configuration(void) { /* --------------------------------------------------------------------------- TIMER2 Configuration: Output Compare Toggle Mode: TIMER2CLK = SystemCoreClock / 36000=2K, CH2 update rate = TIMER2 counter clock / CHCC2 = 2000/4000 = 0.5 Hz ----------------------------------------------------------------------------*/ TIMER_BaseInitPara TIMER_TimeBaseStructure; TIMER_OCInitPara TIMER_OCInitStructure; /* TIMERS clock enable */ RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1|RCC_APB2PERIPH_TIMER15|RCC_APB2PERIPH_TIMER16|RCC_APB2PERIPH_TIMER17,ENABLE); RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2|RCC_APB1PERIPH_TIMER3|RCC_APB1PERIPH_TIMER6|RCC_APB1PERIPH_TIMER14,ENABLE); /* TIMER2 configuration */ TIMER_DeInit(TIMER2); TIMER_TimeBaseStructure.TIMER_Prescaler = 35999; TIMER_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP; TIMER_TimeBaseStructure.TIMER_Period = 3999; TIMER_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1; TIMER_BaseInit(TIMER2,&TIMER_TimeBaseStructure); /* CH2 Configuration in OC_TOGGLE mode */ TIMER_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_TOGGLE; TIMER_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH; TIMER_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE; TIMER_OCInitStructure.TIMER_Pulse = 3999; TIMER_OC2_Init(TIMER2, &TIMER_OCInitStructure); TIMER_OC2_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE); /* Auto-reload preload enable */ TIMER_CARLPreloadConfig(TIMER2,ENABLE); /* TIMER enable counter*/ TIMER_Enable( TIMER2, ENABLE ); }
/** * @brief Configure the TIMER peripheral. * @param None * @retval None */ void TIMER_Configuration(void) { /* TIMER1 DMA Transfer example ------------------------------------------------- TIMER1CLK = 72MHz, Prescaler = 72 TIMER1 counter clock = SystemCoreClock/72 = 1MHz. The objective is to configure TIMER1 channel 1 to generate PWM signal with a frequency equal to 1KHz and a variable duty cycle(25%,50%,75%) that is changed by the DMA after a specific number of Update DMA request. The number of this repetitive requests is defined by the TIMER1 Repetition counter, each 2 Update Requests, the TIMER1 Channel 1 Duty Cycle changes to the next new value defined by the buffer . -----------------------------------------------------------------------------*/ TIMER_BaseInitPara TIMER_TimeBaseStructure; TIMER_OCInitPara TIMER_OCInitStructure; /* TIMERS clock enable */ RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1|RCC_APB2PERIPH_TIMER15|RCC_APB2PERIPH_TIMER16|RCC_APB2PERIPH_TIMER17,ENABLE); RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2|RCC_APB1PERIPH_TIMER3|RCC_APB1PERIPH_TIMER6|RCC_APB1PERIPH_TIMER14,ENABLE); /* TIMER1 configuration */ TIMER_DeInit(TIMER1); TIMER_TimeBaseStructure.TIMER_Prescaler = 71; TIMER_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP; TIMER_TimeBaseStructure.TIMER_Period = 999; TIMER_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1; TIMER_TimeBaseStructure.TIMER_RepetitionCounter = 1; TIMER_BaseInit(TIMER1,&TIMER_TimeBaseStructure); /* CH1 Configuration in PWM mode */ TIMER_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1; TIMER_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH; TIMER_OCInitStructure.TIMER_OCNPolarity = TIMER_OCN_POLARITY_HIGH; TIMER_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE; TIMER_OCInitStructure.TIMER_OutputNState = TIMER_OUTPUTN_STATE_ENABLE; TIMER_OCInitStructure.TIMER_OCIdleState = TIMER_OC_IDLE_STATE_SET; TIMER_OCInitStructure.TIMER_OCNIdleState = TIMER_OCN_IDLE_STATE_RESET; TIMER_OCInitStructure.TIMER_Pulse = buffer[0]; TIMER_OC1_Init(TIMER1, &TIMER_OCInitStructure); TIMER_OC1_Preload(TIMER1,TIMER_OC_PRELOAD_DISABLE); /* TIMER1 output enable */ TIMER_CtrlPWMOutputs(TIMER1,ENABLE); /* Auto-reload preload enable */ TIMER_CARLPreloadConfig(TIMER1,ENABLE); /* TIMER1 Update DMA Request enable */ TIMER_DMACmd( TIMER1, TIMER_DMA_UPDATE, ENABLE); /* TIMER enable counter*/ TIMER_Enable( TIMER1, ENABLE ); }
/** * @brief Configure the TIMER peripheral. * @param None * @retval None */ void TIMER_Configuration(void) { /* --------------------------------------------------------------- TIMER2 Configuration: TIMER2CLK = SystemCoreClock / 36000 = 2K, And generate 3 signals with 3 different delays: TIMER2_CH1 delay = 4000/2000 = 2s TIMER2_CH2 delay = 8000/2000 = 4s TIMER2_CH3 delay = 12000/2000 = 6s --------------------------------------------------------------- */ TIMER_BaseInitPara TIMER_TimeBaseStructure; TIMER_OCInitPara TIMER_OCInitStructure; /* TIMERS clock enable */ RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1|RCC_APB2PERIPH_TIMER15|RCC_APB2PERIPH_TIMER16|RCC_APB2PERIPH_TIMER17,ENABLE); RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2|RCC_APB1PERIPH_TIMER3|RCC_APB1PERIPH_TIMER6|RCC_APB1PERIPH_TIMER14,ENABLE); /* TIMER2 configuration */ TIMER_DeInit(TIMER2); TIMER_TimeBaseStructure.TIMER_Prescaler = 35999; TIMER_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP; TIMER_TimeBaseStructure.TIMER_Period = 19999; TIMER_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1; TIMER_BaseInit(TIMER2,&TIMER_TimeBaseStructure); /* CH1,CH2 and CH3 Configuration in OC_ACTIVE mode */ TIMER_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_ACTIVE; TIMER_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH; TIMER_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE; TIMER_OCInitStructure.TIMER_Pulse = 3999; TIMER_OC1_Init(TIMER2, &TIMER_OCInitStructure); TIMER_OC1_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE); TIMER_OCInitStructure.TIMER_Pulse = 7999; TIMER_OC2_Init(TIMER2, &TIMER_OCInitStructure); TIMER_OC2_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE); TIMER_OCInitStructure.TIMER_Pulse = 11999; TIMER_OC3_Init(TIMER2, &TIMER_OCInitStructure); TIMER_OC3_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE); /* Auto-reload preload enable */ TIMER_CARLPreloadConfig(TIMER2,ENABLE); /* TIMER enable counter*/ TIMER_Enable( TIMER2, ENABLE ); }
/** * @brief Configure the TIMER peripheral. * @param None * @retval None */ void TIMER_Configuration(void) { /* ----------------------------------------------------------------------- TIMER2 Configuration: generate 3 PWM signals with 3 different duty cycles: TIMER2CLK = SystemCoreClock / 72 = 1MHz TIMER2 Channel2 duty cycle = (4000/ 16000)* 100 = 25% TIMER2 Channel3 duty cycle = (8000/ 16000)* 100 = 50% TIMER2 Channel4 duty cycle = (12000/ 16000)* 100 = 75% ----------------------------------------------------------------------- */ TIMER_BaseInitPara TIMER_TimeBaseStructure; TIMER_OCInitPara TIMER_OCInitStructure; /* TIMERS clock enable */ RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1|RCC_APB2PERIPH_TIMER15|RCC_APB2PERIPH_TIMER16|RCC_APB2PERIPH_TIMER17,ENABLE); RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2|RCC_APB1PERIPH_TIMER3|RCC_APB1PERIPH_TIMER6|RCC_APB1PERIPH_TIMER14,ENABLE); /* TIMER2 configuration */ TIMER_DeInit(TIMER2); TIMER_TimeBaseStructure.TIMER_Prescaler = 71; TIMER_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP; TIMER_TimeBaseStructure.TIMER_Period = 15999; TIMER_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1; TIMER_BaseInit(TIMER2,&TIMER_TimeBaseStructure); /* CH2,CH3 and CH4 Configuration in PWM mode */ TIMER_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1; TIMER_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH; TIMER_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE; TIMER_OCInitStructure.TIMER_Pulse = 3999; TIMER_OC2_Init(TIMER2, &TIMER_OCInitStructure); TIMER_OC2_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE); TIMER_OCInitStructure.TIMER_Pulse = 7999; TIMER_OC3_Init(TIMER2, &TIMER_OCInitStructure); TIMER_OC3_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE); TIMER_OCInitStructure.TIMER_Pulse = 11999; TIMER_OC4_Init(TIMER2, &TIMER_OCInitStructure); TIMER_OC4_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE); /* Auto-reload preload enable */ TIMER_CARLPreloadConfig(TIMER2,ENABLE); /* TIMER enable counter*/ TIMER_Enable( TIMER2, ENABLE ); }
void pwm_init(void) { GPIO_InitPara GPIO_InitStructure; // timer 3 pin PB1 config ( ch4 ) GPIO_InitStructure.GPIO_Pin = GPIO_PIN_1; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF; GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ; GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLDOWN; GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_PinAFConfig(GPIOB,GPIO_PINSOURCE1,GPIO_AF_1); // GPIO pins PA8 , 9 ,10 setup ( Timer1 ch 1 , 2 , 3) GPIO_InitStructure.GPIO_Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PINSOURCE8,GPIO_AF_2); GPIO_PinAFConfig(GPIOA,GPIO_PINSOURCE9,GPIO_AF_2); GPIO_PinAFConfig(GPIOA,GPIO_PINSOURCE10,GPIO_AF_2); TIMER_BaseInitPara TIM_TimeBaseStructure; RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1,ENABLE); RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER3,ENABLE); // TIMER3 for pins A8 A9 A10 TIM_TimeBaseStructure.TIMER_Prescaler = TIMER_PRESCALER - 1; // TIM_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP; TIM_TimeBaseStructure.TIMER_Period = PWMTOP; TIM_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1; TIMER_BaseInit(TIMER3,&TIM_TimeBaseStructure); //Ch1 , 2 , 3 TIM_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1; TIM_OCInitStructure.TIMER_OCPolarity = OUT_POLARITY; TIM_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE; TIM_OCInitStructure.TIMER_OCIdleState = TIMER_OC_IDLE_STATE_RESET; TIM_OCInitStructure.TIMER_Pulse = 0; TIMER_OC4_Init(TIMER3, &TIM_OCInitStructure); TIMER_CtrlPWMOutputs(TIMER3,ENABLE); TIMER_CARLPreloadConfig(TIMER3,ENABLE); TIMER_Enable( TIMER3, ENABLE ); TIMER_BaseInit(TIMER1,&TIM_TimeBaseStructure); TIMER_OC1_Init(TIMER1, &TIM_OCInitStructure); TIMER_OC2_Init(TIMER1, &TIM_OCInitStructure); TIMER_OC3_Init(TIMER1, &TIM_OCInitStructure); TIMER_CtrlPWMOutputs(TIMER1,ENABLE); TIMER_CARLPreloadConfig(TIMER1,ENABLE); TIMER_OC1_Preload(TIMER1,ESC_PRELOAD); TIMER_OC2_Preload(TIMER1,ESC_PRELOAD); TIMER_OC3_Preload(TIMER1,ESC_PRELOAD); TIMER_OC4_Preload(TIMER3,ESC_PRELOAD); TIMER_Enable( TIMER1, ENABLE ); }
void pwm_init(void) { GPIO_InitPara GPIO_InitStructure; //pin A0 TM2_ch1 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_1; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF; GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ; GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLDOWN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE1, GPIO_AF_2); TIMER_BaseInitPara TIM_TimeBaseStructure; RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2 | RCC_APB1PERIPH_TIMER14 | RCC_APB1PERIPH_TIMER3, ENABLE); RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1, ENABLE); // timer 2 TIM_TimeBaseStructure.TIMER_Prescaler = TIMER_PRESCALER - 1; // TIM_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_CENTER_ALIGNED2; TIM_TimeBaseStructure.TIMER_Period = PWMTOP; TIM_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1; // init timer 2 TIMER_BaseInit(TIMER2, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1; TIM_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH; TIM_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE; TIM_OCInitStructure.TIMER_OCIdleState = TIMER_OC_IDLE_STATE_RESET; TIM_OCInitStructure.TIMER_Pulse = 0; TIMER_OC2_Init(TIMER2, &TIM_OCInitStructure); TIMER_CtrlPWMOutputs(TIMER2, ENABLE); TIMER_CARLPreloadConfig(TIMER2, ENABLE); TIMER_OC2_Preload(TIMER2, TIMER_OC_PRELOAD_DISABLE); // pin A3 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_3; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE3, GPIO_AF_2); TIMER_OC4_Init(TIMER2, &TIM_OCInitStructure); TIMER_OC4_Preload(TIMER2, TIMER_OC_PRELOAD_DISABLE); // TIMER_Enable( TIMER2, ENABLE ); // timer 1 ch3 , ch1 // pins A10 , A8 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10 | GPIO_PIN_8; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE10, GPIO_AF_2); GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE8, GPIO_AF_2); // timer 1 // init timer 1 TIMER_BaseInit(TIMER1, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIMER_Pulse = 0; TIMER_OC3_Init(TIMER1, &TIM_OCInitStructure); TIMER_OC1_Init(TIMER1, &TIM_OCInitStructure); TIMER_CtrlPWMOutputs(TIMER1, ENABLE); TIMER_CARLPreloadConfig(TIMER1, ENABLE); TIMER_OC3_Preload(TIMER1, TIMER_OC_PRELOAD_DISABLE); TIMER_OC1_Preload(TIMER1, TIMER_OC_PRELOAD_DISABLE); // bridge dir 2 /////////////// //pin A9 TM1_ch2 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_9; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE9, GPIO_AF_2); // timer 1 // init timer 1 TIMER_BaseInit(TIMER1, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIMER_Pulse = 0; TIMER_OC2_Init(TIMER2, &TIM_OCInitStructure); TIMER_CtrlPWMOutputs(TIMER1, ENABLE); TIMER_CARLPreloadConfig(TIMER1, ENABLE); TIMER_OC2_Preload(TIMER1, TIMER_OC_PRELOAD_DISABLE); // timer 3 PB1 af1 ch4 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_1; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PINSOURCE1, GPIO_AF_1); // timer 3 // init timer 3 TIMER_BaseInit(TIMER3, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIMER_Pulse = 0; TIMER_OC4_Init(TIMER3, &TIM_OCInitStructure); TIMER_CtrlPWMOutputs(TIMER3, ENABLE); TIMER_CARLPreloadConfig(TIMER3, ENABLE); TIMER_OC4_Preload(TIMER3, TIMER_OC_PRELOAD_DISABLE); // A0 // Timer2 ch1 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE0, GPIO_AF_2); // timer 2 // init timer 2 TIMER_BaseInit(TIMER2, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIMER_Pulse = 0; TIMER_OC1_Init(TIMER2, &TIM_OCInitStructure); TIMER_CtrlPWMOutputs(TIMER2, ENABLE); TIMER_CARLPreloadConfig(TIMER2, ENABLE); TIMER_OC1_Preload(TIMER2, TIMER_OC_PRELOAD_DISABLE); // PA2 tm2 ch3 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_2; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE2, GPIO_AF_2); TIMER_OC3_Init(TIMER2, &TIM_OCInitStructure); TIMER_OC3_Preload(TIMER2, TIMER_OC_PRELOAD_DISABLE); TIMER_Enable(TIMER3, ENABLE); TIMER_Enable(TIMER1, ENABLE); TIMER_Enable(TIMER2, ENABLE); }