本帖最后由 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 未被使能的情况下设置,具体区别见下图。 4)接收器在同步模式下只会在输出时钟时采样,需要从设备保持一定的信号建立时间和保持时间,具体见下图。 关于CH32V103 USART具体信息,可参考CH32V103应用手册。USART标准库函数在第三章节已介绍,在此不再赘述。
2、硬件设计 本章教程使用开发板USART2作主机,SPI1作从机,全双工收发数据。具体连接方式如下: 硬件连线:PA4 —— PA5 PA2 —— PA7 PA3 —— PA6
3、软件设计 本章教程使用开发板USART2作主机,SPI1作从机,全双工收发数据,具体程序如下: usart.h文件 - #ifndef __USART_H
- #define __USART_H
- #include "ch32v10x_conf.h"
- /* Global typedef */
- typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
- void USART2_ReCFG(void);
- void SPI1_INIT(void);
- TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength);
- #endif
usart.h文件主要进行相关定义和函数声明; usart.c文件 - #include "usart.h"
- /*******************************************************************************
- * Function Name : USART2_ReCFG
- * Description : ReInitializes the USART2 peripheral.
- * Input : None
- * Return : None
- *******************************************************************************/
- void USART2_ReCFG(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- USART_ClockInitTypeDef USART_ClockInitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
- /* USART2 Ck-->A.4 TX-->A.2 RX-->A.3 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
- USART_ClockInitStructure.USART_CPOL = USART_CPOL_High; /* Clock is active High */
- USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; /* Data is captured on the second edge */
- USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable; /* The clock pulse of the last data bit is output to the SCLK pin */
- USART_ClockInit(USART2, &USART_ClockInitStructure);
- USART_InitStructure.USART_BaudRate = 115200; //设置串口波特率为115200
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //1个停止位
- USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件流控制
- USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //发送和接收模式
- USART_Init(USART2, &USART_InitStructure); //初始化串口
- USART_Cmd(USART2, ENABLE);
- }
- /*******************************************************************************
- * Function Name : SPI1_INIT
- * Description : Initializes the SPI1 to be Slave Mode.
- * Input : None
- * Return : None
- *******************************************************************************/
- void SPI1_INIT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
- SPI_StructInit(&SPI_InitStructure);
- SPI_I2S_DeInit(SPI1);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; /* SPI1 MISO-->PA.6 */
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; /* SPI1 SCK-->PA.5 MOSI-->PA.7 */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI通讯方向为双线全双工方式
- SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; //设置SPI为从机模式
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI通讯的数据帧大小为8位
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //设置SPI的时钟极性为高电平
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //设置SPI的时钟相位为在SCK的偶数边沿采集数据
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //设置NSS引脚(即片选引脚)的使用模式为软件模式
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
- SPI_Init(SPI1, &SPI_InitStructure);
- SPI_Cmd(SPI1, ENABLE);
- }
- /*******************************************************************************
- * Function Name : Buffercmp
- * Description : Compares two buffers
- * Input : Buf1,Buf2:buffers to be compared
- * BufferLength: buffer's length
- * Return : PASSED: Buf1 identical to Buf2
- * FAILED: Buf1 differs from Buf2
- *******************************************************************************/
- TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength)
- {
- while(BufLength--)
- {
- if(*Buf1 != *Buf2)
- {
- return FAILED;
- }
- Buf1++;
- Buf2++;
- }
- return PASSED;
- }<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文件 - int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- Delay_Init();
- USART_Printf_Init(115200);
- printf("SystemClk:%d\r\n",SystemCoreClock);
- printf("USART SynchromousMode TEST\r\n");
- USART2_ReCFG(); /* USART2 ReInitializes */
- SPI1_INIT();
- while(TxCnt1<TxSize1) /* USART2--->SPI1 */
- {
- USART_SendData(USART2, TxBuffer1[TxCnt1++]);
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
- {
- }
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
- {
- }
- RxBuffer2[RxCnt2++] = SPI_I2S_ReceiveData(SPI1);
- }
- USART_ReceiveData(USART2); /* Clear the USART2 Data Register */
- while(TxCnt2<TxSize2) /* SPI1--->USART2 */
- {
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)== RESET) /* waiting for sending finish */
- {
- }
- SPI_I2S_SendData(SPI1, TxBuffer2[TxCnt2++]);
- USART_SendData(USART2, Tempdata); /* Send Tempdata for SCK */
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
- {
- }
- while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
- {
- }
- RxBuffer1[RxCnt1++] = USART_ReceiveData(USART2);
- }
- TransferStatus1=Buffercmp(TxBuffer1,RxBuffer2,TxSize1);
- TransferStatus2=Buffercmp(TxBuffer2,RxBuffer1,TxSize2);
- if(TransferStatus1&&TransferStatus2)
- {
- printf("\r\nSend Success!\r\n");
- }
- else
- {
- printf("\r\nSend Fail!\r\n");
- }
- printf("TxBuffer1---->RxBuffer2 TxBuffer2---->RxBuffer1\r\n");
- printf("TxBuffer1:%s\r\n",TxBuffer1);
- printf("RxBuffer1:%s\r\n",RxBuffer1);
- printf("TxBuffer2:%s\r\n",TxBuffer2);
- printf("RxBuffer2:%s\r\n",RxBuffer2);
- while(1)
- {
- }
- }
main.c文件进行USART2以及SPI1的数据发送和接收,并将发送和接收数据打印显示。
4、下载验证 将编译好的程序下载到开发版并复位,串口打印如下:
|