[PIC®/AVR®/dsPIC®产品] 【CuriosityNano测评报告】+自己任意长度的串口接收

[复制链接]
2631|0
 楼主| qjp1988113 发表于 2021-5-29 22:27 | 显示全部楼层 |阅读模式
现在我们做下串口的程序:
我们先查看串口的定义引脚,如下电路图所示:

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


USART1设置:

中断设置:

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


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

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

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

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

  11.     // Enable the Global Interrupts
  12.     INTERRUPT_GlobalInterruptEnable();
  13.     // Disable the Global Interrupts
  14.     //INTERRUPT_GlobalInterruptDisable();

  15.     while (1)
  16.     {
  17.         // Add your application code
  18.         //uint8_t data;
  19.         //LED_Toggle();
  20.         //printf("Hello world!\r\n");
  21.         //__delay_ms(500);
  22.         /*
  23.         printf("Enter any string: ");
  24.         do{
  25.         data = UART1_Read();                // Read data received
  26.         UART1_Write(data);                        // Echo back the data received
  27.         }while(!UART1_DataReady);                //check if any data is received
  28.         */
  29.         if(USART_RX_STA&0x8000)
  30.                 {                                          
  31.                         len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
  32.                         printf("\r\nYou send message is :\r\n\r\n");
  33.                         for(t=0;t<len;t++)
  34.                         {
  35.                                 UART1_Write( USART_RX_BUF[t]);//向串口1发送数据
  36.                                 while(0 == PIR4bits.U1TXIF){};//等待发送结束
  37.                         }
  38.                         printf("\r\n\r\n");//插入换行
  39.                         USART_RX_STA=0;
  40.                 }else
  41.                 {
  42.                         times++;
  43.                         if(times%5000==0)
  44.                         {
  45.                                 printf("\r\nPIC18F16Q16 CuriosityNano Board\r\n");
  46.                                 printf("I LOVE PIC MCU by qjp1988113!\r\n\r\n");
  47.                         }
  48.                         if(times%200==0)printf("Please put char end with space and enter key! \n");  
  49.                         if(times%30==0)LED_Toggle();//闪烁LED,提示系统正在运行.
  50.                         __delay_ms(10);   
  51.                 }
  52.     }
  53. }
下载,查看串口输出(输出的以回车换行结束):

串口输出如下:

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




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

111

主题

627

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部