最近在弄apm32,在弄串口发送接收字符串,之前在实习的时候弄过GD32,有点基础,要操作寄存器标志位,所用的库函数不大一样,方法是相同的。主要代码如下:
for(i = 0; i < sizeof(txBuf); i++)
{
while(USART_ReadFlag(USART2, USART_FLAG_TXBE) == RESET);
USART_TxData(USART2, txBuf[i]);
}
其余就是USART串口配置代码了,如下:
GPIO_ConfigStruct_T GPIO_ConfigStruct;
USART_ConfigStruct_T USART_ConfigStruct;
RCM_EnableAPB1PeriphClock((RCM_APB1_PERIPH_T)(RCM_APB1_PERIPH_USART2));
RCM_EnableAPB2PeriphClock(
(RCM_APB2_PERIPH_T)(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_AFIO));
GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
GPIO_ConfigStruct.pin = GPIO_PIN_2;
GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
GPIO_ConfigStruct.pin = GPIO_PIN_3;
GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
USART_ConfigStruct.baudRate = 115200;
USART_ConfigStruct.hardwareFlowCtrl = USART_FLOW_CTRL_NONE;
USART_ConfigStruct.mode = USART_MODE_TX_RX;
USART_ConfigStruct.parity = USART_PARITY_NONE;
USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
USART_Config(USART2, &USART_ConfigStruct);
USART_Enable(USART2);
完整代码如下:
/*!
* @file main.c
*
* @brief Main program body
*
* @version V1.0.1
*
* @date 2021-03-23
*
*/
#include "main.h"
#include "apm32f10x_gpio.h"
#include "apm32f10x_usart.h"
#include "apm32f10x_rcm.h"
#include <rtthread.h>
#include <rthw.h>
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif
volatile uint32_t tick = 0;
uint8_t txBuf[] = "Hello USART2 \r\n";
/*!
* @brief Main program
*
* @param None
*
* @retval None
*
*/
int main(void)
{
uint8_t i;
GPIO_ConfigStruct_T GPIO_ConfigStruct;
USART_ConfigStruct_T USART_ConfigStruct;
RCM_EnableAPB1PeriphClock((RCM_APB1_PERIPH_T)(RCM_APB1_PERIPH_USART2));
RCM_EnableAPB2PeriphClock(
(RCM_APB2_PERIPH_T)(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_AFIO));
GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
GPIO_ConfigStruct.pin = GPIO_PIN_2;
GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
GPIO_ConfigStruct.pin = GPIO_PIN_3;
GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
USART_ConfigStruct.baudRate = 115200;
USART_ConfigStruct.hardwareFlowCtrl = USART_FLOW_CTRL_NONE;
USART_ConfigStruct.mode = USART_MODE_TX_RX;
USART_ConfigStruct.parity = USART_PARITY_NONE;
USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
USART_Config(USART2, &USART_ConfigStruct);
USART_Enable(USART2);
while(1)
{
for(i = 0; i < sizeof(txBuf); i++)
{
while(USART_ReadFlag(USART2, USART_FLAG_TXBE) == RESET);
USART_TxData(USART2, txBuf[i]);
}
}
}
版权声明:本文为CSDN博主「朝朝暮暮柳十岁」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42952472/article/details/122511067
暂无评论