C8051F串口中断发送+缓冲区小程序

2020-01-15 19:23发布

在OURDEV得到很多帮助,不敢藏私,也奉上 我的C8051F采用串口0 中断发送+缓冲区 的方式写的小程序吧~
请大家多多指教~~

主要原理:
上层直接将数据装入缓冲区,若手工发送标记(auto_uart0_tx_flg )为1,则置位串口中断标志TI0,启动发送,直至缓冲区为空(读写指针相等,此时将auto_uart0_tx_flg置0),否则,说明此时缓冲区中仍有数据,不用任何操作。

我的主要功能模块:
/*
********************************************************************************
*
* void UART0_Init (void);
* UART0配置
* Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
*
********************************************************************************
*/

void UART0_Init (void)
{
        SCON0   = 0x50;                     // SCON0: mode 1, 8-bit UART, enable RX
        TMOD    = 0x20;                     // TMOD: timer 1, mode 2, 8-bit reload
        TH1    = -(SYSCLK/BAUDRATE/16);     // set Timer1 reload value for baudrate
        TR1    = 1;                         // start Timer1
        CKCON |= 0x10;                      // Timer1 uses SYSCLK as time base
        PCON  |= 0x80;                      // SMOD00 = 1,倍频
//        PCON  |= (1 << 6);                                        //
//        TI0    = 1;                         // Indicate TX0 ready
        in_uart_tx_buf =  uart_tx_buf;
        out_uart_tx_buf = uart_tx_buf;  
}

/*
********************************************************************************
*
* void UART0_ISR(void);
* USART0中断处理函数.
*
********************************************************************************
*/

void UART0_ISR(void) interrupt iv_UART0
{
        if(RI0)//是接收中断
        {
                RI0 = 0;//清接收中断标志位
                uart_data = SBUF0;//保存接收到的数据
                uart_rcv_data_flag = 1;//标志位置1

        }               

        if(TI0)//是发送中断
        {
                TI0 = 0;//清发送中断标志位,big problem
                uart_out_tx_buf();                         
        }
}

//进入发送中断后调用uart_out_tx_buf进行后续的处理
void uart_out_tx_buf(void)
{
        if(out_uart_tx_buf == in_uart_tx_buf)        //txbuf为空
        {
                ES0 = 0;
                auto_uart0_tx_flg = 1;//本次发送结束,下次还须手动触发
                return;
        }

                SBUF0 = *out_uart_tx_buf;
                out_uart_tx_buf++;
                if(out_uart_tx_buf == (uart_tx_buf + uart_tx_buf_len)) //缓冲区回绕
                        out_uart_tx_buf = uart_tx_buf;
                  
}

//上层程序调用串口发送数据的接口
void uart_in_tx_buf(INT8U *pt,INT8U datalen)
{
        INT8U *t,*p;
        t = in_uart_tx_buf;
        p = pt;
        while(datalen--)
        {
                t++;
                if(t == (uart_tx_buf + uart_tx_buf_len)) //回绕
                        t = uart_tx_buf;
                if(t == out_uart_tx_buf) //txbuf满
                        return;
                *in_uart_tx_buf = *p++;
                in_uart_tx_buf = t;
        }        
       
        uart_tx_start();
}


//手动启动串口发送
void uart_tx_start(void)
{
        if(auto_uart0_tx_flg == 1)
        {
                ES0 = 1;
                TI0 = 1;
                auto_uart0_tx_flg = 0;
        }
}

/*
********************************************************************************
*
*                                  MAIN
*
********************************************************************************
*/

void main(void)
{
        INT8U i;

        ALL_DEVICE_Init();//初始化,开全局中断等

        for(i=0;i<8;i++)
        {
                uart_in_tx_buf("helloworld",10);
//                delay(100);
        }
//        uart_in_tx_buf("helloworld helloworld helloworld",32);
        while(1);          
       
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。