CCS News

Using the CCS C Compiler with Analog-to-Digital Converters with Computation Module

Tuesday 07 June, 2016

Support has been added to the CCS C Compiler for the new Analog-To-Digital Converter with Computation (ADC2) module that the PIC16F188xx family of devices have. In addition to legacy mode of operation the ADC2 module can be setup in Accumulate, Average, Burst Average or Low-Pass Filter modes of operation. When setup for Accumulate mode with each trigger of the ADC the conversion result is added to the accumulator and the ADCNT register is incremented. When setup for Average mode with each trigger of the ADC the conversion result is added to the accumulator and when the specified number of triggers has been done the average will be preformed on the accumulator. When setup for Burst Average mode it is similar to Average mode the difference being that when the ADC is triggered, it will perform the specified number of conversions and then perform the average calculation. When setup for Low-Pass Filter mode with each trigger of the ADC the conversion result is sent through the filter.

To support the new modes of operation the CCS C Compiler's built-in setup_adc() function has been updated to setup the ADC in these modes. Additionally an optional second and third parameter as been added to the function to set some additionally settings when using these the new modes of operation. When using Accumulate, Average or Burst Average modes the second parameter sets how much the accumulated value is divided by after each conversion. When using Low-Pass Filter mode the second parameter sets the cut-off frequency of the filter. The third parameter to the function sets the number of samples to be done before performing a threshold comparison for Average, Burst Average and Low-Pass Filter modes of operation.

In addition to the updates to the setup_adc() function the built-in functions adc_write() and adc_read() have been added to write and read some the other registers used by the ADC2 module. For example when setup for Low-Pass Filter mode the filtered result is stored in the ADFLTRH and ADFLTRL registers. After the calculation is completed the adc_read() function can be then be used to read the filtered result, for example:

Result = adc_read(ADC_FILTER);

Another feature of the ADC2 module is the ability to set a trigger source to start a conversion. To support this feature the built-in function set_adc_trigger() has been added to the CCS C Compiler. For example the following can be used to setup the ADC2 module to trigger the conversion when Timer 2 period match occurs:

set_adc_trigger(ADC_TRIGGER_TIMER2);

The following is an example using the CCS C Compiler to setup and use the ADC2 module for Low-Pass Filter mode, see ex_lowpass_filter_adc2.c in the PICCExamples folder for entire example:

setup_timer_2(T2_CLK_INTERNAL | T2_DIV_BY_128, 155, 10); //~10ms period, 100ms interrupt

setup_adc_ports(ADC_PIN, VSS_VDD);
setup_adc(ADC_LOW_PASS_FILTER_MODE | ADC_CLOCK_INTERNAL | ADC_TAD_MUL_255 | ADC_THRESHOLD_INT_END_OF_CALCULATION, FILTER_CUT_OFF_FREQ, ADC_READINGS);

set_adc_channel(ADC_CHANNEL);
set_adc_trigger(ADC_TRIGGER_TIMER2);

while(TRUE)
{
if(interrupt_active(INT_AD_THRESHOLD))
{
FilteredResult= adc_read(ADC_FILTER);

clear_interrupt(INT_AD_THRESHOLD);
}
}

Like us on Facebook. Follow us on Twitter.

About CCS:

CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit http://www.ccsinfo.com.

PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.