In this section, we will cover the light weight ADC driver, which enable you to read the ADC the output and raw form, and converted to scale form , or even as average level over a number of reads. The light weight ADC driver also allows you to configure a number of attributes of the converter. So you can customize your application.
Quick overview of ADC
ADC is a system that converts an analog signal into digital signal. Simply put, Analog signals here means any variable voltage that is being monitored. It doesn’t matter what the signal is doing, it can remain the same level continuously, it can have slight wave to it, or it can be seemingly random. The analog to digital converter requires you to provide the reference voltage. That defines the maximum level that the input can be. To determine the voltage level of the input the circular ADC generate a local voltage, and just this level until the same level as the input.
This can be done in different ways but the circuitry will adjust the level of the local signal until the comparator to detect the two inputs are the same or lease the same as the resolution of ADC, once the signal level is matched, the output generated. Which can be read by your application. To calculate the signal level the ADC defines the full range from the input into the three levels. ADC typically have a resolution of 12 to 16 bits, so the 212 to 216 different levels. Whatever level input signals determined to be add , the reading from the ADC will be a digital representation of this value compare the reference voltage. The digital output then represents what percentage of the allowed input range the input signal is currently add.
Being at the minimum level will result an output of zero and being the max will resut in an output of 0xffff. And for inputs some in between the result is the output that represents the level measured. So if you are using a reference voltage of 3.3V for example, the reading will represent what flection the 3.3 v the input voltage is . If the input voltage is zero. The reading will be 0 of course. And if the input is 3.3 v, the reading will be all FF, meaning 100 percent.
So in between, if the input voltage is 1v, the result will be roughly 30.3%. And an input of 2v, will result the input roughly 60.6%, and 3v will be 90.9%.
ADC Driver in MQX architecture Diagram
The light weight ADC driver is considered a low level driver and is located alongside the LWGPIO driver. This is because we don’t trade the interfaces as a part of serial communication, they passing data back and forth in blocks or in bytes, also it doesn’t make sense to be accessing the drivers through the IO subsystem.
There is a section dedicated to ADC module in bsp files folder in project. (take twrk70f120m for example)
The ADC priority is defined as 3, though ADC is typically not used with interrupts,
The default reference voltage and maximum voltage is 3300mV.
The input setting of the potentiometer is connected to ADC1_SOURCE_AD20 in Twr-k70 board.
The adc1 device is designated as the default ADC device
This function initializes the ADC module according to the parameters given in the platform specific initialization structure. Call to this function does not start any ADC conversion. This function is normally called in the BSP initialization code.
2 _lwadc_init_input ()
This function initializes the application allocated LWADC_STRUCT with all data needed later for quick control of particular input. This function sets the ADC input to continuous conversion mode if not already in this mode.
3 _lwadc_set_attribute / _lwadc_get_attribute :
both this functions receive a pointer to the LWADC_STRUCT_PRT , and the attributes you interested in. All the attributes are defined in an enum LWADC_ATTRIBUTE.
4 _lwadc_read_raw ()
Read the current value of the ADC input and return the result without applying any scaling.
5 _lwadc_read ()
This function reads the current value of the ADC input, applies scaling according to preset parameters.
6 _lwadc_read_average() This function reads num_sample samples from the specified input and returns the scaled average reading.
This function waits for a new value to be available on the specified ADC input.