0

I am using an 8 bit PIC (PIC16F18326) and one of the main purposes is to process many different I2C messages. The PIC is configured as an I2C slave and needs to respond with various data to about 100 different commands. When a complete message is received via the I2C interrupt a flag is set. The flag is read from the main loop and a function is called to process the data using a switch...case statement and build the message response. Everything works fine when only decoding about 50 different messages but the PIC isn't even able to acknowledge the master when I grow the number of case statements above 50. Has anyone experienced similar problems with 8 bit PICs? Is there a max number of case statements allowed before the PIC does strange things? The compiler and linker don't report any errors or warnings. Please let me know if you have any ideas/suggestions.

Thanks, Jack

Jack V
  • 1
  • 1

3 Answers3

0

Sounds like it breaks when your switch statement exceeds a bank boundary? If that is the case it would be a bug in the compiler. You can easily test this by looking at the generated asm I believe...

Cobusve
  • 1,572
  • 10
  • 23
0

Thanks for the reply. After many messages back and forth with Microchip Support, it appears there is a problem with the XC8 compiler version 2.00. This version of the compiler is the first release for C99 standard and apparently this introduced some major problems. I updated to XC8 vs 2.05 (released in Jan 2019) and haven't seen any similar problems.

Thanks again. Jack

Jack V
  • 1
  • 1
0

An alternative to coding a huge case statement is would be a function list

/* Declarations */
void fun1() {}
void fun2() {}
void fun3() {}

typedef void (*funtype)();

funtype funs[] = { &fun1, &fun2, &fun3, &fun2 };
...

/* Instead of
switch (x)
{
case 0: fun1(); break;
case 1: case 3: fun2(); break;
case 2: fun3(); break;
}
*/

funs[x];
cup
  • 7,589
  • 4
  • 19
  • 42