#include "led.h" #include "delay.h" #include "sys.h" #include "stm32f10x.h" #include "stdio.h" #include "usart.h" #include "adc.h" #include "oled.h" #include "ds18b20.h" #include "HX711.h" #define HwInput1 PBin(8) //红外传感器输入 #define HwInput2 PBin(7) //红外传感器输入 #define HwInput3 PBin(6) //红外传感器输入 #define HwInput4 PBin(5) //红外传感器输入 #define DY_Input PBin(14) #define KEY1 PAin(0) // 手动取消报警按钮 char showbuff[16]={0}; u8 warning_sta=0;//异常状态 u8 warning_flag=0;//警告标志位 int temp_val=0; u16 get_cnt=0; u16 ds_val=0;//滴速 u16 sy_val=0;//剩余量 void KEY_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //使能端口时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6|GPIO_Pin_5|GPIO_Pin_14; // 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化 } u16 count=0; u16 Rpm_count=0; void exti_Init(void) { EXTI_InitTypeDef EXTI_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; GPIO_InitTypeDef GPIO_InitStructure; // KEY_Init();//IO初始化 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//配置外部中断时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOA时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置成输入 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOA8 //io口与中断线映射 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9);//链接到中断线8 GPIO8只能映射到中断线4上面 EXTI_InitStruct.EXTI_Line=EXTI_Line9;//确定中断线 EXTI_InitStruct.EXTI_LineCmd=ENABLE;//使能 EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;//中断模式 EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;//上降沿触发 EXTI_Init(&EXTI_InitStruct);//初始化 NVIC_InitStruct.NVIC_IRQChannel=EXTI9_5_IRQn; /* 需要明确的是,PxN管脚共用外部中断线EXTIN和外部中断向量EXTIN_IRQn 和中断服务程序入口EXTIN_IRQHandler,但是需要注意的是[9...5] 共用EXTI9_5_IRQn和EXTI9_5_IRQHandler、[15...10]共用EXTI15_10_IRQn和EXTI15_10_IRQHandler */ NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;//使能 NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;//中断抢优先级 NVIC_InitStruct.NVIC_IRQChannelSubPriority=2; NVIC_Init(&NVIC_InitStruct);//初始化 } void EXTI9_5_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line9)!=RESET) { delay_ms(2); if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==1) { count++; // ds_val=count;//获取滴速 //OLED_ShowString(0,6,"324",16); } EXTI_ClearITPendingBit(EXTI_Line9);//清除中断 标志 } } //通用定时器4中断初始化 //这里时钟选择为APB1的2倍,而APB1为36M //arr:自动重装值。 //psc:时钟预分频数 //这里使用的是定时器2! void TIM2_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能 //定时器TIM4初始化 TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位 TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE ); //使能指定的TIM4中断,允许更新中断 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断分组 2 //中断优先级NVIC设置 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM6中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器 TIM_Cmd(TIM2, ENABLE); //使能TIMx } u8 sy_warning_flag1=0; u8 sy_warning_flag2=0; void TIM2_IRQHandler() { static u8 st; static u16 pw=0; st=TIM_GetFlagStatus(TIM2, TIM_IT_Update); if(st!=0) { TIM_ClearFlag(TIM2, TIM_IT_Update); if(DY_Input==1) { if(++pw>=100*10)//10s时间到 { pw=0; ds_val=count*6;//获取滴速 count=0; if(ds_val>70||ds_val<10)//滴速 通常成人每分钟40-60滴,儿童每分钟20-40滴。 { warning_flag=1; } } } if(HwInput1==1&&HwInput2==1&&HwInput3==1&&HwInput4==1) { sy_val=150; sy_warning_flag1=0;sy_warning_flag2=0;//清除提醒锁定标志位 } else if(HwInput2==1&&HwInput3==1&&HwInput4==1) { sy_val=100; sy_warning_flag1=0;sy_warning_flag2=0;//清除提醒锁定标志位 } else if(HwInput3==1&&HwInput4==1) { sy_val=50; } else if(HwInput4==1) { sy_val=10; } else { sy_val=0; } get_cnt++; } } int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级 delay_init(); //延时函数初始化 LED_Init(); //初始化与LED连接的硬件接口 uart_init(9600); delay_ms(500); OLED_Init(); OLED_Clear(); DS18B20_Init(); sprintf(showbuff,"温度:%2d.%1d",temp_val/10,temp_val%10); //打印成字符串 OLED_ShowText(0,0,showbuff,0); //显示 sprintf(showbuff,"流速:%3d/min",ds_val); //打印成字符串 OLED_ShowText(0,2,showbuff,0); //显示 sprintf(showbuff,"剩余量:%3d/ml",sy_val); //打印成字符串 OLED_ShowText(0,4,showbuff,0); //显示 TIM2_Int_Init(71,10000) ; // 开启定时器4 10ms buzz=1; exti_Init(); KEY_Init(); while(1) { if(get_cnt>100) //采集时间计数器 { get_cnt=0; temp_val=DS18B20_Get_Temp(); sprintf(showbuff,"温度:%2d.%1d",temp_val/10,temp_val%10); //打印成字符串 OLED_ShowText(0,0,showbuff,0); //显示 sprintf(showbuff,"流速:%3d/min",ds_val); //打印成字符串 OLED_ShowText(0,2,showbuff,0); //显示 sprintf(showbuff,"剩余量:%3d/ml",sy_val); //打印成字符串 OLED_ShowText(0,4,showbuff,0); //显示 0); //打印成字符串 if(sy_val==10&&sy_warning_flag1==0)//到10ml提醒一次 { warning_flag=1; sy_warning_flag1=1; } if(sy_val==0&&sy_warning_flag2==0)//到0ml提醒一次 { warning_flag=1; sy_warning_flag2=1; } printf("SZZ152_X%2d.%1dH%dD%dZ%dK\r\n",temp_val/10,temp_val%10,ds_val,sy_val,warning_flag);//串口发送给wifi模块 if(warning_flag) { buzz=!buzz;//声光报警触发 }else { buzz=1; } } if(KEY1==0) { delay_ms(5);//延时 if(KEY1==0) { while(!KEY1); warning_flag=0; } } delay_ms(5);//延时 } }