关于超声波测距单片机源程序如下:
- #include "stm32f4xx.h"
- #include "led.h"
- #include "beep.h"
- #include "key.h"
- #include "exti.h"
- #include "delay.h"
- #include "time.h"
- #include "usart.h"
- #include "sr04.h"
- #include "OLED_I2C.h"
- int main(void)
- {
- u32 sr04_value=0;
- u8 i;
- char buffer[4];
-
- //设置中断优先级组
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- Delay_Init();
- Usart1_Init();
- Time4_Init();
- Sr04_Init();
- LED_Init();
- BEEP_Init();
- I2C_Configuration();
- OLED_Init();
-
-
- PEout(9) = 1;
- OLED_Fill(0x00);//全屏灭
- OLED_CLS();//清屏
- delay_s(1);
-
- OLED_ShowStr(75,6,"cm",2);
- for(i=0;i<5;i++)
- {
- OLED_ShowCN(22+i*16,0,i);//显示后方距离为中文字
- }
-
-
- while(1)
- {
-
-
- delay_ms(100);
- sr04_value = Get_value();
- printf("sr04_value = %d\n",sr04_value);
- \
- if(sr04_value >=100)
- {
- OLED_ShowStr(45, 6, " ", 2);
- OLED_ShowCN(40,3,7);
- OLED_ShowCN(70,3,8);
- }
-
- if(sr04_value < 100 &&sr04_value>40)
- {
- sprintf(buffer,"%u",sr04_value);
- OLED_ShowStr(45, 6, buffer, 2);
- OLED_ShowCN(40,3,7);//安
- OLED_ShowCN(70,3,8);//全
- }
- if(sr04_value <= 40 && sr04_value > 20)
- {
- PEout(9) = 0;
- delay_ms(700);
- PEout(9) = 1;
- sprintf(buffer,"%u",sr04_value);
- OLED_ShowStr(45, 6, buffer, 2);
- OLED_ShowCN(40,3,5);//危
- OLED_ShowCN(70,3,6);//险
- }
- if(sr04_value >=10 &&sr04_value<=20 )
- {
- PEout(9) = 0;
- delay_ms(300);
- PEout(9) = 1;
- sprintf(buffer,"%u",sr04_value);
- OLED_ShowStr(45, 6, buffer, 2);
- OLED_ShowCN(40,3,5);//危
- OLED_ShowCN(70,3,6);//险
-
- }
- if(sr04_value <10)
- {
- OLED_ShowStr(45, 6, " ", 2);
- OLED_ShowStr(40, 3, " ", 2);
- PEout(9) = 0;
- delay_ms(100);
- PEout(9) = 1;
- sprintf(buffer,"%u",sr04_value);
- OLED_ShowStr(45, 6, buffer, 2);
- OLED_ShowCN(40,3,5);//危
- OLED_ShowCN(70,3,6);//险
-
- //OLED_ShowStr(40, 3, " ", 2);
-
- }
-
- }
- return 0;
- }
复制代码
所有资料51hei提供下载:
结合.rar
(456.11 KB, 下载次数: 67)
|