ADC Library
ADC (Analog to Digital Converter) module is available with a number of dsPIC30/33 and PIC24 MCU modules. ADC is an electronic circuit that converts continuous signals to discrete digital numbers. ADC Library provides you a comfortable work with the module.
Library Routines
ADCx_Init
Prototype |
sub procedure ADCx_Init() |
---|---|
Description |
This routines configures ADC module to work with default settings. The internal ADC module is set to:
|
Parameters |
None. |
Returns |
Nothing. |
Requires |
|
Example |
ADC1_Init() ' Initialize ADC1 module with default settings |
Notes |
|
ADCx_Init_Advanced
Prototype |
' dsPIC30F and PIC24FJ prototype
sub procedure ADC1_Init_Advanced(dim Reference as word) ' dsPIC33FJ and PIC24HJprototypesub procedureADCx_Init_Advanced(dim ADCMode as word, dim Reference as word) |
||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This routine configures the internal ADC module to work with user defined settings. |
||||||||||||||||||||||||||
Parameters |
|
||||||||||||||||||||||||||
Returns |
Nothing. |
||||||||||||||||||||||||||
Requires |
|
||||||||||||||||||||||||||
Example |
ADC1_Init_Advanced(_ADC_10bit, _ADC_INTERNAL_REF) ' sets ADC module in 10-bit resolution mode with internal reference used ADC1_Init_Advanced(_ADC_12bit, _ADC_INTERNAL_VREFL or _ADC_INTERNAL_VREFH) ' sets ADC module in 12-bit resolution mode with the following reference : AVss and VREF+ |
||||||||||||||||||||||||||
Notes |
|
ADCx_Get_Sample
Prototype |
sub function ADCx_Get_Sample(dim channel as word) as word |
---|---|
Description |
The function enables ADC module and reads the specified analog channel input. |
Parameters |
|
Returns |
10-bit or 12-bit (depending on selected mode by ADCx_Init_Advanced or MCU) unsigned value from the specified |
Requires |
|
Example |
dim adc_value as word ... adc_value = ADC1_Get_Sample(10) ' read analog value from ADC1 module channel 10 |
Notes |
|
ADCx_Read
Prototype |
sub function ADCx_Read(dim channel as word) as word |
---|---|
Description |
The function initializes, enables ADC module and reads the specified analog channel input. |
Parameters |
|
Returns |
10-bit or 12-bit (depending on the MCU) unsigned value from the specified |
Requires |
|
Example |
dim adc_value as word ... adc_value = ADC1_Read(10) ' read analog value from ADC1 module channel 10 |
Notes |
|
ADC_Set_Active
Prototype |
sub procedure ADC_Set_Active(dim adc_gs as ^TADC_Get_Sample) |
---|---|
Description |
Sets active ADC module. |
Parameters |
Parameters :
|
Returns |
Nothing. |
Requires |
Routine is available only for MCUs with multiple ADC modules. Used ADC module must be initialized before using this routine. See ADCx_Init and ADCx_Init_Advanced routines. |
Example |
' Activate ADC2 module ADC_Set_Active(@ADC2_Get_Sample) |
Notes |
None. |
Library Example
This code snippet reads analog value from the channel 1 and sends readings as a text over UART1.
program ADC_on_LEDs dim ADCresult as word txt as char[6] main: PORTB = 0x0000 ' clear PORTB TRISB = 0xFFFF ' PORTB is input ADC1_Init() ' Enable ADC module UART1_Init(9600) ' Initialize UART communication while TRUE ADCresult = ADC1_Get_Sample(1) ' Acquire ADC sample WordToStr(ADCresult, txt) ' convert its value to string UART1_Write_Text(txt) ' and send it to UART terminal Delay_ms(50) wend end.
HW Connection
ADC HW connection
What do you think about this topic ? Send us feedback!