GETADC

Top  Previous  Next

Action

Retrieves the analog value from the specified channel.

 

 

Syntax

var = GETADC(channel [,offset])

 

 

Remarks

Var

The variable that is assigned with the A/D value. This should be a Word or other 16 bit variable.

Channel

The channel to measure. Might be higher then 7 on some chips. The Mega2560 has 16 channels. So the range is 0-15 on a Mega2560.

Offset

An optional numeric variable of constant that specifies gain or mode. This option has effect on newer AVR micro’s only. The offset will be added by the channel value and inserted into the ADMUX register. This way you can control gain.

 

The GETADC() function only will work on microprocessors that have an A/D converter.

The pins of the A/D converter input can be used for digital I/O too.

But it is important that no I/O switching is done while using the A/D converter.

 

Make sure you turn on the AD converter with the START ADC statement or by setting the proper bit in the ADC configuration register.

 

Some micro’s have more then 7 channels. This is supported as well. The ADCSRB register contains a bit named MUX5 that must be set when a channel higher then 7 is used. The compiler (lib routine) will handle this automatic. This is true for new chips like Mega1280, Mega2560 and probably other new chips with 100 pins.

 

An example on how to read singled ended input on a Mega1280:

 W = Getadc(0 , 64)  ' from data sheet :  100000 ADC8

 W = Getadc(1, 64)   ' from data sheet :  100001 ADC9

This will read channel 0 and 1. The offset is 64 in order to use singled ended input.

ADC8 is portK.0

 

 

GetADC() returns a word variable since the A/D converter data registers consist of 2 registers. The resolution depends on the chip.

The variable ADCD can be used to access the data register directly. The compiler will handle access to the byte registers automatically.

 

See also

CONFIG ADC

 

 

Example

'--------------------------------------------------------------------------------

'name                     : adc.bas

'copyright                : (c) 1995-2005, MCS Electronics

'purpose                  : demonstration of GETADC() function for 8535 or M163 micro

'micro                    : Mega163

'suited for demo          : yes

'commercial addon needed  : no

'use in simulator         : possible

' Getadc() will also work for other AVR chips that have an ADC converter

'--------------------------------------------------------------------------------

$regfile = "m163def.dat"                                   ' we use the M163

$crystal = 4000000

 

$hwstack = 32                                               ' default use 32 for the hardware stack

$swstack = 10                                               'default use 10 for the SW stack

$framesize = 40                                             'default use 40 for the frame space

 

 

'configure single mode and auto prescaler setting

'The single mode must be used with the GETADC() function

 

'The prescaler divides the internal clock by 2,4,8,16,32,64 or 128

'Because the ADC needs a clock from 50-200 KHz

'The AUTO feature, will select the highest clockrate possible

Config Adc = Single , Prescaler = Auto

'Now give power to the chip

Start Adc

 

'With STOP ADC, you can remove the power from the chip

'Stop Adc

 

Dim W As Word , Channel As Byte

 

Channel = 0

'now read A/D value from channel 0

Do

W = Getadc(channel)

Print "Channel " ; Channel ; " value " ; W

Incr Channel

If Channel > 7 Then Channel = 0

Loop

End

 

'The new M163 has options for the reference voltage

'For this chip you can use the additional param :

'Config Adc = Single , Prescaler = Auto, Reference = Internal

'The reference param may be :

'OFF      : AREF, internal reference turned off

'AVCC     : AVCC, with external capacitor at AREF pin

'INTERNAL : Internal 2.56 voltage reference with external capacitor ar AREF pin

 

'Using the additional param on chip that do not have the internal reference will have no effect.