我用STM32F103的timer3输出PWM,在更新中断里通过改变PrescalerValue来频率,但是输出时间有问题啊,在中断里我设置的应该是6s就会点亮(熄灭)一个LED,此LED引脚用于DIR控制,但是实际30多秒才会变。程序比较简单,是个很笨的方法,请前辈们帮帮忙看一下吧,非常感谢了。 main函数的相关程序: #include "stm32f10x.h" #include "stm32f10x_tim.h" TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; uint16_t PrescalerValue = 0x1c1f; //预分频值=7200,f=10000 uint16_t cishu=0; //记录中断次数 void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); int main(void) { /* System Clocks Configuration */ RCC_Configuration(); /* GPIO Configuration */ GPIO_Configuration(); NVIC_Configuration(); TIM_TimeBaseStructure.TIM_Period = 0x0063; //ARR=100 10ms TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; //TIM_TimeBaseStructure.TIM_Prescaler = 0x1c1f; //预分频值=10000 TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0; //每次溢出都产生事件更新 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_ClearFlag(TIM3,TIM_FLAG_Update); //中断标志位清零 TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //允许更新中断 /* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0x0032; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3, ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); while (1) { } } void RCC_Configuration(void) { /* TIM3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* GPIOA and GPIOB clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); } void GPIO_Configuration(void) { //GPIO_InitTypeDef GPIO_InitStructure; #ifdef STM32F10X_CL /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); #else /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ; //timer3的ch1输出 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_7; //DIR控制 PF7 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOF, &GPIO_InitStructure); GPIO_SetBits(GPIOF,GPIO_Pin_7); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //EN控制 PF6 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOF, &GPIO_InitStructure); GPIO_SetBits(GPIOF,GPIO_Pin_6); #endif } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn ; //选择定时器TIM3 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //选择抢先式优先级(与中断嵌套级别有关) NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //选择子优先级(同抢先式优先级的响应顺序) NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //选择使能中断源 NVIC_Init(&NVIC_InitStructure); } 中断程序如下: void TIM3_IRQHandler(void) { uint16_t i; i=++cishu; if ( TIM_GetITStatus(TIM3 , TIM_IT_Update) != RESET ) //自己的程序 { TIM_ClearITPendingBit(TIM3 , TIM_FLAG_Update); if(i<50) //f=100,10ms,50次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x1c1f ; //7200 } else if(49<i&&i<150) //f=200,5ms,100次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x0e0f ; //3600 } else if(149<i&&i<400) //f=500,2ms,250次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x059f ; //1440 } else if(399<i&&i<3400) //f=1000,1ms,3000次=3s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x02cf ; //720 } else if(3399<i&&i<3650) //f=500,2ms,250次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x059f; } else if(3649<i&&i<3750) //f=200,5ms,100次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x0e0f; } else if(3749<i&&i<3800) //f=100,10ms,50次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x1c1f; } else if(3799<i&&i<3850) //反转 { GPIO_ResetBits(GPIOF,GPIO_Pin_7); TIM_TimeBaseStructureit.TIM_Prescaler =0x1c1f; } else if(3849<i&&i<3950) //f=200,5ms,100次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x0e0f ; } else if(3949<i&&i<4200) //f=500,2ms,250次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x059f; } else if(4199<i&&i<7200) //f=1000,1ms,3000次=3s { TIM_TimeBaseStructureit.TIM_Prescaler = 0x02cf ; } else if(7199<i&&i<7450) //f=500,2ms,250次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler =0x059f; } else if(7449<i&&i<7550) //f=200,5ms,100次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler =0x0e0f; } else if(7549<i&&i<7600) //f=100,10ms,50次=0.5s { TIM_TimeBaseStructureit.TIM_Prescaler =0x1c1f; } else if(i==7600) //再反转 { cishu=0; GPIO_SetBits(GPIOF,GPIO_Pin_7); TIM_TimeBaseStructureit.TIM_Prescaler =0x1c1f; } else {TIM_TimeBaseStructureit.TIM_Prescaler =0x1c1f;} } } 最后先谢谢大家了,帮帮忙。 |
用不用
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM3,TIM_FLAG_Update); //中断标志位清零
&&(i<7600) )
诶,怎么说你好呢!!!
在中断里只修改结构体TIM_TimeBaseStructureit的参数,却不把修改后TIM_TimeBaseStructureit的去初始化TIM外设,又怎么会更新寄存器呢!
-------------------------------------------------------------------------
请在中断中调用TIM_TimeBaseInit()来读改写TIM外设寄存器。
但是还有一个小问题,就是我的引脚输出电压怎么变成了不到0.5V啊?之前刚写第一个简单PWM输出程序时输出是3.3V啊,但是现在不到0.5V了,重写个最简单的输出程序输出电压也还是这样的,真不知道哪里出错了。
最后宣布结贴,谢谢大家了,真心的,嘿嘿,以后我也会常来学习并且和大家讨论的。