我用超声波触发一个全局变量,然后控制舵机转动,结果进入不了中断,这是为啥呢?help#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "pwm.h"
#include "wave.h"
#include "timer.h"
int main(void)
{
u16 led0pwmval=0;
delay_init(); //延时函数初始化
LED_Init(); //初始化与LED连接的硬件接口
TIM1_PWM_Init(1439,999);//不分频。PWM频率=72000/(899+1)=80Khz
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Wave_SRD_Init();
Timer_SRD_Init(5000,7199);
Wave_SRD_Init();
while(1)
{
led0pwmval=37;
TIM_SetCompare1(TIM1,led0pwmval);
delay_ms(1000); //0度,延时1s
Wave_SRD_Init();
if(a==1)
{
led0pwmval=93;
TIM_SetCompare1(TIM1,led0pwmval);
delay_ms(1000); //90度
a=0;
EXTI->IMR = 1;
}
}
}
超声波程序:
#include "wave.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
#define Trig GPIO_Pin_6
#define Echo GPIO_Pin_3
u8 a=0;
float Distance;
void Wave_SRD_Init(void)
{
GPIO_InitTypeDef GPIO_InitSture;
EXTI_InitTypeDef EXTI_InitSture;
NVIC_InitTypeDef NVIC_InitSture;
//如果外部中断的话则一定使能AFIO复用功能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOE,ENABLE);
//配置IO端口
GPIO_InitSture.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出模式
GPIO_InitSture.GPIO_Pin=Trig; //将PE6于Trig相连
GPIO_InitSture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitSture);
GPIO_InitSture.GPIO_Mode=GPIO_Mode_IPD; //拉输? 肽J?
GPIO_InitSture.GPIO_Pin=Echo; //将PE3于Echo相连
GPIO_InitSture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitSture);
//中断和6端口映射一起
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
//外部中断配置
EXTI_InitSture.EXTI_Line=EXTI_Line3;
EXTI_InitSture.EXTI_LineCmd=ENABLE;
EXTI_InitSture.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitSture.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitSture);
//中断优先级管理
NVIC_InitSture.NVIC_IRQChannel=EXTI3_IRQn;
NVIC_InitSture.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitSture.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitSture.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitSture);
}
void EXTI3_IRQHandler(void)
{
delay_us(10);
if(EXTI_GetITStatus(EXTI_Line3)!=RESET)
{
TIM_SetCounter(TIM3,0);
TIM_Cmd(TIM3,ENABLE);
while(GPIO_ReadInputDataBit(GPIOE,Echo)); //等待低电平
TIM_Cmd(TIM3,DISABLE);
Distance=TIM_GetCounter(TIM3)*340/200.0;
if((Distance>10)&&(Distance<100))
{
printf("Distance:%f cm
",Distance);
a=1;
EXTI->IMR = 0;
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
void Wave_SRD_Strat(void)
{
GPIO_SetBits(GPIOE,Trig); //将Trig设置为高电平
delay_us(20); //持续大于10us触发,触发超声波模块工作
GPIO_ResetBits(GPIOE,Trig);
}
一周热门 更多>