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 |
void 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
void ADC1_Init_Advanced(unsigned Reference); // dsPIC33FJ and PIC24HJ prototypevoid ADCx_Init_Advanced(unsigned ADCMode, unsigned Reference); |
||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 | _ADC_INTERNAL_VREFH); // sets ADC module in 12-bit resolution mode with the following reference : AVss and VREF+ |
||||||||||||||||||||||||||
| Notes |
|
||||||||||||||||||||||||||
ADCx_Get_Sample
| Prototype |
unsigned ADCx_Get_Sample(unsigned channel); |
|---|---|
| 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 |
unsigned adc_value; ... adc_value = ADC1_Get_Sample(10); // read analog value from ADC1 module channel 10 |
| Notes |
|
ADCx_Read
| Prototype |
unsigned ADCx_Read(unsigned channel); |
|---|---|
| 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 |
unsigned adc_value; ... adc_value = ADC1_Read(10); // read analog value from ADC1 module channel 10 |
| Notes |
|
ADC_Set_Active
| Prototype |
void ADC_Set_Active(unsigned (*adc_gs)(unsigned)); |
|---|---|
| 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.
unsigned adcRes;
char txt[6];
void main() {
PORTB = 0x0000;
TRISB.F1 = 1; // set pin as input - needed for ADC to work
ADC1_Init();
UART1_Init(9600);
while (1) {
adcRes = ADC1_Get_Sample(1);
WordToStr(adcRes, txt);
UART1_Write_Text(txt);
Delay_ms(50);
}
}
HW Connection

ADC HW connection
What do you think about this topic ? Send us feedback!




