STM32新手根據標準庫改的HAL庫例程,需要的大家可以看看
單片機源程序如下:
- //USART2用來ESP8266的收發,實驗時暫時用USART1與電腦相連查看數據
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32l0xx_hal.h"
- #include "dma.h"
- #include "usart.h"
- #include "gpio.h"
- #include "esp8266/bsp_esp8266.h"
- #define User_ESP8266_ApSsid "ESP8266test02" //要創建的熱點的名稱
- #define User_ESP8266_ApPwd "123456789" //要創建的熱點的密鑰
- #define User_ESP8266_TcpServer_IP "192.168.31.230" //要連接的服務器的 IP
- #define User_ESP8266_TcpServer_Port "8000" //要連接的服務器的端口
- extern __IO uint8_t ucTcpClosedFlag;
- void SystemClock_Config(void);
- char APIP[20];
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- int main(void)
- {
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_USART2_UART_Init();
- MX_USART1_UART_Init();
- printf("正在配置 ESP8266 ......\n" );
-
- if(ESP8266_AT_Test())
- {
- printf("AT test OK\n");
- }
- printf("\n< 1 >\n");
- if(ESP8266_Net_Mode_Choose(AP))
- {
- printf("ESP8266_Net_Mode_Choose OK\n");
- }
- printf("\n< 2 >\n");
- if(ESP8266_BuildAP(User_ESP8266_ApSsid,User_ESP8266_ApPwd,WPA2_PSK))
- {
- printf("ESP8266_BuildAP_OK\n");
- }
- printf("\n< 3 >\n");
- ESP8266_Rst();
- printf("\n< 4 >\n");
- if(ESP8266_Enable_MultipleId(ENABLE))
- {
- printf("ESP8266_Enable_MultipleId_OK\n");
- }
- printf("\n< 5 >\n");
- if(ESP8266_StartOrShutServer(ENABLE,"8080","180"))
- {
- printf("ESP8266_StartOrShutServer_OK\n");
- }
- printf("\n< 6 >\n");
- if(ESP8266_Inquire_ApIp(APIP,20))
- {
- PC_Usart("%s\n",APIP);
- }
- printf("\n< 7 >\n");
-
- //HAL_UART_Receive_IT(&huart2,(uint8_t *)ESP8266_ReceiveString(DISABLE),sizeof(ESP8266_ReceiveString(DISABLE)));
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- // strEsp8266_Fram_Record.InfBit.FramLength = 0; //從新開始接收新的數據包
- //
- // HAL_Delay( 2000 ); //延時
- //
- // strEsp8266_Fram_Record.Data_RX_BUF[ strEsp8266_Fram_Record.InfBit.FramLength ] = '\0';
- // /* USER CODE END WHILE */
- // printf("%s\n",strEsp8266_Fram_Record.Data_RX_BUF);
- // /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Configure the main internal regulator output voltage
- */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
- RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
- 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_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2;
- PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
- PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != 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);
- }
- /**
- * 函數功能: 串口接收完成回調函數
- * 輸入參數: 無
- * 返 回 值: 無
- * 說 明:無
- */
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
- {
- if(strEsp8266_Fram_Record.InfBit.FramLength<(RX_BUF_MAX_LEN-1)) //預留1個字節寫結束符
- strEsp8266_Fram_Record.Data_RX_BUF[strEsp8266_Fram_Record.InfBit.FramLength++]=esp8266_rxdata;
- HAL_UART_Receive_IT(&huart2,&esp8266_rxdata,1);
- }
- /**
- * @brief This function is executed in case of error occurrence.
- * @param file: The file name as string.
- * @param line: The line in file as a number.
- * @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
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
esp8266主機配置.7z
(2.7 MB, 下載次數: 100)
2019-4-18 00:19 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|