Defines | Enumerations

Analog to Digital Converter (hal_adc)
[nRF24LE1 HAL]


This is a general-purpose ADC with up to 14 input channels. The ADC contains an internal 1.2V reference, but can also be used with external reference or full scale range equal to VDD. It can be operated in a single step mode with sampling under software control, or a continuous conversion mode with a programmable sampling rate.

This module contains setup functions for configuring the ADC; operation functions for starting the ADC and reading a sampled value; and status functions for checking if the ADC is busy or ready, or has overflowed.

Defines

#define ADC_STARTUP_CNT   1;
#define HAL_INP_AIN0   0x00
#define HAL_INP_AIN1   0x01
#define HAL_INP_AIN2   0x02
#define HAL_INP_AIN3   0x03
#define HAL_INP_AIN4   0x04
#define HAL_INP_AIN5   0x05
#define HAL_INP_AIN6   0x06
#define HAL_INP_AIN7   0x07
#define HAL_INP_AIN8   0x08
#define HAL_INP_AIN9   0x09
#define HAL_INP_AIN10   0x0A
#define HAL_INP_AIN11   0x0B
#define HAL_INP_AIN12   0x0C
#define HAL_INP_AIN13   0x0D
#define HAL_INP_VDD1_3   0x0E
#define HAL_INP_VDD2_3   0x0F

Enumerations

enum  hal_adc_input_channel_t {
  HAL_ADC_INP_AIN0 = 0x00, HAL_ADC_INP_AIN1 = 0x01, HAL_ADC_INP_AIN2 = 0x02, HAL_ADC_INP_AIN3 = 0x03,
  HAL_ADC_INP_AIN4 = 0x04, HAL_ADC_INP_AIN5 = 0x05, HAL_ADC_INP_AIN6 = 0x06, HAL_ADC_INP_AIN7 = 0x07,
  HAL_ADC_INP_AIN8 = 0x08, HAL_ADC_INP_AIN9 = 0x09, HAL_ADC_INP_AIN10 = 0x0A, HAL_ADC_INP_AIN11 = 0x0B,
  HAL_ADC_INP_AIN12 = 0x0C, HAL_ADC_INP_AIN13 = 0x0D, HAL_ADC_INP_VDD1_3 = 0x0E, HAL_ADC_INP_VDD2_3 = 0x0F
}
enum  hal_adc_reference_t { HAL_ADC_REF_INT = 0x00, HAL_ADC_REF_VDD = 0x01, HAL_ADC_REF_AIN3 = 0x02, HAL_ADC_REF_AIN9 = 0x03 }
enum  hal_adc_input_mode_t { HAL_ADC_SINGLE = 0x00, HAL_ADC_DIFF_AIN2 = 0x01, HAL_ADC_DIFF_AIN6 = 0x02 }
enum  hal_adc_conversion_mode_t { HAL_ADC_SINGLE_STEP = 0x00, HAL_ADC_CONTINOUS = 0x01 }
enum  hal_adc_sampling_rate_t { HAL_ADC_2KSPS = 0x00, HAL_ADC_4KSPS = 0x01, HAL_ADC_8KSPS = 0x02, HAL_ADC_16KSPS = 0x03 }
enum  hal_adc_power_down_delay_t { HAL_ADC_PDD_0US = 0x00, HAL_ADC_PDD_6US = 0x01, HAL_ADC_PDD_24US = 0x02, HAL_ADC_PDD_INF = 0x03 }
enum  hal_adc_acq_window_t { HAL_ADC_AQW_075US = 0x00, HAL_ADC_AQW_3US = 0x01, HAL_ADC_AQW_12US = 0x02, HAL_ADC_AQW_36US = 0x03 }
enum  hal_adc_resolution_t { HAL_ADC_RES_6BIT = 0x00, HAL_ADC_RES_8BIT = 0x01, HAL_ADC_RES_10BIT = 0x02, HAL_ADC_RES_12BIT = 0x03 }
enum  hal_adc_data_just_t { HAL_ADC_JUST_LEFT = 0x00, HAL_ADC_JUST_RIGHT = 0x01 }
enum  hal_adc_overflow_t { HAL_ADC_FLOW_NONE = 0x00, HAL_ADC_FLOW_OVER = 0x01, HAL_ADC_FLOW_UNDER = 0x02, HAL_ADC_FLOW_BOTH = 0x03 }

SETUP FUNCTIONS

