[AT32F405] 【AT-START-F405测评】+自动补水加湿器

[复制链接]
 楼主| suncat0504 发表于 2024-5-8 12:02 | 显示全部楼层 |阅读模式
本帖最后由 suncat0504 于 2024-5-9 08:48 编辑

在完成显示部分和温湿度传感器部分的工作后,追加水位检测传感器和抽水马达、加湿器模块。整个装置如下:
图片1.png
水位检测和加湿器部分,都是现成的模块。抽水马达使用继电器模块和专用的驱动电源。因为抽水马达的工作电流比较大,不能使用开发板上的电源,所以单独为它准备了一个电源,是由9V电源通过降压得到3.7左右。开发板通过GPIO口控制继电器模块接通/关断抽水马达的供电,从而控制补水过程。水源由一个矿泉水桶提供,使用的水桶体积比较大,可以使用好长时间,不需要频繁换水。
水位传感器:
图片2.png
这是一个电容式的水位检测传感器,需要靠近水源即可。感应结果可以直接输入到GPIO口,通过电平信号,确定容器内是否有水。容器的厚度不能太大,否则传感器无法正常感应到是否有水。

整个装置的工作过程:
图片3.png
主处理程序:
  1. /**
  2.   **************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  4.   * [url=home.php?mod=space&uid=247401]@brief[/url]    main program
  5.   **************************************************************************
  6.   *                       Copyright notice & Disclaimer
  7.   *
  8.   * The software Board Support Package (BSP) that is made available to
  9.   * download from Artery official website is the copyrighted work of Artery.
  10.   * Artery authorizes customers to use, copy, and distribute the BSP
  11.   * software and its related documentation for the purpose of design and
  12.   * development in conjunction with Artery microcontrollers. Use of the
  13.   * software is governed by this copyright notice and the following disclaimer.
  14.   *
  15.   * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16.   * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17.   * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18.   * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19.   * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20.   * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21.   *
  22.   **************************************************************************
  23.   */

  24. /* includes */
  25. #include "at32f402_405_board.h"
  26. #include "at32f402_405_clock.h"
  27. #include "spitft.h"
  28. #include "gxht30.h"

  29. #define MYMAC_PORT GPIOB
  30. #define MYMAC_WATER_POS_PIN     GPIO_PINS_8           // 水位采集口,输入
  31. #define MYMAC_MOTOR_PIN         GPIO_PINS_9           // 抽水马达控制口,输出
  32. #define MYMAC_H_POWER_PIN       GPIO_PINS_7           // 加湿器电源

  33. // 启动加湿器时的湿度临界点
  34. #define WORK_HUMI  60

  35. // 每补水一次,隔多长时间再次见此是否缺水
  36. #define WORK_JIANXIE_SEC  -10

  37. // 每次补水的时间
  38. #define WORK_BUSHUI_SEC  4

  39. // 控制加湿器电源
  40. #define OPEN_H_POWER()          gpio_bits_set(MYMAC_PORT, MYMAC_H_POWER_PIN);
  41. #define CLOSE_H_POWER()         gpio_bits_reset(MYMAC_PORT, MYMAC_H_POWER_PIN);

  42. // 控制补水马达工作
  43. #define OPEN_MOTOR()            gpio_bits_set(MYMAC_PORT, MYMAC_MOTOR_PIN);
  44. #define CLOSE_MOTOR()           gpio_bits_reset(MYMAC_PORT, MYMAC_MOTOR_PIN);


  45. // 补水标志
  46. int32_t bushui_flag = 0;

  47. /** @addtogroup AT32F405_periph_examples
  48.   * @{
  49.   */

  50. /** @addtogroup 405_GPIO_led_toggle GPIO_led_toggle
  51.   * @{
  52.   */




  53. /**
  54. * 加湿器相关控制口初始化
  55. * PB8 - 水位传感器采集信号
  56. * PB9 - 抽水马达控制信号
  57. * PB7 - 控制加湿器的电源信号
  58. */
  59. void mymac_Init(void) {
  60.     gpio_init_type gpio_init_struct;

  61.     // 允许总线时钟
  62.     crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);

  63.     // 设置初始化缺省参数
  64.     gpio_default_para_init(&gpio_init_struct);

  65.     // 设置水位信号采集口,输入模式
  66.     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  67.     gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  68.     gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  69.     gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
  70.     gpio_init_struct.gpio_pins = MYMAC_WATER_POS_PIN;
  71.     gpio_init(MYMAC_PORT, &gpio_init_struct);
  72.    
  73.   
  74.     // 抽水马达控制口,输出模式; 加湿器电源,输出模式
  75.     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  76.     gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  77.     gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  78.     gpio_init_struct.gpio_pins = MYMAC_MOTOR_PIN | MYMAC_H_POWER_PIN;
  79.     gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  80.     gpio_init(MYMAC_PORT, &gpio_init_struct);  
  81.    
  82.    
  83. }





  84. /**
  85.   * [url=home.php?mod=space&uid=247401]@brief[/url]  main function.
  86.   * @param  none
  87.   * @retval none
  88.   */
  89. int main(void) {
  90.     float         temperature;        // temperature [°C]
  91.     float        humidity;                // relative humidity [%RH]
  92.     char val[32]={'\0'};
  93.     etError   error;        // error code
  94.    
  95.     system_clock_config();

  96.     at32_board_init();
  97.     uart_print_init(115200);
  98.    

  99.     printf("Start main ...\r\n");
  100.    
  101.     // 初始化TFT显示屏
  102.     Lcd_Init();
  103.    
  104.     // 测试
  105.     Lcd_Clear(BLACK);
  106.     Gui_DrawFont_GBK16(8,10,WHITE, BLACK, (unsigned char *)"Artery & 21IC");
  107.     Gui_DrawFont_GBK16(30,36,WHITE, BLACK, (unsigned char *)"AT32F405");   

  108.     Gui_DrawFont_GBK16(0, 60,WHITE, BLACK, (unsigned char *)"初始化:");
  109.    
  110.     Gui_DrawFont_GBK16(10, 76,WHITE, BLACK, (unsigned char *)"水位控制接口");   
  111.     // 初始化水位传感器、抽水马达控制口
  112.     at32_led_on(LED2);
  113.     mymac_Init();
  114.     Gui_DrawFont_GBK16(10, 92,WHITE, BLACK, (unsigned char *)"OK");   
  115.    
  116.     at32_led_off(LED2);
  117.     at32_led_on(LED3);
  118.     // 初始化温湿度传感器GXHT-30
  119.     Gui_DrawFont_GBK16(10, 76,WHITE, BLACK, (unsigned char *)"温度传感器");
  120.     gxht30_Init(GXHT30_ADDRESS);
  121.     Gui_DrawFont_GBK16(10, 92,WHITE, BLACK, (unsigned char *)"OK");

  122.     Gui_DrawFont_GBK16(0, 60, WHITE, BLACK, (unsigned char *)"            ");
  123.     Gui_DrawFont_GBK16(10, 76,WHITE, BLACK, (unsigned char *)"            ");
  124.     Gui_DrawFont_GBK16(10, 92,WHITE, BLACK, (unsigned char *)"            ");
  125.    
  126.     at32_led_off(LED3);
  127.     Gui_DrawFont_GBK16(0, 110, WHITE, BLACK, (unsigned char *)"温度");
  128.     Gui_DrawFont_GBK16(32,110, WHITE, BLACK, (unsigned char *)"=");
  129.     Gui_DrawFont_GBK16(0, 126,WHITE, BLACK, (unsigned char *)"湿度");
  130.     Gui_DrawFont_GBK16(32,126,WHITE, BLACK, (unsigned char *)"=");
  131.     while(1) {
  132.         //at32_led_toggle(LED2);
  133.         //delay_ms(200);
  134.         //at32_led_toggle(LED3);
  135.         //delay_ms(200);
  136.         //at32_led_toggle(LED4);
  137.         //delay_ms(200);
  138.         
  139.         // 测量温湿度,方法2:OK
  140.         SHT30_read_result(GXHT30_ADDRESS, &temperature, &humidity);
  141.         
  142.         sprintf(val, "%5.1f", temperature);
  143.         Gui_DrawFont_GBK16(48,110,WHITE, BLACK, (unsigned char *)val);

  144.         sprintf(val, "%5.1f", humidity);
  145.         Gui_DrawFont_GBK16(48,126, WHITE, BLACK, (unsigned char *)val);
  146.         
  147.         // 湿度在60以上时,加湿器不工作
  148.         if (humidity >= 60) {
  149.             CLOSE_H_POWER();
  150.             continue;
  151.         } else {
  152.             // 启动加湿器
  153.             OPEN_H_POWER();
  154.         }
  155.         
  156.         // 检查水位
  157.         if (gpio_input_data_bit_read(MYMAC_PORT, MYMAC_WATER_POS_PIN)) {
  158.             Gui_DrawFont_GBK16(0, 60, WHITE, BLACK, (unsigned char *)"            ");
  159.             //gpio_bits_reset(GXHT30_PORT, MYMAC_MOTOR_PIN);
  160.         } else {
  161.             // 水量不足时
  162.             Gui_DrawFont_GBK16(0, 60, WHITE, BLACK, (unsigned char *)"水量不足    ");
  163.             // 设置加水标志
  164.             if (bushui_flag==0) {
  165.                 bushui_flag = 1;
  166.             }
  167.         }
  168.         if (bushui_flag==1) {
  169.             // 启动补水马达
  170.             OPEN_MOTOR();
  171.             Gui_DrawFont_GBK16(0, 76, WHITE, BLACK, (unsigned char *)"开始补水    ");
  172.         }

  173.         if (bushui_flag>0) {
  174.             sprintf(val, "开始补水:%d", bushui_flag);
  175.             Gui_DrawFont_GBK16(0,76, WHITE, BLACK, (unsigned char *)val);
  176.             
  177.             bushui_flag++;
  178.             if (bushui_flag > WORK_BUSHUI_SEC) {
  179.                 // 补水3秒后,关闭补水马达,清除补水标志
  180.                 CLOSE_MOTOR();
  181.                 bushui_flag = WORK_JIANXIE_SEC;
  182.                 Gui_DrawFont_GBK16(0, 76, WHITE, BLACK, (unsigned char *)"补水完成    ");
  183.             }
  184.         }
  185.         
  186.         if (bushui_flag<0) {
  187.             bushui_flag++;
  188.             sprintf(val, "间歇期:%d", 0-bushui_flag);
  189.             Gui_DrawFont_GBK16(0,76, WHITE, BLACK, (unsigned char *)val);
  190.         }

  191.         
  192.         delay_ms(1000);
  193.     }
  194. }

  195. /**
  196.   * @}
  197.   */

  198. /**
  199.   * @}
  200.   */
work.gif
启动抽水马达,开始补水
w1_d.gif
[鑫森淼焱垚] 发表于 2024-5-8 20:11 | 显示全部楼层
牛哇,点赞。。。。
chenqianqian 发表于 2024-5-8 20:59 来自手机 | 显示全部楼层
完整的最小模拟系统,不错啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:大连伊飞特信息技术有限公司软件工程师
简介:本人于1993年毕业于大连理工大学。毕业后从事单片机开发工作5年,之后转入软件开发工作至今。

158

主题

4504

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部