⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 90can32_uart.c

📁 使用avr90can32双串口传输蓝牙数据
💻 C
📖 第 1 页 / 共 2 页
字号:
//由90can32_to_m128  日期:7-21
#include <avr/io.h>
#include <avr/iom128.h>
#include <avr/interrupt.h>
//#include <avr/signal.h>
#define uint  unsigned int
#define uchar unsigned char

#define BLUE   0 //pa2 examine led 

#define BlueOn() PORTA&=~(1<<BLUE)
#define BlueOff() PORTA|=(1<<BLUE)

#define BAUD_RATE_9600  9600
#define BAUD_RATE_57600 57600

#define XTAL_CPU_7 7372800
#define XTAL_CPU_8 8000000

#define UART_BAUD(baudrate,xtalcpu) (uint)((xtalcpu/(((long int)(baudrate))*16))-1)
/*****************************************************************************/
/***********************BT Moduel for one hundred meter drivers***************/
/*****************************************************************************/

const unsigned char HCIReset[4]={0x01,0x03,0x0C,0x00};                    //CMD
const unsigned char Respon_Reset[7]={0x04,0x0e,0x04,0x01,0x03,0x0c,0,};   //Should Received Data;
                                                                 //Down is same to this
const unsigned char HCISetEventFilter[5]={0x01,0x05,0x0C,0x01,0x00};
const unsigned char Respon_SetEventFilter[7]={0x04,0x0e,0x04,0x01,0x05,0x0c,0,};

const unsigned char HCIWriteScanEnable[5]={0x01,0x1A,0x0C,0x01,0x03};
const unsigned char Respon_WriteScanEnable[7]={0x04,0x0e,0x04,0x01,0x1a,0x0c,00,};

//const unsigned char HCIWriteAuthenticationEnable[5]={0x01,0x20,0x0C,0x01,0x00};     //not use
//const unsigned char Respon_WriteAuthenticationEnable[7]={0x04,0x0e,0x04,0x01,0x20,0x0c,00};

const unsigned char HCISetEventFilter1[7]={0x01,0x05,0x0C,0x03,0x02,0x00,0x02};
const unsigned char Respon_SetEventFilter1[7]={0x04,0x0e,0x04,0x01,0x05,0x0c,0x00,};

const unsigned char HCIWriteConnectionAcceptTimeout[6]={0x01,0x16,0x0C,0x02,0xa0,0x1f};  //5s
const unsigned char Respon_WriteConnectionAcceptTimeout[7]={0x04,0x0e,0x04,0x01,0x16,0x0c,0x00,};

const unsigned char HCIWriteConnectionAcceptTimeoutDefault[6]={0x01,0x16,0x0C,0x02,0x01,0x20};
const unsigned char Respon_WriteConnectionAcceptTimeoutDefault[7]=\
                                                       {0x04,0x0e,0x04,0x01,0x16,0x0c,0x00};

const unsigned char HCIWritePageTimeout[6]={0x01,0x18,0x0C,0x02,0x80,0x80};
const unsigned char Respon_WritePageTimeout[7]={0x04,0x0e,0x04,0x01,0x18,0x0c,0x00};

const unsigned char HCIWritePageTimeoutDefault[6]={0x01,0x18,0x0C,0x02,0x00,0x20};
const unsigned char Respon_WritePageTimeoutDefault[7]={0x04,0x0e,0x04,0x01,0x18,0x0c,0x00};

const unsigned char HCIWriteScanActivity[8]={0x01,0x1C,0x0C,0x04,0x00,0x01,0x48,0x05};
const unsigned char Respon_WriteScanActivity[7]={0x04,0x0e,0x04,0x01,0x1C,0x0C,00};

const unsigned char HCIWriteScanActivityDefault[8]={0x01,0x1C,0x0C,0x04,0x00,0x08,0x12,0x00};
const unsigned char Respon_WriteScanActivityDefault[7]={0x04,0x0e,0x04,0x01,0x1C,0x0C,00};

//const unsigned char HCIRead_BD_ADDR[4]={0x01,0x09,0x10,00};        //Not use
//uchar      Respon_Read_BD_ADDR[13];

