ESP IDF开发示例:UART/USART串口通信在物联网中的应用

97 篇文章 ¥59.90 ¥99.00
本文介绍了在物联网应用中串口通信的重要性,特别是使用ESP IDF开发框架实现UART/USART串口通信。ESP IDF是Espressif Systems为ESP32和ESP32-S2芯片提供的开发框架,提供了UART和USART模块,支持全双工通信。通过示例代码展示了如何配置和使用UART进行串口通信,并提到USART的类似实现。ESP IDF简化了在物联网设备上实现可靠串口通信的过程。

物联网(Internet of Things,IoT)是一种将各种物理设备通过互联网连接起来,实现数据传输和通信的技术。在物联网应用中,串口通信是一种常用的通信方式,用于实现设备之间的数据交换和控制。本文将介绍如何使用ESP IDF开发框架实现UART/USART串口通信,并提供相应的源代码示例。

ESP-IDF(Espressif IoT Development Framework)是由乐鑫科技(Espressif Systems)开发的一套适用于ESP32和ESP32-S2芯片的官方开发框架。ESP32是一款低功耗的Wi-Fi和蓝牙双模模块,广泛应用于物联网设备的开发。在ESP IDF中,提供了丰富的API和示例代码,方便开发者使用串口通信功能。

在ESP IDF中,串口通信主要通过UART(通用异步收发器)和USART(通用同步异步收发器)模块实现。这两个模块都支持全双工通信,可以配置不同的波特率、数据位、停止位和校验位等参数。

下面是一个示例,演示了如何在ESP IDF中使用UART模块进行串口通信。在这个示例中,ESP32开发板将通过UART发送数据给连接在串口上的外部设备,并接收来自外部设备的数据。

#include <stdio.h>
这个用的是什么通信协议:#include "wifi_config.h" #include <stdio.h> #include <stdarg.h> struct STRUCT_USARTx_Fram strEsp8266_Fram_Record = { 0 }; /** * @brief WiFi_Config wifi 初始化 * @param 无 * @retval 无 */ void WiFi_Config( void ) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; //RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE ); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //ESP01S 不使用这2个接口 // /* 配置WiFi模块的片选(CH)引脚 复位重启(RST)引脚*/ // /*选择要控制的PA15(CH)引脚和PB2(RST)引脚*/ // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_Init( GPIOB, &GPIO_InitStructure ); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_Init( GPIOA, &GPIO_InitStructure ); // /* 拉低WiFi模块的片选引脚 */ // GPIO_ResetBits( GPIOA, GPIO_Pin_15 ); // /* 拉高WiFi模块的复位重启引脚 */ // GPIO_SetBits( GPIOB, GPIO_Pin_2 ); //UART3使用下面代码 // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10 // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 // GPIO_Init(GPIOB, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PB11 // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 // GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB 11 //UART2使用下面代码,gpio GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA 3 //USART2 初始化设置,串口 //USART_InitStructure.USART_BaudRate = 9600;//串口波特率 USART_InitStructure.USART_BaudRate = 115200;//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART2, &USART_InitStructure); //初始化串口3 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启串口接受和总线空闲中断 USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); USART_Cmd(USART2, ENABLE); //使能串口3 //USART2 NVIC 配置,中断 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 } void USART2_IRQHandler( void ) { char ch; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断 { ch = USART_ReceiveData( USART2 ); //(USART2->DR); //读取接收到的数据 if( strEsp8266_Fram_Record .InfBit .FramLength < ( RX_BUF_MAX_LEN - 1 ) ) //预留1个字节写结束符 { strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ++ ] = ch; } } if ( USART_GetITStatus( USART2, USART_IT_IDLE ) == SET ) //数据帧接收完毕 { strEsp8266_Fram_Record .InfBit .FramFinishFlag = 1; ch = USART_ReceiveData( USART2 ); //由软件序列清除中断标志位(先读USART_SR,然后读USART_DR) } } /* * 函数名:itoa * 描述 :将整形数据转换成字符串 * 输入 :-radix =10 表示10进制,其他结果为0 * -value 要转换的整形数 * -buf 转换后的字符串 * -radix = 10 * 输出 :无 * 返回 :无 * 调用 :被USART2_printf()调用 */ static char *itoa( int value, char *string, int radix ) { int i, d; int flag = 0; char *ptr = string; /* This implementation only works for decimal numbers. */ if (radix != 10) { *ptr = 0; return string; } if (!value) { *ptr++ = 0x30; *ptr = 0; return string; } /* if this is a negative value insert the minus sign. */ if (value < 0) { *ptr++ = '-'; /* Make the value positive. */ value *= -1; } for (i = 10000; i > 0; i /= 10) { d = value / i; if (d || flag) { *ptr++ = (char)(d + 0x30); value -= (d * i); flag = 1; } } /* Null terminate the string. */ *ptr = 0; return string; } /* NCL_Itoa */ /* * 函数名:USART2_printf * 描述 :格式化输出,类似于C库中的printf,但这里没有用到C库 * 输入 :-USARTx 串口通道,这里只用到了串口2,即USART2 * -Data 要发送到串口的内容的指针 * -... 其他参数 * 输出 :无 * 返回 :无 * 调用 :外部调用 * 典型应用USART2_printf( USART2, "\r\n this is a demo \r\n" ); * USART2_printf( USART2, "\r\n %d \r\n", i ); * USART2_printf( USART2, "\r\n %s \r\n", j ); */ void USART2_printf( USART_TypeDef* USARTx, char *Data, ... ) { const char *s; int d; char buf[16]; va_list ap; va_start(ap, Data); while ( *Data != 0) // 判断是否到达字符串结束符 { if ( *Data == 0x5c ) //'\' { switch ( *++Data ) { case 'r': //回车符 USART_SendData(USARTx, 0x0d); Data ++; break; case 'n': //换行符 USART_SendData(USARTx, 0x0a); Data ++; break; default: Data ++; break; } } else if ( *Data == '%') { // switch ( *++Data ) { case 's': //字符串 s = va_arg(ap, const char *); for ( ; *s; s++) { USART_SendData(USARTx,*s); while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET ); } Data++; break; case 'd': //十进制 d = va_arg(ap, int); itoa(d, buf, 10); for (s = buf; *s; s++) { USART_SendData(USARTx,*s); while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET ); } Data++; break; default: Data++; break; } } /* end of else if */ else USART_SendData(USARTx, *Data++); while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET ); } }
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值