STM32 Project Sharing: Intelligent Fruit and Vegetable Preservation System

STM32 Project Sharing: Intelligent Fruit and Vegetable Preservation SystemSTM32 Project Sharing: Intelligent Fruit and Vegetable Preservation System

Bilibili Video Link:

https://www.bilibili.com/video/BV1wB5XzuEQT/?share_source=copy_web&vd_source=097fdeaf6b6ecfed8a9ff7119c32faf2

01

Project Overview

1. Function Details

STM32 Intelligent Fruit and Vegetable Preservation System

Functions are as follows:

  1. STM32 microcontroller as the main control chip

  2. Environmental monitoring: monitoring temperature and humidity, carbon dioxide concentration

  3. Display function: displays current environmental parameters and mode information via OLED

  4. Control function: the system can control the fan, humidifier, and ventilation (simulated by stepper motor)

  5. Manual mode: peripherals can be controlled via a mobile app or buttons

  6. Automatic mode: fan turns on when temperature exceeds threshold, turns off when below threshold. Humidifier turns on when humidity is below threshold, turns off when above threshold, ventilation opens when CO2 concentration exceeds threshold, closes when below threshold

  7. Alarm function: buzzer alarm when parameters exceed thresholds

  8. APP control: use the Gizwits Cloud APP to view information and issue control commands

2. Bill of Materials

  • STM32F103C8T6 microcontroller

  • OLED screen

  • DHT11 temperature and humidity sensor

  • JW01 carbon dioxide sensor

  • Relay

  • ESP8266 module (WIFI)

  • Fan module

  • Humidifier module

  • Stepper motor

02

Schematic Design

STM32 Project Sharing: Intelligent Fruit and Vegetable Preservation System

03

PCB Hardware Design

PCB DiagramSTM32 Project Sharing: Intelligent Fruit and Vegetable Preservation SystemSTM32 Project Sharing: Intelligent Fruit and Vegetable Preservation System

04

Program Design