/*****************************************************************************/
/*****************Inquiry Procedure refer code array**************************/
/*****************************************************************************/
const unsigned char HCIInquiry[9]={0x01,0x01,0x04,0x05,0x33,0x8B,0x9E,0x03,0x00};
const unsigned char Respon_Inquiry[7]={04,0x0f,0x04,00,0x01,0x01,0x04};
const unsigned char Respon_Inquiry_Result[18]=\
{0x04,0x02,0x0f,0x01,0x05,0x60,00,0x84,0x0a,00,0x01,00,00,00,00,00,0xa3,0x1c};
const unsigned char Respon_Inquiry_Complete[4]={04,01,01,00};
/*****************************************************************************/
/****************Creat Acl Connection refer code array************************/
/*****************************************************************************/
const unsigned char HCICreateACLConnection[17]=\
{0x01,0x05,0x04,0x0D,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00};
const unsigned char Respon_CreateACLConnection[7]={04,0x0f,04,00,01,05,04};
const unsigned char Respon_CreateACLConnection_Complete[14]=\
{04,03,0x0b,00,0x29,00,05,0x60,00,0x84,0x0a,00,01,00};
const unsigned char Respon_Change[10]={04,0x20,07,0x05,0x60,00,0x84,0x0a,0x00,0x01};
const unsigned char Respon_Max_time[6]={0x04,0x1b,03,29,00,05};
/*****************************************************************************/
/****************Creat SCO Connection refer code array************************/
/*****************************************************************************/
const unsigned char HCICreateSCOConnection[8]={0x01,0x07,0x04,0x04,0x28,0x00,0x20,0x00};
const unsigned char Respon_CreateSCOConnection[7]={0x04,0x0F,0x04,0x00,0x01,0x07,0x04,};
const unsigned char Respon_SCO_Change[6]={0x04,0x1B,0x03,0x28,0x00,0x01};
const unsigned char Respon_CreateSCOConnection_Complete[14]=\
{0x04,0x03,0x0B,0x00,0x2B,0x00,0x61,0x02,0x00,0x84,0x0A,0x00,0x00,0x00};
/*****************************************************************************/
/*******************Disconnect acl & sco refer code array*********************/
/*****************************************************************************/
const unsigned char HCIDisconnect[7]={0x01,0x06,0x04,0x03,0x28,0x00,0x14};
const unsigned char Respon_Disconnect[7]={0x04,0x0f,0x04,0x00,0x01,0x06,0x04};
const unsigned char Respon_Disconnect_Complete[7]={0x04,0x05,0x04,0x00,0x28,0x00,0x16};
/*****************************************************************************/
//bt modeul address  should Replace The Six Zero In HCICreateACLConnection;
uchar  Btaddr0[6]=  {0x61,0x02,0x00,0x84,0x0a,0x00};     //0261  jinlei
uchar  Btaddr2[6]=  {0xf3,0xe0,0x00,0x84,0x0a,0x00};     //e0f3  zhao
uchar  Btaddr1[6]=  {0x67,0x02,0x00,0x84,0x0a,0x00};     //e0f7  zhao
//264 57600
//e0f3 115200
//****************************************************************************/
uchar HCIHandle0=0;
uchar HCIHandle1=0;                 //Handle
uchar UARTTXLen =0;                 //Send Lenth
uchar UARTRXLen =0;                 //Rece Lenth
uchar j=0,flag=0;
uchar HCICMDSbuf[20];
uchar connect_cmd_fin=0;
uchar connect_fin=0;
uchar connect_interrupt=0;
uchar connect_failure=0;
uchar init_error=0;
uchar btdata_send_fin_cmd=0;
uchar DteArray[10];
uchar p=0;
uchar data_buffer[20];
uchar ReadCmd=0;
uchar CtrlCmd=0;
uchar ShouldReturnDteData=0;
uchar Scom0Start=0;
uint  SystemLoop=0;
uchar ShouldSendData=0;
uchar ReadCmdHasReceived=0;

void Timer0(void)
{
    TCNT0=0x00;
    TCCR0=(1<<CS02)|(1<<CS00);
    //TCCR0A=(1<<CS02)|(1<<CS00);  //HOOP722 与M128区别
}

