国产免费AV|泡泡玛特欧洲总部将设在伦敦|中文天堂网www新版资源在线|一本久道综合在线中文|国精产品一二三产区的使用方法|香蕉鱼在线观看|www.27eee
ELEOK
標題:
超聲波模塊程序 STM32F103ZET6 附件內有完整源碼和視頻
[打印本頁]
作者:
路漫漫
時間:
2020-6-5 10:20
標題:
超聲波模塊程序 STM32F103ZET6 附件內有完整源碼和視頻
完整源碼和測試視頻
STM32F103ZET6.7z
(3.1 MB, 售價: 1 E幣)
2020-6-5 10:20 上傳
點擊文件名下載附件
售價: 1 E幣
[記錄]
[
購買
]
部分代碼:
include "wave.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#define Trig GPIO_Pin_4
#define Echo GPIO_Pin_6
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; //將PE4于Trig相連
GPIO_InitSture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitSture);
GPIO_InitSture.GPIO_Mode=GPIO_Mode_IPD; //拉輸入模式
GPIO_InitSture.GPIO_Pin=Echo; //將PE6于Echo相連
GPIO_InitSture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitSture);
//中斷和6端口映射一起
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource6);
//外部中斷配置
EXTI_InitSture.EXTI_Line=EXTI_Line6;
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=EXTI9_5_IRQn;
NVIC_InitSture.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitSture.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitSture.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitSture);
}
void EXTI9_5_IRQHandler(void)
{
delay_us(10);
if(EXTI_GetITStatus(EXTI_Line6)!=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>0)
{
printf("Distance:%f cm\r\n",Distance);
}
EXTI_ClearITPendingBit(EXTI_Line6);
}
}
void Wave_SRD_Strat(void)
{
GPIO_SetBits(GPIOE,Trig); //將Trig設置為高電平
delay_us(20); //持續大于10us觸發,觸發超聲波模塊工作
GPIO_ResetBits(GPIOE,Trig);
}
復制代碼
歡迎光臨 ELEOK (http://www.afoofa.cn/)
Powered by Discuz! X5.0