I use interrupt for SCI transmitting and receiving a number of chars. I use a fixed packet format and read it into an array called RxData. Example....
/* *********** SCI Packet *************
RxData[0] = $ start char
RxData[1-2] = Packet size (Word)
Rxdata[3] = Command ID (byte)
and at the end
RxData[????] = Checksum (32bit)
When the whole packet is received the flag GotRx data is set. I call the function CheckCommsActivity(); every 10 msec to see if there is any data.
Below is how I do it...
//----------------------- SCI0 Rx/Tx Interrupt -----------------------------
interrupt void SCI0_ISR(void) {
static uint16_t pkt = 0;
static uint16_t x = 0;
static uint8_t Schar = FALSE; // Start char for comms
uint8_t RxByte;
static uint16_t i = 0;
unsigned char scicr2,scisr1; //prection against randomly flag clearing
scisr1 = SCI0SR1; // save status register actual status
scicr2 = SCI0CR2; // save control register actual status
//if receiver interrupt is enabled and corresponding interrupt flag is set
if((scicr2 & SCI0CR2_RIE_MASK) && ((scisr1 & (SCI0SR1_OR_MASK | SCI0SR1_RDRF_MASK)))) {
if(scisr1 & SCI0SR1_OR_MASK) { // if overrun error do nothing
(void)SCI0DRL; // clear interrupt flag
} else {
RxByte = SCI0DRL; // read received character + clear interrupt flag
switch (RxByte) {
case 0x24 : RxData[x++] = RxByte; // $ Start char
Schar = TRUE;
break;
default : if (Schar == TRUE) {
RxData[x++] = RxByte;
}
break;
} // End Switch
if (x == 3) {
pkt = (RxData[x - 2] << 8);
pkt = pkt + (RxByte & 0xFF);
}
if (x == pkt) { // Last byte received
x = 0;
pkt = 0;
Schar = FALSE;
gotRx = TRUE;
}
}
}
}
//------------------ Check Comms Activity ---------------------------
void CheckCommsActivity(void) {
uint8_t cmd;
if (gotRx == TRUE) {
cmd = RxData[3]; // Read the forth byte which is the command
switch (cmd) {
case 0x01 : sendRealTimeData();
break;
case 0x02 : receiveWholeMap();
break;
case 0x03 : WriteStructureToEEprom((uint8_t *)&cal, sizeof(cal)); // Lock changes into flash
sendReply(0x03,TRUE); // Okay
break;
case 0x04 : sendWholeMap();
break;
case 0x05 : changeOfValueRequested();
break;
case 0x06 : SetFpsMin();
break;
case 0x07 : SetFpsMax();
break;
case 0x08 : SetFuelTanksMin();
break;
case 0x09 : SetFuelTanksMax();
break;
}
gotRx = FALSE;
}
}