STC uart1,uart2 初始化,及uart2 中断

#if 1               //uart1,2 using T2
    //uart1
    SCON = 0x50;
#ifdef __DEBUG_VERSION__
    S2CON = 0x50;       //uart2
#endif
    AUXR |= 0x04;       //T2 1T
    AUXR |= 0x01;       //T2 for uart1
    T2L = BAUD_RATE;
    T2H = BAUD_RATE >> 8;
    AUXR |= 0x10;       //EN T2
    ES = 1;             //EN uart1
#ifdef __DEBUG_VERSION__
    IE2 |= 0x01;        //EN uart2
#endif
#endif

#if 0               //uart1 using T1, uart2 using T2
    //T1 for uart1
    SCON = 0x50;
    AUXR = 0x40;
    TMOD = 0x00;
    TL1 = BAUD_RATE;
    TH1 = BAUD_RATE >> 8;
    TR1 = 1;
    ES = 1;

    //T2 for uart2
    S2CON = 0x50;
    AUXR |= 0x04;
    T2L = BAUD_RATE;
    T2H = BAUD_RATE >> 8;
    AUXR |= 0x10;
    IE2 |= 0x01;
#endif

    EA = 1;

cpu.c

#ifdef __DEBUG_VERSION__

void uart2(void) interrupt 8{
    if(S2CON & S2RI){
        S2CON &= ~S2RI;
        g_debug_uart_recvbuf[g_debug_uart_recvtail] = S2BUF;
        g_debug_uart_recvtail = (g_debug_uart_recvtail + 1) % DEBUG_UART_RX_BUFSIZE;        
    }
    if(S2CON & S2TI){
        S2CON &= ~S2TI;
        busy2 = 0;
    }
}
#endif  //__DEBUG_VERSION__

debug.c

void debug_recvmsg(void){
    u8 ch;
    u16 tmp_tail;
    tmp_tail = g_debug_uart_recvtail;

    if(tmp_tail != g_debug_uart_recvhead){

        ch = g_debug_uart_recvbuf[g_debug_uart_recvhead];
        g_debug_uart_recvhead = (g_debug_uart_recvhead + 1) % DEBUG_UART_RX_BUFSIZE;

        debug_send(ch);      //echo

        tmp_tail = g_debug_uart_recvtail;
    }
}


//send str
void debug(BYTE *str){
    while(*str){
        debug_send(*(str++));
    }
}


//send byte
void debug_send(BYTE ch){
    while(busy2);
    busy2 = 1;
    S2BUF = ch;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值