|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
用的是正点原子mini开发板stm32f103RCT6
************main函数**************
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "usart.h"
#include "exti.h"
#include "key.h"
#include "time.h"
#include "pwm.h"
#include "capture.h"
int main(void)
{
u16 i=0;
u32 temp=0;
delay_init();
LED_Init();
KEY_Init();
//My_EXTIX_Init();
//USART1_Init(9600);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÖжÏÓÅÏȼ¶·Ö×é ·Ö2×é
//My_TIM3_Init(4999,7199);//Tout=((4999+1)*(7199+1))/72M=500ms
//My_TIM1_PWM_Init(300,0);
My_TIM5_capture_Init(0xffff,71);//Tout=((4999+1)*(71+1))/72M=500ms
//APB1=36M£¬µ±·ÖƵϵÊý¡Ù1ʱ£¬ÔòAPB1ʱÖÓƵÂÊx2£¬¹ÊΪ72M
while(1)
{
//LED0=!LED0;
//delay_ms(100);//ÏÖÏó£ºLED0ÉÁ˸½Ï¿ì¼ä¸ô100ms£¬LED1ÉÁ˸½ÏÂý¼ä¸ô500ms
if(TIM5_CH1_CAPTURE_STA&0x80)
{
temp=TIM5_CH1_CAPTURE_STA&0x3f;//»ñÈ¡Òç³ö´ÎÊý
temp*=0xffff;//Òç³öÒ»´ÎÊÇ0xffff Òç³öʱ¼ä×ܺÍ
temp+=TIM5_CH1_CAPTURE_VAL;//×ܵĸߵçƽ³ÖÐøʱ¼ä
printf("¸ßµçƽ³ÖÐøʱ¼ä£º%d us\r\n",temp);
TIM5_CH1_CAPTURE_STA=0;
}
i++;
if(i%20==0)
{
LED1=!LED1;
}
delay_ms(10);
}
}
********capture.c************
#include "capture.h"
#include "capture.h"
u8 TIM5_CH1_CAPTURE_STA=0;
u16 TIM5_CH1_CAPTURE_VAL=0;
void My_TIM5_capture_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;//PA0ÏÂÀ
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);//PA0ÏÂÀ
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; // TDTS=Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//ÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;//²»·ÖƵ
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;//ÅäÖÃIC1Ö±½ÓÓ³Éäµ½TI1ÉÏ
TIM_ICInitStructure.TIM_ICFilter=0x00;//IC1F=0000;ÅäÖÃÊäÈëÂ˲¨Æ÷ ²»Â˲¨
TIM_ICInit(TIM5,&TIM_ICInitStructure);//ÊäÈ벶»ñ
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//ÇÀÕ¼ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//×ÓÓÅÏȼ¶
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM5,ENABLE);
}
void TIM5_IRQHandler(void)
{
//ÒòΪ¿ªÆôÁ˸üкͲ¶»ñÁ½¸öÖжÏTIM_IT_Update|TIM_IT_CC1£¬ËùÒÔÊ×ÏÈÒªÅжÏÖжÏÀàÐÍ
if((TIM5_CH1_CAPTURE_STA&0x80)==0)
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update)!= RESET)//½øÈë¸üÐÂÖжÏ
{
if(TIM5_CH1_CAPTURE_STA&0x40)//ÅжϴθßλΪ1
{
if((TIM5_CH1_CAPTURE_STA&0x3f)==0x3f)//0x3fΪ0011 1111£¬Òç³ö´ÎÊý³¬¹ý×î´óÖµ
{
TIM5_CH1_CAPTURE_STA|=0X80;//Ç¿ÖƲ¶»ñÍê³É
TIM5_CH1_CAPTURE_VAL=0xffff;//´Ëʱ×î´ó²¶»ñֵΪ0xffff (u16ÀàÐÍ)
}
else
{
TIM5_CH1_CAPTURE_STA++;
}
}
}
if(TIM_GetITStatus(TIM5,TIM_IT_CC1)!= RESET)//½øÈ벶»ñÖжÏ
{
if(TIM5_CH1_CAPTURE_STA&0x40) //0x40¼´0100 0000£¬´Î¸ßλΪ¸ßµçƽ²¶»ñ±ê־λ ÅжÏÏàÓëÊÇ·ñµÈÓÚ1
{
TIM5_CH1_CAPTURE_STA|=0X80;//0x80¼´1000 0000£¬×î¸ßλΪ²¶»ñÍê³É±ê־λ TIM5_CH1_CAPTURE_STA=TIM5_CH1_CAPTURE_STA|0X80 ×î¸ßλÖÃ1
TIM5_CH1_CAPTURE_VAL=TIM_GetCapture1(TIM5);//²¶»ñ±È½Ï¼Ä´æÆ÷µÄÖµ¾ÍÊǼÆÊýÆ÷µÄÖµ
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising);
}
else
{
TIM5_CH1_CAPTURE_STA=0;//²¶»ñ״̬Çå0
TIM5_CH1_CAPTURE_VAL=0;
TIM5_CH1_CAPTURE_STA|=0X40;//¼´TIM5_CH1_CAPTURE_STA=TIM5_CH1_CAPTURE_STA|0X40 ´Î¸ßλÖÃ1
TIM_Cmd(TIM5,DISABLE );//¹Ø±Õ¶¨Ê±Æ÷
TIM_SetCounter(TIM5,0);//ÉèÖüÆÊýÆ÷³õֵΪ0
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling);//TIM_OCPolarity_Low¼´ÎªTIM_ICPolarity_Falling£¬¾ùΪ0x0002
TIM_Cmd(TIM5,ENABLE );
}
}
}
TIM_ClearITPendingBit(TIM5,TIM_IT_Update|TIM_IT_CC1);
}
*********capture.h***********
#ifndef _capture_H
#define _capture_H
#include "sys.h"
extern u8 TIM5_CH1_CAPTURE_STA;
extern u16 TIM5_CH1_CAPTURE_VAL;
void My_TIM5_capture_Init(u16 arr,u16 psc);
void TIM5_IRQHandler(void);
#endif
|
|