void hal_adc_set_input_channel (hal_adc_input_channel_t chsel)
void hal_adc_set_reference (hal_adc_reference_t refsel)
void hal_adc_set_input_mode (hal_adc_input_mode_t input_mode)
void hal_adc_set_conversion_mode (hal_adc_conversion_mode_t conv_mode)
void hal_adc_set_sampling_rate (hal_adc_sampling_rate_t rate)
void hal_adc_set_power_down_delay (hal_adc_power_down_delay_t pdd)
void hal_adc_set_acq_window (hal_adc_acq_window_t tacq)
void hal_adc_set_resolution (hal_adc_resolution_t res)
void hal_adc_set_data_just (hal_adc_data_just_t just)

OPERATION FUNCTIONS

void hal_adc_start (void)
uint8_t hal_adc_read_LSB (void)
uint8_t hal_adc_read_MSB (void)

STATUS FUNCTIONS

_Bool hal_adc_busy (void)
hal_adc_overflow_t hal_adc_get_overflow_status (void)

Define Documentation

#define ADC_STARTUP_CNT   1;

The following example show a typical usage of the ADC:

 void main(void)
 {
   uint16_t adc_out;

   adc_init();                                         // Initialise ADC

   while(true)                                         // Endless loop
   {
     hal_adc_start();                                  // Start ADconversion
                                                     
     while(hal_adc_busy());                            // Wait for data ready
        
     adc_out = hal_adc_read_LSB();                     // Read LSB of output
     adc_out += ((uint16_t)hal_adc_read_MSB()) << 8;   // Add MSB of output
 
     // Output from ADC is now stored in adc_out

   }
 }

 // Initialize ADC: Set input channel to AIN7 and VDD as reference. Use 
 // single ended mode and single step conversion. Use 10 bits to represent
 // the analog input signal, set the output data to be right justified 

 void adc_init(void)
 {
   hal_adc_set_input_channel(HAL_ADC_INP_AIN7);                     
   hal_adc_set_reference(HAL_ADC_REF_VDD);                        
   hal_adc_set_input_mode(HAL_ADC_SINGLE);                             
   hal_adc_set_conversion_mode(HAL_ADC_SINGLE_STEP);               
   hal_adc_set_resolution(HAL_ADC_RES_10BIT);                          
   hal_adc_set_data_just(HAL_ADC_JUST_RIGHT);    
 }

Definition at line 76 of file hal_adc.h.

#define HAL_INP_AIN0   0x00

Definition at line 78 of file hal_adc.h.

#define HAL_INP_AIN1   0x01

Definition at line 79 of file hal_adc.h.

#define HAL_INP_AIN2   0x02

Definition at line 80 of file hal_adc.h.

#define HAL_INP_AIN3   0x03

Definition at line 81 of file hal_adc.h.

#define HAL_INP_AIN4   0x04

Definition at line 82 of file hal_adc.h.

#define HAL_INP_AIN5   0x05

Definition at line 83 of file hal_adc.h.

#define HAL_INP_AIN6   0x06

Definition at line 84 of file hal_adc.h.

#define HAL_INP_AIN7   0x07

Definition at line 85 of file hal_adc.h.

#define HAL_INP_AIN8   0x08

Definition at line 86 of file hal_adc.h.

#define HAL_INP_AIN9   0x09

Definition at line 87 of file hal_adc.h.

#define HAL_INP_AIN10   0x0A

Definition at line 88 of file hal_adc.h.

#define HAL_INP_AIN11   0x0B

Definition at line 89 of file hal_adc.h.

#define HAL_INP_AIN12   0x0C

Definition at line 90 of file hal_adc.h.

#define HAL_INP_AIN13   0x0D

Definition at line 91 of file hal_adc.h.

#define HAL_INP_VDD1_3   0x0E

Definition at line 92 of file hal_adc.h.

#define HAL_INP_VDD2_3   0x0F

Definition at line 93 of file hal_adc.h.


Enumeration Type Documentation

An enum describing the ADC's input channel.

Enumerator:
HAL_ADC_INP_AIN0 
HAL_ADC_INP_AIN1 
HAL_ADC_INP_AIN2 
HAL_ADC_INP_AIN3 
HAL_ADC_INP_AIN4 
HAL_ADC_INP_AIN5 
HAL_ADC_INP_AIN6 
HAL_ADC_INP_AIN7 
HAL_ADC_INP_AIN8 
HAL_ADC_INP_AIN9 
HAL_ADC_INP_AIN10 
HAL_ADC_INP_AIN11 
HAL_ADC_INP_AIN12 
HAL_ADC_INP_AIN13 
HAL_ADC_INP_VDD1_3 
HAL_ADC_INP_VDD2_3 

