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

📄 90can32_uart.c

📁 使用avr90can32双串口传输蓝牙数据
💻 C
📖 第 1 页 / 共 2 页
字号:
    SendHCICmd(HCIDisconnect);
}
/*****************************************************************************/
/******initialize bt(it should be run at power up);it is very important ******/
/*****************************************************************************/
uchar InitBT()
{
  //  uchar  i;                 // Structure  is same ,pay attention to up
    UARTRXLen =7;
    j=0;
        /**********************write hcicommand HCIReset****************************/
 //   for (i=0;i<4;i++) HCICMDSbuf[i]=HCIReset[i];
    UARTTXLen=4;
    SendHCICmd(HCIReset);
    while(flag!=01) {;}
    flag=0;
    if(HCICMDSbuf[0]==04&&HCICMDSbuf[6]==00){;}
    else  return('n');
        /*****************write hcicommand HCISetEventFilter************************/
  //  for (i=0;i<5;i++) HCICMDSbuf[i]=HCISetEventFilter[i];
    UARTTXLen=5;
    UARTRXLen =7;
    j=0;
    SendHCICmd(HCISetEventFilter);
    while(flag!=01) {;}
    flag=0;
    if(HCICMDSbuf[0]==04&&HCICMDSbuf[6]==00){;}
    else  return('n');
        /******************write hcicommand HCIWriteScanEnable**********************/
  //  for (i=0;i<5;i++) HCICMDSbuf[i]=HCIWriteScanEnable[i];
    UARTTXLen=5;
    UARTRXLen =7;
    j=0;
    SendHCICmd(HCIWriteScanEnable);
    while(flag!=01) {;}
    flag=0;
    if(HCICMDSbuf[0]==04&&HCICMDSbuf[6]==00){;}
    else  return('n');
        /*************write hcicommand HCIWriteAuthenticationEnable****************
        for (i=0;i<5;i++) HCICMDSbuf[i]=HCIWriteAuthenticationEnable[i];
        UARTTXLen=5;
        UARTRXLen =7;
        SendHCICmd();
        while(flag!=01) {;}
        flag=0;
        if(HCICMDSbuf[0]==04&&HCICMDSbuf[6]==00){;}
        else  return('n');                                                            */
        /******************write hcicommand HCISetEventFilter1**********************/
  //  for (i=0;i<7;i++) HCICMDSbuf[i]=HCISetEventFilter1[i];
    UARTTXLen=7;
    UARTRXLen =7;
    j=0;
    SendHCICmd(HCISetEventFilter1);
    while(flag!=01) {;}
    flag=0;
    if(HCICMDSbuf[0]==04&&HCICMDSbuf[6]==00){;}
    else  return('n');
        /*************write hcicommand HCIWriteConnectionAcceptTimeout**************/
 //   for (i=0;i<6;i++) HCICMDSbuf[i]=HCIWriteConnectionAcceptTimeout[i];
    UARTTXLen=6;
    UARTRXLen =7;
    j=0;
    SendHCICmd(HCIWriteConnectionAcceptTimeout);
    while(flag!=01) {;}
    flag=0;
    if(HCICMDSbuf[0]==04&&HCICMDSbuf[6]==00){;}
    else  return('n');
        /******************write hcicommand HCIWritePageTimeout*********************/
 //   for (i=0;i<6;i++) HCICMDSbuf[i]=HCIWritePageTimeout[i];
    UARTTXLen=6;
    UARTRXLen =7;
    j=0;
    SendHCICmd(HCIWritePageTimeout);
    while(flag!=01) {;}
    flag=0;
    if(HCICMDSbuf[0]==04&&HCICMDSbuf[6]==00){;}
    else  return('n');
        /***************************************************************************/
    j=0;/////
    return('y');
}

//#pragma vector=USART0_RX_vect
//__interrupt void Comm0(void) //串口0接收中断服务程序
SIGNAL(SIG_UART0_RECV)         //HOOP722 于M128区别
{
    DteArray[p]=UDR0;
    p++;
    if(p==10)
    {
        p=0;
        ShouldReturnDteData=1;
    }
}
//#pragma vector=USART1_RX_vect
//__interrupt void Comm1(void) //串口1接收中断服务程序
SIGNAL(SIG_UART1_RECV)         //HOOP722 于M128区别

