ボーレート=38400 で PIC 18f4550 のセンサーから読み取ろうとしています。FIFO 循環バッファーを使用すると、センサーからのデータを配列に格納できます。
センサーは要求コマンドに応答し、15 バイトの測定値を返します (作成した循環バッファーと同じです)。区切り記号がないため、 15 バイトすべてを取得し、最後に \r\n を配置して各測定値を区切る必要があります。
そこで、inputpointerとoutputpointerの 2 つのポインターを使用して、バイトを格納し、バイトを送信しました。18f4550 にはハード UART が 1 つしかないため、これを使用してデータを読み取り、センサーにコマンドを送信すると同時に、ソフトウェア 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.
}