Reputation: 25
Say I have a PCB with a header connector. Some of these pins are analog inputs, called A0-A15. These pins are connected to the ADC inputs on a MCU, but to ease the layout task, the signals were not connected with indexes one to one. So in my board specific header file I have defined the pin mapping, where BOARD_A0 refers to input A0 on the board connector followed by the corresponding index of the MCU ADC:
#define BOARD_A0 13
#define BOARD_A1 4
#define BOARD_A2 5
#define BOARD_A3 14
....
There are 16 channels total. I want to be able to dump all the channels values using a for loop, so I will need run through the whole mapping and translate each pin. For this I have the function:
unsigned int adc_get_board_channel(int channel)
{
int adc_channel;
switch(channel) {
case 0:
adc_channel = BOARD_A0;
break;
case 1:
adc_channel = BOARD_A1;
break;
case 2:
adc_channel = BOARD_A2;
break;
case 3:
adc_channel = BOARD_A3;
break;
case 4:
adc_channel = BOARD_A4;
break;
....
}
adc_get_channel_value(ADC,adc_channel);
}
I cant help wondering if there is a more elegant way of writing code for such pin mapping?
Upvotes: 0
Views: 451
Reputation: 50883
Yes there is:
unsigned int adc_get_board_channel(int channel)
{
static int boards[] =
{BOARD_A0, BOARD_A1, ..., BOARD_16}; // I let you take care of the typing
return adc_get_channel_value(ADC, boards[channel]);
}
And BTW: in your original function there is probably a return
missing right before adc_get_channel_value
.
Upvotes: 5