本设计主要是由STM32f103c8t6,wifi模块用的ESP8266,主要控制鱼缸的灯光,换水,换氧,过滤,水位的功能,通过机制云开发平台设置数据点如图,完成超声波控制液位,温度DS18B20检测水温,最初想法水温也可以控制,但是最后加热棒功率不够,无法加热水,放弃此功能,有兴趣得可以自己加,最后液晶显示页面主要是显示上次换水时间,换水次数,加氧时间,水温。
单片机源程序如下:
- #include "main.h"
- #include "stm32f1xx_hal.h"
- /* USER CODE BEGIN Includes */
- #include "hal_key.h"
- #include "gizwits_product.h"
- #include "common.h"
- #include "UltrasonicWave.h"
- #include "ds18b20.h"
- #include "timer.h"
- #include "ctrl.h"
- #include "oled.h"
- #include "stm32_hal_legacy.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- TIM_HandleTypeDef htim2;
- UART_HandleTypeDef huart1;
- UART_HandleTypeDef huart2;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- #define GPIO_KEY_NUM 2 ///< Defines the total number of key member
- keyTypedef_t singleKey[GPIO_KEY_NUM]; ///< Defines a single key member array pointer
- keysTypedef_t keys;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_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_NVIC_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /**
- * key1 short press handle
- * @param none
- * @return none
- */
- void key1ShortPress(void)
- {
- GIZWITS_LOG("KEY1 PRESS ,Production Mode\n");
- gizwitsSetMode(WIFI_PRODUCTION_TEST);
- }
- /**
- * key1 long press handle
- * @param none
- * @return none
- */
- void key1LongPress(void)
- {
- GIZWITS_LOG("KEY1 PRESS LONG ,Wifi Reset\n");
- gizwitsSetMode(WIFI_RESET_MODE);
- }
- /**
- * key2 short press handle
- * @param none
- * @return none
- */
- void key2ShortPress(void)
- {
- GIZWITS_LOG("KEY2 PRESS ,Soft AP mode\n");
- #if !MODULE_TYPE
- gizwitsSetMode(WIFI_SOFTAP_MODE);
- #endif
- }
- /**
- * key2 long press handle
- * @param none
- * @return none
- */
- void key2LongPress(void)
- {
- //AirLink mode
- GIZWITS_LOG("KEY2 PRESS LONG ,AirLink mode\n");
- #if !MODULE_TYPE
- gizwitsSetMode(WIFI_AIRLINK_MODE);
- #endif
- }
- /**
- * Key init function
- * @param none
- * @return none
- */
- void keyInit(void)
- {
- singleKey[0] = keyInitOne(NULL, KEY1_GPIO_Port, KEY1_Pin, key1ShortPress, key1LongPress);
- singleKey[1] = keyInitOne(NULL, KEY2_GPIO_Port, KEY2_Pin, key2ShortPress, key2LongPress);
- keys.singleKey = (keyTypedef_t *)&singleKey;
- keyParaInit(&keys);
- }
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* 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();
- Wave_Init();
- DS18B20_Init();
- MX_Relay_Init();
- OLED_Init();
- OLED_Clear();
- MX_TIM2_Init();
- MX_TIM3_Init();
- MX_TIM4_Init();
- // TIM3_PWM_Init(36000,0);
- MX_USART1_UART_Init();
- MX_USART2_UART_Init();
- /* Initialize interrupts */
- MX_NVIC_Init();
- /* USER CODE BEGIN 2 */
- uartInit();
- userInit();
- gizwitsInit();
- keyInit();
- OLED_Clear();
- /* USER CODE END 2 */
- while (1)
- {
- gizwitsGetNTP();
- Display_Oled();
- Wave_ctr();
- if(flag3==0)
- {
- Ctr_Level();
- }
- userHandle();
- gizwitsHandle((dataPoint_t *)¤tDataPoint);
- }
- /* 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__);
- }
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOD_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin : KEY1_Pin */
- GPIO_InitStruct.Pin = KEY1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(KEY1_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : KEY2_Pin */
- GPIO_InitStruct.Pin = KEY2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(KEY2_GPIO_Port, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- /* 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****/
复制代码
所有资料51hei提供下载:
智能鱼缸 2018.6.1.rar
(3.7 MB, 下载次数: 433)
|