Definition at line 98 of file hal_adc.h.

An enum describing the ADC's reference.

Enumerator:
HAL_ADC_REF_INT 

Internal 1.22V reference

HAL_ADC_REF_VDD 

VDD as reference

HAL_ADC_REF_AIN3 

External reference on AIN3

HAL_ADC_REF_AIN9 

External reference on AIN9

Definition at line 120 of file hal_adc.h.

An enum describing the ADC's single ended or differential mode.

Enumerator:
HAL_ADC_SINGLE 

Single ended mode

HAL_ADC_DIFF_AIN2 

Differential with AIN2 as inv. input

HAL_ADC_DIFF_AIN6 

Differential with AIN6 as inv. input

Definition at line 130 of file hal_adc.h.

An enum describing the ADC's conversion mode.

Enumerator:
HAL_ADC_SINGLE_STEP 

Single step conversion

HAL_ADC_CONTINOUS 

Cont. conversion, def. sampling rate

Definition at line 139 of file hal_adc.h.

An enum describing the ADC's sampling rate (Continuous conversion).

Enumerator:
HAL_ADC_2KSPS 

ADC sampling rate = 2kbps

HAL_ADC_4KSPS 

ADC sampling rate = 4kbps

HAL_ADC_8KSPS 

ADC sampling rate = 8kbps

HAL_ADC_16KSPS 

ADC sampling rate = 16kbps

Definition at line 147 of file hal_adc.h.

An enum describing the ADC's power down delay (Single step conversion).

Enumerator:
HAL_ADC_PDD_0US 

ADC power down delay = 0us

HAL_ADC_PDD_6US 

ADC power down delay = 6us

HAL_ADC_PDD_24US 

ADC power down delay = 24us

HAL_ADC_PDD_INF 

ADC power down delay = infinite

Definition at line 157 of file hal_adc.h.

An enum describing the ADC's input acquisition window.

Enumerator:
HAL_ADC_AQW_075US 

Input acquisition window = 0.75us

HAL_ADC_AQW_3US 

Input acquisition window = 3us

HAL_ADC_AQW_12US 

Input acquisition window = 12us

HAL_ADC_AQW_36US 

Input acquisition window = 36us

Definition at line 167 of file hal_adc.h.

An enum describing the ADC's resolution.

Enumerator:
HAL_ADC_RES_6BIT 

ADC resolution set to 6 bit

HAL_ADC_RES_8BIT 

ADC resolution set to 8 bit

HAL_ADC_RES_10BIT 

ADC resolution set to 10 bit

HAL_ADC_RES_12BIT 

ADC resolution set to 12 bit

Definition at line 177 of file hal_adc.h.

An enum describing the position of output data.

Enumerator:
HAL_ADC_JUST_LEFT 

Left justified data

HAL_ADC_JUST_RIGHT 

Right justified data

Definition at line 187 of file hal_adc.h.

An enum describing the ADC's resolution.

Enumerator:
HAL_ADC_FLOW_NONE 

No overflow or underflow

HAL_ADC_FLOW_OVER 

Overflow

HAL_ADC_FLOW_UNDER 

Underflow

HAL_ADC_FLOW_BOTH 

Both overflow and underflow

Definition at line 195 of file hal_adc.h.


Function Documentation

void hal_adc_set_input_channel ( hal_adc_input_channel_t  chsel )

Function to set the input channel for the ADC. This function sets the input channel for the ADC.

Parameters:
chselInput channel

Definition at line 26 of file hal_adc.c.

{ // Update "chsel" bits
  ADCCON1 = ((ADCCON1 & 0xC3) | (((uint8_t)(chsel) << 2) & ~0xC3));
}
void hal_adc_set_reference ( hal_adc_reference_t  refsel )

Function to set the reference for ADC This function sets the reference for conversion.

Parameters:
refselReference

Definition at line 32 of file hal_adc.c.

{ // Update "refsel" bits
  ADCCON1 = ((ADCCON1 & 0xFC) | (((uint8_t)(refsel)) & ~0xFC));
}
void hal_adc_set_input_mode ( hal_adc_input_mode_t  input_mode )

Function to set the ADC single ended or differential mode. This function sets the single ended or differential mode of the ADC.

Parameters:
input_modeSingle ended or differential mode

Definition at line 38 of file hal_adc.c.

