[RISC-V MCU 应用开发] 第六十三章、CH32V103应用教程——USART-同步模式

[复制链接]
 楼主| RISCVLAR 发表于 2021-1-26 16:16 | 显示全部楼层 |阅读模式
USART, pi, ST, ni, IO
本帖最后由 RISCVLAR 于 2021-1-26 16:15 编辑

CH32V103应用教程——USART-同步模式

本章教程使用USART2作主机,SPI1作从机,进行全双工收发数据。

1、USART简介及相关函数介绍
同步模式使得系统在使用USART模块时可以输出时钟信号。在开启同步模式对外发送数据时,CK引脚会同时对外输出时钟。
开启同步模式的方式是对控制寄存器2(R16_USARTx_CTLR2)的CLKEN位置位,但同时需要关闭LIN模式、智能卡模式、红外模式和半双工模式,即保证 SCEN、HDSEL和IREN位处于复位状态,这三位在控制寄存器3(R16_USARTx_CTLR3)中。
同步模式使用的要点在于时钟的输出控制。有以下几点需要注意:
1)USART 模块同步模式只工作在主模式,即 CK 引脚只输出时钟,不接收输入;
2)只在 TX 引脚输出数据时输出时钟信号;
3)LBCL 位决定在发送最后一位数据位时是否输出时钟,CPOL 位决定时钟的极性,CPHA 决定时钟的相位,这三个位在控制寄存器2(R16_USARTx_CTLR2)中,这三个位需要在 TE 和 RE 未被使能的情况下设置,具体区别见下图。
图片1.png
4)接收器在同步模式下只会在输出时钟时采样,需要从设备保持一定的信号建立时间和保持时间,具体见下图。  
图片2.png
关于CH32V103 USART具体信息,可参考CH32V103应用手册。USART标准库函数在第三章节已介绍,在此不再赘述。

2、硬件设计
本章教程使用开发板USART2作主机,SPI1作从机,全双工收发数据。具体连接方式如下:
硬件连线:PA4  —— PA5
      PA2  —— PA7
      PA3  —— PA6

3软件设计
本章教程使用开发板USART2作主机,SPI1作从机,全双工收发数据,具体程序如下:
usart.h文件
  1. #ifndef __USART_H
  2. #define __USART_H

  3. #include "ch32v10x_conf.h"

  4. /* Global typedef */
  5. typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;

  6. void USART2_ReCFG(void);
  7. void SPI1_INIT(void);
  8. TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength);

  9. #endif
usart.h文件主要进行相关定义和函数声明;
usart.c文件
  1. #include "usart.h"

  2. /*******************************************************************************
  3. * Function Name  : USART2_ReCFG
  4. * Description    : ReInitializes the USART2 peripheral.
  5. * Input          : None
  6. * Return         : None
  7. *******************************************************************************/
  8. void USART2_ReCFG(void)
  9. {
  10.     GPIO_InitTypeDef  GPIO_InitStructure;
  11.     USART_InitTypeDef USART_InitStructure;
  12.     USART_ClockInitTypeDef USART_ClockInitStructure;

  13.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
  14.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);

  15.     /* USART2  Ck-->A.4   TX-->A.2   RX-->A.3 */
  16.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
  17.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  18.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  19.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  20.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  21.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  22.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  23.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  24.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  25.     USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
  26.     USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;           /* Clock is active High */
  27.     USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;          /* Data is captured on the second edge */
  28.     USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;   /* The clock pulse of the last data bit is output to the SCLK pin */
  29.     USART_ClockInit(USART2, &USART_ClockInitStructure);

  30.     USART_InitStructure.USART_BaudRate = 115200;                 //设置串口波特率为115200
  31.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //字长为8位数据格式
  32.     USART_InitStructure.USART_StopBits = USART_StopBits_1;       //1个停止位
  33.     USART_InitStructure.USART_Parity = USART_Parity_No;          //无奇偶校验位
  34.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件流控制
  35.     USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //发送和接收模式
  36.     USART_Init(USART2, &USART_InitStructure);                    //初始化串口

  37.     USART_Cmd(USART2, ENABLE);
  38. }

  39. /*******************************************************************************
  40. * Function Name  : SPI1_INIT
  41. * Description    : Initializes the SPI1 to be Slave Mode.
  42. * Input          : None
  43. * Return         : None
  44. *******************************************************************************/
  45. void SPI1_INIT(void)
  46. {
  47.     GPIO_InitTypeDef GPIO_InitStructure;
  48.     SPI_InitTypeDef SPI_InitStructure;

  49.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
  50.     SPI_StructInit(&SPI_InitStructure);
  51.     SPI_I2S_DeInit(SPI1);

  52.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;               /* SPI1 MISO-->PA.6 */
  53.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  54.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  55.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  56.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;  /* SPI1 SCK-->PA.5 MOSI-->PA.7 */
  57.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  58.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  59.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI通讯方向为双线全双工方式
  60.     SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;      //设置SPI为从机模式
  61.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI通讯的数据帧大小为8位
  62.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;  //设置SPI的时钟极性为高电平
  63.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //设置SPI的时钟相位为在SCK的偶数边沿采集数据
  64.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;    //设置NSS引脚(即片选引脚)的使用模式为软件模式
  65.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
  66.     SPI_Init(SPI1, &SPI_InitStructure);

  67.     SPI_Cmd(SPI1, ENABLE);
  68. }
  69. /*******************************************************************************
  70. * Function Name  : Buffercmp
  71. * Description    : Compares two buffers
  72. * Input          : Buf1,Buf2:buffers to be compared
  73. *                  BufferLength: buffer's length
  74. * Return         : PASSED: Buf1 identical to Buf2
  75. *                  FAILED: Buf1 differs from Buf2
  76. *******************************************************************************/
  77. TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength)
  78. {
  79.   while(BufLength--)
  80.   {
  81.     if(*Buf1 != *Buf2)
  82.     {
  83.       return FAILED;
  84.     }
  85.     Buf1++;
  86.     Buf2++;
  87.   }
  88.   return PASSED;
  89. }<span style="font-family: 宋体; font-size: large; text-indent: 24pt; background-color: rgb(255, 255, 255);"> </span>
