stm32超声波程序问题

2019-08-12 17:20发布

[mw_shl_code=c,true]#include "UltrasonicWave.h" #include "usart.h" #include "timer.h" #include "delay.h" #define TRIG_PORT GPIOC //TRIG #define ECHO_PORT GPIOC //ECHO #define TRIG_PIN GPIO_Pin_13 //TRIG #define ECHO_PIN GPIO_Pin_14 //ECHO float UltrasonicWave_Distance; //计算出的距离 /* * ????????UltrasonicWave_Configuration * ?è?? ?????ù?¨???é???????? * ???? ???? * ???? ???? */ void UltrasonicWave_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //关闭jtag RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = TRIG_PIN; //PC8接TRIG GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //?è?????ì???????? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(TRIG_PORT, &GPIO_InitStructure); //?????????èGPIO GPIO_InitStructure.GPIO_Pin = ECHO_PIN; //PC7接ECH0 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //?è?????? GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //??????GPIOA RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PC8接TRIG GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //?è?????ì???????? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //?????????èGPI RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PC8接TRIG GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //?è?????ì???????? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //?????????èGPI //GPIOC.7 中断线以及中断初始化配置 GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource14); EXTI_InitStructure.EXTI_Line=EXTI_Line14; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE;//ji'cu?????÷ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能键所在的外部中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //??????????2 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×???????2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //?????????????¨?? NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化NVIC寄存器 } void EXTI9_5_IRQHandler(void) { delay_us(10); //???±10us if(EXTI_GetITStatus(EXTI_Line14) != RESET) { TIM_SetCounter(TIM2,0); TIM_Cmd(TIM2, ENABLE); //?????±?? while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); //?????????? TIM_Cmd(TIM2, DISABLE); //?¨?±?÷2?§?? UltrasonicWave_Distance=TIM_GetCounter(TIM2)*5*34/200.0; //?????à??&&UltrasonicWave_Distance<150 if(UltrasonicWave_Distance>40) { GPIO_SetBits(GPIOA,GPIO_Pin_2); GPIO_SetBits(GPIOB,GPIO_Pin_7); } else if(UltrasonicWave_Distance<40) { GPIO_SetBits(GPIOA,GPIO_Pin_2); GPIO_ResetBits(GPIOB,GPIO_Pin_7); } EXTI_ClearITPendingBit(EXTI_Line14); //????EXTI7???·?????? } } /* * ????????UltrasonicWave_StartMeasure * ?è?? ?????????à??·???????>10us???????????ó????·????????????±?? * ???? ???? * ???? ???? */ void UltrasonicWave_StartMeasure(void) { GPIO_SetBits(TRIG_PORT,TRIG_PIN); //??>10US?????????TRIG_PORT,TRIG_PIN????????define????? delay_us(20); //???±20US GPIO_ResetBits(TRIG_PORT,TRIG_PIN); } /******************* (C) 1209 Lab *****END OF FILE************/ [/mw_shl_code]



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
正点原子
1楼-- · 2019-08-12 20:58
 精彩回答 2  元偷偷看……
追晨沐阳0311
2楼-- · 2019-08-12 22:49
可以借鉴一下源代码吗,谢谢1306501801QQ

一周热门 更多>