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, 下载次数: 98)
|