jolati は、終了値が低すぎて 11 バイトを取得できないことについて良い点を指摘しましたが、他のバイトを読み取る前に、他のバイトが使用可能になるまで待つ必要があることを付け加えなければなりません。
あなたの例では;
char inBytes[11];
int i;
unsigned char temp;
while (!U1STAbits.URXDA ); //Wait until at least one byte is available
temp = U1RXREG;
if (temp == '$')
{
inBytes[0] = temp;
for(i = 1; i < 11; i++) //Loop over range i = 1 to 10 inclusively
{
while (!U1STAbits.URXDA ); //Wait until at least one byte is available
inBytes[i] = U1RXREG;
}
}
理想的には、割り込みを使用してブロックしない方法でこれを行い、データが入ってきたら処理しますが、割り込みを使用できない場合は、次のような非ブロック ポーリングをいつでも使用できます。
void AsyncRX()
{
//Note that the static variables keeps their value between invocations of the
// function. i is set to 0 only on the first run of this function, it keeps
// its value on every other run after that.
static int i = 0;
static char inBytes[11];
//Nothing more to do until there is at least 1 byte available
if( !U1STAbits.URXDA )
return;
//Save the byte and test that our message starts with $
inBytes[i] = U1RXREG;
if( inBytes[0] != '$' )
return;
//Test the counter to see if we have a full 11 bytes
i++;
if( i < 11 )
return;
//Do something with your 11 bytes
//...
//...
//Reset the counter for the next message
i = 0;
}
割り込みの例では、ポーリングされたバージョンを単純に取得して ISR にスローできます。以下は一例です。使用している dsp33 がわからないことに注意してください。しばらくの間、ハイエンド コア (ベクター テーブルを使用) で割り込みをプログラムしていないため、1 つまたは 2 つの変更が必要になる場合があります。また、デフォルトでは有効になっていないため、適切なレジスタを設定して割り込みを有効にする必要があることにも注意してください。
void __attribute__ ((interrupt, no_auto_psv)) _U1RXInterrupt(void)
{
//Note that the static variables keeps their value between invocations of the
// function. i is set to 0 only on the first run of this function, it keeps
// its value on every other run after that.
static int i = 0;
static char inBytes[11];
//Reset the interrupt flag
IFS0bits.U1RXIF = 0;
//Use up all bytes in the buffer (the interrupt can be set to only fire
// once the buffer has multiple bytes in it).
while( U1STAbits.URXDA )
{
//Save the byte and test that our message starts with $
inBytes[i] = U1RXREG;
if( inBytes[0] != '$' )
continue;
//Test the counter to see if we have a full 11 bytes
i++;
if( i < 11 )
continue;
//Do something with your 11 bytes
//...
//...
//Reset the counter for the next message
i = 0;
}
}