初中生
最后登录1970-1-1
在线时间 小时
注册时间2018-1-22
|
我用STM32F103ZET霸道开发板,我运用通用定时器做2路PWM输出(呼吸灯),程序写好后,用仿真器下载后正常运行,但是断电在上电之后就不能正常运行了,断电在上电10次有6次这样是不正常运行的,4次正常运行。不知道原因,求帮助,谢谢!
下面是主程序main代码
- #include "stm32f10x.h"
- #include "bsp_GeneralTim.h"
- void Delay( uint32_t count )
- {
- for(; count!=0; count--);
- }
- int main(void)
- {
- volatile uint8_t n1=1;
- volatile uint8_t n2=0;
- GENERAL_TIM_Init();
- while(1)
- {
- if(n1)
- {
- TIM2->CCR3+=1;
- if(TIM2->CCR3>199)
- n1=0;
- }
- else
- {
- TIM2->CCR3-=1;
- if(TIM2->CCR3<1)
- n1=1;
- }
- if(n2)
- {
- TIM2->CCR4+=1;
- if(TIM2->CCR4>199)
- n2=0;
- }
- else
- {
- TIM2->CCR4-=1;
- if(TIM2->CCR4<1)
- n2=1;
- }
- Delay(0xFFFF);
- }
- }
复制代码
bsp_GeneralTim.h代码
- #ifndef __BSP_GENERALTIME_H
- #define __BSP_GENERALTIME_H
- #include "stm32f10x.h"
- #define GENERAL_TIM TIM2
- #define GENERAL_TIM_APBxClock_FUN RCC_APB1PeriphClockCmd
- #define GENERAL_TIM_CLK RCC_APB1Periph_TIM2
- #define GENERAL_TIM_PERIOD (200-1)
- #define GENERAL_TIM_PSC (4-1)
- #define GENERAL_TIM_CH3_GPIO_CLK RCC_APB2Periph_GPIOA
- #define GENERAL_TIM_CH3_PORT GPIOA
- #define GENERAL_TIM_CH3_PIN GPIO_Pin_2
- #define GENERAL_TIM_CH4_GPIO_CLK RCC_APB2Periph_GPIOA
- #define GENERAL_TIM_CH4_PORT GPIOA
- #define GENERAL_TIM_CH4_PIN GPIO_Pin_3
- void GENERAL_TIM_Init(void);
- #endif /* __BSP_GENERALTIME_H */
复制代码
bsp_GeneralTim.c代码
- #include "bsp_GeneralTim.h"
- static void GENERAL_TIM_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(GENERAL_TIM_CH3_GPIO_CLK, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GENERAL_TIM_CH3_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GENERAL_TIM_CH3_PORT, &GPIO_InitStructure);
- RCC_APB2PeriphClockCmd(GENERAL_TIM_CH4_GPIO_CLK, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GENERAL_TIM_CH4_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GENERAL_TIM_CH4_PORT, &GPIO_InitStructure);
- }
- static void GENERAL_TIM_Mode_Config(void)
- {
- // 开启定时器时钟,即内部时钟CK_INT=72M
- GENERAL_TIM_APBxClock_FUN(GENERAL_TIM_CLK,ENABLE);
- /*--------------------时基结构体初始化-------------------------*/
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- // 自动重装载寄存器的值,累计TIM_Period+1个频率后产生一个更新或者中断
- TIM_TimeBaseStructure.TIM_Period=GENERAL_TIM_PERIOD;
- // 驱动CNT计数器的时钟 = Fck_int/(psc+1)
- TIM_TimeBaseStructure.TIM_Prescaler= GENERAL_TIM_PSC;
- // 时钟分频因子 ,配置死区时间时需要用到
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- // 计数器计数模式,设置为向上计数
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- // 重复计数器的值,没用到不用管
- //TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
- // 初始化定时器
- TIM_TimeBaseInit(GENERAL_TIM, &TIM_TimeBaseStructure);
-
- TIM_OCInitTypeDef TIM_OCInitStructure;
- // 配置为PWM模式1
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- // 输出使能
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- // 设置占空比大小
- TIM_OCInitStructure.TIM_Pulse = 100;
- // 输出通道电平极性配置
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- // 输出通道空闲电平极性配置
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OC3Init(GENERAL_TIM, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
- TIM_OCInitStructure.TIM_Pulse = 101;
- TIM_OC4Init(GENERAL_TIM, &TIM_OCInitStructure);
- TIM_OC4PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(GENERAL_TIM, ENABLE);
- // 使能计数器
- TIM_Cmd(GENERAL_TIM, ENABLE);
- }
- void GENERAL_TIM_Init(void)
- {
- GENERAL_TIM_GPIO_Config();
- GENERAL_TIM_Mode_Config();
- }
复制代码
|
|