2.3 ADC Single Mode
The ADC can be configured in different operation modes. These modes can be split into three groups:
- Single mode - Single conversion per trigger, with 8- or 12-bit conversion output
- Series Accumulation mode - One conversion per trigger, with an accumulation of n samples
- Burst Accumulation mode - A burst with n samples accumulated as fast as possible after a single trigger
Series and Burst modes utilize 12-bit conversions and can be configured with or without scaling the accumulated result.
In this assignment, the focus is on Single mode and Burst Accumulation mode. Configuration of both setups will be shown starting with Single mode.
Todo: Edit adc_init() to configure the ADC as listed below:
- ADC reference voltage: VDD
- ADC Clock: CLK_PER/4
- Input Channel: AIN5 (Pin PA5)
- ADC sampling in Single mode
- Resolution: 12 bits
- Open adc.c by double clicking
adc.c in the Solution Explorer, and locate adc_init(). Figure 2-2 illustrates the expected result.
- Enable the ADC by writing a
‘
1
’ to the ENABLE bit in the Control A (ADCn.CTRLA) register:ADC0.CTRLA = ADC_ENABLE_bm;
- Configure the ADC prescaler to produce the
desired CLK_PER/4 ADC clock by writing the Control B (ADCn.CTRLB) register:
ADC0.CTRLB = ADC_PRESC_DIV4_gc;
Tip: Refer to the ATtiny1627 device data sheet and family manual available from www.microchip.com/wwwproducts/en/ATTINY1627 for detailed register descriptions. - Select the reference voltage and configure
the time base by writing to the bit fields in the Control C (ADCn.CTRLC)
register:
ADC0.CTRLC = ADC_REFSEL_VDD_gc | TIMEBASE_VALUE << ADC_TIMEBASE_gp;
Info: Time base is used for timing internal delays in the ADC before starting a conversion, such as the guard time between changing input reference or PGA gain settings. Use the definition in adc.h to get a period equal to or greater than 1 μs:#define TIMEBASE_VALUE (uint8_t)ceil(F_CPU*0.000001)
ceil is used to round up to an integer to write it to the Control C (ADCn.CTRLC) register.Info: A single-ended ADC will measure the voltage difference between one pin and ground. The voltage reference defines the conversion range of the ADC. Ground is used as zero, while the voltage reference is the highest value within the range. The ADC voltage reference can be selected among different levels between ground and the microcontroller supply voltage to utilize as much of the ADC resolution as possible for inputs in a given voltage range.Tip: For detailed info on Differential and Single-Ended ADC, refer to the white paper Differential and Single-Ended ADC available from www.microchip.com/DS00003197. - Configure the Sample Duration bit field in the
Control E (ADCn.CTRLE) register:
ADC0.CTRLE = 3;
Info: Use Sample Duration to configure the sampling period of the ADC input. The sample duration is given in ADC clock cycles. - Enable Free-Running mode in the Control F
(ADCn.CTRLF)
register:
ADC0.CTRLF = ADC_FREERUN_bm;
Info: Free-running starts a new conversion when the previous is finished. Free-running requires the first conversion to be initiated before taking effect. - Configure the ADC to use the AIN5 input
channel by writing the MUXPOS (ADCn.MUXPOS) register:
ADC0.MUXPOS = ADC_MUXPOS_AIN5_gc;
Info: The device data sheet, section I/O Multiplexing and Considerations, indicates which device pins the various analog input channels are connected to. AIN5 is available at pin PA5. - Configure the mode of operation for the ADC
by writing to the MODE bit field in the COMMAND (ADCn.COMMAND) register:
ADC0.COMMAND = ADC_MODE_SINGLE_12BIT_gc;
Info: When the ADC is in Single mode, there is only one conversion per trigger. - Start conversion by writing to the START
bit field in the COMMAND (ADCn.COMMAND) register. Place this in main.c
before the while loop:
ADC0.COMMAND |= ADC_START_IMMEDIATE_gc;
- Verify that the solution builds with no errors by selecting Build → Build Solution from the top menu bar in Microchip Studio or pressing the F7 key.
Result: Code for
initializing the ADC is complete, and the expected result is listed below:
void adc_init() { ADC0.CTRLA = ADC_ENABLE_bm; ADC0.CTRLB = ADC_PRESC_DIV4_gc; ADC0.CTRLC = ADC_REFSEL_VDD_gc | TIMEBASE_VALUE << ADC_TIMEBASE_gp; ADC0.CTRLE = 3; ADC0.CTRLF = ADC_FREERUN_bm; ADC0.MUXPOS = ADC_MUXPOS_AIN5_gc; ADC0.COMMAND = ADC_MODE_SINGLE_12BIT_gc; }