void InitUART_0 (uint baud)
{
    UCSR0B = 0 ;  // disable UART_0
    UBRR0H = (uchar) (baud>>8);
    UBRR0L = (uchar)  baud    ;

    UCSR0A = (1<<UDRE0);
    UCSR0C = (0<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00);//8bit 1stop
    
    UCSR0B = (1<<RXEN0) | (1<<TXEN0);  // then, (re)enable UART_0
    UCSR0B|= (1<<RXCIE0);
}
void InitUART_1(uint baud1)
{
    UCSR1B = 0 ;  // disable UART_1
    UBRR1H = (uchar) (baud1>>8);
    UBRR1L = (uchar)  baud1;
    UCSR1A = (1<<UDRE1);

    UCSR1C = (0<<UPM11)|(0<<UPM10);
    UCSR1C = (0<<USBS1) | (1<<UCSZ11) | (1<<UCSZ10);//8bit 1stop

    UCSR1B = (1<<RXEN1) | (1<<TXEN1);  // then, (re)enable UART_1
    UCSR1B|= (1<<RXCIE1);
}

/*****************************************************************************/
/*Long time Delay for  delx(ms)*/
/*****************************************************************************/
void gycdelay(unsigned int delx)
{
    unsigned int r=0;
    while(delx--)            //MS DELAY
    {
        for(r=0;r<0x104c;r++)
        {asm("nop");}
    }
}

/*********************************************************************/
/*******************SendHCICmd procedure******************************/
/*********************************************************************/
void SendHCICmd(unsigned char const * p)
{   
    uchar i,temp;
    temp=*(p+3)+4;
    while ( !( UCSR1A & (1<<UDRE1)) );
    for(i=0;i<temp;i++)
    {
        UDR1=*p;
        while ( !( UCSR1A & (1<<UDRE1)) );     /* 等待发送缓冲器为空 */
        p++;
    }
}
/*****************************************************************************/
/*Send data cmd include a flag that means cmd or data selection pay attention*/
/*****************************************************************************/
void SendData(uchar num)
{                                 //send EEprom data to PC Via Bt
    uchar loop;
    data_buffer[0]=HCIHandle1;//HCICMDSbuf[4];
    data_buffer[1]=HCIHandle0|0x20;//HCICMDSbuf[5];
    data_buffer[2]=num+4;
    data_buffer[3]=00;
    data_buffer[4]=num;
    data_buffer[5]=00;
    data_buffer[6]=00;
    data_buffer[7]=00;
    UDR1=0x02;                        //data flag in BT
    while ( !( UCSR1A & (1<<UDRE1)) );
    for(loop=0;loop<8;loop++)
    {
        UDR1=data_buffer[loop];
        while ( !( UCSR1A & (1<<UDRE1)) );
    }
    
    for(loop=0;loop<num;loop++)
    {
        UDR1=DteArray[loop];
        while ( !( UCSR1A & (1<<UDRE1)) );
    }
}
/*********************************************************************/
/***************************Send Dte Command**************************/
/*********************************************************************/
void Dte_SendReadCmd(void)
{
    UDR0=0x05;
    while ( !( UCSR0A & (1<<UDRE0)) );
    UDR0=0x05;
    while ( !( UCSR0A & (1<<UDRE0)) );
    UDR0=0x06;
    while ( !( UCSR0A & (1<<UDRE0)) );
    UDR0=0x00;
    while ( !( UCSR0A & (1<<UDRE0)) );
}
void Dte_SendCtrlCmd(void)
{
    uchar i;
    UDR0=0x05;
    while ( !( UCSR0A & (1<<UDRE0)) );
    UDR0=0x05;
    while ( !( UCSR0A & (1<<UDRE0)) );
    UDR0=0x06;
    while ( !( UCSR0A & (1<<UDRE0)) );
    for(i=0;i<5;i++)
    {
        UDR0=HCICMDSbuf[i+13];
        while ( !( UCSR0A & (1<<UDRE0)) );
    }
}
/*********************************************************************/
/***************************Disconnect********************************/
/*********************************************************************/
void Disconnect()
{
    uchar  i;              //loop   variable
    UARTRXLen =7;
    j=0;
        /**********************write hcicommand Disconnect**************************/
    for (i=0;i<7;i++) HCICMDSbuf[i]=HCIDisconnect[i];
    UARTTXLen=7;
    HCICMDSbuf[4]=HCIHandle1; //28  handle
    HCICMDSbuf[5]=HCIHandle0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -