[MM32硬件] 【灵动微电子MM32F0121测评】2.串口1收发

[复制链接]
4745|1
 楼主| 阿源玩电子 发表于 2025-6-23 22:12 | 显示全部楼层 |阅读模式
本帖最后由 阿源玩电子 于 2025-6-23 22:20 编辑

串口1收发

  • 原理图

串口部分原理图

串口部分原理图

2.编辑相应的驱动文件

usart_dsp.C
  1. #include "usart_dsp.h"

  2. void USART1_InitConsole(uint32_t Baudrate)
  3. {
  4.     GPIO_InitTypeDef GPIO_InitStruct;
  5.     USART_InitTypeDef USART_InitStruct;

  6.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

  7.     USART_StructInit(&USART_InitStruct);
  8.     USART_InitStruct.USART_BaudRate      = Baudrate;
  9.     USART_InitStruct.USART_WordLength    = USART_WordLength_8b;
  10.     USART_InitStruct.USART_StopBits      = USART_StopBits_1;
  11.     USART_InitStruct.USART_Parity        = USART_Parity_No;
  12.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  13.     USART_InitStruct.USART_Mode          = USART_Mode_Tx | USART_Mode_Rx;
  14.     USART_Init(USART1, &USART_InitStruct);

  15.     RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE);
  16.        
  17.     GPIO_StructInit(&GPIO_InitStruct);
  18.           GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
  19.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
  20.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_FLOATING;
  21.     GPIO_Init(GPIOB, &GPIO_InitStruct);
  22.        
  23.     GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);

  24.     GPIO_StructInit(&GPIO_InitStruct);
  25.     GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_6;
  26.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  27.     GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;
  28.     GPIO_Init(GPIOB, &GPIO_InitStruct);
  29.     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  30.     USART_Cmd(USART1, ENABLE);
  31.                
  32.                
  33.     NVIC_InitTypeDef NVIC_InitStruct;
  34.     NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
  35.     NVIC_InitStruct.NVIC_IRQChannelPriority = 1;  
  36.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  37.     NVIC_Init(&NVIC_InitStruct);               
  38.                
  39. }


  40. int fputc(int ch, FILE *f)
  41. {
  42.     USART_SendData(USART1, (uint8_t)ch);

  43.     while (RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC))
  44.     {
  45.     }

  46.     return (ch);
  47. }


  48. void USART1_IRQHandler(void)
  49. {
  50.     if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  51.     {
  52.         uint8_t data = USART_ReceiveData(USART1);
  53.         USART_SendData(USART1, data);
  54.         while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  55.     }
  56. }

usart_dsp.h
  1. #include "hal_conf.h"
  2. #include <stdio.h>
  3. void USART1_InitConsole(uint32_t Baudrate);
3.main函数部分

  1. #include "main.h"
  2. int main(void)
  3. {
  4.                 GPIO_Configure();
  5.                 InitDelay();
  6.                 USART1_InitConsole(115200);
  7.     while (1)
  8.     {
  9.                                 GPIOB->ODR ^= GPIO_Pin_14 | GPIO_Pin_15;
  10.                                 Delay_MS(300);
  11.                                 printf("Hello 21ic, HelloMM32F0121  \r\n");
  12.     }
  13. }


4.试验现象

每300毫秒发送一次“Hello 21ic, HelloMM32F0121

发数据.jpg

收数据并且回发

收数据并且回发.jpg





MM32F0121C6PV_02USART.zip

5.46 MB, 下载次数: 0

工程

AdaMaYun 发表于 2025-7-31 17:49 | 显示全部楼层
串口收发通讯必备
您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

35

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部