前言
我们平时在写代码,特别是用到UART时,希望可以动态更改波特率,这样可以更方便地适应各种环境。现在我们就来看看如何在CYPRESS的BLE中实现动态更改波特率。
PSOC Creator配置界面的设置

时钟的配置

UART配置


关键代码的实现
- 宏定义
#define UART_BUFFER_SIZE (512u)//这里我设置缓冲区大小是512Bytes
uint8 bufferRx[UART_BUFFER_SIZE + 1u]; /* RX software buffer requires one extra entry for correct operation in UART mode */
uint8 bufferTx[UART_BUFFER_SIZE]; /* TX software buffer 发送缓冲区可以不用这么大,一般8字节就够用了*/
/***************************************
* 串口波特率设置
* Div SCBCLK=48MHz/BAUD_RATE*OVERSAMPLE-1
* 其中注释的是CYPRESS不支持的波特率
***************************************/
#define BAUD_1200 (2499u)
#define BAUD_2400 (1249u)
#define BAUD_4800 (624u)
#define BAUD_9600 (312u)
//#define BAUD_14400 (207u)
#define BAUD_19200 (155u)
//#define BAUD_28800 (103u)
#define BAUD_38400 (77u)
#define BAUD_57600 (51u)
//#define BAUD_64000 (46u)
//#define BAUD_76800 (38u)
#define BAUD_115200 (25u) /* UART: 115200 kbps with OVS = 16. Required SCBCLK = 1.846 MHz, Div = 26 */
- UART参选数的初始化配置
UART_UART_INIT_STRUCT configUart;//定义一个全局的结构体变量
/******************************************************************************
* Function Name: ConfigUartInit
***************************************************************************//**
*
* UART配置初始化
*
* \return
* None
*
******************************************************************************/
void ConfigUartInit(void)
{
configUart.mode=UART_UART_MODE_STD; /* mode: Standard */
configUart.direction=UART_UART_TX_RX; /* direction: RX + TX */
configUart.dataBits=8; /* dataBits: 8 bits */
configUart.parity=UART_UART_PARITY_NONE; /* parity: None */
configUart.stopBits=UART_UART_STOP_BITS_1; /* stopBits: 1 bit */
configUart.oversample=16; /* oversample: 16u */
configUart.enableIrdaLowPower=0; /* enableIrdaLowPower: disable */
configUart.enableMedianFilter=1; /* enableMedianFilter: enable */
configUart.enableRetryNack=0; /* enableRetryNack: disable */
configUart.enableInvertedRx=0; /* enableInvertedRx: disable */
configUart.dropOnParityErr=0; /* dropOnParityErr: disable */
configUart.dropOnFrameErr=0; /* dropOnFrameErr: disable */
configUart.enableWake=0; /* enableWake: disable */
configUart.rxBufferSize=UART_BUFFER_SIZE; /* rxBufferSize: software buffer 10 bytes */
configUart.rxBuffer=bufferRx; /* rxBuffer: RX software buffer enable */
configUart.txBufferSize=UART_BUFFER_SIZE; /* txBufferSize: software buffer 10 bytes */
configUart.txBuffer=bufferTx; /* txBuffer: TX software buffer enable */
configUart.enableMultiproc=0; /* enableMultiproc: disable */
configUart.multiprocAcceptAddr=0; /* multiprocAcceptAddr: disable */
configUart.multiprocAddr=0; /* multiprocAddr: N/A for this configuration */
configUart.multiprocAddrMask=0; /* multiprocAddrMask: N/A for this configuration */
configUart.enableInterrupt=1; /* enableInterrupt: enable to process software buffer */
configUart.rxInterruptMask=UART_INTR_RX_NOT_EMPTY; /* rxInterruptMask: enable NOT_EMPTY for RX software buffer operations */
configUart.rxTriggerLevel=0; /* rxTriggerLevel: N/A for this configuration */
configUart.txInterruptMask=0; /* txInterruptMask: NOT_FULL is enabled when there is data to transmit */
configUart.txTriggerLevel=0; /* txTriggerLevel: N/A for this configuration */
}
- 开启UART
其中的DIVIDER_CLOCK就是波特率参数,具体的值如上面的宏定义所示
/******************************************************************************
* Function Name: SetUartConfiguration
***************************************************************************//**
*
* 设置串口配置
* \param DIVIDER_CLOCK:配置波特率的时钟分频,不同的频率分频系统不同。
* \return
* CYRET_SUCCESS //Successful
* CYRET_BAD_PARAM //Uknowns operation mode - no actions
******************************************************************************/
void SetUartConfiguration(uint32_t DIVIDER_CLOCK)
{
// cystatus status = CYRET_SUCCESS;
UART_Stop(); /* Disable component before configuration change */
/* Change clock divider */
UART_CLOCK_Stop();
UART_CLOCK_SetFractionalDividerRegister(DIVIDER_CLOCK, 0u);
UART_CLOCK_Start();
/* Configure to UART operation */
UART_UartInit(&configUart);
UART_Start(); /* Enable component after configuration change */
}
总结
如果想要动态地更改波特率,只需要调用SetUartConfiguration(波特率)即可实现。