PIC中断驱动UART,具有高波特率的循环缓冲器

时间:2013-02-08 05:17:32

标签: interrupt pic circular-buffer uart

我试图用波特率= 38400的PIC 18f4550传感器读取。使用FIFO循环缓冲区,我能够将传感器中的数据存储到阵列中。

传感器将响应请求命令并返回15字节的测量值(与我创建的循环缓冲区相同)。我必须抓住所有 15个字节并在末尾放置\ r \ n来分隔每个测量,因为没有分隔符。

所以我使用两个指针 inputpointer outputpointer 来存储字节并传输字节输出。由于18f4550只有一个硬UART,我用它来读取数据并向传感器发送命令,同时使用软件UART通过RS232输出到笔记本电脑。

当读取缓冲区并将其发送到串行端口时,我请求新的测量。

它运行良好但我只是想知道当头指针超出尾指针时是否有更好的方法来避免FIFO溢出,即当有大量数据被缓冲但是它们无法及时输出时。

以下是代码:16MHZ PIC 18f4550 mikroC编译器

char dataRx[15];
char unsigned inputpointer=0;
char unsigned outputpointer=0;
        // initialize pointers and buffer.
void main() {
      ADCON1=0x0F;  //turn analog off

      UART1_Init(115200);  //initialize hardware UART @baudrate=115200, the same setting for the sensor
      Soft_UART_Init(&PORTD,7,6,38400,0); //Initialize soft UART to commnuicate with a laptop
      Delay_ms(100); //let them stablize



      PIE1.RCIE = 1;                //enable interrupt source
      INTCON.PEIE = 1;
      INTCON.GIE = 1;


      UART1_Write(0x00);            //request a measurement.
      UART1_Write(0xE1);            //each request is 2 bytes

      while(1){

        Soft_UART_Write(dataRx[outputpointer]);  //output one byte from the buffer to laptop
        outputpointer++;                        //increment output pointer
        if(outputpointer==0x0F)                 //reset pointer if it's at the end of the array
        {
            outputpointer=0x00;
            Soft_UART_Write(0x0D);              //if it's at the end, that means the buffer is filled with exactly one measurement and it has been output to the laptop. So I can request another measurement.
            Soft_UART_Write(0x0A);              //add \r\n
            UART1_Write(0x00);                  //request another measurement
            UART1_Write(0xE1);
        }



}
void interrupt(void){ //interrupt routine when a byte arrives
      dataRx[inputpointer]=UART1_Read();            //put a byte to a buffer
      inputpointer++;
      if (inputpointer==0x0F){inputpointer=0;}      //reset pointer.

}

1 个答案:

答案 0 :(得分:0)

也许你可以在传输传感器读取OUT的同时禁用中断,然后在完成传输数据后重新启用它?

它可能没有显着差异,但它可能会阻止你提到的FIFO超限?