STM32F030C8T6输出3路PWM

1,常规套餐 上电初始化
SystemInit();
TIM1_Init();
2,TIMI1初始化
TIM_TimeBaseInitTypeDef TIM1_TimeBaseStructure;
TIM_OCInitTypeDef TIM1_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//记得打开时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructInit(&TIM1_TimeBaseStructure);
/* Time Base configuration */
//定时周期和频率配置
TIM1_TimeBaseStructure.TIM_Prescaler = PWM_PRSC;
TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM1_TimeBaseStructure.TIM_Period = (PWM_PERIOD-1);
// f(HZ)=48M/(TIM_Period+1)(TIM_Prescaler+1)
TIM1_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2;

// Initial condition is REP=0 to set the UPDATE only on the underflow
TIM1_TimeBaseStructure.TIM_RepetitionCounter = REP_RATE;
TIM_TimeBaseInit(TIM1, &TIM1_TimeBaseStructure);

TIM_OCStructInit(&TIM1_OCInitStructure);
/* Channel 1N in PWM mode /
TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM1_OCInitStructure.TIM_Pulse = PWM_PERIOD/2; //dummy value
//有效极性初始化 这个看硬件怎么搞咯
TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
TIM_OC1Init(TIM1, &TIM1_OCInitStructure);
TIM_OC2Init(TIM1, &TIM1_OCInitStructure);
TIM_OC3Init(TIM1, &TIM1_OCInitStructure);
//IO 初始化 打开复用功能
GPIO_StructInit(&GPIO_InitStructure);
/
***** Configure PA.08,09,10 as channel 1,2,3 output ****/
GPIO_InitStructure.GPIO_Pin =
GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;

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_UP ;
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);
//配置中断 打开输出
/* Enables the TIM1 Preload on CC1 Register /
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/
Enables the TIM1 Preload on CC2 Register /
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
/
Enables the TIM1 Preload on CC3 Register /
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/
TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
// Disnable update interrupt
//打开定时器更新中断 多个中断 阔以干点别的
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
//配置优先级咯
NVIC_InitStructure.NVIC_IRQChannel=
TIM1_BRK_UP_TRG_COM_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//宏定义这样子 搞在h文件中 后续方便修改
#define PWM_FREQ ((u16) 32000)
#define CKTIM ((u32)48000000uL)
#define REP_RATE 0
#define PWM_PRSC 0
#define PWM_PERIOD
((u16) (CKTIM / (u32)(PWM_FREQ *(PWM_PRSC+1))))
//定时器 更新中断中阔以改变占空比
// g_pwm_grade 自己定义 全局修改
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
TIM1->CCR1 = g_pwm_grade;
TIM1->CCR2 = g_pwm_grade;//
TIM1->CCR3 = g_pwm_grade;//
}
//上述配置 就阔以 输出 三路PWM 咯

你可能感兴趣的:(个人总结,stm32,单片机,嵌入式硬件)