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:

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 :
  • adv_setting: ADC module configuration flags. Predefined library constants (see the table below) can be ORed to form appropriate configuration value.
    Description Predefined library const
    Voltage reference constants:
    Internal voltage reference _INTERNAL_REF
    External voltage reference _EXTERNAL_REF
    Justification constants:
    Left justification _LEFT_ADJUSTMENT
    Right justification _RIGHT_ADJUSTMENT
Note:
  • Advanced configuration of the ADC module is supported only by Silicon Laboratories MCU's. Please consult appropriate datasheet.
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
sub function ADCx_Get_Sample(dim adv_setting as byte) as word

' for MCUs with 16-bit ADC module
sub function ADCx_Get_Sample() as word

Returns

8-bit, 10-bit or 12-bit unsigned value read from the specified channel (MCU dependant).
16-bit unsigned value read from the dedicated channel (for Silicon Laboratories C8051F06x MCU's).

Description

Reads analog value from the specified port and channel.

Parameters :

  • channel: represents the channel from which the analog value is to be acquired. Refer to the appropriate datasheet for channel-to-pin mapping.
  • adv_setting: represents the advanced setting for the MCU's that have ADC module routed to different ports. Refer to the appropriate datasheet.
Predefined library constants (see the table below) can be ORed with the desired channel number to form the appropriate configuration value :
    Description Predefined library const
    ADC port constants:
    Port 0 _P0
    Port 1 _P1
    Port 2 _P2
    Port 3 _P3

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

ADC HW connection

Copyright (c) 2002-2013 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code