实物图:
前言
要实现的功能如下图:
一、设计思路
功能1:使用光敏电阻进行ADC的采集,判断环境。 功能2:设置两种模式,用标志位便可解决 功能3:采用超声波传感器测距,写一个if即可 功能4:设置两个按键 功能5:蓝牙串口 功能6:超声波与蜂鸣器
二、代码实现
主函数如下,.c.h太多了,想要的私信我吧
#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "math.h"
#include "stdio.h"
#include "LED.h"
#include "OLED.h"
#include "adc.h"
#include "MS1100.h"
#include "math.h"
#include "usart.h"
#include "dht11.h"
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "key.h"
#include "usart.h"
#include "exti.h"
#include "led.h"
#include "motor.h"
#include "timer.h"
#include "CSB.h"
#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "math.h"
#include "stdio.h"
#include "LED.h"
#include "key.h"
#include "OLED.h"
#include "ADC.h"
#include "beep.h"
#include "math.h"
#include "usart.h"
#include "hc05.h"
#include "usart3.h"
extern float UltrasonicWave_Distance;
int main(void)
{
u16 i=0,a=0,adcx7,b=0;
float temp7;
u16 reclen=0;
vu8 key=0;
u16 adcx;
SystemInit();
delay_init();
NVIC_Configuration();
uart_init(9600);
LED_Init();
KEY_Init();
Timerx_Init(5000,7199);
UltrasonicWave_Configuration();
BEEP_Init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
KEY_Init();
Adc_Init();
HC05_Init();
delay_ms(100);
USART2_RX_STA=0;
TIM3_PWM_Init(899,0);
OLED_Init();
OLED_Clear();
OLED_ShowCHinese(0,0,5);
OLED_ShowCHinese(14,0,6);
OLED_ShowString(28,1,":",4);
OLED_ShowCHinese(0,2,13);
OLED_ShowCHinese(14,2,14);
OLED_ShowString(28,3,":",4);
OLED_ShowCHinese(0,4,11);
OLED_ShowCHinese(14,4,12);
OLED_ShowString(30,5,":",4);
LED0=0;
LED1=0;
while(1)
{
adcx7=Get_Adc_Average(ADC_Channel_7,10);
temp7=(float)adcx7*(3.3/4096);
adcx7=temp7 ;
temp7-=adcx7;
temp7*=1000;
UltrasonicWave_StartMeasure();
if(UltrasonicWave_Distance<5)
{
BEEP=1;
OLED_ShowCHinese(35,4,15);
}
if(UltrasonicWave_Distance>20)
{
BEEP=0;
OLED_ShowCHinese(35,4,16);
}
if(b==0)
{
if(adcx7==1)
{
TIM_SetCompare1(TIM3, 10);
OLED_ShowCHinese(35,2,0);
OLED_ShowCHinese(49,2,2);
}
if(adcx7==0)
{
TIM_SetCompare1(TIM3, 1500);
}
}
if(KEY0==0)
{
delay_ms(10);
if(KEY0==0)
{
a=1;
LED0=1;
LED1=0;
OLED_ShowCHinese(35,0,7);
OLED_ShowCHinese(49,0,8);
}
}
if(KEY1==0)
{
delay_ms(10);
if(KEY1==0)
{
a=2;
LED1=1;
LED0=0;
OLED_ShowCHinese(35,0,9);
OLED_ShowCHinese(49,0,10);
}
}
if(a==2)
{
if(KEY2==0)
{
delay_ms(10);
if(KEY2==0)
{b=2;
OLED_ShowCHinese(35,2,0);
OLED_ShowCHinese(49,2,2);
while(1)
{
TIM_SetCompare1(TIM3, 10);
if(KEY3==0||KEY0==0||KEY1==0 )
break;
}
}
if(UltrasonicWave_Distance<5)
{
BEEP=1;
OLED_ShowCHinese(35,4,15);
}
}
if(KEY3==0)
{
delay_ms(10);
if(KEY3==0)
{b=1;
while(1)
{
TIM_SetCompare1(TIM3, 700);
OLED_ShowCHinese(35,2,1);
OLED_ShowCHinese(49,2,2);
if(KEY2==0||KEY0==0||KEY1==0)
break;
}
}
}
}
if(a==1)
{
UltrasonicWave_StartMeasure();
if(UltrasonicWave_Distance<5)
{
BEEP=1;
OLED_ShowCHinese(35,4,15);
}
if(UltrasonicWave_Distance>10&&UltrasonicWave_Distance<20)
{
BEEP=0;
OLED_ShowCHinese(35,2,3);
OLED_ShowCHinese(49,2,4);
OLED_ShowCHinese(35,4,16);
for(i=0;i<=1;i++)
{
TIM_SetCompare1(TIM3, 700);
delay_ms(5000);
TIM_SetCompare1(TIM3, 10);
delay_ms(5000);
if(i==1)
break;
}
}
if(UltrasonicWave_Distance>20)
{
BEEP=0;
OLED_ShowCHinese(35,4,16);
}
}
if(USART2_RX_STA&0X8000)
{
reclen=USART2_RX_STA&0X7FFF;
printf("%d\r\n",reclen);
USART2_RX_BUF[reclen]=0;
if(reclen==1)
{
b=1;
if(strcmp((const char*)USART2_RX_BUF,"0")==0)
{
TIM_SetCompare1(TIM3, 700);
OLED_ShowCHinese(35,2,1);
OLED_ShowCHinese(49,2,2);
}
if(strcmp((const char*)USART2_RX_BUF,"1")==0)
{
TIM_SetCompare1(TIM3,10);
OLED_ShowCHinese(35,2,0);
OLED_ShowCHinese(49,2,2);
}
if(strcmp((const char*)USART2_RX_BUF,"2")==0)
{
BEEP=1;
OLED_ShowCHinese(35,4,15);
}
if(strcmp((const char*)USART2_RX_BUF,"3")==0)
{
BEEP=0;
OLED_ShowCHinese(35,4,16);
}
}
USART2_RX_STA=0;
}
delay_ms(100);
}
}
代码很简单,就是几个if。
|