采用ASCLL的通讯方式,应该怎么配置,给出代码。基于stm32f407,基于标准库
时间: 2024-03-02 20:51:59 浏览: 182
STM32ASCII码表和转义字符
以下是基于STM32F407与标准库实现ASCLL通讯的示例代码:
```c
#include "stm32f4xx.h"
#define USARTx USART2
#define USARTx_CLK RCC_APB1Periph_USART2
#define USARTx_CLK_INIT RCC_APB1PeriphClockCmd
#define USARTx_IRQn USART2_IRQn
#define USARTx_IRQHandler USART2_IRQHandler
#define USARTx_TX_PIN GPIO_Pin_2
#define USARTx_TX_GPIO_PORT GPIOA
#define USARTx_TX_GPIO_CLK RCC_AHB1Periph_GPIOA
#define USARTx_TX_SOURCE GPIO_PinSource2
#define USARTx_TX_AF GPIO_AF_USART2
#define USARTx_RX_PIN GPIO_Pin_3
#define USARTx_RX_GPIO_PORT GPIOA
#define USARTx_RX_GPIO_CLK RCC_AHB1Periph_GPIOA
#define USARTx_RX_SOURCE GPIO_PinSource3
#define USARTx_RX_AF GPIO_AF_USART2
#define USART_BAUDRATE 9600
#define USART_WORDLENGTH USART_WordLength_7b
#define USART_STOPBITS USART_StopBits_1
#define USART_PARITY USART_Parity_No
#define USART_HARDWAREFLOWCONTROL USART_HardwareFlowControl_None
void USART_Config(void);
void USART_SendString(const char *str);
int main(void)
{
USART_Config();
while(1)
{
/* 发送数据 */
USART_SendString("Hello, World!\r\n");
}
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* 使能GPIO和USART时钟 */
RCC_AHB1PeriphClockCmd(USARTx_TX_GPIO_CLK | USARTx_RX_GPIO_CLK, ENABLE);
USARTx_CLK_INIT(USARTx_CLK, ENABLE);
/* 配置USART TX引脚为复用功能,无上拉下拉 */
GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF);
/* 配置USART RX引脚为复用功能,无上拉下拉 */
GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF);
/* 配置USART参数 */
USART_InitStructure.USART_BaudRate = USART_BAUDRATE;
USART_InitStructure.USART_WordLength = USART_WORDLENGTH;
USART_InitStructure.USART_StopBits = USART_STOPBITS;
USART_InitStructure.USART_Parity = USART_PARITY;
USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USARTx, &USART_InitStructure);
/* 使能USART中断 */
NVIC_InitStructure.NVIC_IRQChannel = USARTx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* 使能USART接收中断 */
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
/* 使能USART */
USART_Cmd(USARTx, ENABLE);
}
void USART_SendString(const char *str)
{
const char *p = str;
while(*p != '\0')
{
/* 将字符转换为ASCLL码并发送 */
USART_SendData(USARTx, (uint16_t)(*p & 0x7F));
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
p++;
}
}
```
在上述代码中,我们通过`USART_SendString`函数将要发送的字符串转换为ASCLL码,并以字节的形式发送。在接收数据时,我们也需要将接收到的字节转换为ASCLL码,并将其转换为相应的字符。
阅读全文