1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include "stm32f10x.h"
void PWM_Init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_6; GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_InternalClockConfig(TIM2); TIM_InternalClockConfig(TIM3); TIM_TimeBaseInitTypeDef Time_ClockInitStructure; Time_ClockInitStructure.TIM_ClockDivision= TIM_CKD_DIV1; Time_ClockInitStructure.TIM_CounterMode= TIM_CounterMode_Up; Time_ClockInitStructure.TIM_Period= 100-1; Time_ClockInitStructure.TIM_Prescaler= 720-1; Time_ClockInitStructure.TIM_RepetitionCounter= 0; TIM_TimeBaseInit(TIM2,&Time_ClockInitStructure); TIM_TimeBaseInit(TIM3,&Time_ClockInitStructure); TIM_OCInitTypeDef TIME_OCInitStructure; TIM_OCStructInit(&TIME_OCInitStructure); TIME_OCInitStructure.TIM_OCMode= TIM_OCMode_PWM1; TIME_OCInitStructure.TIM_OCPolarity= TIM_OCPolarity_High; TIME_OCInitStructure.TIM_OutputState= TIM_OutputState_Enable; TIME_OCInitStructure.TIM_Pulse= 0; TIM_OC2Init(TIM2,&TIME_OCInitStructure); TIM_OC1Init(TIM3,&TIME_OCInitStructure); TIM_Cmd(TIM2,ENABLE); TIM_Cmd(TIM3,ENABLE); }
void PWM_SetCompare1_TIM3(uint16_t Compare) { TIM_SetCompare1(TIM3,Compare); }
void PWM_SetCompare2_TIM2(uint16_t Compare) { TIM_SetCompare2(TIM2,Compare); }
void PWM_SetPrescaler(uint16_t prescaler) { TIM_PrescalerConfig(TIM2,prescaler,TIM_PSCReloadMode_Immediate); }
|