我正尝试使用st-link将NUCLEO-L053R8的数据通过针脚PA2和PA3发送到我的PC COM端口。我有一个示例代码,但对我不起作用。当我只想通过UART将数据发送到我的PC时,哪种方法是最受欢迎的?中断还是DMA?这是我的代码。
#include <stm32l0xx_hal.h>
#include <stm32_hal_legacy.h>
#ifdef __cplusplus
extern "C"
#endif
void SysTick_Handler(void)
{
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
}
static UART_HandleTypeDef USART2_UARTHandle = UART_HandleTypeDef();
int main(void)
{
HAL_Init();
__USART2_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure;
// PA2 & PA3 als USART Configurieren
GPIO_InitStructure.Pin = GPIO_PIN_2;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_InitStructure.Alternate = GPIO_AF0_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_3;
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
USART2_UARTHandle.Instance = USART2;
USART2_UARTHandle.Init.BaudRate = 9600;
USART2_UARTHandle.Init.WordLength = UART_WORDLENGTH_8B;
USART2_UARTHandle.Init.StopBits = UART_STOPBITS_1;
USART2_UARTHandle.Init.Parity = UART_PARITY_NONE;
USART2_UARTHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
USART2_UARTHandle.Init.Mode = UART_MODE_TX_RX;
if (HAL_UART_Init(&USART2_UARTHandle) != HAL_OK) // Check if USART2 init funktioniert
{
asm("bkpt 255"); // Enter Processor Debug State
}
for (; ;)
{
uint8_t buffer[4];
HAL_UART_Transmit(&USART2_UARTHandle, buffer, sizeof(buffer), HAL_MAX_DELAY);
HAL_UART_Receive(&USART2_UARTHandle, buffer, sizeof(buffer), HAL_MAX_DELAY);
//HAL_UART_Receive(&USART2_UARTHandle, buffer, sizeof(buffer), HAL_MAX_DELAY);
// HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_SET);
// HAL_Delay(500);
// HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_RESET);
// HAL_Delay(500);
}
}
我很高兴能得到提示!