Analogue Input

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.

To add an analogue input from Project Designer then drag the entry to your list of devices. This will open a window:

Change the name to something more meaningful to your project and select the ADC channel from the drop down list and then click Ok. Regenerate the code for your project and you will see some example code showing how to read the input. Note that you can always move the input to another channel in the drop down list and, so long as you don't change the name, then your code won't need modifying.

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.

There are times, e.g. a data acquisition/logging project, where you want to be able to read various inputs within a for loop and so accessing the inputs by name may not be ideal. In this case you can access the channel directly. For example: this will read all of the analogue channels on the board:

for(int i=0; i<NUM_ADC_CHANNELS;i++){

a2dConvert8bit(ADC_NUMBER_TO_CHANNEL(i));

}

Function Summary

Function

a2dConvert8bit
- Read the value from an ADC channel and return it as a single byte in the range 0-255.

a2dConvert10bit
- Read the value from an ADC channel and return it as a value in the range 0-1023.

a2dReadPercent
- Read the value from an ADC channel and return it as a single byte percentage in the range 0-100.

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

a2dGetAVcc
- Returns the AVcc voltage in millivolts ie 5 volts would be returned as 5000.

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.

Syntax

a2dConvert8bit(name)

a2dConvert8bit(channel)

Parameters

name - this is the name you have specified in Project Designer for the device.

channel - the channel to be read (see example below).

Returns

A value in the range 0 to 255 which can be stored in an 'uint8_t'.

Example

If you have created the Analogue Input in Project Designer with a name such as 'myADC' then you can read the channel using:

uint8_t value = a2dConvert8bit( myADC );

Alternatively: if you want to read a specific channel, e.g. channel 9, then you should write:

uint8_t value = a2dConvert8bit( ADC_NUMBER_TO_CHANNEL(9) );

uint16_t a2dConvert10bit(ADC_CHANNEL channel)

Read the value from an ADC channel and return it as a value in the range 0-1023.

Syntax

a2dConvert10bit(name)

a2dConvert10bit(channel)

Parameters

name - this is the name you have specified in Project Designer for the device.

channel - the channel to be read (see example below).

Returns

A value in the range 0 to 1023 which can be stored in an 'uint16_t'.

Example

If you have created the Analogue Input in Project Designer with a name such as 'myADC' then you can read the channel using:

uint16_t value = a2dConvert10bit( myADC );

Alternatively: if you want to read a specific channel, e.g. channel 9, then you should write:

uint16_t value = a2dConvert10bit( ADC_NUMBER_TO_CHANNEL(9) );

uint8_t a2dReadPercent(ADC_CHANNEL channel)

Read the value from an ADC channel and return it as a single byte percentage in the range 0-100.

Syntax

a2dReadPecent(name)

a2dReadPercent(channel)

Parameters

name - this is the name you have specified in Project Designer for the device.

channel - the channel to be read (see example below).

Returns

A value in the range 0 to 100 which can be stored in an 'uint8_t'.

Example

If you have created the Analogue Input in Project Designer with a name such as 'myADC' then you can read the channel using:

uint8_t percent = a2dReadPercent( myADC );

Alternatively: if you want to read a specific channel, e.g. channel 9, then you should write:

uint8_t percent = a2dReadPercent( ADC_NUMBER_TO_CHANNEL(9) );

Notes

This function is useful, for example, if you have got a potentiometer connected to an Analogue Input pin and you want to read the 'position' rather than the actual voltage. The same piece of code would then work without modification if used on another board that uses a different reference voltage for the analogue to digital unit of the processor.

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.

Syntax

a2dReadMv(name)

a2dReadMv(channel)

Parameters

name - this is the name you have specified in Project Designer for the device.

channel - the channel to be read (see example below).

Returns

An 'uint16_t' representing the number of millivolts.

Example

If you have created the Analogue Input in Project Designer with a name such as 'myADC' then you can read the channel using:

uint16_t mV = a2dReadMv( myADC );

Alternatively: if you want to read a specific channel, e.g. channel 9, then you should write:

uint16_t mV = a2dReadMv( ADC_NUMBER_TO_CHANNEL(9) );

uint16_t a2dGetAVcc(void)

Returns the AVcc voltage in millivolts ie 5 volts would be returned as 5000.

Syntax

a2dGetAVcc()

Parameters

None

Returns

The millivolts used by the analogue to digital conversion unit as an 'uint16_t'.

Notes

This will always return the same fixed value for a given board design.

a2dOff()

This will turn off the ADC unit which will save power.

Syntax

a2dOff()

Parameters

None

Returns

Nothing

Note

Before reading any more values you should call a2dOn() to turn the ADC unit back on again. If you are using power hungry devices like motors and servos then the power saved by turning the ADC unit off will be so minimal that it is not worth doing.

See Also

a2dOn()

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().

Syntax

a2dOn()

Parameters

None

Returns

None

See Also

a2dSetPrescaler(uint8_t prescale)

Change the prescaler that sets the ADC speed vs accuracy.

Syntax

a2dSetPrescaler(prescale)

Parameters

prescale: may be one of the following values:-

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. The default value is ADC_PRESCALE_DIV64.