[PIC®/AVR®/dsPIC®产品] 【CuriosityNano测评报告】串口轮询接收数据回显

[复制链接]
6096|32
 楼主| dql2015 发表于 2021-6-14 09:14 | 显示全部楼层 |阅读模式
串口是最常用的外设之一,本次实验使用debug串口1,mcc配置如下:




main.c函数如下,依据mcc生成的文件uart1.h来编写,里面详细介绍了api的使用,非常nice。
  1. /**
  2.   Generated Main Source File

  3.   Company:
  4.     Microchip Technology Inc.

  5.   File Name:
  6.     main.c

  7.   Summary:
  8.     This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs

  9.   Description:
  10.     This header file provides implementations for driver APIs for all modules selected in the GUI.
  11.     Generation Information :
  12.         Product Revision  :  PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.7
  13.         Device            :  PIC18F16Q40
  14.         Driver Version    :  2.00
  15. */

  16. /*
  17.     (c) 2018 Microchip Technology Inc. and its subsidiaries.
  18.    
  19.     Subject to your compliance with these terms, you may use Microchip software and any
  20.     derivatives exclusively with Microchip products. It is your responsibility to comply with third party
  21.     license terms applicable to your use of third party software (including open source software) that
  22.     may accompany Microchip software.
  23.    
  24.     THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
  25.     EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
  26.     IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
  27.     FOR A PARTICULAR PURPOSE.
  28.    
  29.     IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
  30.     INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
  31.     WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
  32.     HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
  33.     THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
  34.     CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
  35.     OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
  36.     SOFTWARE.
  37. */

  38. #include "mcc_generated_files/mcc.h"
  39. /*
  40.                          Main application
  41. */
  42. uint8_t r;
  43. void main(void)
  44. {
  45.     // Initialize the device
  46.     SYSTEM_Initialize();

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

  50.     // Enable the Global Interrupts
  51.    //INTERRUPT_GlobalInterruptEnable();

  52.     // Disable the Global Interrupts
  53.     //INTERRUPT_GlobalInterruptDisable();
  54.     while (1)
  55.     {      
  56.         do
  57.         {
  58.             if(UART1_is_rx_ready())
  59.             {
  60.                 r = UART1_Read();               
  61.                 if(UART1_is_tx_ready())
  62.                 {
  63.                     UART1_Write(r);                       
  64.                 }
  65.             }
  66.         }while(UART1_DataReady);               
  67.     }
  68. }
  69. /**
  70. End of File
  71. */

测试结果:

测试工程源码:

本帖子中包含更多资源

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

×
usysm 发表于 2021-7-6 13:45 | 显示全部楼层
中断与轮洵方式编写串口接收程序的不同之处?
typeof 发表于 2021-7-6 13:45 | 显示全部楼层
轮询模式串口接收函数超时等待问题  
yujielun 发表于 2021-7-6 13:46 | 显示全部楼层
:中断模式、轮询模式、DMA 模式  
htmlme 发表于 2021-7-6 13:46 | 显示全部楼层
没有使用中断,适合于初学者   
pklong 发表于 2021-7-6 13:47 | 显示全部楼层
常规的串口接收设计  
touser 发表于 2021-7-6 13:48 | 显示全部楼层
可以做串口接收不定长数据时轮询吗  
myiclife 发表于 2021-7-6 13:48 | 显示全部楼层
谢谢楼主分享的资料了
uytyu 发表于 2021-7-6 13:49 | 显示全部楼层
  轮询串口配置
iyoum 发表于 2021-7-6 13:51 | 显示全部楼层
效率最高的还是DMA中断   
jkl21 发表于 2021-7-6 13:51 | 显示全部楼层
轮询方式将接收函数放在while循环中  
pklong 发表于 2021-7-6 13:53 | 显示全部楼层
需要设计当前接收字节后的超时识别  
htmlme 发表于 2021-7-6 13:53 | 显示全部楼层
              
yujielun 发表于 2021-7-6 13:53 | 显示全部楼层
使用轮询方式来检测串口的接收数据  
touser 发表于 2021-7-6 13:53 | 显示全部楼层
配置FIFO了吗   
typeof 发表于 2021-7-6 13:53 | 显示全部楼层

只有轮询方式吗     
myiclife 发表于 2021-7-6 13:53 | 显示全部楼层
           
usysm 发表于 2021-7-6 13:53 | 显示全部楼层
为什么不用中断呢  
jkl21 发表于 2021-7-6 13:53 | 显示全部楼层
轮询模式为堵塞模式吗  
iyoum 发表于 2021-7-6 13:53 | 显示全部楼层
对于串口的使用基本都是轮询发送、中断接收   
您需要登录后才可以回帖 登录 | 注册

本版积分规则

104

主题

384

帖子

8

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