最近需要用到stm32产生spwm波,在网上找到一段源代码,现象实现了,但是程序有些不能理解:
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 ,ENABLE); //时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);
/* GPIOA配置:通道PA.7和PC.6作为输出引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = fre_cha;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方式
TIM_TimeBaseStructure.TIM_Period = TimerPeriod - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_DeInit(TIM4); //这边是普通定时器的初始化和中断申请
TIM_TimeBaseStructure.TIM_Period=TimerPeriod - 1; //自动重装载寄存器的值
TIM_TimeBaseStructure.TIM_Prescaler= fre_cha; //时钟预分频数
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //采样分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//计数方式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update); //清除溢出中断标志
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); // 计数溢出时触发中断
// //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //通道TIM3
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //副优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
这里用的定时器是TIM4 TIM8初始化引脚PA7,PC6,查了下材料这两个定时器没有这两个脚输出啊?????求解
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>