新手上路
- 积分
- 47
- 金钱
- 47
- 注册时间
- 2014-3-10
- 在线时间
- 2 小时
|

楼主 |
发表于 2015-11-18 20:32:24
|
显示全部楼层
回复【2楼】正点原子:
---------------------------------
多谢原子大师的指点!找到了一点问题,现在是TIM2~TIM4可以正常计数,TIM1还是不行,TIM2~4和TIM1的配置是一样的,痛苦了2天了!!!详细代码如下,请大师再帮分析指点!
//??????????????
void TIMER1_Counter_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //?±??????
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //?±??????
//IO????
GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_TIM1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //????
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //??????
GPIO_Init(GPIOE, &GPIO_InitStructure); //??????IO
//?????÷????
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler = 0x00; //?¤·???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //????????
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //×?????×°????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //???????¨?±?÷
// TIM_TIxExternalClockConfig(TIM1, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0); //????TIx?±??????
// TIM_ETRClockMode1Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //?????±??????1????
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //?????±??????2????
TIM_SetCounter(TIM1, 0); //?????????÷
TIM_Cmd(TIM1, ENABLE); //?¨?±?÷????
}
//??????????????
void TIMER2_Counter_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //?±??????
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //?±??????
//IO????
GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //????
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //??????
GPIO_Init(GPIOA, &GPIO_InitStructure); //??????IO
//?????÷????
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Prescaler = 0x00; //?¤·???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //????????
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //×?????×°????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //???????¨?±?÷
// TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0); //????TIx?±??????
// TIM_ETRClockMode1Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //?????±??????1????
TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //?????±??????2????
TIM_SetCounter(TIM2, 0); //?????????÷
TIM_Cmd(TIM2, ENABLE); //?¨?±?÷????
}
//??????????????
void TIMER3_Counter_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //?±??????
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //?±??????
//IO????
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_TIM3);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //????
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //??????
GPIO_Init(GPIOD, &GPIO_InitStructure); //??????IO
//?????÷????
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Prescaler = 0x00; //?¤·???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //????????
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //×?????×°????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //???????¨?±?÷
// TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0); //????TIx?±??????
// TIM_ETRClockMode1Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //?????±??????1????
TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //?????±??????2????
TIM_SetCounter(TIM3, 0); //?????????÷
TIM_Cmd(TIM3, ENABLE); //?¨?±?÷????
}
//??????????????
void TIMER4_Counter_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //?±??????
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //?±??????
//IO????
GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_TIM4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //????
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //??????
GPIO_Init(GPIOE, &GPIO_InitStructure); //??????IO
//?????÷????
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Prescaler = 0x00; //?¤·???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //????????
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //×?????×°????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //???????¨?±?÷
// TIM_TIxExternalClockConfig(TIM4, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0); //????TIx?±??????
// TIM_ETRClockMode1Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //?????±??????1????
TIM_ETRClockMode2Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //?????±??????2????
TIM_SetCounter(TIM4, 0); //?????????÷
TIM_Cmd(TIM4, ENABLE); //?¨?±?÷????
} |
|