初级会员
- 积分
- 146
- 金钱
- 146
- 注册时间
- 2016-5-31
- 在线时间
- 30 小时
|
#include "stm32f10x.h"
#define MODE_T 0 //2aÖü
#define MODE_D 1 //2aÆμ
u8 modeSlect; //2aá¿Ä£ê½Ñ¡Ôñ±ê־λ
u8 flag; //ÆμÂê¼ì2aíê3é±ê־λ
double freq; //ÆμÂêÖμ
u8 Index=0; //¼ì2aμ½±ßÑØ′Îêy¼Æêy
u32 flowCount=0; //Äú2¿¶¨ê±Æ÷òæ′|¼Æêy
u32 time_1=0,time_2=0; //μúò»′Îoíμú¶t′μì2aμ½±ßÑØTIM3¼ÆêyÖμ
double nCount=0; //2aÆμÄ£ê½Ï£¬′æ·ÅTIM2Âö3å¼Æêy¸öêy
double Index_1=0; //2aÆμÄ£ê½Ï£¬′æ·ÅTIM2òç3ö′Îêy
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
void TIM4_TConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PA0 Çå3y֮ǰéèÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //PA0 êäèë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //ê1ÄüTIM2ê±Öó
TIM_TimeBaseStructure.TIM_Period = 1; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =0; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ETRClockMode2Config(TIM4, TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted, 0); //éè¼Æía2¿ê±ÖóÄ£ê½2
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);//ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_Cmd(TIM4,ENABLE ); //ê1Äü¶¨ê±Æ÷
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM2ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÏèÕ¼óÅÏ輶2¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //′óóÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
}
void TIM3_TConfig(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê1ÄüTIM2ê±Öó
TIM_DeInit(TIM3);
TIM_InternalClockConfig(TIM3);
TIM_TimeBaseStructure.TIM_Period =0xFFFF-1; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =0; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM2ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏèÕ¼óÅÏ輶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¼Ä′æÆ÷
}
void function(void)
{
if(modeSlect==MODE_T)
{
//ÖØDÂÔ¤Öö¨ê±Æ÷TIM2¡¢TIM3ÎaÖüÆúÄ£ê½
TIM_TimeBaseStructure.TIM_Period = 1; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =0; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_TimeBaseStructure.TIM_Period =0xFFFF-1; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =0; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
}
else if(modeSlect==MODE_D)
{
//ÖØDÂÔ¤Öö¨ê±Æ÷TIM2¡¢TIM3ÎaÖüÆúÄ£ê½
TIM_TimeBaseStructure.TIM_Period = 0xFFFF-1; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =0; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_TimeBaseStructure.TIM_Period =2000; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler =36000-1; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
}
}
//TIM2ÖD¶Ï·tÎñ3ìDò
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) == SET)
{
//Çå3yTIM2òç3öÖD¶Ï′y′|àíλ
TIM_ClearITPendingBit(TIM4, TIM_IT_Update );
if(MODE_T==modeSlect)
{
//¼ì2aμ½Âö3åμ½à′£¬¿aÆôTIM3¿aê¼¼Æê±
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
TIM_Cmd(TIM3, ENABLE);
if(Index==1)
{
//»ñè¡μúò»¸ö±ßÑØ′¥·¢¶¨ê±Æ÷TIM3¼ÆêyÖμ
time_1=flowCount*65535+TIM_GetCounter(TIM3);
}
if(Index==2)
{
//»ñè¡μú¶t¸ö±ßÑØ′¥·¢¶¨ê±Æ÷TIM3¼ÆêyÖμ
time_2=flowCount*65535+TIM_GetCounter(TIM3);
//Çå3yTIM3¼ÆêyÖμoíòç3öÖμ
TIM_SetCounter(TIM3,0);
Index=0;
flowCount=0;
//ÆμÂê=2*TIM3ê±Öó/TIM3¼Æêy¸öêy(3Ë2êÇòòÎaὸöÖüÆú2aò»′Î)
freq=(2*72000000.0/(time_2-time_1));
flag=1; //ò»¸öÖüÆú2aá¿íê3é±ê־λÖÃλ
}
Index++;
}
if(MODE_D==modeSlect)
{
Index++;
//¼ì2aμ½Âö3åμ½à′£¬¿aÆôTIM3¿aê¼¼Æê±
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
TIM_Cmd(TIM3, ENABLE);
}
}
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
{
//Çå3yTIM3òç3öÖD¶Ï′y′|àíλ
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
if(MODE_T==modeSlect)
{
flowCount++; //òç3ö′Îêy+1
}
else
{
//»ñè¡1SÄúÂö3å¼Æêy¸öêy
nCount=TIM_GetCounter(TIM4);
//¼ÆêyÆ÷ÖμÇåá㣬±üÃa¸éèÅÏÂ′ÎÖμ
TIM_SetCounter(TIM4,0);
//1رն¨ê±Æ÷oí¼ÆêyÆ÷£¬èÃ΢¿ØÖÆÆ÷′|àíÖμoíÏÔê¾
TIM_ITConfig(TIM4,TIM_IT_Update,DISABLE);
//ÆμÂê=òç3öÖμ*òç3ö′Îêy+Î′òç3öÖμ
freq=65535*Index_1+nCount;
Index_1=0; //òç3ö′ÎêyÇåáã
flag=1; //íê3éò»¸ö1S2a῱ê־λ
}
}
}
#endif
|
|