Nucleo STM32f103RB/F4 探索 [英] Nucleo STM32f103RB/F4 Discovery

查看:34
本文介绍了Nucleo STM32f103RB/F4 探索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人在 Nucleo 上通过 VCP 使用双工通信/或发现具有 RX TX 中断的单个 USART.

Has anybody used Duplex communication via VCP on Nucleo/or discovery single USART with RX TX interrupts.

希望能够回显(传输)接收到的内容的示例代码.

Would appreciate sample code to Echo back(transmit) what is received .

推荐答案

STM32CubeF4STM32CubeF1 包.

另见此示例,其中微控制器使用 UART RX 中断将接收到的字节回传给发送方:

Also see this example, in which the microcontroller echos back the received bytes to the sender using UART RX interrupt:

#include "stm32f4xx.h"

UART_HandleTypeDef huart2;

/* Single byte to store input */
uint8_t byte;

void SystemClock_Config(void);

/* UART2 Interrupt Service Routine */
void USART2_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart2);
}

/* This callback is called by the HAL_UART_IRQHandler when the given number of bytes are received */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart->Instance == USART2)
  {
    /* Transmit one byte with 100 ms timeout */
    HAL_UART_Transmit(&huart2, &byte, 1, 100);

    /* Receive one byte in interrupt mode */ 
    HAL_UART_Receive_IT(&huart2, &byte, 1);
  }
}

void uart_gpio_init()
{
  GPIO_InitTypeDef GPIO_InitStruct;

  __GPIOA_CLK_ENABLE();

  /**USART2 GPIO Configuration
  PA2     ------> USART2_TX
  PA3     ------> USART2_RX
  */
  GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void uart_init()
{
  __USART2_CLK_ENABLE();

  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  HAL_UART_Init(&huart2);

  /* Peripheral interrupt init*/
  HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(USART2_IRQn);
}

int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  uart_gpio_init();
  uart_init();

  HAL_UART_Receive_IT(&huart2, &byte, 1);

  while(1)
  {

  }
}

  1. 初始化 UART 的 GPIO 引脚.

  1. Initialize the GPIO pins of the UART.

一个.启用相应 GPIO 端口的时钟.

a. Enable the appropriate GPIO port's clock.

B.在交替功能模式下配置 UART 引脚.

b. Configure the UART pins in alternate function mode.

初始化 UART 外设.

Initialize the UART peripheral.

一个.启用相应 UART 外设的时钟.

a. Enable the clock of the appropriate UART peripheral.

B.配置波特率、字长、停止位和奇偶校验位、流量控制等

b. Configure BAUD rate, word length, stop and parity bits, flow control etc.

c.在 NVIC 中启用 UART IRQ 并设置优先级.

c. Enable the UART IRQ in NVIC and set the priority.

在 UART ISR (USART2_IRQHandler) 中调用 HAL_UART_IRQHandler(UART_HandleTypeDef* huart);.

Call the HAL_UART_IRQHandler(UART_HandleTypeDef* huart); in the UART ISR (USART2_IRQHandler).

HAL_UART_IRQHandler 将在接收过程完成时调用 HAL_UART_RxCpltCallback.在此回调中,您可以传输接收到的字节.

The HAL_UART_IRQHandler will call the HAL_UART_RxCpltCallback when the receive procedure is complete. In this callback you can transmit the received bytes.

使用单个 HAL_UART_Receive_IT(&huart2, &byte, 1); 调用启动回声循环.

Start the echo loop with a single HAL_UART_Receive_IT(&huart2, &byte, 1); call.

这篇关于Nucleo STM32f103RB/F4 探索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