Wixel SDK
Macros | Functions
adc.h File Reference

Go to the source code of this file.

Macros

#define ADC_REFERENCE_INTERNAL   0b10000000
 
#define ADC_REFERENCE_VDD   0
 
#define ADC_BITS_7   0b00110000
 
#define ADC_BITS_9   0b00100000
 
#define ADC_BITS_10   0b00010000
 
#define ADC_BITS_12   0
 

Functions

uint16 adcRead (uint8 channel)
 
int16 adcReadDifferential (uint8 channel)
 
uint16 adcReadVddMillivolts ()
 
void adcSetMillivoltCalibration (uint16 vddMillivolts)
 
int16 adcConvertToMillivolts (int16 adcResult)
 

Detailed Description

The adc.lib library provides functions for using the CC2511's Analog-to-Digital Converter (ADC). The ADC can measure several different things, including voltages on any of the six Port 0 pins, the voltage on the VDD line (the 3V3 pin on the Wixel), and the temperature.

The ADC has 14 channels:

CC2511 ADC Channels
Channel NumberNameAppropriate Function
0AIN0 (P0_0)adcRead()
1AIN1 (P0_1)adcRead()
2AIN2 (P0_2)adcRead()
3AIN3 (P0_3)adcRead()
4AIN4 (P0_4)adcRead()
5AIN5 (P0_5)adcRead()
8AIN0 - AIN1adcReadDifferential()
9AIN2 - AIN3adcReadDifferential()
10AIN4 - AIN5adcReadDifferential()
11AIN6 - AIN7adcReadDifferential()
12GND
13Internal 1.25 V ReferenceadcRead()
14Temperature SensoradcRead()
15VDD/3

The channel parameter

Most functions in this library require a channel parameter to specify which channel to use. The value of this parameter should be one of the channel numbers in the table above.

You can also use the bitwise OR operator (|) to specify advanced options in the channel parameter:

The ADCCFG register

The CC2511F32 datasheet says that to configure a pin on Port 0 to be used as an analog pin, the corresponding bit in the ADCCFG register must be set to 1. However, we have not found this to be necessary, and this library does not touch ADCCFG.

If you want to follow the datasheet, then you could write to ADCCFG at the beginning of your program with some code like this:

ADCCFG |= (1 << 3); // Enable ADC input on P0_3.

Definition in file adc.h.

Macro Definition Documentation

#define ADC_BITS_10   0b00010000

Specifies that the decimation rate should be 256, which gives 10 bits of resolution. With this setting, each conversion takes 68 microseconds.

Definition at line 88 of file adc.h.

#define ADC_BITS_12   0

Specifies that the decimation rate should be 512, which gives 12 bits of resolution. With this setting, each conversion takes 132 microseconds. This is the default setting.

Definition at line 94 of file adc.h.

#define ADC_BITS_7   0b00110000

Specifies that the decimation rate should be 64, which gives 7 bits of resolution. With this setting, each conversion takes 20 microseconds.

Definition at line 76 of file adc.h.

#define ADC_BITS_9   0b00100000

Specifies that the decimation rate should be 128, which gives 9 bits of resolution. With this setting, each conversion takes 36 microseconds.

Definition at line 82 of file adc.h.

#define ADC_REFERENCE_INTERNAL   0b10000000

Specifies that the internal 1.25 voltage reference should be used. This means that a value of 2047 corresponds to 1.25 V instead of 3.3 V.

Definition at line 65 of file adc.h.

#define ADC_REFERENCE_VDD   0

Specifies that the VDD line should be used as a voltage reference. This means that a value of 2047 corresponds to VDD (usually 3.3 V). This is the default setting.

Definition at line 70 of file adc.h.

Function Documentation

int16 adcConvertToMillivolts ( int16  adcResult)

Converts an ADC result to millivolts.

Parameters
adcResultAn ADC result between -2048 and 2047 that was measured using VDD as a reference. You can obtain such a measurement by calling adcRead() or adcReadDifferential().
Returns
The voltage in units of millivolts.

By default, this function assumes that your VDD is at 3300 mV. If you expect your VDD to go above or below that, or you want more accurate results, you should use adcSetMillivoltCalibration().

This function only applies to AD conversions where VDD was used as a reference. If you used the internal 1.25 V reference instead, you can convert your result to millivolts by multiplying it by 1250 and then dividing it by 2047.

Definition at line 17 of file millivolts.c.

uint16 adcRead ( uint8  channel)

Reads the voltage on the specified channel.

Parameters
channelThe number of the channel to measure (0-6 or 13-15). This parameter can also contain advanced options (see above).
Returns
A number between 0 and 2047, where 0 represents a voltage of 0 V and 2047 represents a voltage equal to the selected voltage reference (usually 3.3 V).

Example:

1 uint16 result1, result2;
2 result1 = adcRead(3); // Measures voltage on P0_3.
3 result2 = adcRead(4 | ADC_REFERENCE_INTERNAL | ADC_BITS_7);

This function returns an unsigned number so it is not appropriate for differential channels. See adcReadDifferential().

Definition at line 5 of file adc.c.

int16 adcReadDifferential ( uint8  channel)

Reads the voltage difference on the specified differential channel.

Parameters
channelThe number of the differential channel to measure (8-11). This parameter can also contain advanced options (see above).
Returns
A number between -2048 and 2047. A value of 2047 means that the voltage difference was equal to the selected voltage reference. A value of -2048 means that the voltage difference was equal to the negation of the selected voltage reference. A value of 0 means that the voltage difference was zero.

Example:

1 int16 diff1, diff2;
2 diff1 = adcReadDifferential(8); // Measures (voltage of P0_0) - (voltage of P0_1).
3 diff2 = adcReadDifferential(9 | ADC_REFERENCE_INTERNAL | ADC_BITS_7);

Definition at line 25 of file adc.c.

uint16 adcReadVddMillivolts ( )

Reads the voltage of the VDD (3V3) line using the internal voltage reference and returns the voltage of VDD in units of millivolts (mV).

Definition at line 6 of file millivolts.c.

void adcSetMillivoltCalibration ( uint16  vddMillivolts)

Sets the calibration parameter that is used by adcConvertToMillivolts().

Parameters
vddMillivoltsThe voltage of the VDD line in units of millivolts.

If your VDD is going to drop below 3.3 V, and you want to measure a voltage in units of millivolts (as opposed to the raw ADC units) you should run the following code regularly:

1 adcSetMillivoltCalibration(adcReadVddMillivolts());

Definition at line 12 of file millivolts.c.