CYPRESS BLE学习心得(三):如何在CYPRESS BLE中动态更改波特率

[复制链接]
923|0
手机看帖
扫描二维码
随时随地手机跟帖
xiaolong_ba|  楼主 | 2016-6-30 12:52 | 显示全部楼层 |阅读模式
本帖最后由 xiaolong_ba 于 2016-6-30 12:52 编辑

写在最前面

   欢迎加入Cypress BLE技术讨论群130946269进行技术讨论,供应商和销售就不要进来了,发现了一律都T掉(不喜欢群里一堆广告,而不是持术问题)

一、前言   

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

二、PSOC Creator配置界面的设置

时钟的配置

UART配置


三、关键代码的实现

1、宏定义   
  • #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 */


2、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 */
  • }


3、开启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(波特率)即可实现。

如何在CYPRESS BLE中动态更改波特率.pdf

532.5 KB, 阅读权限: 20

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:CY BLE教程地址:http://bbs.wireless-tech.cn/

15

主题

88

帖子

8

粉丝