In this use case, the Analog Comparator module is configured for:
- Comparator peripheral in manually triggered (e.g. "Single Shot" mode)
- One comparator channel connected to input MUX pin 0 and compared to a scaled VCC/2 voltage
This use case sets up the Analog Comparator to compare an input voltage fed into a GPIO pin of the device against a scaled voltage of the microcontroller's VCC power rail. The comparisons are made on-demand in single-shot mode, and the result stored into a local variable which is then output to the board LED to visually show the comparison state.
Setup
Prerequisites
There are no special setup requirements for this use-case.
Code
Copy-paste the following setup code to your user application:
#define AC_COMPARATOR_CHANNEL AC_CHAN_CHANNEL_0
void configure_ac(void)
{
ac_init(&ac_instance, AC, &config_ac);
}
void configure_ac_channel(void)
{
#if (SAMR30E)
#else
#endif
ac_chan_conf.vcc_scale_factor = 32;
ac0_pin_conf.mux_position = CONF_AC_MUX;
}
Add to user application initialization (typically the start of main()
):
configure_ac();
configure_ac_channel();
Workflow
- Create an AC device instance struct, which will be associated with an Analog Comparator peripheral hardware instance.
- Note
- Device instance structures shall never go out of scope when in use.
- Define a macro to select the comparator channel that will be sampled, for convenience.
#define AC_COMPARATOR_CHANNEL AC_CHAN_CHANNEL_0
- Create a new function
configure_ac()
, which will be used to configure the overall Analog Comparator peripheral.
- Create an Analog Comparator peripheral configuration structure that will be filled out to set the module configuration.
- Fill the Analog Comparator peripheral configuration structure with the default module configuration values.
- Initialize the Analog Comparator peripheral and associate it with the software instance structure that was defined previously.
ac_init(&ac_instance, AC, &config_ac);
- Create a new function
configure_ac_channel()
, which will be used to configure the overall Analog Comparator peripheral. void configure_ac_channel(void)
- Create an Analog Comparator channel configuration structure that will be filled out to set the channel configuration.
- Fill the Analog Comparator channel configuration structure with the default channel configuration values.
- Alter the channel configuration parameters to set the channel to one-shot mode, with the correct negative and positive MUX selections and the desired voltage scaler.
#if (SAMR30E)
#else
#endif
ac_chan_conf.vcc_scale_factor = 32;
- Note
- The voltage scalar formula is documented in description for ac_chan_config::vcc_scale_factor.
- Configure the physical pin that will be routed to the AC module channel 0.
ac0_pin_conf.mux_position = CONF_AC_MUX;
- Initialize the Analog Comparator channel and configure it with the desired settings.
- Enable the now initialized Analog Comparator channel.
- Enable the now initialized Analog Comparator peripheral.
Implementation
Code
Copy-paste the following code to your user application:
while (true) {
do {
AC_COMPARATOR_CHANNEL);
}
}
Workflow
- Trigger the first comparison on the comparator channel.
- Create a local variable to maintain the current comparator state. Since no comparison has taken place, it is initialized to AC_CHAN_STATUS_UNKNOWN.
- Make the application loop infinitely, while performing triggered comparisons.
- Check if the comparator is ready for the last triggered comparison result to be read.
- Read the comparator output state into the local variable for application use, re-trying until the comparison state is ready.
do {
AC_COMPARATOR_CHANNEL);
- Set the board LED state to mirror the last comparison state.
- Trigger the next conversion on the Analog Comparator channel.