{
    uint chu,as;
    HCICMDSbuf[j]=UDR1;
    if(HCICMDSbuf[0]==0x04||HCICMDSbuf[0]==0x02);
    else{j=0;return;}
    j++;
    if((j==3)&&(HCICMDSbuf[0]==04)){UARTRXLen=HCICMDSbuf[2]+j;} //cmd or datas
    if((j==6)&&(HCICMDSbuf[0]==02)){UARTRXLen=HCICMDSbuf[5]+9;}
    if(j==UARTRXLen)
    {
        flag=1;
        j=0;
        as=(uint)HCICMDSbuf[0];
        switch(as)
        {
            case 0x04:
            {
                chu=(uint)HCICMDSbuf[1];
                switch(chu)
                {
                    case 0x0e: if(HCICMDSbuf[6]==0) ;      //
                               else   init_error=1;
                               j=0;
                               break;
                    case 0x0f: connect_cmd_fin=1;          //cmd return
                               UARTRXLen=14;
                               j=0;
                               break;
/*Attention  */     case 0x03: if(HCICMDSbuf[3]==0)        //should add addr judge
                               {
                                   HCIHandle0=HCICMDSbuf[5];  //00
                                   HCIHandle1=HCICMDSbuf[4];  //28
                                   connect_cmd_fin=0;
                                   UARTRXLen=10;
                               }
                               else  {connect_failure=1;}
                               j=0;
                               break;
                    case 0x20: UARTRXLen=6;j=0;
                               break;      //scan mode change
                    case 0x1b: connect_fin=1;
                               connect_interrupt=0;
                               j=0;        //should judge BT addr
                               break;      //max  time change
                    case 0x02: j=0; break;      //inquiry part,now not use
                    case 0x13: j=0;btdata_send_fin_cmd=1; break;
                    case 0x05: j=0;
                               connect_interrupt=1;        //connect interrupt
                               HCIHandle0=0;
                               HCIHandle1=0;
                               connect_fin=0;
                    default: j=0;break;             
                }
            }
            j=0;
            break;             //data part
            case 0x02:
            {
                switch(HCICMDSbuf[12])
                {
                    case 0x00: 
                    if((HCICMDSbuf[1]==HCIHandle1)&&(HCICMDSbuf[2]==(HCIHandle0|0x20)))
                    {
                        if(HCICMDSbuf[9]==0x05&&HCICMDSbuf[10]==0x05&&HCICMDSbuf[11]==0x06)
                        {
                            ReadCmd=1;
                        }
                    }
                    j=0;
                    break;
                    case 0xff:
                    if((HCICMDSbuf[1]==HCIHandle1)&&(HCICMDSbuf[2]==(HCIHandle0|0x20)))
                    {
                        if(HCICMDSbuf[9]==0x05&&HCICMDSbuf[10]==0x05&&HCICMDSbuf[11]==0x06)
                        {
                            CtrlCmd=1;
                        }
                    }
                    j=0;
                    break;
                    default :j=0; break ;
                }
            }
            j=0;
            break;
            default:j=0;break;
        }
    }
}

//#pragma vector=TIMER0_OVF_vect
//__interrupt void Timer0Interrupt(void)//SIG_OVERFLOW0
SIGNAL(SIG_OVERFLOW0)                   //HOOP722 于M128区别
{
    TCNT0 = 0x00;
    if(SystemLoop==65000) SystemLoop=0;//approximately 30times for 2sec
    else SystemLoop++;
    if(connect_fin==1&&SystemLoop>40)
    {
        ShouldSendData=1;
        SystemLoop=0;
    }
}

int main( void )
{
    PORTC=0xFF;
    DDRC=0xff;
    DDRA=0xFF;
    PORTA=0xff;
    DDRD=0xFF;
    PORTD=0xFF;
    Timer0();
    TCNT0=0; // T/C0 开始值
    //TCCR0A|=(1<<CS02)|(1<<CS00); // 预分频 ck/1024 ,计数允许
    TCCR0|=(1<<CS02)|(1<<CS00); // 预分频 ck/1024 ,计数允许//HOOP722 于M128区别
    //TIMSK0|=(1<<TOIE0); //T/C0 中断允许
    TIMSK|=(1<<TOIE0); //T/C0 中断允许

    InitUART_1(UART_BAUD(BAUD_RATE_57600,XTAL_CPU_7));
    BlueOff();
    InitUART_0(UART_BAUD(BAUD_RATE_9600,XTAL_CPU_7));
    
    asm("sei"); //总中断标志置位
    asm("nop");
    j=0;  
    if(InitBT()=='n')
    {
        BlueOn();
    }
    else BlueOff();
    gycdelay(20);
    BlueOn();
    while(1)
    {
        if(ReadCmd==1||ShouldSendData==1)
        {
            ReadCmd=0;
            SystemLoop=0;
            p=0;
            ShouldSendData=0;
            Dte_SendReadCmd();
        }
        if(CtrlCmd==1)
        {
            CtrlCmd=0;
            Dte_SendCtrlCmd();
        }
        if(ShouldReturnDteData==1)
        {
            ShouldReturnDteData=0;
            SendData(10);
        }//DteArray
    }
}

⌨️ 快捷键说明

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