查看: 2160|回复: 4

[经验] c++gd207usart缓存收发

[复制链接]
  • TA的每日心情
    开心
    2017-9-24 20:12
  • 签到天数: 3 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2016-1-23 22:33:53 | 显示全部楼层 |阅读模式
    分享到:
    收到gd207的板子还是原来的红黄色可惜没有USB 只能使用串口和PC通信
    #include "colibri_bsp_uart.h"
    //namespace mydfinenamespace{
    #define TXSIZE  10
    #define RXSIZE  10
    namespace hjw{
    class mydefine{
    public :
            mydefine();
            ~mydefine();
            void mydefineInit(void);
            void usart_write();
            void usart_read();
            void usart_writefifo(char txdata);
      bool is_usartready(void);
      void set_usartready(void);
            static void bufferInit();
      static char TxBuffer[2][TXSIZE];
            static char RxBuffer[2][RXSIZE];
            unsigned char txselect;
            static unsigned char txcnt;
      static unsigned char rxcnt;
      char txready;
    };
    }
    using namespace hjw;
    char mydefine::TxBuffer[2][TXSIZE]={{0},{0}};
    char mydefine::RxBuffer[2][TXSIZE]={{0},{0}};
            unsigned char  mydefine::txcnt=0;
      unsigned char  mydefine::rxcnt=0;
    void mydefine::usart_write(void)
    {
           if(is_usartready()==0){
                                      return ;
                             }
                        USART_DataSend(USART1,TxBuffer[txselect][txcnt]);
            while (USART_GetBitState(USART1, USART_FLAG_TBE) == RESET)
                  ;
                                if((txcnt==0)&&(txselect==1)){
                                       txselect=0;
                                       txcnt=TXSIZE-1;
                                     }                       
                                     if((txcnt==0)&&(txselect==0))
                                            {
                                             txcnt=0;
                                              txready=0;
                                      }               
                                            txcnt--;       
    }
    void mydefine::usart_writefifo(char txdata)
    {
                            TxBuffer[txselect][txcnt++]=txdata;
                            if(txcnt==RXSIZE){
                                    txcnt=0;
                                    txselect=!txselect?1:0;
                            }               
    }
    bool mydefine::is_usartready(void)
    {
       return txready;
    }
    void mydefine::set_usartready(){
             txready=1;
            txcnt=txcnt-1;
    }
    void mydefine::usart_read()
    {
    }

    代码就是上面的 使用双缓冲接收发送的数据再将接收的数据整体发送出去类似中断或者DMA方式处理数据不用对数据单个发送。缺点就是大于最大长度就是溢出和复位不容易停止,有喜欢的朋友可以来讨论下。



    回复

    使用道具 举报

  • TA的每日心情
    开心
    2017-9-24 20:12
  • 签到天数: 3 天

    连续签到: 1 天

    [LV.2]偶尔看看I

     楼主| 发表于 2016-1-23 22:34:55 | 显示全部楼层
    /* ´¦ÀíÆ÷BOOTÖ®ºó»áµ÷ÓÃmainº¯Êý£¬±ØÐëÌṩ */

    int main(void)
    {
        /* ×¢²á¸÷¸öÄں˺¯Êý£¬Æô¶¯ÄÚºË */

            mydefine  own;
    own.mydefineInit();
           
    own.usart_writefifo(0x23);
    own.usart_writefifo(0x24);
    own.usart_writefifo(0x25);
             own.usart_writefifo(0x1);
    own.usart_writefifo(0x2);
    own.usart_writefifo(0x3);
             own.usart_writefifo(0x4);
    own.usart_writefifo(0x5);
    own.usart_writefifo(0x6);
             own.usart_writefifo(0x7);
    own.usart_writefifo(0x8);
    own.usart_writefifo(0x9);
            own.usart_writefifo(0x10);
    own.usart_writefifo(0x11);
            own.usart_writefifo(0x12);
    own.usart_writefifo(0x13);

      own.usart_writefifo(0x14);
            own.usart_writefifo(0x15);
    own.usart_writefifo(0x16);

            own.set_usartready();
            own.set_usartready();
            while(1)
            {
                     own.usart_write();
                     
            }
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2016-1-24 00:28:29 | 显示全部楼层
    顶一个
    编程结构可以
    学习学习
    回复 支持 反对

    使用道具 举报

  • TA的每日心情

    2021-12-7 12:35
  • 签到天数: 1354 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2016-1-24 22:28:50 | 显示全部楼层
    写的好,参考和学习了
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-11-19 16:39
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2016-1-25 10:08:37 | 显示全部楼层
    亲,可以将内容一并发到经验频道,很有机会获得每月之星的呢http://jingyan.eeboard.com/
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-5-10 17:50 , Processed in 0.156716 second(s), 22 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.