查看: 1586|回复: 0
收起左侧

第六十三章:CH32V103应用教程——USART-同步模式

[复制链接]

  离线 

  • TA的每日心情
    慵懒
    2021-7-23 17:16
  • 签到天数: 17 天

    [LV.4]

    发表于 2021-4-30 18:38:07 | 显示全部楼层 |阅读模式

    有人预言,RISC-V或将是继Intel和Arm之后的第三大主流处理器体系。欢迎访问全球首家只专注于RISC-V单片机行业应用的中文网站

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    本帖最后由 草帽王子 于 2021-9-10 17:52 编辑

    本章教程使用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 未被使能的情况下设置,具体区别见下图。
    CH32V CH573单片机芯片-第六十三章:CH32V103应用教程——USART-同步模式risc-v单片机中文社区(1)
    4)接收器在同步模式下只会在输出时钟时采样,需要从设备保持一定的信号建立时间和保持时间,具体见下图。  
    CH32V CH573单片机芯片-第六十三章:CH32V103应用教程——USART-同步模式risc-v单片机中文社区(2)
    关于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、下载验证

    将编译好的程序下载到开发版并复位,串口打印如下:
    CH32V CH573单片机芯片-第六十三章:CH32V103应用教程——USART-同步模式risc-v单片机中文社区(3)
    62、USART-同步模式.rar
    CH32V CH573单片机芯片-第六十三章:CH32V103应用教程——USART-同步模式risc-v单片机中文社区(4) 62、USART-同步模式.rar (490.06 KB, 下载次数: 10)
    链接:https://pan.baidu.com/s/1A9qaFUVlNCZ1mKI2SY6DIQ
    提取码:nftt
    复制这段内容后打开百度网盘手机App,操作更方便哦







    上一篇:第六十二章:CH32V103应用教程——USART-硬件流控制
    下一篇:第六十四章:CH32V103应用教程——USART-中断
    RISCV作者优文
    全球首家只专注于RISC-V单片机行业应用的中文网站
    回复

    使用道具 举报

    高级模式
    B Color Image Link Quote Code Smilies

    本版积分规则

    关闭

    RISC-V单片机中文网上一条 /2 下一条



    版权及免责声明|RISC-V单片机中文网 |网站地图

    GMT+8, 2024-3-28 21:30 , Processed in 0.563393 second(s), 48 queries .

    快速回复 返回顶部 返回列表