如题:配置的是TIM1的CH1(PA8)用来控制舵机的 下载完程序和重新上电后其他程序都可以正常跑 但是就是重新上电后PA8就没有PWM了,下载完程序跑是有的,看到有说修改硬件初始化顺序的,但是没有成功。最后贴上这部分的代码,有知道的请指教,不胜感激! //定时器初始化配置函数 //psc 定时器分频系数 //per 重装载值 void Tim1_Configuration(uint16_t psc,uint16_t per) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //定时器1时钟打开 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO外设时钟使能 //设置该引脚为复用输出功能,输出TIM1 CH1的PWM脉冲波形 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_DeInit(TIM1); //外设 TIMx 寄存器重设为缺省值 TIM_TimeBaseStructure.TIM_Period = per-1; //计数周期 TIM_TimeBaseStructure.TIM_Prescaler = psc-1; //设置预分频从APB2-->72M TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频(这里用不到) TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //PWM1 Mode configuration TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 TIM_OCInitStructure.TIM_Pulse = 0; //设置跳变值,当计数器计数到这个值时,电平发生跳变 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平 TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能通道1 TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //通道1重装载使能 TIM_CtrlPWMOutputs(TIM1,ENABLE); //使能定时器1 PWM TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIM1重载寄存器ARR TIM_Cmd(TIM1, ENABLE); //使能定时器1 } void Pwm_Configuration(void) { Tim1_Configuration(72,20000); //1us*20000 20ms 50Hz频率 Tim4_Configuration(36,1000); //1us*1000 1ms 1Khz频率 } 下面是TIM4的 应该没什么影响,也贴上 //定时器初始化配置函数 //psc 定时器分频系数 //per 重装载值 void Tim4_Configuration(uint16_t psc,uint16_t per) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); TIM_DeInit(TIM4); //外设 TIMx 寄存器重设为缺省值 TIM_TimeBaseStructure.TIM_Period = per-1; //计数周期 TIM_TimeBaseStructure.TIM_Prescaler = psc-1; //设置预分频从APB1-->36M TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频(这里用不到) TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //PWM1 Mode configuration TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; //设置跳变值,当计数器计数到这个值时,电平发生跳变 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平 //Channel3 TIM_OC3Init(TIM4, &TIM_OCInitStructure); //使能通道3 TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //通道3重装载使能 //Channel4 TIM_OC4Init(TIM4, &TIM_OCInitStructure); //使能通道4 TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); //通道4重装载使能 TIM_ARRPreloadConfig(TIM4, ENABLE); //使能TIM4重载寄存器ARR TIM_Cmd(TIM4, ENABLE); //使能定时器4 } |
【MCU实战经验】基于STM32F103C8T6的hart总线调试器设计
求教STM32F103进入STOP模式后用外部中断唤醒的问题
基于STM32F103RCT6的无源蜂鸣器音乐播放(生日快乐歌)
STM32F103c8t6有没有DAC 功能?
STM32F103x中文数据手册
新手求教,为何在我电脑上找不到STM32F1Xx.h文件
金龙107例程汇总(STM32F107)
万利STM32F107VC 原理图
STM32F103 ADC多通道检测必须要DMA吗?
【官方例程】STM32F107以太网官方例程
//定时器初始化配置函数
//psc 定时器分频系数
//per 重装载值
void Tim1_Configuration(uint16_t psc,uint16_t per)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //定时器1时钟打开
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO外设时钟使能
//设置该引脚为复用输出功能,输出TIM1 CH1的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_DeInit(TIM1); //外设 TIMx 寄存器重设为缺省值
TIM_TimeBaseStructure.TIM_Period = per-1; //计数周期
TIM_TimeBaseStructure.TIM_Prescaler = psc-1; //设置预分频从APB2-->72M
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频(这里用不到)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//PWM1 Mode configuration
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能通道1
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //通道1重装载使能
TIM_CtrlPWMOutputs(TIM1,ENABLE); //使能定时器1 PWM
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIM1重载寄存器ARR
TIM_Cmd(TIM1, ENABLE); //使能定时器1
}
void Pwm_Configuration(void)
{
Tim1_Configuration(72,20000); //1us*20000 20ms 50Hz频率
Tim4_Configuration(36,1000); //1us*1000 1ms 1Khz频率
}
下面是TIM4的 应该没什么影响,也贴上
//定时器初始化配置函数
//psc 定时器分频系数
//per 重装载值
void Tim4_Configuration(uint16_t psc,uint16_t per)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_DeInit(TIM4); //外设 TIMx 寄存器重设为缺省值
TIM_TimeBaseStructure.TIM_Period = per-1; //计数周期
TIM_TimeBaseStructure.TIM_Prescaler = psc-1; //设置预分频从APB1-->36M
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频(这里用不到)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//PWM1 Mode configuration
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
//Channel3
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //使能通道3
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //通道3重装载使能
//Channel4
TIM_OC4Init(TIM4, &TIM_OCInitStructure); //使能通道4
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); //通道4重装载使能
TIM_ARRPreloadConfig(TIM4, ENABLE); //使能TIM4重载寄存器ARR
TIM_Cmd(TIM4, ENABLE); //使能定时器4
}
评分
查看全部评分