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 |
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
procedure ADC1_Init_Advanced(Reference : word); // dsPIC33FJ and PIC24HJ prototypeprocedure ADCx_Init_Advanced(ADCMode : word; Reference : 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 |
function ADCx_Get_Sample(channel : word) : 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 |
var adc_value : word; ... adc_value = ADC1_Get_Sample(10); // read analog value from ADC1 module channel 10 |
| Notes |
|
ADCx_Read
| Prototype |
function ADCx_Read(channel : word) : 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 |
var adc_value : word; ... adc_value = ADC1_Read(10); // read analog value from ADC1 module channel 10 |
| Notes |
|
ADC_Set_Active
| Prototype |
procedure ADC_Set_Active(adc_gs : ^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;
var ADCresult : word;
txt : array[6] of char;
begin
PORTB := 0x0000; // clear PORTB
TRISB := 0xFFFF; // PORTB is input
ADC1_Init(); // Enable ADC module
UART1_Init(9600); // Initialize UART communication
while (TRUE) do
begin
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);
end;
end.
HW Connection

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




