完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
利用超声波测距,并把实时距离显示在屏幕上,利用手机蓝牙调试助手修改报警距离,并且达到报警距离后向手机端发送报警消息,同是可利用PC端查询实时距离
串口初始化 void uart_init(u32 bound) { //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟 //USART1_TX GPIOA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9 //USART1_RX GPIOA.10初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10 //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART1, &USART_InitStructure); //初始化串口1 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断 USART_Cmd(USART1, ENABLE); //使能串口1 } void uart2_init(u32 bound) { //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能USART2,GPIOA时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_AFIO, ENABLE); //使能USART2,GPIOA时钟 //USART1_TX GPIOA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9 //USART1_RX GPIOA.10初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10 //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3 ; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART2, &USART_InitStructure); //初始化串口2 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启串口接受中断 USART_Cmd(USART2, ENABLE); //使能串口2 } 中断服务函数 int upLimTemp=50; int downLimTemp=0; //float upLimDis=400; float downLimDis=5; extern int FLAG; void distance(void){ float length; length = Hcsr04GetLength(); printf("距离为:%.3fn",length); printf("rn"); } //当前阈值 void limit(void){ printf("报警距离:%f",downLimDis); printf("rn"); } void USART2_IRQHandler(void) //串口2中断服务程序 { if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾) { Res = USART_ReceiveData(USART2); //读取接收到的数据 if(Res==tr[0]){ distance(); } else if(Res==tr[1]){ downLimDis+=1; printf("报警距离:%.3f",downLimDis); printf("rn"); } else if(Res==tr[2]){ downLimDis-=1; printf("报警距离:%.3f",downLimDis); printf("rn"); } else if(Res==tr[3]){ limit(); } else{ printf("rn请输入正确指令rn"); } USART_ClearITPendingBit(USART2,USART_IT_RXNE); } } Sr04初始化 #ifndef __SR04_H #define __SR04_H #include "stm32f10x.h" #include "delay.h" #define HCSR04_PORT GPIOB #define HCSR04_CLK RCC_APB2Periph_GPIOB #define HCSR04_TRIG GPIO_Pin_1 #define HCSR04_ECHO GPIO_Pin_2 #define TRIG_Send PBout(1) #define ECHO_Reci PBin(2) void Hcsr04Init(void); static void OpenTimerForHc(void); static void CloseTimerForHc(void); void hcsr04_NVIC(void); void TIM6_IRQHandler(void); u32 GetEchoTimer(void); float Hcsr04GetLength(void ); #endif sr04头文件 #ifndef __SR04_H #define __SR04_H #include "stm32f10x.h" #include "delay.h" #define HCSR04_PORT GPIOB #define HCSR04_CLK RCC_APB2Periph_GPIOB #define HCSR04_TRIG GPIO_Pin_1 #define HCSR04_ECHO GPIO_Pin_2 #define TRIG_Send PBout(1) #define ECHO_Reci PBin(2) void Hcsr04Init(void); static void OpenTimerForHc(void); static void CloseTimerForHc(void); void hcsr04_NVIC(void); void TIM6_IRQHandler(void); u32 GetEchoTimer(void); float Hcsr04GetLength(void ); #endif 主函数逻辑 #include "led.h" #include "delay.h" #include "key.h" #include "sys.h" #include "usart.h" #include "beep.h" #include "dht11.h" #include "lcd.h" #include "sr04.h" extern float downLimDis; int main(void) { float length; delay_init(); //延时函数初始化 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 uart2_init(9600); //蓝牙模块9600 uart_init(115200); //串口初始化为115200 LED_Init(); //LED端口初始化 KEY_Init(); //初始化与按键连接的硬件接口 BEEP_Init(); LCD_Init(); Hcsr04Init(); POINT_COLOR=RED; //设置字体为红色 LCD_ShowString(30,50,200,16,16,"Ultrasonic Ranging"); LCD_ShowString(30,70,200,16,16,"NINE SR04"); LCD_ShowString(30,110,200,16,16,"SR04 OK"); POINT_COLOR=BLUE;//设置字体为蓝色 LCD_ShowString(30,150,200,16,16,"Distance: cm"); // LCD_ShowString(30,190,200,16,16,"Humi: . %"); LCD_ShowString(30,190,200,16,16,"Distance war: cm"); // LCD_ShowString(30,230,200,16,16,"Distance down: C"); dht11_readdata(0); while(1) { // dht11_readdata(0); length = Hcsr04GetLength(); if(length<=downLimDis){ printf("WARING"); printf("rn"); warning(); } LCD_ShowNum(30+75,150,length,2,16); //显示温度 LCD_ShowNum(30+105,190,downLimDis,2,16); //显示湿度 } } |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1537 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1490 浏览 1 评论
910 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
654 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1538 浏览 2 评论
1845浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
596浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
499浏览 3评论
499浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
483浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-5 15:20 , Processed in 0.532028 second(s), Total 45, Slave 38 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号