usart.c文件主要包括三个函数:USART2_ReCFG函数、SPI1_INIT函数以及Buffercmp函数。USART2_ReCFG函数主要进行串口2初始化配置;SPI1_INIT函数主要对SPI1进行从机初始化配置;Buffercmp函数主要进行发送数据和接收数据的比较。
main.c文件
  1. int main(void)
  2. {
  3.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  4.     Delay_Init();
  5.     USART_Printf_Init(115200);
  6.     printf("SystemClk:%d\r\n",SystemCoreClock);

  7.     printf("USART SynchromousMode TEST\r\n");
  8.     USART2_ReCFG();                                                   /* USART2 ReInitializes */
  9.     SPI1_INIT();

  10.     while(TxCnt1<TxSize1)                                             /* USART2--->SPI1 */
  11.     {
  12.         USART_SendData(USART2, TxBuffer1[TxCnt1++]);
  13.         while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)     /* waiting for sending finish */
  14.         {
  15.         }
  16.         while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
  17.     {
  18.     }
  19.         RxBuffer2[RxCnt2++] = SPI_I2S_ReceiveData(SPI1);
  20.     }
  21.     USART_ReceiveData(USART2);                                        /* Clear the USART2 Data Register */
  22.     while(TxCnt2<TxSize2)                                             /* SPI1--->USART2 */
  23.     {
  24.         while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)== RESET)    /* waiting for sending finish */
  25.     {
  26.     }
  27.         SPI_I2S_SendData(SPI1, TxBuffer2[TxCnt2++]);
  28.         USART_SendData(USART2, Tempdata);                               /* Send Tempdata for SCK */
  29.         while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
  30.         {
  31.         }
  32.     while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
  33.     {
  34.     }
  35.         RxBuffer1[RxCnt1++] = USART_ReceiveData(USART2);
  36.     }

  37.     TransferStatus1=Buffercmp(TxBuffer1,RxBuffer2,TxSize1);
  38.     TransferStatus2=Buffercmp(TxBuffer2,RxBuffer1,TxSize2);
  39.     if(TransferStatus1&&TransferStatus2)
  40.     {
  41.       printf("\r\nSend Success!\r\n");
  42.     }
  43.     else
  44.     {
  45.       printf("\r\nSend Fail!\r\n");
  46.     }
  47.     printf("TxBuffer1---->RxBuffer2     TxBuffer2---->RxBuffer1\r\n");
  48.     printf("TxBuffer1:%s\r\n",TxBuffer1);
  49.     printf("RxBuffer1:%s\r\n",RxBuffer1);
  50.     printf("TxBuffer2:%s\r\n",TxBuffer2);
  51.     printf("RxBuffer2:%s\r\n",RxBuffer2);

  52.     while(1)
  53.     {
  54.     }
  55. }
main.c文件进行USART2以及SPI1的数据发送和接收,并将发送和接收数据打印显示。

4下载验证
将编译好的程序下载到开发版并复位,串口打印如下:
图片3.png

62、USART-同步模式.rar

490.06 KB, 下载次数: 111

734774645 发表于 2021-2-22 11:59 | 显示全部楼层
这个模式没用过,学习学习。
答案很长吧 发表于 2021-2-23 15:52 | 显示全部楼层
这个模式之前的时候,没有使用过,我需要仔细消化一下的。
看别人照片 发表于 2021-2-23 16:34 | 显示全部楼层
我想问一下,串口的几种模式,都有什么区别?一般应用在什么场合?

评论

这个可以去手册看具体介绍  发表于 2021-2-23 17:28
jvfthvjhgc 发表于 2021-3-2 13:40 | 显示全部楼层
我在仿真过程中,spi在11和10模式下接收的数据不是usart发送的数据,在00和01模式下就正常,请问可能是什么原因呢?下图为11模式通信结果

spi在11模式下

spi在11模式下


评论

[url=home.php?mod=space&uid=3141872]@RISCVLAR[/url] :好的,谢谢  发表于 2021-3-3 11:33
[url=home.php?mod=space&uid=2633538]@jvfthvjhgc[/url] :你可以去查查SPI配置时时钟极性和时钟相位不同配置模式下的区别, CPOL和CPHA可以组成四种模式,不同模式下SCK时钟和采样时刻不同的,你可以百度具体查一下深入了解一下  发表于 2021-3-2 16:25
[url=home.php?mod=space&uid=3141872]@RISCVLAR[/url] :我的意思就是当CPOL = HIGH CPHA = 2EDGE 和CPOL = LOW CPHA = 2EDGE时数据会出现图上问题 当CPOL = HIGH CPHA = 1EDGE 和CPOL = LOW CPHA = 1EDGE时通信正常  发表于 2021-3-2 15:19
修改SPI配置里极性、采样时刻看看  发表于 2021-3-2 14:52
momoto 发表于 2023-4-17 23:01 | 显示全部楼层
USART_SendData(USART2, Tempdata);                               /* Send Tempdata for SCK */
这句的含义是什么意思啊,为什么要插入这句呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

133

主题

296

帖子

44

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

133

主题

296

帖子

44

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