- #include "stm32f10x.h"
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void TIM8_Configuration(void);
- void TIM1_Configuration(void);
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- TIM8_Configuration();
- TIM1_Configuration();
- while(1);
- }
- void GPIO_Configuration()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC , &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- }
- void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus;
- RCC_DeInit();
- RCC_HSEConfig(RCC_HSE_ON);
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- RCC_PCLK2Config(RCC_HCLK_Div1);
- RCC_PCLK1Config(RCC_HCLK_Div2);
- RCC_ADCCLKConfig(RCC_PCLK2_Div6);
- FLASH_SetLatency(FLASH_Latency_2);
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- RCC_PLLCmd(ENABLE);
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- while(RCC_GetSYSCLKSource() != 0x08);
- }
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_TIM8, ENABLE);
- }
- void TIM8_Configuration(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- TIM_TimeBaseStructure.TIM_Period = 65000;
- TIM_TimeBaseStructure.TIM_Prescaler=1000-1;
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM8,&TIM_TimeBaseStructure);
- TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising ;
- TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
- TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
- TIM_ICInitStructure.TIM_ICFilter = 0xF; //这句即使放在本函数的最后TIM1也没有输出
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
- TIM_ICInit(TIM8, &TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
- TIM_ICInit(TIM8, &TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
- TIM_ICInit(TIM8, &TIM_ICInitStructure);
- TIM_ITConfig(TIM8, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3,ENABLE);
- TIM_Cmd(TIM1,ENABLE);
- }
- void TIM1_Configuration(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_TimeBaseStructure.TIM_Period = 90-1;
- TIM_TimeBaseStructure.TIM_Prescaler=10000-1;
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_CenterAligned1;
- TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
- TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OutputNState=TIM_OutputState_Disable;
- TIM_OCInitStructure.TIM_Pulse=0;
- TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OC1Init(TIM1,&TIM_OCInitStructure);
- TIM_OC2Init(TIM1,&TIM_OCInitStructure);
- TIM_OC3Init(TIM1,&TIM_OCInitStructure);
- TIM_OC4Init(TIM1,&TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_SetCompare1(TIM1,10);
- TIM_SetCompare2(TIM1,20);
- TIM_SetCompare3(TIM1,30);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- TIM_Cmd(TIM1,ENABLE);
-
- }
复制代码 这个程序是设置TIM8进行捕获,TIM1进行PWM输出。TIM1没有输出。如果在主程序中先写TIM1在写TIM8,TIM1输出正常。如果在TIM8的配置中设置滤波为0x0;TIM1有输出。如果在TIM1中取消通道1的设置,TIM1有输出。如果在TIM1设置最后重新设置TIM1->CR2=0x0;TIM1有输出。以上情况都可以使TIM1有输出,调试过程中观察TIM1寄存器,TIM->CR2和TIM1->CRC会被意外改变。不能解释以上各种原因。
|
73 TIM_ITConfig(TIM8, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3,ENABLE);
74 TIM_Cmd(TIM1,ENABLE); //这里的寄存器错了!!!
第74行代码写错了,你TIM8没有被使能。-----------------------------------------------------
其他问题没有细看,大致浏览了一下,先找到了这一处。
真的很无解为什么会这样。
支持一下
位 15 MOE:主输出使能 (Main output enable)
只要断路输入变为有效状态,此位便由硬件异步清零。此位由软件置 1,也可根据 AOE 位状
态自动置 1。此位仅对配置为输出的通道有效。
0:OC 和 OCN 输出禁止或被强制为空闲状态。
1:如果 OC 和 OCN 输出的相应使能位(TIMx_CCER 寄存器中的 CCxE 和 CCxNE 位)均
置 1,则使能 OC 和 OCN 输出。
有关详细信息,请参见 OC/OCN 使能说明( 第 379 页的第 14.4.9 节: TIM1 和 TIM8 捕获 /
比较使能寄存器 (TIMx_CCER))。
要使能PWM 才会有输出的