- /**
- **************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=247401]@brief[/url] main program
- **************************************************************************
- * Copyright notice & Disclaimer
- *
- * The software Board Support Package (BSP) that is made available to
- * download from Artery official website is the copyrighted work of Artery.
- * Artery authorizes customers to use, copy, and distribute the BSP
- * software and its related documentation for the purpose of design and
- * development in conjunction with Artery microcontrollers. Use of the
- * software is governed by this copyright notice and the following disclaimer.
- *
- * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
- * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
- * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
- * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
- * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
- *
- **************************************************************************
- */
- /* includes */
- #include "at32f402_405_board.h"
- #include "at32f402_405_clock.h"
- #include "spitft.h"
- #include "gxht30.h"
- #define MYMAC_PORT GPIOB
- #define MYMAC_WATER_POS_PIN GPIO_PINS_8 // 水位采集口,输入
- #define MYMAC_MOTOR_PIN GPIO_PINS_9 // 抽水马达控制口,输出
- #define MYMAC_H_POWER_PIN GPIO_PINS_7 // 加湿器电源
- // 启动加湿器时的湿度临界点
- #define WORK_HUMI 60
- // 每补水一次,隔多长时间再次见此是否缺水
- #define WORK_JIANXIE_SEC -10
- // 每次补水的时间
- #define WORK_BUSHUI_SEC 4
- // 控制加湿器电源
- #define OPEN_H_POWER() gpio_bits_set(MYMAC_PORT, MYMAC_H_POWER_PIN);
- #define CLOSE_H_POWER() gpio_bits_reset(MYMAC_PORT, MYMAC_H_POWER_PIN);
- // 控制补水马达工作
- #define OPEN_MOTOR() gpio_bits_set(MYMAC_PORT, MYMAC_MOTOR_PIN);
- #define CLOSE_MOTOR() gpio_bits_reset(MYMAC_PORT, MYMAC_MOTOR_PIN);
- // 补水标志
- int32_t bushui_flag = 0;
- /** @addtogroup AT32F405_periph_examples
- * @{
- */
- /** @addtogroup 405_GPIO_led_toggle GPIO_led_toggle
- * @{
- */
- /**
- * 加湿器相关控制口初始化
- * PB8 - 水位传感器采集信号
- * PB9 - 抽水马达控制信号
- * PB7 - 控制加湿器的电源信号
- */
- void mymac_Init(void) {
- gpio_init_type gpio_init_struct;
- // 允许总线时钟
- crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
- // 设置初始化缺省参数
- gpio_default_para_init(&gpio_init_struct);
- // 设置水位信号采集口,输入模式
- gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
- gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
- gpio_init_struct.gpio_pins = MYMAC_WATER_POS_PIN;
- gpio_init(MYMAC_PORT, &gpio_init_struct);
-
-
- // 抽水马达控制口,输出模式; 加湿器电源,输出模式
- gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
- gpio_init_struct.gpio_pins = MYMAC_MOTOR_PIN | MYMAC_H_POWER_PIN;
- gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
- gpio_init(MYMAC_PORT, &gpio_init_struct);
-
-
- }
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] main function.
- * @param none
- * @retval none
- */
- int main(void) {
- float temperature; // temperature [°C]
- float humidity; // relative humidity [%RH]
- char val[32]={'\0'};
- etError error; // error code
-
- system_clock_config();
- at32_board_init();
- uart_print_init(115200);
-
- printf("Start main ...\r\n");
-
- // 初始化TFT显示屏
- Lcd_Init();
-
- // 测试
- Lcd_Clear(BLACK);
- Gui_DrawFont_GBK16(8,10,WHITE, BLACK, (unsigned char *)"Artery & 21IC");
- Gui_DrawFont_GBK16(30,36,WHITE, BLACK, (unsigned char *)"AT32F405");
- Gui_DrawFont_GBK16(0, 60,WHITE, BLACK, (unsigned char *)"初始化:");
-
- Gui_DrawFont_GBK16(10, 76,WHITE, BLACK, (unsigned char *)"水位控制接口");
- // 初始化水位传感器、抽水马达控制口
- at32_led_on(LED2);
- mymac_Init();
- Gui_DrawFont_GBK16(10, 92,WHITE, BLACK, (unsigned char *)"OK");
-
- at32_led_off(LED2);
- at32_led_on(LED3);
- // 初始化温湿度传感器GXHT-30
- Gui_DrawFont_GBK16(10, 76,WHITE, BLACK, (unsigned char *)"温度传感器");
- gxht30_Init(GXHT30_ADDRESS);
- Gui_DrawFont_GBK16(10, 92,WHITE, BLACK, (unsigned char *)"OK");
- Gui_DrawFont_GBK16(0, 60, WHITE, BLACK, (unsigned char *)" ");
- Gui_DrawFont_GBK16(10, 76,WHITE, BLACK, (unsigned char *)" ");
- Gui_DrawFont_GBK16(10, 92,WHITE, BLACK, (unsigned char *)" ");
-
- at32_led_off(LED3);
- Gui_DrawFont_GBK16(0, 110, WHITE, BLACK, (unsigned char *)"温度");
- Gui_DrawFont_GBK16(32,110, WHITE, BLACK, (unsigned char *)"=");
- Gui_DrawFont_GBK16(0, 126,WHITE, BLACK, (unsigned char *)"湿度");
- Gui_DrawFont_GBK16(32,126,WHITE, BLACK, (unsigned char *)"=");
- while(1) {
- //at32_led_toggle(LED2);
- //delay_ms(200);
- //at32_led_toggle(LED3);
- //delay_ms(200);
- //at32_led_toggle(LED4);
- //delay_ms(200);
-
- // 测量温湿度,方法2:OK
- SHT30_read_result(GXHT30_ADDRESS, &temperature, &humidity);
-
- sprintf(val, "%5.1f", temperature);
- Gui_DrawFont_GBK16(48,110,WHITE, BLACK, (unsigned char *)val);
- sprintf(val, "%5.1f", humidity);
- Gui_DrawFont_GBK16(48,126, WHITE, BLACK, (unsigned char *)val);
-
- // 湿度在60以上时,加湿器不工作
- if (humidity >= 60) {
- CLOSE_H_POWER();
- continue;
- } else {
- // 启动加湿器
- OPEN_H_POWER();
- }
-
- // 检查水位
- if (gpio_input_data_bit_read(MYMAC_PORT, MYMAC_WATER_POS_PIN)) {
- Gui_DrawFont_GBK16(0, 60, WHITE, BLACK, (unsigned char *)" ");
- //gpio_bits_reset(GXHT30_PORT, MYMAC_MOTOR_PIN);
- } else {
- // 水量不足时
- Gui_DrawFont_GBK16(0, 60, WHITE, BLACK, (unsigned char *)"水量不足 ");
- // 设置加水标志
- if (bushui_flag==0) {
- bushui_flag = 1;
- }
- }
- if (bushui_flag==1) {
- // 启动补水马达
- OPEN_MOTOR();
- Gui_DrawFont_GBK16(0, 76, WHITE, BLACK, (unsigned char *)"开始补水 ");
- }
- if (bushui_flag>0) {
- sprintf(val, "开始补水:%d", bushui_flag);
- Gui_DrawFont_GBK16(0,76, WHITE, BLACK, (unsigned char *)val);
-
- bushui_flag++;
- if (bushui_flag > WORK_BUSHUI_SEC) {
- // 补水3秒后,关闭补水马达,清除补水标志
- CLOSE_MOTOR();
- bushui_flag = WORK_JIANXIE_SEC;
- Gui_DrawFont_GBK16(0, 76, WHITE, BLACK, (unsigned char *)"补水完成 ");
- }
- }
-
- if (bushui_flag<0) {
- bushui_flag++;
- sprintf(val, "间歇期:%d", 0-bushui_flag);
- Gui_DrawFont_GBK16(0,76, WHITE, BLACK, (unsigned char *)val);
- }
-
- delay_ms(1000);
- }
- }
- /**
- * @}
- */
- /**
- * @}
- */
启动抽水马达,开始补水