main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "stm32f10x.h"
  5. #include "stdio.h"
  6. #include "usart.h"
  7. #include "adc.h"
  8. #include "oled.h"
  9. #include "ds18b20.h"
  10. #include "HX711.h"
  11. #define HwInput1 PBin(8) //红外传感器输入
  12. #define HwInput2 PBin(7) //红外传感器输入
  13. #define HwInput3 PBin(6) //红外传感器输入
  14. #define HwInput4 PBin(5) //红外传感器输入
  15. #define DY_Input PBin(14)
  16. #define KEY1 PAin(0) // 手动取消报警按钮
  17. char showbuff[16]={0};
  18. u8 warning_sta=0;//异常状态
  19. u8 warning_flag=0;//警告标志位
  20. int temp_val=0;
  21. u16 get_cnt=0;
  22. u16 ds_val=0;//滴速
  23. u16 sy_val=0;//剩余量
  24. void KEY_Init(void)
  25. {
  26. GPIO_InitTypeDef GPIO_InitStructure;
  27. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //使能端口时钟
  28. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // 端口配置
  29. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
  30. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
  31. GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化
  32. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6|GPIO_Pin_5|GPIO_Pin_14; // 端口配置
  33. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
  34. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
  35. GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化
  36. }
  37. u16 count=0;
  38. u16 Rpm_count=0;
  39. void exti_Init(void)
  40. {
  41. EXTI_InitTypeDef EXTI_InitStruct;
  42. NVIC_InitTypeDef NVIC_InitStruct;
  43. GPIO_InitTypeDef GPIO_InitStructure;
  44. // KEY_Init();//IO初始化
  45. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//配置外部中断时钟
  46. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOA时钟
  47. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//
  48. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置成输入
  49. GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOA8
  50. //io口与中断线映射
  51. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9);//链接到中断线8 GPIO8只能映射到中断线4上面
  52. EXTI_InitStruct.EXTI_Line=EXTI_Line9;//确定中断线
  53. EXTI_InitStruct.EXTI_LineCmd=ENABLE;//使能
  54. EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;//中断模式
  55. EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;//上降沿触发
  56. EXTI_Init(&EXTI_InitStruct);//初始化
  57. NVIC_InitStruct.NVIC_IRQChannel=EXTI9_5_IRQn;
  58. /*
  59. 需要明确的是,PxN管脚共用外部中断线EXTIN和外部中断向量EXTIN_IRQn
  60. 和中断服务程序入口EXTIN_IRQHandler,但是需要注意的是[9...5]
  61. 共用EXTI9_5_IRQn和EXTI9_5_IRQHandler、[15...10]共用EXTI15_10_IRQn和EXTI15_10_IRQHandler
  62. */
  63. NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;//使能
  64. NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;//中断抢优先级
  65. NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
  66. NVIC_Init(&NVIC_InitStruct);//初始化
  67. }
  68. void EXTI9_5_IRQHandler(void)
  69. {
  70. if(EXTI_GetITStatus(EXTI_Line9)!=RESET)
  71. {
  72. delay_ms(2);
  73. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==1)
  74. {
  75. count++;
  76. // ds_val=count;//获取滴速
  77. //OLED_ShowString(0,6,"324",16);
  78. }
  79. EXTI_ClearITPendingBit(EXTI_Line9);//清除中断 标志
  80. }
  81. }
  82. //通用定时器4中断初始化
  83. //这里时钟选择为APB1的2倍,而APB1为36M
  84. //arr:自动重装值。
  85. //psc:时钟预分频数
  86. //这里使用的是定时器2!
  87. void TIM2_Int_Init(u16 arr,u16 psc)
  88. {
  89. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  90. NVIC_InitTypeDef NVIC_InitStructure;
  91. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
  92. //定时器TIM4初始化
  93. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  94. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  95. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
  96. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  97. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
  98. TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE ); //使能指定的TIM4中断,允许更新中断
  99. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断分组 2
  100. //中断优先级NVIC设置
  101. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM6中断
  102. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
  103. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
  104. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  105. NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
  106. TIM_Cmd(TIM2, ENABLE); //使能TIMx
  107. }
  108. u8 sy_warning_flag1=0;
  109. u8 sy_warning_flag2=0;
  110. void TIM2_IRQHandler()
  111. {
  112. static u8 st;
  113. static u16 pw=0;
  114. st=TIM_GetFlagStatus(TIM2, TIM_IT_Update);
  115. if(st!=0)
  116. {
  117. TIM_ClearFlag(TIM2, TIM_IT_Update);
  118. if(DY_Input==1)
  119. {
  120. if(++pw>=100*10)//10s时间到
  121. {
  122. pw=0;
  123. ds_val=count*6;//获取滴速
  124. count=0;
  125. if(ds_val>70||ds_val<10)//滴速 通常成人每分钟40-60滴,儿童每分钟20-40滴。
  126. {
  127. warning_flag=1;
  128. }
  129. }
  130. }
  131. if(HwInput1==1&&HwInput2==1&&HwInput3==1&&HwInput4==1)
  132. {
  133. sy_val=150;
  134. sy_warning_flag1=0;sy_warning_flag2=0;//清除提醒锁定标志位
  135. }
  136. else if(HwInput2==1&&HwInput3==1&&HwInput4==1)
  137. {
  138. sy_val=100;
  139. sy_warning_flag1=0;sy_warning_flag2=0;//清除提醒锁定标志位
  140. }
  141. else if(HwInput3==1&&HwInput4==1)
  142. {
  143. sy_val=50;
  144. }
  145. else if(HwInput4==1)
  146. {
  147. sy_val=10;
  148. }
  149. else
  150. {
  151. sy_val=0;
  152. }
  153. get_cnt++;
  154. }
  155. }
  156. int main(void)
  157. {
  158. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  159. delay_init(); //延时函数初始化
  160. LED_Init(); //初始化与LED连接的硬件接口
  161. uart_init(9600);
  162. delay_ms(500);
  163. OLED_Init();
  164. OLED_Clear();
  165. DS18B20_Init();
  166. sprintf(showbuff,"温度:%2d.%1d",temp_val/10,temp_val%10); //打印成字符串
  167. OLED_ShowText(0,0,showbuff,0); //显示
  168. sprintf(showbuff,"流速:%3d/min",ds_val); //打印成字符串
  169. OLED_ShowText(0,2,showbuff,0); //显示
  170. sprintf(showbuff,"剩余量:%3d/ml",sy_val); //打印成字符串
  171. OLED_ShowText(0,4,showbuff,0); //显示
  172. TIM2_Int_Init(71,10000) ; // 开启定时器4 10ms
  173. buzz=1;
  174. exti_Init();
  175. KEY_Init();
  176. while(1)
  177. {
  178. if(get_cnt>100) //采集时间计数器
  179. {
  180. get_cnt=0;
  181. temp_val=DS18B20_Get_Temp();
  182. sprintf(showbuff,"温度:%2d.%1d",temp_val/10,temp_val%10); //打印成字符串
  183. OLED_ShowText(0,0,showbuff,0); //显示
  184. sprintf(showbuff,"流速:%3d/min",ds_val); //打印成字符串
  185. OLED_ShowText(0,2,showbuff,0); //显示
  186. sprintf(showbuff,"剩余量:%3d/ml",sy_val); //打印成字符串
  187. OLED_ShowText(0,4,showbuff,0); //显示 0); //打印成字符串
  188. if(sy_val==10&&sy_warning_flag1==0)//到10ml提醒一次
  189. {
  190. warning_flag=1;
  191. sy_warning_flag1=1;
  192. }
  193. if(sy_val==0&&sy_warning_flag2==0)//到0ml提醒一次
  194. {
  195. warning_flag=1;
  196. sy_warning_flag2=1;
  197. }
  198. printf("SZZ152_X%2d.%1dH%dD%dZ%dK\r\n",temp_val/10,temp_val%10,ds_val,sy_val,warning_flag);//串口发送给wifi模块
  199. if(warning_flag)
  200. {
  201. buzz=!buzz;//声光报警触发
  202. }else
  203. {
  204. buzz=1;
  205. }
  206. }
  207. if(KEY1==0)
  208. {
  209. delay_ms(5);//延时
  210. if(KEY1==0)
  211. {
  212. while(!KEY1);
  213. warning_flag=0;
  214. }
  215. }
  216. delay_ms(5);//延时
  217. }
  218. }