Hi all,
I'm having an intermittent issue and wanted to get some help figuring out how to go about debugging. I've pasted the shortest extent of code I could to try and simplify the issue, so I imagine there will be some questions to help me figure this one out. Here's my setup:
TM4C123GXL connected to SIM900 GSM cell phone module via UART1. Programmed using Keil uv4
When a text message comes in, the UART1 interrupt handler increments counter msgCount.
The while loop (excerpt below) in my main program then triggers message processing. If the message is four bits and comes from the right sender, the program then toggles four relays on or off as appropriate.
I also have an ADC running as part of this while loop. Currently, the ADC is just printing the value to the screen, but eventually it will also be used to trigger actions with the relays.
In my testing, I am finding that occasionally when two or three messages come in short succession, the proper relay state is not applied. The first one or two relays states will be changed, but not the last ones. You can see I added an additional condition to check and make sure that the relay state matches the most recent message, but even still it fails. When I disable the ADC, I don't see this problem.
I believe at least one of these three things is somehow causing this problem:
First, receiving and processing a messages takes a few seconds, due to slow response of GSM module, and error checking of the message.
Second, toggling relays also takes a bit of time, and I have to update relay state to EEPROM and an external display, which takes some time.
Finally, my ADC function includes a 250ms time delay in order to make the ADC value readable (the numbers flash too fast on the display to read them otherwise).
My hunch is that these time delays are somehow causing a problem with my code (as evidenced by the fact that when I take the time delay out of the ADC the problem goes away).
Here's the code:
while(1)
{
// Process new messages - an interrupt will increment msgCount when a new message comes in
if (msgCount > 0)
{
// Start working on the oldest message
msgOpen = msgCount;
msgCount--;
// Process message for envelope and content
GSMprocessMessage(msgOpen);
// If message content is good, act on message. A "good message" is four characters, each
// 0 or 1, and from a specific sender.
if (strstr(msgSender,ctrlID) != NULL && strlen(msgContent) == 4) {
for (ctr1=0;ctr1<4;ctr1++){
if ( msgContent[ctr1] == '0' && relayStatus[ctr1] == 1 ) { relayOff(ctr1+1); }
else if (msgContent[ctr1] == '1' && relayStatus[ctr1] == 0 ) { relayOn(ctr1+1); }
}
}
}
// When there are no new messages, verify relays are in the right state
else if ( msgCount == 0 ) {
for (ctr1=0;ctr1<4;ctr1++){
if ( msgContent[ctr1] == '0' && relayStatus[ctr1] == 1 ) { relayOff(ctr1+1); }
else if (msgContent[ctr1] == '1' && relayStatus[ctr1] == 0 ) { relayOn(ctr1+1); }
}
}
// Run the ADC
if ( testADC && msgCount == 0 ) { runADC(); }
}
Any help is greatly appreciated!
-- Lee