{ // Update "diffm" bits
  ADCCON2 = ((ADCCON2 & 0x3F) | ((uint8_t)(input_mode) << 6));
}
void hal_adc_set_conversion_mode ( hal_adc_conversion_mode_t  conv_mode )

Function to set the ADC conversion mode. This function sets the conversion mode of the ADC conversion.

Parameters:
conv_modeThe conversion mode

Definition at line 44 of file hal_adc.c.

{ // Update "cont" bit
  ADCCON2 = ((ADCCON2 & 0xDF) | (((uint8_t)(conv_mode) << 5) & ~0xDF));
}
void hal_adc_set_sampling_rate ( hal_adc_sampling_rate_t  rate )

Function to set the ADC sampling rate. This function sets the sampling rate for the ADC.

Parameters:
rateThe sampling rate

Definition at line 50 of file hal_adc.c.

{ // Update "rate" bits
  ADCCON2 = ((ADCCON2 & 0xE3) | (((uint8_t)(rate) << 2) & ~0xE3));
}
void hal_adc_set_power_down_delay ( hal_adc_power_down_delay_t  pdd )

Function to set the ADC power down delay. This function sets the power down delay of the ADC.

Parameters:
pddThe power down delay

Definition at line 56 of file hal_adc.c.

{ // Update "rate" bits
  ADCCON2 = ((ADCCON2 & 0xE3) | (((uint8_t)(pdd) << 2) & ~0xE3));
}
void hal_adc_set_acq_window ( hal_adc_acq_window_t  tacq )

Function to set the duration of the acquisition window for the ADC. This function sets the duration of the acquisition window.

Parameters:
tacqDuration of the acquisition window

Definition at line 62 of file hal_adc.c.

{ // Update "tacq" bits
  ADCCON2 = ((ADCCON2 & 0xFC) | (((uint8_t)(tacq)) & ~0xFC));
}
void hal_adc_set_resolution ( hal_adc_resolution_t  res )

Function to set the ADC resolution. This function sets the resolution of the ADC conversion.

Parameters:
resThe resolution of the ADC

Definition at line 68 of file hal_adc.c.

{ // Update "resol" bits
  ADCCON3 = ((ADCCON3 & 0x3F) | ((uint8_t)(res) << 6));
}
void hal_adc_set_data_just ( hal_adc_data_just_t  just )

Function to set the ADC data output justification. This function sets the ADC output to be left or right justified.

Parameters:
justThe resolution of the ADC

Definition at line 74 of file hal_adc.c.

{ // Update "rljust" bit
  ADCCON3 = ((ADCCON3 & 0xDF) | (((uint8_t)(just) << 5) & ~0xDF));
}
void hal_adc_start ( void   )

Function to start the ADC conversion. This function starts the conversion.

< The value of bit 7

Definition at line 80 of file hal_adc.c.

{
  uint8_t cnt = ADC_STARTUP_CNT;             // Get the counter value
  ADCCON1 = ADCCON1 | BIT_7;                 // Set "pwrup" bit

  while(cnt--){}                         // Wait for busy bit to stabilize
}
uint8_t hal_adc_read_LSB ( void   )

Function that returns the LSB of the data from the ADC. This function returns the LSB of the current data stored in the ADC.

Returns:
LSB of data from the ADC

Definition at line 89 of file hal_adc.c.

{
  return ADCDATL;                            // Return value stored in ADCDATL
}
uint8_t hal_adc_read_MSB ( void   )

Function that returns the MSB of the data from the ADC. This function returns the MSB of the current data stored in the ADC.

Returns:
MSB of data from the ADC

Definition at line 94 of file hal_adc.c.

{
  return ADCDATH;                            // Return value stored in ADCDATH
}
_Bool hal_adc_busy ( void   )

Function that returns the status of the ADC. This function returns true if the ADC is busy.

Returns:
ADC busy bit
Return values:
FALSEno conversion in progress
TRUEconversion in progress

< The value of bit 6

Definition at line 99 of file hal_adc.c.

{
  return ((ADCCON1 & BIT_6));            // Return status of "busy" bit
}
hal_adc_overflow_t hal_adc_get_overflow_status ( void   )

Function that returns the flow status of the ADC. Use this function to check for overflow and/or underflow.

Returns:
Overflow status

< The value of bit 3

< The value of bit 4

Definition at line 104 of file hal_adc.c.

{
  return (hal_adc_overflow_t)((ADCCON3 & (BIT_3 | BIT_4)) >> 3);
}