一级 亮绿灯 没有火
二级 亮黄灯 有小火
三级 亮蓝灯 有大火
开启按键中断 按键按下的时候 串口打印“楼层 + 火势情况”
(楼层用五向按键判断 上下左右中分别代表1-5层)
#include <stdio.h>
void lou(uint16_t X);
void huo(uint16_t Y);
int val,key;
int fputc(int ch, FILE *f)
{
while(!(USART1->ISR & 1<<7)){};
USART1->TDR =ch;
return ch;
}
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin==GPIO_PIN_8)
{
//首先读取ADC采集的数据,包括光敏及按键的ADC数值
HAL_ADC_Start(&hadc1);//启动转换
while(!(ADC1->ISR & 1<<2)){};//等待EOC信号 第一个通道转换完成
buf[0]=HAL_ADC_GetValue(&hadc1);//读取ADC数值
while(!(ADC1->ISR & 1<<3)){};//等待EOS信号 系列转换完成
buf[1]=HAL_ADC_GetValue(&hadc1);//读取ADC数值
HAL_ADC_Stop(&hadc1);//停止转换
//printf("火势: %d\n",buf[0]);
//printf("楼层: %d\n",buf[1]);
lou(buf[1]);
huo(buf[0]);
}
}
void lou(uint16_t X)
{
if(X>2100&&X<2200)
{
printf("楼层:1楼 ");
}
if(X>500&&X<600)
{
printf("楼层:2楼 ");
}
if(X>1500&&X<1600)
{
printf("楼层:3楼 ");
}
if(X>2950&&X<3050)
{
printf("楼层:4楼 ");
}
if(X>2500&&X<2600)
{
printf("楼层:5楼 ");
}
}
void huo(uint16_t Y)
{
if(Y>3500)
{
printf("火势:无火\n");
HAL_GPIO_WritePin(GPIOB, LED2_Pin|LED3_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, LED1_Pin, GPIO_PIN_RESET);
}
if(Y>3000&&Y<3500)
{
printf("火势:小火\n");
HAL_GPIO_WritePin(GPIOB, LED1_Pin|LED2_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, LED3_Pin, GPIO_PIN_RESET);
}
if(buf[0]<3000)
{
printf("火势:大火\n");
HAL_GPIO_WritePin(GPIOB, LED1_Pin|LED3_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, LED2_Pin, GPIO_PIN_RESET);
}
}