main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 HwInput PBin(7) //红外传感器输入
  12. #define KEY1 PAin(0) // 手动控制加热
  13. #define JR_OUT_PIN PBout(12) //控制加热输出
  14. #define CS_OUT_PIN PBout(13) //控制出水 电磁阀
  15. void Hw_Init(void)
  16. {
  17. GPIO_InitTypeDef GPIO_InitStructure;
  18. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能端口时钟
  19. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; // 端口配置
  20. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //上拉输入
  21. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
  22. GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOC13
  23. }
  24. #define Trig PBout(10) // 超声波测距
  25. #define CW_ECHO1 PBin(11)
  26. u16 Distance=0;
  27. //超声波端口初始化
  28. void Wave_IO_Init(void)
  29. {
  30. GPIO_InitTypeDef GPIO_InitStructure;
  31. NVIC_InitTypeDef NVIC_InitStructure;
  32. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  33. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
  34. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB , ENABLE);
  35. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB0接TRIG
  36. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设为推挽输出模式
  37. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38. GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化外设GPIO
  39. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //接ECH0
  40. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设为输入
  41. GPIO_Init(GPIOB, &GPIO_InitStructure);
  42. TIM_DeInit(TIM2);
  43. TIM_TimeBaseStructure.TIM_Period=65535; /* 自动重装载寄存器周期的值(计数值) */
  44. /* 累计 TIM_Period个频率后产生一个更新或者中断 */
  45. TIM_TimeBaseStructure.TIM_Prescaler= (360 - 1); /* 时钟预分频数 72M/360 */
  46. TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; /* 采样分频 */
  47. TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; /* 向上计数模式 */
  48. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  49. TIM_ClearFlag(TIM2, TIM_FLAG_Update); /* 清除溢出中断标志 */
  50. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  51. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  52. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  53. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  54. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  55. NVIC_Init(&NVIC_InitStructure);
  56. //TIM2_NVIC_Configuration();//定时器初始化
  57. //TIM2_Configuration();
  58. }
  59. //void TIM2_IRQHandler()
  60. //{
  61. // static u8 st;
  62. // st=TIM_GetFlagStatus(TIM2, TIM_IT_Update);
  63. // if(st!=0)
  64. // {
  65. // TIM_ClearFlag(TIM2, TIM_IT_Update);
  66. // }
  67. //}
  68. //测距
  69. //开始测距,发送一个>10us的脉冲,然后测量返回的高电平时间
  70. u16 Wave_Start(void)
  71. {
  72. u32 Distance;
  73. u32 timeout=0;
  74. Trig=1;
  75. delay_us(20); //输入一个10us的高电平
  76. Trig=0;
  77. TIM_SetCounter(TIM2,0);
  78. timeout=0;
  79. while(!CW_ECHO1) //等待高电平
  80. {
  81. if(++timeout>60000)break;
  82. delay_us(1);
  83. }
  84. TIM_Cmd(TIM2, ENABLE); //开启时钟
  85. timeout=0;
  86. while(CW_ECHO1) //等待低电平
  87. {
  88. if(++timeout>600000)break;
  89. delay_us(1);
  90. }
  91. TIM_Cmd(TIM2, DISABLE); //定时器2失能
  92. Distance=TIM_GetCounter(TIM2)*5*34/2000; //计算距离 厘米cm
  93. TIM_SetCounter(TIM2,0);
  94. if(Distance>200)Distance=200;
  95. return Distance;
  96. }
  97. u8 jr_flag=0;
  98. u8 get_cnt=0;
  99. u8 hw_cnt=0;
  100. u8 cs_out_flag=0; //出水标志
  101. u16 sw_val=0;//水位值
  102. u8 cs_out_zs_flag=0;//出水自锁标志位
  103. char showbuff[16];
  104. short temp_val=0;
  105. u8 mode_flag=0;
  106. int main(void)
  107. {
  108. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  109. delay_init(); //延时函数初始化
  110. LED_Init(); //初始化与LED连接的硬件接口
  111. uart_init(9600);
  112. ADC1_Init();
  113. OLED_Init();
  114. OLED_Clear();
  115. Hw_Init();
  116. DS18B20_Init();
  117. Wave_IO_Init();
  118. while(1)
  119. {
  120. if(++get_cnt>100) //采集时间计数器
  121. {
  122. get_cnt=0;
  123. temp_val = DS18B20_Get_Temp();//采集温度值
  124. sprintf(showbuff,"temp:%2d.%1d",temp_val/10,temp_val%10); //打印成字符串
  125. OLED_ShowString(0,2,(u8*)showbuff,16); //显示
  126. }
  127. if(mode_flag) //自动模式
  128. {
  129. if(HwInput==0) //检测是否有杯子
  130. {
  131. if(++hw_cnt>20) //计数100ms
  132. {
  133. sw_val = Wave_Start();
  134. if(sw_val>4)
  135. {
  136. CS_OUT_PIN=0;//控制出水
  137. }else
  138. {
  139. CS_OUT_PIN=1;//停止输出
  140. }
  141. cs_out_flag=1;
  142. }
  143. }else
  144. {
  145. hw_cnt=0;
  146. cs_out_flag=0;
  147. CS_OUT_PIN=1;//停止输出
  148. }
  149. if(jr_flag) //加热开关flag
  150. {
  151. JR_OUT_PIN=0;
  152. OLED_ShowString(0,4,(u8*)"heater:on ",16); //显示
  153. }else
  154. {
  155. JR_OUT_PIN=1;
  156. OLED_ShowString(0,4,(u8*)"heater:off",16); //显示
  157. }
  158. }else //手动模式
  159. {
  160. if(KEY1==0) //按键检测
  161. {
  162. delay_ms(5);
  163. if(KEY1==0)
  164. {
  165. jr_flag^=1; //控制加热
  166. while(!KEY1); //等待松手
  167. }
  168. }
  169. }
  170. delay_ms(5);//延时
  171. }
  172. }