新手上路
- 积分
- 48
- 金钱
- 48
- 注册时间
- 2018-8-26
- 在线时间
- 9 小时
|
单片机型号STM32F03X
使用PA8叫对外部脉冲计数,我就是做个频率计。
首先段扩设置
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB,ENABLE); //开启GPIOA,GPIOB时钟
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //这里要映射模式
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_Level_2;
GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2); //这里也别忘了。
其次是定时器设置,我这里采用的事TIM1,且是CH1 端口计脉冲
TIM_TimeBaseStructure.TIM_Period=0xffff; //48M 最快时钟,用来计数脉冲
TIM_TimeBaseStructure.TIM_Prescaler=0X0; //
TIM_TimeBaseStructure.TIM_ClockDivision=0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
/*
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICPrescaler=0;
TIM_ICInitStructure.TIM_ICFilter=0;
TIM_ICInit(TIM1,&TIM_ICInitStructure);
*/
TIM_ARRPreloadConfig(TIM1,ENABLE); //ARR预装载使能
TIM_TIxExternalClockConfig(TIM1,TIM_TIxExternalCLK1Source_TI1,TIM_ICPolarity_Rising,0);
//TIM_ITRxExternalClockConfig(TIM1,TIM_TIxExternalCLK1Source_TI1);
//TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x00);
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
TIM_ClearFlag(TIM1,TIM_FLAG_Break);
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //更新中断有效
TIM_Cmd(TIM1,ENABLE); //TIM1使能
以上程序已经验证过。当我给PA8脉冲信号,CNT开始计数了。
研究了2天。终于还是靠自己解决了。
|
|