1. 程式人生 > >輸入捕獲

輸入捕獲

bit rcc 速度 IE cmd 作者 技術支持 開啟 lld

#include "timer.h"
#include "led.h"
#include "usart.h"


//TIM14 PWM部分初始化
//PWM輸出初始化
//arr:自動重裝值
//psc:時鐘預分頻數
void TIM14_PWM_Init(u32 arr,u32 psc)
{
//此部分需手動修改IO口設置

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14時鐘使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //使能PORTF時鐘

GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GPIOF9復用位定時器14

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOA9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//復用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽復用輸出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOF,&GPIO_InitStructure); //初始化PF9

TIM_TimeBaseStructure.TIM_Prescaler=psc; //定時器分頻
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上計數模式
TIM_TimeBaseStructure.TIM_Period=arr; //自動重裝載值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;

TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);

//初始化TIM14 Channel1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //選擇定時器模式:TIM脈沖寬度調制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //輸出極性:TIM輸出比較極性高
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC1Init(TIM14, &TIM_OCInitStructure); //根據T指定的參數初始化外設TIM3 OC2

TIM_OC2PreloadConfig(TIM14, TIM_OCPreload_Enable); //使能TIM3在CCR2上的預裝載寄存器

TIM_ARRPreloadConfig(TIM14,ENABLE);

TIM_Cmd(TIM14, ENABLE); //使能TIM14

}

TIM_ICInitTypeDef TIM5_ICInitStructure;

//定時器5通道1輸入捕獲配置
//arr:自動重裝值(TIM2,TIM5是32位的!!)
//psc:時鐘預分頻數
void TIM5_CH1_Cap_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;


RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //TIM5時鐘使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA時鐘

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOA0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//復用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽復用輸出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA0

GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5); //PA0復用位定時器5


TIM_TimeBaseStructure.TIM_Prescaler=psc; //定時器分頻
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上計數模式
TIM_TimeBaseStructure.TIM_Period=arr; //自動重裝載值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;

TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);

//初始化TIM5輸入捕獲參數
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 選擇輸入端 IC1映射到TI1上
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕獲
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置輸入分頻,不分頻
TIM5_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置輸入濾波器 不濾波
TIM_ICInit(TIM5, &TIM5_ICInitStructure);

TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允許更新中斷 ,允許CC1IE捕獲中斷

TIM_Cmd(TIM5,ENABLE ); //使能定時器5


NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//搶占優先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子優先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的參數初始化VIC寄存器、


}
//捕獲狀態
//[7]:0,沒有成功的捕獲;1,成功捕獲到一次.
//[6]:0,還沒捕獲到低電平;1,已經捕獲到低電平了.
//[5:0]:捕獲低電平後溢出的次數(對於32位定時器來說,1us計數器加1,溢出時間:4294秒)
u8 TIM5CH1_CAPTURE_STA=0; //輸入捕獲狀態
u32 TIM5CH1_CAPTURE_VAL; //輸入捕獲值(TIM2/TIM5是32位)
//定時器5中斷服務程序
void TIM5_IRQHandler(void)
{

if((TIM5CH1_CAPTURE_STA&0X80)==0)//還未成功捕獲
{
if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)//溢出
{
if(TIM5CH1_CAPTURE_STA&0X40)//已經捕獲到高電平了
{
if((TIM5CH1_CAPTURE_STA&0X3F)==0X3F)//高電平太長了
{
TIM5CH1_CAPTURE_STA|=0X80; //標記成功捕獲了一次
TIM5CH1_CAPTURE_VAL=0XFFFFFFFF;
}else TIM5CH1_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)//捕獲1發生捕獲事件
{
if(TIM5CH1_CAPTURE_STA&0X40) //捕獲到一個下降沿
{
TIM5CH1_CAPTURE_STA|=0X80; //標記成功捕獲到一次高電平脈寬
TIM5CH1_CAPTURE_VAL=TIM_GetCapture1(TIM5);//獲取當前的捕獲值.
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising); //CC1P=0 設置為上升沿捕獲
}else //還未開始,第一次捕獲上升沿
{
TIM5CH1_CAPTURE_STA=0; //清空
TIM5CH1_CAPTURE_VAL=0;
TIM5CH1_CAPTURE_STA|=0X40; //標記捕獲到了上升沿
TIM_Cmd(TIM5,DISABLE ); //關閉定時器5
TIM_SetCounter(TIM5,0);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling); //CC1P=1 設置為下降沿捕獲
TIM_Cmd(TIM5,ENABLE ); //使能定時器5
}
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update); //清除中斷標誌位
}

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"


//ALIENTEK 探索者STM32F407開發板 實驗10
//輸入捕獲實驗 -庫函數版本
//技術支持:www.openedv.com
//淘寶店鋪:http://eboard.taobao.com
//廣州市星翼電子科技有限公司
//作者:正點原子 @ALIENTEK



extern u8 TIM5CH1_CAPTURE_STA; //輸入捕獲狀態
extern u32 TIM5CH1_CAPTURE_VAL; //輸入捕獲值


int main(void)
{
long long temp=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設置系統中斷優先級分組2
delay_init(168); //初始化延時函數
uart_init(115200);//初始化串口波特率為115200

TIM14_PWM_Init(500-1,84-1); //84M/84=1Mhz的計數頻率計數到500,PWM頻率為1M/500=2Khz
TIM5_CH1_Cap_Init(0XFFFFFFFF,84-1); //以1Mhz的頻率計數
while(1)
{
delay_ms(10);
TIM_SetCompare1(TIM14,TIM_GetCapture1(TIM14)+1);
if(TIM_GetCapture1(TIM14)==300)TIM_SetCompare1(TIM14,0);
if(TIM5CH1_CAPTURE_STA&0X80) //成功捕獲到了一次高電平
{
temp=TIM5CH1_CAPTURE_STA&0X3F;
temp*=0XFFFFFFFF; //溢出時間總和
temp+=TIM5CH1_CAPTURE_VAL; //得到總的高電平時間
printf("HIGH:%lld us\r\n",temp); //打印總的高點平時間
TIM5CH1_CAPTURE_STA=0; //開啟下一次捕獲
}
}
}

輸入捕獲