zoukankan      html  css  js  c++  java
  • 基于C8T6的DA14580蓝牙方案

    1:高电平表示连接断开,低电平表示连接成功。

     2:设置为高,表示从机,设置为低,表示主机。

    3:设置为高,进入透传模式,设置为低,进入配置模式

    4:没有收到数据,保持高电平。收到数据,拉低,10ms后,串口开始发数据。

    5。设置为高,立即休眠。设置为低,立即唤醒。

    一个简单的串口响应代码。判定是否为一个有效帧,如果是,返回同样的内容。

    #ifndef _HJ580X_APP_HA_
    #define _HJ580X_APP_HA_
    /********************************Data Structure*****************/
    #define QueueSize 255
    #define T unsigned char
    typedef struct{
        T data[QueueSize];
        int front,rear;
        int count;
    }CirQueue,*pCirQueue;
    
    void InitQueue(pCirQueue Q);
    int QueueEmpty(pCirQueue Q);
    int QueueFull (pCirQueue Q);
    void EnQueue(pCirQueue Q,T element);
    T GetFront(pCirQueue Q);
    T DeQueue(pCirQueue Q);
    void DeQueueN(pCirQueue Q,int n);
    /*******************************************************************/
    /*process protocal*/
    void handle_protocal(pCirQueue);
    int isFrame(pCirQueue);
    void handle_cmd(pCirQueue);
    void prepareBuf(pCirQueue,unsigned char*,int);
    void FlushBuf(unsigned char*,int);
    extern pCirQueue pQ;
    #endif
    
    
    
    #include "HJ580X.h"
    #include "USART.h"
    #define PSIZE 40
    CirQueue dataQ;
    pCirQueue pQ=&dataQ;
    unsigned char sendBuf[PSIZE];
    
    void handle_protocal(pCirQueue Q){
        while(Q->count>=PSIZE){
            if(isFrame(Q)){
                handle_cmd(Q);
                DeQueueN(Q,PSIZE);
            }else{
                DeQueue(Q);
            }
        }
    }
    void handle_cmd(pCirQueue Q){
        unsigned char cmd;
        cmd=Q->data[(Q->front+2)%QueueSize];
        switch(cmd){
            case 7:
                prepareBuf(Q,sendBuf,PSIZE);
                FlushBuf(sendBuf,PSIZE);
                break;
            default:
                break;
        }
    }
    void prepareBuf(pCirQueue Q,unsigned char* buf,int len)
    {
        int i=0;
        for(;i<len;i++){
            buf[i]=Q->data[(Q->front+i)%QueueSize];
        }
    }
    void FlushBuf(unsigned char* buf,int len)
    {
        int i;
        for(i=0;i<len;i++){
        USART_SendData(USART1,buf[i] );//
           while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
        }
    }
    int isFrame(pCirQueue Q){
        unsigned char a1;
        unsigned char a2;
        unsigned char b1;
        unsigned char b2;
        unsigned short sum;
        unsigned char chk_sum;
        unsigned char tmp_sum;
        int i;
        if(Q->count<PSIZE)
            return 0;
        a1=Q->data[Q->front];
        a2=Q->data[(Q->front+1)%QueueSize];
        b1=Q->data[(Q->front+PSIZE-2)%QueueSize];
        b2=Q->data[(Q->front+PSIZE-1)%QueueSize];
        sum=0;
        for(i=2;i<PSIZE-3;i++){
        sum+=Q->data[(Q->front+i)%QueueSize];
        }
        sum%=256;
        chk_sum=0xFF&(sum);
        tmp_sum=Q->data[(Q->front+PSIZE-3)%QueueSize];
        if(a1==0x55 && a2==0x55 && b1==0xAA && b2==0xAA &&chk_sum==tmp_sum)
            return 1;
        
        return 0;
        
    }    
    /*Data Strcture*?//////*/
    void InitQueue(pCirQueue Q){
        Q->front=Q->rear=0;
        Q->count=0;
    }
    int QueueEmpty(pCirQueue Q){
        return Q->rear==Q->front;
    }
    int QueueFull (pCirQueue Q){
        return (Q->rear+1)%QueueSize ==Q->front;
    }
    void EnQueue(pCirQueue Q,T element){
        if(!QueueFull(Q)){
            Q->data[Q->rear]=element;
            Q->rear=(Q->rear +1)%QueueSize;
            (Q->count)++;
        }
    }
    T GetFront(pCirQueue Q){
        if(!QueueEmpty(Q)){
            return Q->data[Q->front];
        }
    }
    T DeQueue(pCirQueue Q){
        T x;
        if(!QueueEmpty(Q)){
            x=Q->data[Q->front];
            Q->front=(Q->front+1)%QueueSize;
            (Q->count)--;
            return x;
        }
    }
    void DeQueueN(pCirQueue Q,int n){
        int i;
        for(i=0;i<n;i++){
            DeQueue(Q);
        }
    }
    View Code

    使用的时候,

  • 相关阅读:
    设计模式---适配器模式
    【面经】2019-4-1 杭州边锋网络面经
    web前端基础——jQuery编程进阶
    web前端基础——jQuery编程基础
    web前端基础——初识HTML DOM编程
    web前端基础——初识JavaScript
    web前端基础——初识CSS
    web前端基础——初识HTML
    CentOS 6.5上安装python2.7、pip以及Python命令行补全和yum冲突解决
    Python中常用技巧整理
  • 原文地址:https://www.cnblogs.com/legion/p/6767393.html
Copyright © 2011-2022 走看看