ADC Library
ADC (Analog to Digital Converter) module is available with a number of MCUs. Library routines are included to provide you comfortable work with the module in single-ended mode.
Library Routines
Notes:
- Some of the MCUs do not support ADCx_Init_Advanced routine. Please, refer to the appropriate datasheet.
- ADC routines require you to specify the module you want to use. To select the desired module, simply change the letter
x
in the prototype for a number from1
to3
.
Number of ADC modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
ADCx_Init
Prototype |
sub procedure ADCx_Init() |
---|---|
Returns |
Nothing |
Description |
Initializes MCU’s internal ADC module to work with default settings; internal voltage reference and right justification of the ADC data. |
Requires |
MCU must have ADC module. |
Example |
ADC1_Init() ' Initializes MCU’s internal ADC module to work with default settings |
ADCx_Init_Advanced
Prototype | sub procedure ADCx_Init_Advanced(dim channel as byte) | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
Nothing. |
||||||||||||||
Description |
This routine configures and enables the internal ADC module to work with user defined settings. Parameters :
|
||||||||||||||
Requires |
MCU must have ADC module. |
||||||||||||||
Example |
ADC1_Init_Advanced(_EXTERNAL_REF or _RIGHT_ADJUSTMENT) ' initializes ADC module to work with external voltage reference and right justification of the ADC data. |
ADCx_Get_Sample
Prototype |
sub function ADCx_Get_Sample(dim channel as byte) as word
' for MCUs with ADC port selection
' for MCUs with 16-bit ADC module |
||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
8-bit, 10-bit or 12-bit unsigned value read from the specified channel (MCU dependant).
|
||||||||||||
Description |
Reads analog value from the specified port and channel. Parameters :
|
||||||||||||
Requires |
ADC module must be initialized before using this function. See ADCx_Init and ADCx_Init_Advanced routines. |
||||||||||||
Example |
dim tmp as word ADC1_Init() ' Initialize ADC module tmp = ADC1_Get_Sample(2) ' Reads analog value from channel 2 ' for MCU's with advanced channel configuration: tmp = ADC1_Get_Sample(_P1 or 3) ' Reads analog value from channel 3 of PORT1 |
Library Example
This example code reads analog value from channel AIN2.2 (P1.2) and sends it to USART Terminal.
program ADC dim adc_result as word txt as char[6] main: WDTCN = 0xDE ' Sequence for WDTCN = 0xAD ' disabling the watchdog timer OSCICN = 0x83 ' Enable internal oscillator (24.5MHz divided by 1) P0MDOUT = P0MDOUT or 0x01 ' Configure P0.0 (TX) pin as push-pull UART2_Init(4800) ' Initialize UART2 Delay_100ms() P1MDIN.B2 = 0 ' Configure P1.2 as Analog Input ADC2_Init() ' Initialize ADC2 module while TRUE adc_result = ADC2_Get_Sample(2) ' Read AIN2.2 (P1.2) analog input WordToStr(adc_result, txt) ' convert result to string UART2_Write_Text(txt) ' send string to UART UART2_Write(13) UART2_Write(10) ' send new line (CR+LF) Delay_ms(500) wend end.
HW Connection
ADC HW connection
What do you think about this topic ? Send us feedback!