实例的目的是演示微控制器里边的自动比较功能
。要使用此功能,通道5连接到电位器应启用。用户可以改变
电压与阈值比较,终端将输出电流电压和配置的窗口值。
以下是程序:
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "asf.h"
/** Reference voltage for AFEC in mv. */
#define VOLT_REF (3300)
/** The maximal digital value */
#define MAX_DIGITAL (4095UL)
#define STRING_EOL "\r"
#define STRING_HEADER "-- AFEC Automatic Comparison Example --\r\n" \
"-- "BOARD_NAME" --\r\n" \
"-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL
/** Low threshold */
static uint16_t gs_us_low_threshold = 0;
/** High threshold */
static uint16_t gs_us_high_threshold = 0;
/**
* \brief Configure UART console.
*/
static void configure_console(void)
{
const usart_serial_options_t uart_serial_options = {
.baudrate = CONF_UART_BAUDRATE,
#ifdef CONF_UART_CHAR_LENGTH
.charlength = CONF_UART_CHAR_LENGTH,
#endif
.paritytype = CONF_UART_PARITY,
#ifdef CONF_UART_STOP_BITS
.stopbits = CONF_UART_STOP_BITS,
#endif
};
/* Configure console UART. */
sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
stdio_serial_init(CONF_UART, &uart_serial_options);
}
/**
* \brief Callback function for AFEC enter compasion window interrupt.
*/
static void afec_print_comp_result(void)
{
uint16_t us_adc;
/* Disable Compare Interrupt. */
afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_COMP_ERROR);
us_adc = afec_channel_get_value(AFEC0, AFEC_CHANNEL_POTENTIOMETER);
printf("-ISR-:Potentiometer voltage %d mv is in the comparison "
"window:%d -%d mv!\n\r",
(int)(us_adc * VOLT_REF / MAX_DIGITAL),
(int)(gs_us_low_threshold * VOLT_REF / MAX_DIGITAL),
(int)(gs_us_high_threshold * VOLT_REF / MAX_DIGITAL));
}
/**
* \brief Application entry point.
*
* \return Unused (ANSI-C compatibility).
*/
int main(void)
{
/* Initialize the SAM system. */
sysclk_init();
board_init();
configure_console();
/* Output example information. */
puts(STRING_HEADER);
gs_us_low_threshold = 0x0;
gs_us_high_threshold = 0x7FF;
afec_enable(AFEC0);
struct afec_config afec_cfg;
afec_get_config_defaults(&afec_cfg);
afec_init(AFEC0, &afec_cfg);
struct afec_ch_config afec_ch_cfg;
afec_ch_get_config_defaults(&afec_ch_cfg);
#if (SAMV71 || SAMV70 || SAME70 || SAMS70)
/*
* Because the internal AFEC offset is 0x200, it should cancel it and shift
* down to 0.
*/
afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x200);
afec_ch_cfg.gain = AFEC_GAINVALUE_0;
#else
/*
* Because the internal AFEC offset is 0x800, it should cancel it and shift
* down to 0.
*/
afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x800);
#endif
afec_ch_set_config(AFEC0, AFEC_CHANNEL_POTENTIOMETER, &afec_ch_cfg);
afec_set_trigger(AFEC0, AFEC_TRIG_SW);
afec_set_comparison_mode(AFEC0, AFEC_CMP_MODE_2, AFEC_CHANNEL_POTENTIOMETER, 0);
afec_set_comparison_window(AFEC0, gs_us_low_threshold, gs_us_high_threshold);
/* Enable channel for potentiometer. */
afec_channel_enable(AFEC0, AFEC_CHANNEL_POTENTIOMETER);
afec_set_callback(AFEC0, AFEC_INTERRUPT_COMP_ERROR, afec_print_comp_result, 1);
while (1) {
afec_start_software_conversion(AFEC0);
delay_ms(1000);
}
}
可见该程序是经过配置后,就AFEC的通道及模式,最后还设了回调函数
最后运行结果如下:
|