|
學(xué)習(xí)stm32的捕獲功能,能夠?qū)崿F(xiàn)2米內(nèi)精確測(cè)距。
#include "stm32f10x.h"
#include "input_capture.h"
#include <stdio.h>
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
void delay_us(u16 time)
{
u16 i;
for(i=0;i<time*8;i++);
}
/* Private functions ---------------------------------------------------------*/
void start_measor_init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
void start_measor_start(void)
{
TIM_CCxCmd(TIM2, TIM_Channel_1, TIM_CCx_Enable);
GPIO_SetBits(GPIOA, GPIO_Pin_1);
delay_us(40);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
start_measor_init();
input_capture_init();
flag=0;
while (1)
{
unsigned i;
i++;
if(i%100==0)
{
start_measor_start();
}
}
}
#include "input_capture.h"
#include "stm32f10x_tim.h"
unsigned char flag;
float distance ;
void input_capture_init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=65535;
TIM_TimeBaseInitStruct.TIM_Prescaler=7199;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
TIM_ICInitTypeDef TIM_ICInitStruct;
TIM_ICInitStruct.TIM_Channel=TIM_Channel_1;
TIM_ICInitStruct.TIM_ICFilter=0xF;
TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInit(TIM2, &TIM_ICInitStruct);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStruct);
// TIM_Cmd(TIM3,ENABLE );
//TIM_CCxCmd(TIM3, TIM_Channel_1, TIM_CCx_Enable);
//TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
TIM_Cmd(TIM2,ENABLE );
}
void TIM2_IRQHandler(void)
{
u16 record_rising,record_falling;
if( TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
if(flag==1)
{ record_falling=TIM_GetCapture1 ( TIM2 );
distance=(float)record_falling*340/200.0;
//record_rising=TIM_GetCapture1 ( TIM2 );
flag=0;
TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 設(shè)置為上升沿捕獲
}
else
{
record_rising=0;
record_falling=0;
TIM_SetCounter(TIM2,0);
TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Falling); //CC1P=1 設(shè)置為下降沿捕獲
flag=1;
//
}
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
}
|
評(píng)分
-
查看全部評(píng)分
|