0

Hey all I have my usart setup to read interrupts from the keyboard and transmit ADC readings from my breadboard using my pic. The interrupts are keyboard commands the user can enter that will change the upper and lower limits of my simulated breadboard temperature sensor.

My issue is sometimes my usart just randomly crashes. It freezes up and stops transmitting. This happens when I'm entering in my keyboard inputs, and even sometimes when it is transmitting something mid sentence (like halfway through a printf) and then I press enter and the interrupt seems to crash the usart resulting in an image like the one below.

http://imageshack.us/photo/my-images/5/ftfk.png/

Taking a look at the highlighted line, that is where i pressed enter, to output my stream of characters i had been collecting through the buffer, and it seems to have just cut that output in half and just skipped the interrupt output as well.

What could be the issue here, and what are things i should look for?

Please find samples of the code below

void mainNema( char *nemaSentence) //this function processes my output and prints it out
{
int i;
for (i = 0; i< 20; i++) {
   GPGGA_Tokens[i] = '\0';
}

parseNemaSentence(nemaSentence);

i = 0;
while (strcmp(GPGGA_Tokens[i], msgEndOfSentence)!=0) {
   printf("\r\ntoken %i: %s\r\n",i, GPGGA_Tokens[i]);
    i++;
}
evaluateTokens();
changeNema(token1,token2,token3);

if (token2 == 1)
    printf("your new upper limit for channel %i is %i", token1, token3);
else
    printf("your new lower limit for channel %i is %i", token1, token3);


}

Below a snippet of the function that reads from ADC converters and prints out "readings" to the user through usart

ConvertADC(); //take value from potentiometer

while(BusyADC() == TRUE) 

Delay1TCY();//wait until value is received

sampledValue = ReadADC(); //store value as sampledValue

setCurrentTemperatureForChannel(channelsel, sampledValue); //call function with channelsel and sampledValue

readSwitches(channelsel); //read inputs from pushbuttons

    printf ("\n\rcurrent Temp of channel %i is %#x, highlimit is %i, low limit is %i \n\r", (channelsel+1), getCurrentTemperatureForChannel(channelsel), 
getUpperLimitForChannel(channelsel),getLowerLimitForChannel(channelsel));       

1 Answers1

0

Candidate problem: Nothing to prevent an endless loop and UB.

Note: Either GPGGA_Tokens[i] = '\0' or strcmp(GPGGA_Tokens[i], ... is wrong. The first make sense if GPGGA_Tokens[i] is a char. The 2nd if it is a char *. I assume the latter. I think OP wants GPGGA_Tokens[i] = NULL.

There is noting to guarantee that any GPGGA_Tokens[i] will ever match msgEndOfSentence. You need some limit to prevent going off to lala land.

i = 0;
while ((i < 20) && (GPGGA_Tokens[i] != NULL) && 
    (strcmp(GPGGA_Tokens[i], msgEndOfSentence)!=0)) {
  printf("\r\ntoken %i: %s\r\n",i, GPGGA_Tokens[i]);
  i++;
}

Maybe during you interrupt handling of input, GPGGA_Tokens is changed whil eit sin the while loop. Recommend interrupt protecting GPGGA_Tokens access.

disable_interrupts();
i = 0;
while ((i < 20) && (GPGGA_Tokens[i] != NULL) && 
    (strcmp(GPGGA_Tokens[i], msgEndOfSentence)!=0)) {
  printf("\r\ntoken %i: %s\r\n",i, GPGGA_Tokens[i]);
  i++;
}
enable_interrupts();
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256