打印
[PIC®/AVR®/dsPIC®产品]

【CuriosityNano测评报告】+自己任意长度的串口接收

[复制链接]
1818|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
现在我们做下串口的程序:
我们先查看串口的定义引脚,如下电路图所示:

打开MCC,进行外设的初始化设置:
GPIO设置:


USART1设置:

中断设置:

时钟设置还是与上例相同:


生成代码,我们修改下main函数,让它将我们输入的数据打印出数据:
 // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();
        printf("Enter any string: ");
        do{
        data = UART1_Read();                // Read data received
        UART1_Write(data);                        // Echo back the data received
        }while(!UART1_DataReady);                //check if any data is received
下载,查看:

我们查看自动生成的接收函数:
    uart1RxBuffer[uart1RxHead++] = U1RXB;
    if(sizeof(uart1RxBuffer) <= uart1RxHead)
    {
        uart1RxHead = 0;
    }
    uart1RxCount++;*/
它将接收的直接放到我们定义好长度的数组当中。我们不想用自带的这个,不是很灵活。
我们修改下,我们仅开接收中断,取消掉发送中断(很少有人用)

我们重新写串口接收中断函数:
我们定义"\r\n"为接收结束的结束标志,所以发送的都遵循这个要求。
uint8_t USART_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15,        接收完成标志
//bit14,        接收到0x0d
//bit13~0,        接收到的有效字节数目
uint16_t USART_RX_STA=0;       //接收状态标记
void UART1_RxDataHandler(void){
    // use this default receive interrupt handler code
    /*
    uart1RxBuffer[uart1RxHead++] = U1RXB;
    if(sizeof(uart1RxBuffer) <= uart1RxHead)
    {
        uart1RxHead = 0;
    }
    uart1RxCount++;*/
    //写入自己的串口代码
    uint8_t Res;
   
    Res =U1RXB;        //读取接收到的数据
               
    if((USART_RX_STA&0x8000)==0)//接收未完成
    {
        if(USART_RX_STA&0x4000)//接收到了0x0d
        {
        if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
        else USART_RX_STA|=0x8000;        //接收完成了
        }
        else //还没收到0X0D
        {        
            if(Res==0x0d)USART_RX_STA|=0x4000;
            else
            {
                USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
                USART_RX_STA++;
                if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收         
            }                 
        }
    }  
}
我们修改下main函数:
void main(void)
{
    uint16_t t;  
        uint16_t len;        
        uint16_t times=0;
    // Initialize the device
    SYSTEM_Initialize();

    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
    // Use the following macros to:

    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();
    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    while (1)
    {
        // Add your application code
        //uint8_t data;
        //LED_Toggle();
        //printf("Hello world!\r\n");
        //__delay_ms(500);
        /*
        printf("Enter any string: ");
        do{
        data = UART1_Read();                // Read data received
        UART1_Write(data);                        // Echo back the data received
        }while(!UART1_DataReady);                //check if any data is received
        */
        if(USART_RX_STA&0x8000)
                {                                          
                        len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
                        printf("\r\nYou send message is :\r\n\r\n");
                        for(t=0;t<len;t++)
                        {
                                UART1_Write( USART_RX_BUF[t]);//向串口1发送数据
                                while(0 == PIR4bits.U1TXIF){};//等待发送结束
                        }
                        printf("\r\n\r\n");//插入换行
                        USART_RX_STA=0;
                }else
                {
                        times++;
                        if(times%5000==0)
                        {
                                printf("\r\nPIC18F16Q16 CuriosityNano Board\r\n");
                                printf("I LOVE PIC MCU by qjp1988113!\r\n\r\n");
                        }
                        if(times%200==0)printf("Please put char end with space and enter key! \n");  
                        if(times%30==0)LED_Toggle();//闪烁LED,提示系统正在运行.
                        __delay_ms(10);   
                }
    }
}
下载,查看串口输出(输出的以回车换行结束):

串口输出如下:

达到预期设定目标。
好了串口就到这里了。
谢谢大家观看~




使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

111

主题

627

帖子

2

粉丝