WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

a2d.h

Performs analogue to digital conversion (ADC).
The ADC unit works with channels - each microprocessor will map a channel to a given pin. These pins are often the same ones as the standard I/O pins - but not always.
The number of channels available will also depend on your microprocessor.
In order to make your code more portable then you should reference each channel by its 'name' as defined in this file. e.g.:-
ADC_CH_ADC0
ADC_CH_ADC1
ADC_CH_ADC2
ADC_CH_ADC3
ADC_CH_ADC4
ADC_CH_ADC5
ADC_CH_ADC6
ADC_CH_ADC7
etc
This list also contains any other hardware supported options such as the difference between different channels. However: these options are not often used and so I have also created shortcut names for the individual channels called: ADC0, ADC1, ADC2 etc.
By using these names (rather than just numbers like 0, 1, 2 etc) then the compiler will generate an error if the target processor does not support that channel.
Note that the channel numbers are not sequential numbers so you cannot go through the channels with a 'for' loop. Channels 0 to 7 are one sequence of numbers but channels 8 to 15 are another sequence.
This code will NOT work as you might expect:-
for(int i=0; i<NUM_ADC_CHANNELS;i++){
    a2dConvert8bit(i);
}
You must use the ADC_NUMBER_TO_CHANNEL macro to convert a 'number' into a valid 'channel'. So the code should be written as:-
for(int i=0; i<NUM_ADC_CHANNELS;i++){
    a2dConvert8bit(ADC_NUMBER_TO_CHANNEL(i));
}
Whenever you read a channel the library will automatically initialise the ADC system so you don't need to call 'a2dInit' yourself. It is only there so that code converted from AVRlib doesn't generate any errors.
The ADC will, by default, be initialised with a prescaler of 64 and a reference voltage of AVCC. These settings are fine for general usage. Alternatively you can change the values at runtime with the a2dSetPrescaler and a2dSetReference commands.

 

Function

 


a2dOff()

This will turn off the ADC unit which will save power.
Before reading any more values you should call a2dInit();

a2dSetPrescaler(uint8_t prescale)

Change the prescaler. Possible values are:-
ADC_PRESCALE_DIV2
ADC_PRESCALE_DIV4
ADC_PRESCALE_DIV8
ADC_PRESCALE_DIV16
ADC_PRESCALE_DIV32
ADC_PRESCALE_DIV64
ADC_PRESCALE_DIV128
Lower values will be quicker but less accurate.

a2dSetReference(uint8_t ref)

Set the reference voltage for the ADC unit. The parameter should be one of the following:
ADC_REFERENCE_AREF
ADC_REFERENCE_AVCC
ADC_REFERENCE_INTERNAL
NB - This function has been deprecated and AVCC is always used.

a2dInit()

This will turn on the ADC unit.
This is called automatically at start up and so you should not need to call it again other than after an a2dOff().

ADC_CHANNEL ADC_NUMBER_TO_CHANNEL

This macro can be used to convert a number into a channel.
For example to read all of the channels could be done like this:
for(uint8_t num=0; num<NUM_ADC_CHANNELS; num++){
    uint8_t value = a2dConvert8bit( ADC_NUMBER_TO_CHANNEL(num) );
}

uint8_t a2dConvert8bit(ADC_CHANNEL channel)

Read the value from an ADC channel and return it as a single byte in the range 0-255
NB The parameter should either be one of the constant values such as ADC_CH_ADC3 or you can convert a number into a channel by using the ADC_NUMBER_TO_CHANNEL macro.
So to read channel 3 you could either write:
uint8_t value = a2dConvert8bit(ADC_CH_ADC3);
or
uint8_t value = a2dConvert8bit( ADC_NUMBER_TO_CHANNEL(3));
But you should never write:
uint8_t value = a2dConvert8bit(3);
This will definitely cause problems for channels 8 onwards.

uint16_t a2dConvert10bit(ADC_CHANNEL channel)

Returns the value in the range 0-1023 from an ADC channel.
NB The parameter should either be one of the constant values such as ADC_CH_ADC3 or you can convert a number into a channel by using the ADC_NUMBER_TO_CHANNEL macro.
So to read channel 3 you could either write:
uint16_t value = a2dConvert10bit(ADC_CH_ADC3);
or
uint16_t value = a2dConvert10bit( ADC_NUMBER_TO_CHANNEL(3));
But you should never write:
uint16_t value = a2dConvert10bit(3);
This will definitely cause problems for channels 8 onwards.

uint8_t NUM_ADC_CHANNELS

Returns the number of ADC channels on this device.

uint8_t a2dReadPercent(ADC_CHANNEL channel)

Returns the value of the ADC channel as a percentage ie 0 to 100.

uint16_t a2dReadMv(ADC_CHANNEL channel)

Returns the value of an ADC_CHANNEL in milli-volts. ie a value of 1.65V would return 1650.

uint16_t a2dGetAVcc(void)

Returns the AVcc voltage in milli volts ie 5 volts would be returned at 5000.

Valid XHTML 1.0 Transitional