新手上路
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2018-8-18
- 在线时间
- 5 小时
|
用外部中断和定时器中断来测距,但是用F1写的程序可以用,换成F4的却不行。
#include "timer.h"
#include "sys.h"
void TIM5_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
TIM_TimeBaseInitStruct.TIM_Period=arr;
TIM_TimeBaseInitStruct.TIM_Prescaler=psc;
TIM_TimeBaseInitStruct.TIM_ClockDivision=0;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);
TIM_ITConfig(TIM5,TIM_IT_Trigger,ENABLE);
NVIC_InitStruct.NVIC_IRQChannel=TIM5_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0x00;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=0x03;
NVIC_Init(&NVIC_InitStruct);
}
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
}
}
#include "ultrasonic.h"
#include "sys.h"
#include "timer.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#define TRIG_PORT GPIOA
#define ECHO_PORT GPIOA
#define TRIG_PIN GPIO_Pin_0
#define ECHO_PIN GPIO_Pin_3
float Distance;
void Wave_SRD_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
GPIO_InitStruct.GPIO_Pin=TRIG_PIN;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(TRIG_PORT,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=ECHO_PIN;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_Init(ECHO_PORT,&GPIO_InitStruct);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource3);
EXTI_InitStruct.EXTI_Line=EXTI_Line3;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel=EXTI3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStruct);
}
void EXTI3_IRQHandler(void)
{
delay_us(10);
if(EXTI_GetITStatus(EXTI_PinSource3)!=RESET)
{
TIM_SetCounter(TIM5,0);
TIM_Cmd(TIM5,ENABLE);
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN));
TIM_Cmd(TIM5,DISABLE);
Distance=TIM_GetCounter(TIM5)*340/200.0;
if(Distance>0) printf("Distance:%f cm\r\n",Distance);
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
void Wave_SRD_Strat(void)
{
GPIO_SetBits(TRIG_PORT,TRIG_PIN);
delay_us(20);
GPIO_ResetBits(TRIG_PORT,TRIG_PIN);
}
#include "ultrasonic.h"
#include "led.h"
#include "sys.h"
#include "timer.h"
#include "usart.h"
#include "delay.h"
int main(void)
{
delay_init(168);
LED_Init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
TIM5_Init(4999,8399);
uart_init(115200);
Wave_SRD_Init();
while(1)
{
Wave_SRD_Strat();
LED2=!LED2;
delay_ms(2000);
}
}
哪里出问题了?
|
|