STM32——串口实践
1.蓝牙控制点灯
项目需求
通过蓝牙模块,实现手机控制蓝牙插座/风扇/灯。
本质:
-
采用蓝牙的透传功能;
-
控制 IO 口的输出。
硬件清单
HC01蓝牙模块
CH340
杜邦线
项目设计
HC01_TX – RX1
HC01_RX – TX1
项目实现
串口非中断法
HAL_UART_Receive(&huart1, ch, 19, 100);
//HAL_UART_Transmit(&huart1, ch, strlen(ch), 100);
//printf((char *)ch);
printf("%s", ch);
if (!strcmp((const char *)ch, "open")) {HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_RESET)printf("LED1已打开\n");
}else if(!strcmp((const char *)ch, "close")) {HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_SET)printf("LED1已关闭\n");
} else {if(ch[0] != '\0')printf("指令发送错误:%s", ch);
}
串口中断法
printf("收到数据:");
if (!strcmp((const char *)UART1_RX_Buffer, "open")) {HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_RESET)printf("LED1已打开\n");
}else if(!strcmp((const char *)UART1_RX_Buffer, "close")) {HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_SET)printf("LED1已关闭\n");
} else {if(UART1_RX_Buffer[0] != '\0')printf("指令发送错误:%s", UART1_RX_Buffer);
}
2.WI_FI点灯
项目需求
通过ESP8266模块,实现手机控制wifi插座/风扇/灯。
硬件清单
ESP8266模块
CH340
杜邦线
项目实现
注意:
-
工作中一般不直接在中断服务函数里处理数据,而是在收到数据后直接丢给队列,再处理数据;
-
在中断服务函数里尽量减少使用延时函数及打印函数。
AP模式
#define SIZE 12char buffer[SIZE];
char LJWL[] = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n"; //入网指令
char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.130\",8880\r\n"; //连接服务器指令
char TCMS[] = "AT+CIPMODE=1\r\n"; //透传指令
char SJCS[] = "AT+CIPSEND\r\n"; //数据传输开始指令
char CQMK[] = "AT+RST\r\n"; //重启模块指令
char AT_OK_Flag = 0; //OK返回值的标志位
char AT_Connect_Net_Flag = 0; //WIFI GOT IP返回值的标志位
// 接收完成回调函数,收到一个数据后,在这里处理
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{// 判断中断是由哪个串口触发的if(huart->Instance == USART1){// 判断接收是否完成(UART1_RX_STA bit15 位是否为1)if((UART1_RX_STA & 0x8000) == 0){// 如果已经收到了 0x0d (回车),if(UART1_RX_STA & 0x4000){// 则接着判断是否收到 0x0a (换行)if(buf == 0x0a){// 如果 0x0a 和 0x0d 都收到,则将 bit15 位置为1UART1_RX_STA |= 0x8000;// 查看是否收到 WIFI GOT IPif(!strcmp((uint8_t *)UART1_RX_Buffer, "WIFI GOT IP"))AT_Connect_Net_Flag = 1;// 查看是否收到 OKif(!strcmp((uint8_t *)UART1_RX_Buffer, "OK"))AT_OK_Flag = 1;// 查看是否收到 FAILif(!strcmp((uint8_t *)UART1_RX_Buffer, "FAIL")){int i = 0;for(i = 0; i < 5; i++){HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_8);HAL_Delay(1000);}HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);printf(CQMK);}// 灯控指令if(!strcmp((uint8_t *)UART1_RX_Buffer, "L-1"))HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);if(!strcmp((uint8_t *)UART1_RX_Buffer, "L-0"))HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);memset(UART1_RX_Buffer, 0, UART1_REC_LEN);UART1_RX_STA = 0;}else// 否则认为接收错误,重新开始UART1_RX_STA = 0;}else // 如果没有收到了 0x0d (回车){//则先判断收到的这个字符是否是 0x0d (回车)if(buf == 0x0d){// 是的话则将 bit14 位置为1UART1_RX_STA |= 0x4000;}else{// 否则将接收到的数据保存在缓存数组里UART1_RX_Buffer[UART1_RX_STA & 0X3FFF] = buf;UART1_RX_STA++;// 如果接收数据大于UART1_REC_LEN(200字节),则重新开始接收if(UART1_RX_STA > UART1_REC_LEN - 1)UART1_RX_STA = 0;}}}// 重新开启中断HAL_UART_Receive_IT(&huart1, &buf, 1);}
}
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();MX_USART1_UART_Init();MX_USART2_UART_Init();/* USER CODE BEGIN 2 */HAL_NVIC_SetPriority(SysTick_IRQn,0,0);// 开启接收中断HAL_UART_Receive_IT(&huart1, &buf, 1);HAL_UART_Transmit(&huart2, "let's go!!\r\n", strlen("let's go!!\r\n"), 100);//发送联网AT指令并等待成功printf(LJWL);//while(!AT_Connect_Net_Flag);while(!AT_OK_Flag) HAL_Delay(50);AT_OK_Flag = 0;//发送连服务器指令并等待成功printf(LJFWQ);while(!AT_OK_Flag) HAL_Delay(50);AT_OK_Flag = 0;//发送透传模式指令并等待成功printf(TCMS);while(!AT_OK_Flag) HAL_Delay(50);AT_OK_Flag = 0;//发送数据传输指令并等待成功printf(SJCS);while(!AT_OK_Flag) HAL_Delay(50);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */printf("liangxu shuai\r\n");HAL_UART_Transmit(&huart2, "hello liangxu\r\n", strlen("hello liangxu\r\n"),100);HAL_Delay(3000);}/* USER CODE END 3 */
}
STA模式:
#include <stdio.h>
#include <string.h>
char buffer[SIZE];
//1 工作在路由模式
char LYMO[] = "AT+CWMODE=2\r\n";
//2 使能多链接
char DLJ[] = "AT+CIPMUX=1\r\n";
//3 建立TCPServer
char JLFW[] = "AT+CIPSERVER=1\r\n"; // default port = 333
//发送数据
char FSSJ[] = "AT+CIPSEND=0,5\r\n";
char AT_OK_Flag = 0; //OK返回值的标志位
char AT_Connect_Net_Flag = 0; //WIFI GOT IP返回值的标志位
char Client_Connect_Flag = 0;
// 接收完成回调函数,收到一个数据后,在这里处理
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{// 判断中断是由哪个串口触发的if(huart->Instance == USART1){// 判断接收是否完成(UART1_RX_STA bit15 位是否为1)if((UART1_RX_STA & 0x8000) == 0){// 如果已经收到了 0x0d (回车),if(UART1_RX_STA & 0x4000){// 则接着判断是否收到 0x0a (换行)if(buf == 0x0a){// 如果 0x0a 和 0x0d 都收到,则将 bit15 位置为1UART1_RX_STA |= 0x8000;// 查看是否收到 WIFI GOT IPif(!strcmp(UART1_RX_Buffer, "WIFI GOT IP"))AT_Connect_Net_Flag = 1;// 查看是否收到 OKif(!strcmp(UART1_RX_Buffer, "OK"))AT_OK_Flag = 1;// 查看是否收到 FAILif(!strcmp(UART1_RX_Buffer, "0,CONNECT"))Client_Connect_Flag = 1;// 灯控指令if(!strcmp(UART1_RX_Buffer, "L-1"))HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);if(!strcmp(UART1_RX_Buffer, "L-0"))HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);memset(UART1_RX_Buffer, 0, UART1_REC_LEN);UART1_RX_STA = 0;}else// 否则认为接收错误,重新开始UART1_RX_STA = 0;}else // 如果没有收到了 0x0d (回车){//则先判断收到的这个字符是否是 0x0d (回车)if(buf == 0x0d){// 是的话则将 bit14 位置为1UART1_RX_STA |= 0x4000;}else{// 否则将接收到的数据保存在缓存数组里UART1_RX_Buffer[UART1_RX_STA & 0X3FFF] = buf;UART1_RX_STA++;// 如果接收数据大于UART1_REC_LEN(200字节),则重新开始接收if(UART1_RX_STA > UART1_REC_LEN - 1)UART1_RX_STA = 0;}}}// 重新开启中断HAL_UART_Receive_IT(&huart1, &buf, 1);}
}
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();MX_USART1_UART_Init();MX_USART2_UART_Init();/* USER CODE BEGIN 2 */HAL_NVIC_SetPriority(SysTick_IRQn,0,0);// 开启接收中断HAL_UART_Receive_IT(&huart1, &buf, 1);HAL_UART_Transmit(&huart2, "let's go\r\n", strlen("let's go\r\n"), 100);printf(LYMO);while(!AT_OK_Flag) HAL_Delay(50);AT_OK_Flag = 0;printf(DLJ);while(!AT_OK_Flag) HAL_Delay(50);AT_OK_Flag = 0;printf(JLFW);while(!Client_Connect_Flag) HAL_Delay(50);AT_OK_Flag = 0;if(Client_Connect_Flag){HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);}/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 *///4 发送数据printf(FSSJ);HAL_Delay(2000);printf("Hello");HAL_Delay(2000);}/* USER CODE END 3 */
}