【问题标题】:UART buffer not raising a flag on PIC24FUART 缓冲区未在 PIC24F 上引发标志
【发布时间】:2015-07-11 19:51:59
【问题描述】:

我正在使用 RN42-XV 蓝牙模块将字符从计算机发送到 PIC24F。 模块连接/配对正确,发送的字符也正确(使用示波器)。

这是它的初始化方式:

void initUART(){

   //Peripheral Pin Mapping
   RPINR19bits.U2RXR = 5; //pin 14 UART Receive
   RPOR5bits.RP11R = 3; //pin 17 UART Transmit

   //Configuring the UART
   U2BRG = BRGVAL;
   U2MODEbits.UARTEN = 1;
   U2MODEbits.UEN = 0;
   U2MODEbits.PDSEL = 0;// 8 bit no parity
   U2MODEbits.STSEL = 0; // 1 stop bit
   U2STAbits.UTXEN = 0;
   U2STAbits.URXISEL = 0;

   //Putting the UART interrupt flag down.
   IFS1bits.U2RXIF = 0;
 }

我也在用这个函数来获取缓冲区的内容:

int waitForChar(){
   int receivedChar;
   // Use the UART RX interrupt flag to wait until we recieve a character.
   while(IFS1bits.U2RXIF == 1){
      // Clear the UART RX interrupt flag to we can detect the reception
      // of another character.
      IFS1bits.U2RXIF = 0;
      // U2RXREG stores the last character received by the UART. Read this
      // value into a local variable before processing.
      receivedChar = U2RXREG;
   }
return receivedChar;
}

问题是程序永远不会进入函数 waitForChar() 内的 while 循环,因为硬件永远不会引发 UART 中断标志​​。 我尝试了不同的 PIC24F,但都遇到了同样的问题。

【问题讨论】:

    标签: c bluetooth uart microchip pic24


    【解决方案1】:

    我注意到几件事:

    1. 在完全配置之前启用模块 (UARTEN)。

    2. U2STA.URXDA 不应该用作标志来测试接收吗?

    3. 您不会在两个寄存器中配置多个位。没关系,但如果您完全确定启动状态是您喜欢的状态。

    【讨论】:

      【解决方案2】:

      函数类型被声明为void,因此它不返回任何内容。如果您尝试分配其返回值,您应该会收到编译器警告。此外,它不等待一个字符。它是“非阻塞”的,它无论如何都会返回,但你需要一个返回值来告诉你它是否有一个 char 或者它是否没有。如果你想让它等待并返回一个字符,它可能是这样的

      int waitForChar(){                           // declare a return type
          int receivedChar;
          while(IFS1bits.U2RXIF == 0);             // wait
          receivedChar = U2RXREG;
          IFS1bits.U2RXIF = 0;                     // clear status
          return receivedChar;
      }
      

      【讨论】:

      • 是的,已更改为返回 int。这无助于缓冲区读取任何内容。
      【解决方案3】:

      UART 初始化代码缺少这一行:

       AD1PCFG = 0xFFFF
      

      ADC 标志优先于 UART。此行禁用它们。

      【讨论】:

        猜你喜欢
        • 2022-01-03
        • 2019-09-24
        • 1970-01-01
        • 1970-01-01
        • 2021-03-07
        • 2021-06-16
        • 2021-11-21
        • 2019-10-01
        • 2016-01-15
        相关资源
        最近更新 更多