The LWADC driver in MQX v4.0 has been updated to support Kinetis. The LWADC driver is simpler to use than the Kinetis ADC driver, and it allows more than 2 channels per ADC. The LWADC driver does not use the PDB timer for hardware triggering of the ADC conversion like the ADC driver does.
The only Kinetis LWADC example provided in MQX v4.0 is for the TWR-K21D50M BSP, but the LWADC driver can be added to other Kinetis BSPs. The Kinetis LWADC example is configured to only scan 1 ADC channel for the potentiometer on the board. But with some simple changes, the BSP and example code can be modified to enable converting multiple channels.
In the BSP, modify twrk21d50m.h to add more ADC channel defines. In my example, I added two more channels for the Tower AN7 and AN6 signals. These are tied to ADC0_SE8 and ADC0_SE9 on the MCU. My twrk21d50m.h file is attached. Also, enable the BSPCFG_ENABLE_LWADC macro in user_config.h, then rebuild the BSP.
| #define BSP_ADC_TWR_AN7 |
|
|
|
|
|
|
|
|
(ADC0_SOURCE_AD8) |
|
|
| #define BSP_ADC_TWR_AN6 |
|
|
|
|
|
|
|
|
(ADC0_SOURCE_AD9) |
|
|
In the example \Freescale_MQX_4_0\mqx\examples\lwadc, I found the application scanned all enabled channels in the BSP, but it did not change the channels in the continuous scan loop. I added calls to _lwadc_init_input() in the infinite scan loop in test.c, also attached. Now with the app rebuilt, it scans 3 ADC channels continuously.
printf("Monitoring ADC Inputs\n");
while (1) {
for (i=0;i<ELEMENTS_OF(adc_inputs);i++) {
/* DAS - Reinitialize to change channel */
if ( !_lwadc_init_input(&lwadc_inputs[i],adc_inputs[i].input) ) {
/* Failed to initialize this input. We will end up failing the reads below as well. */
printf("Failed to initialize ADC input %s\n",adc_inputs[i].name);
}
/* This waits until a new conversion is read on the channel */
if (_lwadc_wait_next(&lwadc_inputs[i])) {
if (_lwadc_read(&lwadc_inputs[i], &scaled) &&
_lwadc_read_raw(&lwadc_inputs[i], &raw)) {
/* Obtained data, is the change significant enough to display? */
delta = (raw>last[i])?raw-last[i]:last[i]-raw;
if (delta > max_delta) {
printf("%-30s = %04x (%d mv)\n",adc_inputs[i].name, raw,scaled);
last[i]=raw;
}
}
}
}
}