The MCU is very important for the debugging of the communication interface--uart

Source: Internet
Author: User

1, configuration steps in Cubemx

(1) RCC


Open HSE




Peripheral clock Configuration, HCLK = 72MHZ


(2) USART1


Asynchronous transceiver



Complete the above configuration to generate the code for the Keil project.


2,

Test procedures: In the Serial debugging assistant to send data to the UART1, using the method of interruption, stm32f103 received a string after entering the interrupt service function, and then write the received data like a serial port, displayed in the Serial debugging assistant.


(1) GPIO Init

void Hal_uart_mspinit (uart_handletypedef* huart)
{

  gpio_inittypedef gpio_initstruct;
  if (Huart->instance==usart1)
  {/
  * user code BEGIN usart1_mspinit 0 */

  * user code END usart1_mspinit 0 */
  /* Peripheral Clock Enable *
    /__hal_rcc_usart1_clk_enable ();
  
    /**usart1 GPIO Configuration    
    PA9     ------> Usart1_tx
    PA10     ------> Usart1_rx 
    * *
    Gpio_initstruct.pin = Gpio_pin_9;
    Gpio_initstruct.mode = gpio_mode_af_pp;
    Gpio_initstruct.speed = Gpio_speed_freq_high;
    Hal_gpio_init (Gpioa, &gpio_initstruct);

    Gpio_initstruct.pin = gpio_pin_10;
    Gpio_initstruct.mode = Gpio_mode_input;
    Gpio_initstruct.pull = Gpio_nopull;
    Hal_gpio_init (Gpioa, &gpio_initstruct);

  /* User code BEGIN usart1_mspinit 1 */

  * user code END usart1_mspinit 1 */
  }

(2) Main func

/** ****************************************************************************** * File name:main.c * D 
  Escription:main Program Body ****************************************************************************** * * COPYRIGHT (c) stmicroelectronics * * Redistribution and use of source and binary forms, with or without modif Ication, * is permitted provided that the following conditions is met: * 1. Redistributions of source code must retain the above copyright notice, * This list of conditions and the following
  Disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * This list of conditions and the FOLLOWI
  NG Disclaimer in the documentation * and/or and materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * could be used to endorse or promote produc TS derived from this software * without specific Prior written permission. * * This software are provided by the COPYRIGHT holders and CONTRIBUTORS "as are" * and any EXPRESS OR implied warrantie
  S, including, but not LIMITED to, the * implied warranties of merchantability and FITNESS for A particular PURPOSE is * disclaimed. In NO EVENT shall the COPYRIGHT HOLDER OR CONTRIBUTORS is liable * for any DIRECT, INDIRECT, incidental, special, Exempl ARY, or consequential * damages (including, but not LIMITED to, procurement of substitute GOODS OR * SERVICES; LOSS of Use, DATA, OR profits;  or business interruption) however * caused and on any theory of liability, WHETHER in contract, STRICT liability, * OR TORT (including negligence OR OTHERWISE) arising in any-out-of-the----the-software, even IF advised of the P
  Ossibility of SUCH DAMAGE. * ****************************************************************************** */* Includes-------------------- ----------------------------------------------*/#iNclude "Main.h" #include "stm32f1xx_hal.h" #include "variable.h"/* user code BEGIN includes */* user code END includ

ES */* Private variables---------------------------------------------------------*/Uart_handletypedef Huart1; /* User code BEGIN PV */* Private Variables---------------------------------------------------------*/* User code EN
D PV */* Private function prototypes-----------------------------------------------*/void Systemclock_config (void);
void Error_Handler (void);
static void Mx_gpio_init (void);

static void Mx_usart1_uart_init (void); /* User code BEGIN PFP */* Private function Prototypes-----------------------------------------------*/* User code E
ND PFP */* User code BEGIN 0 */* user code END 0 */unsigned char uart_rx_buffer[20] = {0}; int main (void) {/* user code BEGIN 1 */* User code END 1 */* MCU Configuration------------------------------ ----------------------------*/* Reset of all peripherals, initializes theFlash interface and the Systick.

  */Hal_init ();

  /* Configure the system clock */Systemclock_config ();
  /* INITIALIZE all configured Peripherals */Mx_gpio_init ();

  Mx_usart1_uart_init ();  /* User code BEGIN 2 */* User Code END 2 */* Infinite loop */* user code begin while */while (1) {/*
USER CODE END while *///Hal_uart_transmit (&huart1, (uint8_t *) & "33\n", 3, 100);
  Hal_delay (50);
		/* USER CODE BEGIN 3 */hal_uart_receive_it (&huart1,uart_rx_buffer,4);

  Hal_delay (50); }/* USER CODE END 3 */}/** System Clock Configuration */void Systemclock_config (void) {Rcc_oscinittypedef rcc_
  Oscinitstruct;

    Rcc_clkinittypedef rcc_clkinitstruct;
  /**initializes the CPU, AHB and APB busses clocks */rcc_oscinitstruct.oscillatortype = Rcc_oscillatortype_hse;
  Rcc_oscinitstruct.hsestate = rcc_hse_on;
  Rcc_oscinitstruct.hsepredivvalue = RCC_HSE_PREDIV_DIV1;
  Rcc_oscinitstruct.hsistate = rcc_hsi_on; Rcc_oscinitstruct.Pll.
  Pllstate = rcc_pll_on;
  RCC_OscInitStruct.PLL.PLLSource = Rcc_pllsource_hse;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (Hal_rcc_oscconfig (&rcc_oscinitstruct)! = HAL_OK) {Error_Handler (); }/**initializes the CPU, AHB and APB busses clocks */Rcc_clkinitstruct.clocktype = rcc_clocktype_hclk| RCC_CLOCKTYPE_SYSCLK | rcc_clocktype_pclk1|
  RCC_CLOCKTYPE_PCLK2;
  Rcc_clkinitstruct.sysclksource = RCC_SYSCLKSOURCE_PLLCLK;
  Rcc_clkinitstruct.ahbclkdivider = RCC_SYSCLK_DIV1;
  Rcc_clkinitstruct.apb1clkdivider = Rcc_hclk_div2;

  Rcc_clkinitstruct.apb2clkdivider = RCC_HCLK_DIV1;
  if (Hal_rcc_clockconfig (&rcc_clkinitstruct, flash_latency_2)! = HAL_OK) {Error_Handler ();

    }/**configure the Systick interrupt Time */Hal_systick_config (Hal_rcc_gethclkfreq ()/1000);

  /**configure the Systick */Hal_systick_clksourceconfig (SYSTICK_CLKSOURCE_HCLK); /* SYSTICK_IRQN Interrupt configuration */Hal_nviC_setpriority (systick_irqn, 0, 0); }/* USART1 init function */static void Mx_usart1_uart_init (void) {Huart1.
  Instance = USART1; Huart1.
  Init.baudrate = 9600; Huart1.
  Init.wordlength = uart_wordlength_8b; Huart1.
  Init.stopbits = Uart_stopbits_1; Huart1.
  init.parity = Uart_parity_none; Huart1.
  Init.mode = Uart_mode_tx_rx; Huart1.
  Init.hwflowctl = Uart_hwcontrol_none; Huart1.
  init.oversampling = uart_oversampling_16;
  if (Hal_uart_init (&huart1)! = HAL_OK) {Error_Handler ();
  } hal_nvic_setpriority (USART1_IRQN, 1, 1);  
  HAL_NVIC_ENABLEIRQ (USART1_IRQN); 
	__hal_uart_enable (&HUART1);

Hal_uart_receive_it (&huart1,uart_rx_buffer,4); } void Hal_uart_rxcpltcallback (Uart_handletypedef *uarthandle) {if (uart_rx_buffer[0] = = ' 0 ' && uart_rx_buff
		ER[3] = = ' 3 ') {Hal_uart_transmit (&huart1, (uint8_t *) &uart_rx_buffer[1], 2, 100);
	Hal_uart_transmit (&huart1, (uint8_t *) & "\ n", 1,100); }//hal_uart_receive_it (&huart1,uart_rx_buffer,4);
Hal_uart_transmit (&huart1, (uint8_t *) & "22\n", 3, 100); }/** Configure Pins as * Analog * Input * Output * event_out * exti * * STAT

IC void Mx_gpio_init (void) {/* GPIO Ports Clock Enable */__hal_rcc_gpioa_clk_enable ();
  }/* User code BEGIN 4 */* User code END 4 *//** * @brief This function is executed in case of error occurrence. * @param None * @retval none */void Error_Handler (void) {/* user CODE BEGIN Error_Handler */* user can add

His own implementation to report the HAL error return state */while (1) {}/* USER CODE END Error_Handler */} #ifdef Use_full_assert/** * @brief Reports The name of the source file and the source line number * where the
   Ert_param error has occurred.
   * @param file:pointer to the source file name * @param line:assert_param Error Line source number * @retval None */void Assert_failed (uint8_t* file, uint32_t line) {/* USER CODE BEGIN 6 */* User can add his own implementation to report the file name and line number, ex:printf ("wrong p
Arameters value:file%s on line%d\r\n ", file, line) */*/* USER CODE END 6 */} #endif/** * @} *//** * @}
 *//************************ (C) COPYRIGHT STMicroelectronics *****end of file****/




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.