3.2 ADC Series Mode With Event Trigger
Todo:
- Edit the adc_init(), so the ADC uses Series Conversion mode and works while the device is in Standby sleep mode
- Change the RTC period by editing rtc_init()
- Edit the program so the ATtiny1627 can go to Standby sleep mode
- Configure the ADC for Series Conversion mode to be used in Standby sleep mode.
- Change the Conversion
mode by changing the Command register:
ADC0.COMMAND = ADC_MODE_SERIES_gc | ADC_START_EVENT_TRIGGER_gc;
- Make the ADC trigger an
ADC result ready interrupt to wake up the CPU from sleep every time an
ADC result is ready by adding the following line to the adc_init():
/*Activate interrupt on Result ready to wake up from sleep*/ ADC0.INTCTRL = ADC_RESRDY_bm;
- Change the following code
to adc_init() to enable the ADC to run in Standby sleep mode:
ADC0.CTRLA = ADC_ENABLE_bm | ADC_RUNSTDBY_bm; /*Enable the ADC and run in standby sleep mode*/
- Change the adc_result_is_ready() to the following, so it
works with an interrupt based
ADC:
bool adc_result_is_ready() { if(adc_result_is_ready_flag){ adc_result_is_ready_flag = 0; return true; } return false; }
- Change the Conversion
mode by changing the Command register:
- Change the rtc_init() in rtc.c, so the RTC period is dependent on the
ADC sampling frequency and not the ADC result frequency by
writing:
RTC.PER = (RTC_CLOCK/(float)(ADC_SAMPLING_FREQ))+0.5;
Info: When the ADC is in Series mode, each event triggers one single sample as opposed to the Burst mode, where one event triggers all the samples needed for one result, which means that the sampling frequency must be a lot higher than the result frequency.Tip: To control the result frequency instead of the sampling frequency, define the sampling frequency using the result frequency and the number of samples by writing:#define ADC_SAMPL_FREQ (uint16_t)(ADC_RESULT_FREQ << ADC_SAMPLES)
- Configure ATtiny1627
so it can go to standby sleep and still retain its previous functionality.
- Study the slpctrl_init() in sleep_control.c to understand how the sleep controller is configured.
- Add slpctrl_init() to the initialization sequence in the main function.
- Add sei() to the end of the initialization sequence in the main function to enable interrupts so the device can wake up from Standby sleep mode.
- Add sleep_cpu() to the while function so the device will go
to sleep between each result:
while (1) { sleep_cpu(); if(adc_result_is_ready()) { adc_t.adc_sample = adc_get_sample(); adc_t.adc_result = adc_get_result(); adc_t.adc_average_result = adc_t.adc_result>>ADC_SAMPLES; trasmit_to_DV(); PORTB.OUTTGL = LED0_bm; } }
- Add the following code to
the end of the transmit_to_DV() in
data_streamer.c to make sure the device does not go to sleep
before the transmission is
complete:
while (!(USART0.STATUS & USART_TXCIF_bm)); /* Wait for all USART transmission to be finished */ USART0.STATUS |= USART_TXCIF_bm; /* Clear Tx complete flag */
Info: When the device does not go to sleep, it uses 24 mW, while when it goes to Standby sleep mode, it uses 12 mW, so it can be seen that going to sleep between results can reduce the power by up to 50%. The measurements were taken with a result frequency of 20 Hz, 512 accumulated samples, without the blinking LED. - 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.
- Flash the device by selecting Debug → Start without debugging for the top menu bar in Microchip Studio or pressing the Ctrl+Alt+F5 keys.
- Plot the single ADC sample vs. the ADC average
result using the MPLAB Data Visualizer. Try different sampling
frequencies and change the force applied to the force sensor periodically.Info: Due to the configured clock frequency of the RTC, the highest sampling frequency is 32768 Hz. The result frequency is dependent on the sampling frequency and the number of samples, meaning that the maximum result frequency for 1024 samples is 32 Hz.
Result: Measuring the
analog signal from the Force click using the ADC in Series mode and using the RTC
and event system to continuously trigger the ADC sample. The device is in Standby
sleep mode between each result. When the sampling frequency is low, the result should
look similar to Figure 3-3, and
when it is high, it should look similar to Figure 3-4. See that, if the sampling frequency is
too low, the result becomes the average of the rapidly changing signal, and a lot of
information is lost.