新手上路
- 积分
- 27
- 金钱
- 27
- 注册时间
- 2016-5-18
- 在线时间
- 4 小时
|
楼主 |
发表于 2016-5-25 16:27:34
|
显示全部楼层
外部计数模式没用过,之前比赛老师和我说过输入捕获测频率,所以我就想用捕获来测频率,,原子哥帮我看看这个程序,上升沿捕获,示波器的1000hz的频率可以捕获到 可是为什么几hz的就乱显示呢
void TIM2_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 êäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
TIM_TimeBaseStructure.TIM_Period = arr; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM2êäèë2¶»ñ2Îêy
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM2_ICInitStructure.TIM_ICFilter = 0x01;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
//ÖD¶Ï·Ö×é3õê¼»ˉ
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÏèÕ¼óÅÏ輶2¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //′óóÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);//ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
//TIM_IT_Update|
TIM_Cmd(TIM2,ENABLE ); //ê1Äü¶¨ê±Æ÷2
}
u8 TIM2CH1_CAPTURE_STA=0; //êäèë2¶»ñ×′ì¬
u16 TIM2CH1_CAPTURE_VAL; //êäèë2¶»ñÖμ
void TIM2_IRQHandler(void)
{
static unsigned char flag_PWM=0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
if(flag_PWM==0)
{
Data[0] = TIM_GetCapture1(TIM2);
flag_PWM=1;
// LED0=!LED0;
}
else if(flag_PWM==1)
{
flag_PWM=0;
Data[1] = TIM_GetCapture1(TIM2);
if(Data[1]>Data[0])
tim=Data[1]-Data[0];
else
{
tim=65536-Data[0]+Data[1];
}
}
// if((TIM2CH1_CAPTURE_STA&0X80)==0)//»1Î′3é1|2¶»ñ
// {
// if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
//
// {
// if(TIM2CH1_CAPTURE_STA&0X40)//òѾ-2¶»ñμ½¸ßμçƽáË
// {
// if((TIM2CH1_CAPTURE_STA&0X3F)==0X3F)//¸ßμçƽì«3¤áË
// {
// TIM2CH1_CAPTURE_STA|=0X80;//±ê¼Ç3é1|2¶»ñáËò»′Î
// TIM2CH1_CAPTURE_VAL=0XFFFF;
// }else TIM2CH1_CAPTURE_STA++;
// }
// }
// if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)//2¶»ñ1·¢éú2¶»ñê¼t
// {
// if(TIM2CH1_CAPTURE_STA&0X40) //2¶»ñμ½ò»¸öϽμÑØ
// {
// TIM2CH1_CAPTURE_STA|=0X80; //±ê¼Ç3é1|2¶»ñμ½ò»′ÎéÏéyÑØ
// TIM2CH1_CAPTURE_VAL=TIM_GetCapture1(TIM2);
// TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 éèÖÃÎaéÏéyÑØ2¶»ñ
// }else //»1Î′¿aê¼,μúò»′Î2¶»ñéÏéyÑØ
// {
// TIM2CH1_CAPTURE_STA=0; //Çå¿Õ
// TIM2CH1_CAPTURE_VAL=0;
// TIM_SetCounter(TIM2,0);
// TIM2CH1_CAPTURE_STA|=0X40; //±ê¼Ç2¶»ñμ½áËéÏéyÑØ
// TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
// }
// }
// }
//
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
}
int main(void)
{
u32 temp=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// éèÖÃÖD¶ÏóÅÏ輶·Ö×é2
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
uart_init(9600); //9600
LED_Init(); //3õê¼»ˉóëLEDᬽóμÄó2¼t½ó¿ú
TIM1_PWM_Init(899,0);
TIM2_Cap_Init(0XFFFF,72-1);
LCD_Init();
POINT_COLOR=RED;
while(1)
{
delay_ms(10);
LCD_ShowString(30,40,200,24,24,"Mini STM32 ^_^");
LCD_ShowxNum(156,150,1000000/tim,7,16,0);//ÏÔê¾μçÑ1Öμ
}
} |
|