/* Includes ------------------------------------------------------------------*/#include "main.h"#include "stm32f1xx_hal.h"/* USER CODE BEGIN Includes */#include "hal_key.h"#include "gizwits_product.h"#include "common.h"#include "dht11.h"#include "OLED.h"#include "motor.h"/* USER CODE END Includes *//* Private variables ---------------------------------------------------------*/TIM_HandleTypeDef htim2;UART_HandleTypeDef huart1;UART_HandleTypeDef huart2;UART_HandleTypeDef huart3;DMA_HandleTypeDef hdma_usart3_rx;DMA_HandleTypeDef hdma_usart3_tx;/* USER CODE BEGIN PV *//* Private variables ---------------------------------------------------------*/char timerFlag_1ms = 0;char timerFlag_10ms = 0;char timerFlag_1000ms = 0; char Mode = 1 ; uint8_t Temp , Humi ; char Set_Mode = 0; uint8_t Temp_HIGHT=30,Temp_LOW=20; uint8_t Humi_HIGHT=80,Humi_LOW=40; uint16_t CO2_HIGHT =400; uint16_t CO2_x  =  0; char Flay_num = 2; char FLay_num_last =0  ; u8 motor_flag;/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_DMA_Init(void);static void MX_TIM2_Init(void);static void MX_USART1_UART_Init(void);static void MX_USART2_UART_Init(void);static void MX_USART3_UART_Init(void);static void MX_NVIC_Init(void);/* USER CODE BEGIN PFP *//* Private function prototypes -----------------------------------------------*/void softwareTimer(void);void openBuzzerimer(int time);void usart3(void);char KEY_scanf(void);void GET_DATA(void){   DHT11_Read_Data(&Temp,&Humi);   usart3(); }void Meun_Show(void){    //Display system name    OLED_ShowChinese(1, 1, 0);OLED_ShowChinese(1, 2, 1);OLED_ShowChinese(1, 3, 2);OLED_ShowChinese(1, 4, 3);    OLED_ShowChinese(1, 5, 4);OLED_ShowChinese(1, 6, 5);OLED_ShowChinese(1, 7, 6);OLED_ShowChinese(1, 8, 7);//Display "System Mode:"OLED_ShowChinese(2, 1, 6);OLED_ShowChinese(2, 2, 7);OLED_ShowChinese(2, 3, 19);OLED_ShowChinese(2, 4, 20);    OLED_ShowChar(2, 9, ':');    //Display "Temperature:  C"OLED_ShowChinese(3,1,8);OLED_ShowChinese(3,2,9); OLED_ShowChinese(3,5,10);OLED_ShowChinese(3,6,9);    //Display "Carbon Dioxide:"    OLED_ShowChinese(4, 1, 24);OLED_ShowChinese(4, 2, 25);OLED_ShowChinese(4, 3, 26);OLED_ShowChinese(4, 4, 27);    WS_OLED_Printf(4,9,":%dppm",(CO2_x));    WS_OLED_Printf(3,5,":%dC",Temp);    WS_OLED_Printf(3,13,":%d%%",Humi);     if(Mode == 2)    {        OLED_ShowChinese(2, 6, 21);OLED_ShowChinese(2, 7, 22);    }    else if(Mode == 1)    {        OLED_ShowChinese(2, 6, 23);OLED_ShowChinese(2, 7, 22);     }}void Vol_Mode(void){    if(Temp>Temp_HIGHT)    {        //Turn on fan        HAL_GPIO_WritePin(Feng_GPIO_Port,Feng_Pin,GPIO_PIN_SET);    }    if(Temp<Temp_LOW)    {        //Turn off fan        HAL_GPIO_WritePin(Feng_GPIO_Port,Feng_Pin,GPIO_PIN_RESET);    }    if(Humi>Humi_HIGHT)    {         //Turn off humidifier         HAL_GPIO_WritePin(JIA_GPIO_Port,JIA_Pin,GPIO_PIN_RESET);    }    if(Humi<Humi_LOW)    {       //Turn on humidifier         HAL_GPIO_WritePin(JIA_GPIO_Port,JIA_Pin,GPIO_PIN_SET);    }    if(CO2_x>CO2_HIGHT)    {        //Open ventilation if(motor_flag==0)            {       Stepper_Move(1,512);                motor_flag=1;            }    }    else{        //Close ventilation if(motor_flag==1)            {        Stepper_Move(0,512);                motor_flag=0;            }    }}void Set_show(void){    //Interface title    OLED_ShowChinese(1, 1, 17);OLED_ShowChinese(1, 2, 18);OLED_ShowChinese(1, 3, 30);OLED_ShowChinese(1, 4, 31);    OLED_ShowChinese(1, 5, 32);OLED_ShowChinese(1, 6, 33);OLED_ShowChinese(1, 7, 34);OLED_ShowChinese(1, 8, 35);}void Temp_set_show(void){OLED_ShowString(2, 3, "  ");    //Display "Temperature Upper Limit:"OLED_ShowChinese(2, 3, 8);OLED_ShowChinese(2, 4, 9);OLED_ShowChinese(2, 5, 36);OLED_ShowChinese(2, 6, 37);    OLED_ShowChar(2, 11, ':');//Display "Temperature Lower Limit:"OLED_ShowChinese(3, 3, 8);OLED_ShowChinese(3, 4, 9);OLED_ShowChinese(3, 5, 38);OLED_ShowChinese(3, 6, 39);    OLED_ShowChar(3, 11, ':'); }void Humi_set_show(void){    //Display "Humidity Upper Limit:"OLED_ShowChinese(2, 3 ,10);OLED_ShowChinese(2, 4, 9);OLED_ShowChinese(2, 5, 36);OLED_ShowChinese(2, 6, 37);    OLED_ShowChar(2, 11, ':');//Display "Humidity Lower Limit:"OLED_ShowChinese(3, 3, 10);OLED_ShowChinese(3, 4, 9);OLED_ShowChinese(3, 5, 38);OLED_ShowChinese(3, 6, 39);    OLED_ShowChar(3, 11, ':');}void CO2_set_show(void){       //Display "Carbon Dioxide:"OLED_ShowChinese(2, 2, 24);OLED_ShowChinese(2, 3, 25);OLED_ShowChinese(2, 4, 26);OLED_ShowChinese(2, 5, 27);OLED_ShowChar(2, 11, ':');       }/*****Carbon Dioxide Reading******///void JIAN_call (unsigned char *buffer)//{//   if (buffer[0] == 0x2C)//   {//       CO2_x= buffer[1]+buffer[2]*256;//   }//}void usart3(void){    static char  RxState = 0 ;if (USART3_RX_STA == 1)    {        USART3_RX_STA = 0;         /*            HAL_UART_Transmit_IT(&huart3,u3NewBuffer,sizeof u3NewBuffer);   Serial port reception debug port            memset(u3NewBuffer, '\0', strlen((char *)u3NewBuffer)); // Clear data            When debugging the serial port, please close the clear function, otherwise the sent data will have problems            Usage:            1. Connect the serial port to be tested to the computer via USB-TTL and open the serial assistant bps9600.            2. In the uart3() function of serial port 3, open HAL_UART_Transmit_IT comment to clear data.            3. The computer sends data and checks whether the received data is complete.        */       switch(RxState)       {           case 0:               if(u3NewBuffer[0]  ==0x2c)//Header frame verification               {                   RxState = 1;//                    WS_OLED_Printf(1,1,"CO2");               }               else               {                   memset(u3NewBuffer, '\0', strlen((char *)u3NewBuffer)); // Clear data               }               break;           case 1:               if(u3NewBuffer[5] == (uint8_t)(u3NewBuffer[0] + u3NewBuffer[1]+ u3NewBuffer[2] + u3NewBuffer[3] + u3NewBuffer[4]))    //Verify if the received data is correct               {                  RxState = 0;                  CO2_x = u3NewBuffer[1]*256+u3NewBuffer[2];               }                else               {                   RxState = 0;               }       }    }}/* USER CODE END PFP *//* USER CODE BEGIN 0 *//* USER CODE END 0 */int main(void){  /* USER CODE BEGIN 1 */    char  key_NUM  = 0 ;    static char operation = 0 ;    static char flay=0,flay_1=0,flay_2=0;  /* USER CODE END 1 */  /* MCU Configuration----------------------------------------------------------*/  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */  HAL_Init();  /* USER CODE BEGIN Init */  /* USER CODE END Init */  /* Configure the system clock */  SystemClock_Config();  /* USER CODE BEGIN SysInit */  /* USER CODE END SysInit */  /* Initialize all configured peripherals */  MX_GPIO_Init();  MX_DMA_Init();  MX_TIM2_Init();  MX_USART1_UART_Init();  MX_USART2_UART_Init();  MX_USART3_UART_Init();  /* Initialize interrupts */  MX_NVIC_Init();  /* USER CODE BEGIN 2 */    timerInit();    uartInit();    userInit();    gizwitsInit();    OLED_Init();    OLED_Clear();    CZL_USART_UART_Init(huart3);//uart3 serial port initialization, enable DMA reception    gizwitsSetMode(WIFI_AIRLINK_MODE);    GIZWITS_LOG("MCU Init Success , SoftVersion = %s\r\n",SOFTWARE_VERSION);  /* USER CODE END 2 */  /* Infinite loop */  /* USER CODE BEGIN WHILE */    while (1)    {          key_NUM = KEY_scanf();               // Data reading          if(timerFlag_1ms  != 0 )          {                timerFlag_1ms = 0;                  GET_DATA();if(Mode == 1)//Automatic mode                    {Vol_Mode(); // Power on defaults to automatic mode                    }softwareTimer();          }      /******Do not remove this delay, it buffers data!!!***********/          if(timerFlag_10ms  != 0)          {              timerFlag_10ms  =  0;          }           //Refresh screen           if(timerFlag_1000ms != 0)           {                timerFlag_1000ms = 0;                if(Mode < 3)                {                    Meun_Show();                }           }            if(key_NUM > 0)           {               if(key_NUM  ==  1)               {                   Mode++;                   if(Mode >3)                   {                       if(flay_2==0)                       {                           flay_2=~flay_2;                           OLED_Clear();                       }                       flay=~flay;                       flay_1=~flay_1;                       flay_2=~flay_2;                       Mode =1;                       Set_Mode =0;                   }               }               if(Mode == 2)//Manual mode               {                   if (flay == 0)                   {                        flay=~flay;                        OLED_Clear();                   }                    if(key_NUM ==4)                    {                        operation ++;                        if(operation>2)                        {                            operation=0;                        }                    }                    if(operation == 0)                    {                        if(key_NUM  ==2)                        {                            //Turn on fan                            HAL_GPIO_WritePin(Feng_GPIO_Port,Feng_Pin,GPIO_PIN_SET);                        }                        if(key_NUM  ==3)                        {                            //Turn off fan                            HAL_GPIO_WritePin(Feng_GPIO_Port,Feng_Pin,GPIO_PIN_RESET);                        }                    }                  if(operation ==1)                  {                            if(key_NUM  ==2)                            {                                //Turn on motor                                 Stepper_Move(1,512);                            }                            if(key_NUM  ==3)                            {                                //Turn off motor                                Stepper_Move(0,512);                            }                    }                  if(operation ==2)                  {                            if(key_NUM  ==2)                            {                                //Turn on humidifier                                 HAL_GPIO_WritePin(JIA_GPIO_Port,JIA_Pin,GPIO_PIN_SET);                            }                            if(key_NUM  ==3)                            {                                //Turn off humidifier                                HAL_GPIO_WritePin(JIA_GPIO_Port,JIA_Pin,GPIO_PIN_RESET);                            }                    }               }               if(Mode ==3) //Setting value mode               {                  // OLED_ShowChar(2,1,'>'); //Cursor                   if(flay_1==0)                   {                       flay_1=~flay_1;                       OLED_Clear();                   }                   if(key_NUM == 4)                   {                       Set_Mode++;                       if(Set_Mode>4)                       {                           Set_Mode =0;                       }                   }                   if(Set_Mode ==0)                   {                       if(key_NUM ==2)                       {                           Temp_HIGHT++;                       }                       if(key_NUM ==3)                       {                           Temp_HIGHT--;                       }                       Temp_set_show();                       OLED_ShowChar(2,2,'>');                       WS_OLED_Printf(2,12,"%d    ",Temp_HIGHT);                       WS_OLED_Printf(3,12,"%d    ",Temp_LOW);                   }                   if(Set_Mode ==1)                   {                        if(key_NUM ==2)                       {                           Temp_LOW++;                       }                       if(key_NUM ==3)                       {                           Temp_LOW--;                       }                        Temp_set_show();                       OLED_ShowChar(2,2,' ');  //Clear previous cursor position                       OLED_ShowChar(3,2,'>');                       WS_OLED_Printf(2,12,"%d    ",Temp_HIGHT);                       WS_OLED_Printf(3,12,"%d    ",Temp_LOW);    //                 Humi_set_show();                   }                   if(Set_Mode ==2)                   {                       if(key_NUM ==2)                       {                           Humi_HIGHT++;                       }                       if(key_NUM ==3)                       {                           Humi_HIGHT--;                       }                       Humi_set_show();                       OLED_ShowChar(3,2,' ');//Clear previous cursor position                       OLED_ShowChar(2,2,'>');                       WS_OLED_Printf(2,12,"%d    ",Humi_HIGHT);                       WS_OLED_Printf(3,12,"%d    ",Humi_LOW);                   }                   if(Set_Mode ==3)                   {                        if(key_NUM ==2)                       {                           Humi_LOW++;                       }                       if(key_NUM ==3)                       {                           Humi_LOW--;                       }                        Humi_set_show();                       OLED_ShowChar(2,2,' ');                       OLED_ShowChar(3,2,'>');                       WS_OLED_Printf(2,12,"%d    ",Humi_HIGHT);                       WS_OLED_Printf(3,12,"%d    ",Humi_LOW);                   }                   if(Set_Mode==4)                   {                       if(key_NUM ==2)                       {                           CO2_HIGHT++;                       }                       if(key_NUM ==3)                       {                           CO2_HIGHT--;                       }                        CO2_set_show();                       WS_OLED_Printf(2,12,"%d    ",CO2_HIGHT);                       WS_OLED_Printf(3,2,"                  ");                                        }//                   OLED_ShowChar(Flay_num,1,'>');                   Set_show();  //Setting interface               }                openBuzzerimer(1);           }  /* USER CODE END WHILE */  /* USER CODE BEGIN 3 */        userHandle();        gizwitsHandle((dataPoint_t *)&currentDataPoint);    }  /* USER CODE END 3 */}/** System Clock Configuration*/void SystemClock_Config(void){  RCC_OscInitTypeDef RCC_OscInitStruct;  RCC_ClkInitTypeDef RCC_ClkInitStruct;    /**Initializes the CPU, AHB and APB busses clocks    */  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;  RCC_OscInitStruct.HSEState = RCC_HSE_ON;  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;  RCC_OscInitStruct.HSIState = RCC_HSI_ON;  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)  {    _Error_Handler(__FILE__, __LINE__);  }    /**Initializes the CPU, AHB and APB busses clocks    */  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)  {    _Error_Handler(__FILE__, __LINE__);  }    /**Configure the Systick interrupt time    */  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);    /**Configure the Systick    */  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);  /* SysTick_IRQn interrupt configuration */  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);}/** NVIC Configuration*/static void MX_NVIC_Init(void){  /* TIM2_IRQn interrupt configuration */  HAL_NVIC_SetPriority(TIM2_IRQn, 1, 0);  HAL_NVIC_EnableIRQ(TIM2_IRQn);  /* USART2_IRQn interrupt configuration */  HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);  HAL_NVIC_EnableIRQ(USART2_IRQn);}/* TIM2 init function */static void MX_TIM2_Init(void){  TIM_ClockConfigTypeDef sClockSourceConfig;  TIM_MasterConfigTypeDef sMasterConfig;  htim2.Instance = TIM2;  htim2.Init.Prescaler = 7200-1;  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;  htim2.Init.Period = 10-1;  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)  {    _Error_Handler(__FILE__, __LINE__);  }  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)  {    _Error_Handler(__FILE__, __LINE__);  }  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)  {    _Error_Handler(__FILE__, __LINE__);  }}/* USART1 init function */static void MX_USART1_UART_Init(void){  huart1.Instance = USART1;  huart1.Init.BaudRate = 115200;  huart1.Init.WordLength = UART_WORDLENGTH_8B;  huart1.Init.StopBits = UART_STOPBITS_1;  huart1.Init.Parity = UART_PARITY_NONE;  huart1.Init.Mode = UART_MODE_TX_RX;  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;  huart1.Init.OverSampling = UART_OVERSAMPLING_16;  if (HAL_UART_Init(&huart1) != HAL_OK)  {    _Error_Handler(__FILE__, __LINE__);  }}/* USART2 init function */static void MX_USART2_UART_Init(void){  huart2.Instance = USART2;  huart2.Init.BaudRate = 9600;  huart2.Init.WordLength = UART_WORDLENGTH_8B;  huart2.Init.StopBits = UART_STOPBITS_1;  huart2.Init.Parity = UART_PARITY_NONE;  huart2.Init.Mode = UART_MODE_TX_RX;  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;  huart2.Init.OverSampling = UART_OVERSAMPLING_16;  if (HAL_UART_Init(&huart2) != HAL_OK)  {    _Error_Handler(__FILE__, __LINE__);  }}/* USART3 init function */static void MX_USART3_UART_Init(void){  huart3.Instance = USART3;  huart3.Init.BaudRate = 9600;  huart3.Init.WordLength = UART_WORDLENGTH_8B;  huart3.Init.StopBits = UART_STOPBITS_1;  huart3.Init.Parity = UART_PARITY_NONE;  huart3.Init.Mode = UART_MODE_TX_RX;  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;  huart3.Init.OverSampling = UART_OVERSAMPLING_16;  if (HAL_UART_Init(&huart3) != HAL_OK)  {    _Error_Handler(__FILE__, __LINE__);  }}/**  * Enable DMA controller clock  */static void MX_DMA_Init(void){  /* DMA controller clock enable */  __HAL_RCC_DMA1_CLK_ENABLE();  /* DMA interrupt init */  /* DMA1_Channel2_IRQn interrupt configuration */  HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);  HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);  /* DMA1_Channel3_IRQn interrupt configuration */  HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);  HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);}/** Configure pins as        * Analog        * Input        * Output        * EVENT_OUT        * EXTI*/static void MX_GPIO_Init(void){  GPIO_InitTypeDef GPIO_InitStruct;  __HAL_AFIO_REMAP_SWJ_NOJTAG();  /* GPIO Ports Clock Enable */  __HAL_RCC_GPIOD_CLK_ENABLE();  __HAL_RCC_GPIOA_CLK_ENABLE();  __HAL_RCC_GPIOB_CLK_ENABLE();  /*Configure GPIO pin Output Level */  HAL_GPIO_WritePin(GPIOA, BUZZ_Pin|MOTOR_A_Pin|MOTOR_B_Pin|MOTOR_D_Pin, GPIO_PIN_RESET);  /*Configure GPIO pin Output Level */  HAL_GPIO_WritePin(GPIOB, OLED_SDA_Pin|Feng_Pin|JIA_Pin|MOTOR_C_Pin, GPIO_PIN_RESET);  /*Configure GPIO pin Output Level */  HAL_GPIO_WritePin(OLED_SCL_GPIO_Port, OLED_SCL_Pin, GPIO_PIN_SET);  /*Configure GPIO pin Output Level */  HAL_GPIO_WritePin(DHT11_GPIO_Port, DHT11_Pin, GPIO_PIN_SET);  /*Configure GPIO pins : BUZZ_Pin DHT11_Pin MOTOR_A_Pin MOTOR_B_Pin                           MOTOR_D_Pin */  GPIO_InitStruct.Pin = BUZZ_Pin|DHT11_Pin|MOTOR_A_Pin|MOTOR_B_Pin                          |MOTOR_D_Pin;  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);  /*Configure GPIO pins : OLED_SDA_Pin OLED_SCL_Pin Feng_Pin JIA_Pin                           MOTOR_C_Pin */  GPIO_InitStruct.Pin = OLED_SDA_Pin|OLED_SCL_Pin|Feng_Pin|JIA_Pin                          |MOTOR_C_Pin;  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);  /*Configure GPIO pins : K4_Pin K3_Pin K2_Pin K1_Pin */  GPIO_InitStruct.Pin = K4_Pin|K3_Pin|K2_Pin|K1_Pin;  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;  GPIO_InitStruct.Pull = GPIO_PULLUP;  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);}/* USER CODE BEGIN 4 */char KEY_scanf(void){            uint8_t KeyNum = 0;            if (HAL_GPIO_ReadPin(GPIOB, K1_Pin) == 0)            {                HAL_Delay(20);                while (HAL_GPIO_ReadPin(GPIOB, K1_Pin) == 0);                HAL_Delay(20);                KeyNum = 1;            }            if (HAL_GPIO_ReadPin(GPIOB, K2_Pin) == 0)            {                HAL_Delay(20);                while (HAL_GPIO_ReadPin(GPIOB, K2_Pin) == 0);                HAL_Delay(20);                KeyNum = 2;            }            if (HAL_GPIO_ReadPin(GPIOB, K3_Pin) == 0)            {                HAL_Delay(20);                while (HAL_GPIO_ReadPin(GPIOB, K3_Pin) == 0);                HAL_Delay(20);                KeyNum = 3;            }            if (HAL_GPIO_ReadPin(GPIOB, K4_Pin) == 0)            {                HAL_Delay(20);                while (HAL_GPIO_ReadPin(GPIOB, K4_Pin) == 0);                HAL_Delay(20);                KeyNum = 4;            }            return KeyNum; }void softwareTimer_IRQ(void) // 1ms timer interrupt{static char count_10ms = 0;static int count_1000ms = 0;if (++count_10ms == 10)    {        count_10ms = 0;        timerFlag_10ms = 1;    }if (++count_1000ms == 100)    {        count_1000ms = 0;        timerFlag_1000ms = 1;    }    timerFlag_1ms = 1;}int  buzzerTimerCount = 0;//  Function to control the buzzer open time//  time:  Open time, unit MSvoid openBuzzerimer(int time){     HAL_GPIO_WritePin(BUZZ_GPIO_Port,BUZZ_Pin,GPIO_PIN_SET)  ;    buzzerTimerCount = time ;}// Software timer, called periodically, interval 1MSvoid softwareTimer(void){       if(buzzerTimerCount > 0)        {          if( -- buzzerTimerCount == 0)            {                HAL_GPIO_WritePin(BUZZ_GPIO_Port,BUZZ_Pin,GPIO_PIN_RESET)  ;            }        }        }/* USER CODE END 4 *//**  * @brief  This function is executed in case of error occurrence.  * @param  None  * @retval None  */void _Error_Handler(char * file, int line){  /* USER CODE BEGIN Error_Handler_Debug */    /* User can add his own implementation to report the HAL error return state */    while(1)     {    }  /* USER CODE END Error_Handler_Debug */}#ifdef USE_FULL_ASSERT/**   * @brief Reports the name of the source file and the source line number   * where the assert_param error has occurred.   * @param file: pointer to the source file name   * @param line: assert_param error line source number   * @retval None   */void assert_failed(uint8_t* file, uint32_t line){  /* USER CODE BEGIN 6 */    /* User can add his own implementation to report the file name and line number,        ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */  /* USER CODE END 6 */}#endif/**  * @}  *//**  * @}*//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

05

Experimental Results

STM32 Project Sharing: Intelligent Fruit and Vegetable Preservation System

Leave a Comment