View previous topic :: View next topic |
Author |
Message |
shson
Joined: 12 Feb 2016 Posts: 11
|
Jump Table warning, code has no effect |
Posted: Mon Feb 20, 2017 6:19 pm |
|
|
CCS V5.065, PIC18F44K22
Hello
I have a couple of 8:1 analogue multiplexers, and in my program depending on the value of unsigned int8 variable 'number' I need to use 'output_high()' and 'output_low()' to send output selector signal to the multiplexers. I first coded using switch statement like so:
Code: |
unsigned int8 number = 0;
void MUX_Select(void)
{
if (number > 16)
number = 0;
switch (number)
{
case 0:
output_high(PIN_D1);
output_low(PIN_D2);
output_high(PIN_D3);
break;
// ... case 0 through to 16
case 16:
output_low(PIN_A3);
output_low(PIN_B0);
output_high(PIN_C1);
break;
default:
break;
}
number++;
}
|
The code compiles and works on the breadboard as I wanted to. But I just learned about jump tables and since the variable 'number' only goes to 16, I wanted to try implement it using jump table like this:
Code: |
typedef void (*FuncPtr)(void);
FuncPtr SWSenseSel[16] = { MUX_VS0, MUX_VS1, ... through to, MUX_VS15 };
void MUX_VS0(void) // equivalent to case 0 using switch
{
output_high(PIN_D1);
output_low(PIN_D2);
output_high(PIN_D3);
}
// ... case 0 through to 15
void MUX_VS15(void) // equivalent to case 16 using switch
{
output_low(PIN_A3);
output_low(PIN_B0);
output_high(PIN_C1);
}
void main(void)
{
unsigned int8 number = 0;
while(TRUE)
{
SWSenseSel[number]; //Code has no effect warning on this line
//Now correct MUX channel is connected using selector, get ADC reading here
}
}
|
I get "Code has no effect" warning on "SWSenseSel[number]", I can't seem to understand why it would have no effect. Should a jump table always return something?
Thank you
Eric |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 20, 2017 9:27 pm |
|
|
I made the necessary changes to make it work. See below. I also added
some printf's so I could test it in MPLAB vs. 8.92 simulator. It displays
the following in the UART1 output window. It's working:
Test program:
Code: |
#include <18F44K22.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
void MUX_VS0(void)
{
printf("MUX_VS0 \r");
output_high(PIN_D1);
output_low(PIN_D2);
output_high(PIN_D3);
}
void MUX_VS15(void)
{
printf("MUX_VS15 \r");
output_low(PIN_A3);
output_low(PIN_B0);
output_high(PIN_C1);
}
typedef void (*FuncPtr)(void);
FuncPtr SWSenseSel[2] = {MUX_VS0, MUX_VS15};
//======================================
void main(void)
{
int8 i = 0;
(*SWSenseSel[i])();
i++;
(*SWSenseSel[i])();
while(TRUE);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Feb 21, 2017 12:07 pm |
|
|
It's also worth just remarking that if you get rid of the default in the switch case code, the compiler will automatically generate a jump table for you. Given that you already test for a value outside the case range, there is no point in the default anyway... |
|
|
shson
Joined: 12 Feb 2016 Posts: 11
|
|
Posted: Wed Feb 22, 2017 3:58 am |
|
|
@PCM
Replacing SWSenseSel[i]; with (*SWSenseSel[i])(); made it work, rather simple oversight, the fact that it is a pointer itself rather than a straight array.
@Ttelmah
That makes sense, I'll try to compile switch w/o default case and now-working jump tables and compare the LST to better understand CCS's behaviour.
Thank you |
|
|
|