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
{
}
{
#if (SAMR30E)
#else
#endif
config_ac_chan.vcc_scale_factor = 32;
ac0_pin_conf.mux_position = CONF_AC_MUX;
}
{
}
{
}
Add to user application initialization (typically the start of main()
):
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.
- Create a new function
configure_ac_channel()
, which will be used to configure the overall Analog Comparator peripheral.
- 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.
- Note
- The voltage scalar formula is documented in description for ac_chan_config::vcc_scale_factor.
- Select when the interrupt should occur. In this case an interrupt will occur at every finished conversion.
#if (SAMR30E)
#else
#endif
config_ac_chan.vcc_scale_factor = 32;
- 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 initialized Analog Comparator channel.
- Create a new callback function.
- Create a callback status software flag.
- Let the callback function set the calback_status flag to true.
- Create a new function
configure_ac_callback()
, which will be used to configure the callbacks.
- Register callback function.
- Enable the callbacks.
- Enable the now initialized Analog Comparator peripheral.
- Note
- This should not be done until after the AC is setup and ready to be used.
Implementation
Code
Copy-paste the following code to your user application:
while (true) {
if (callback_status == true) {
do
{
} while (last_comparison & AC_CHAN_STATUS_UNKNOWN);
callback_status = false;
}
}
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 a new comparison is complete.
if (callback_status == true) {
- Check if the comparator is ready for the last triggered comparison result to be read.
do
{
} while (last_comparison & AC_CHAN_STATUS_UNKNOWN);
- Read the comparator output state into the local variable for application use, re-trying until the comparison state is ready.
do
{
} while (last_comparison & AC_CHAN_STATUS_UNKNOWN);
- Set the board LED state to mirror the last comparison state.
- After the interrupt is handled, set the software callback flag to false.
- Trigger the next conversion on the Analog Comparator channel.