[Atmel] 用SAM-BA或JLINK跑ATSAM4E16的程序(24)AFEC自动比较

[复制链接]
 楼主| ddllxxrr 发表于 2015-12-9 21:42 | 显示全部楼层 |阅读模式
实例的目的是演示微控制器里边的自动比较功能
。要使用此功能,通道5连接到电位器应启用。用户可以改变
电压与阈值比较,终端将输出电流电压和配置的窗口值。

以下是程序:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include "asf.h"

  5. /** Reference voltage for AFEC in mv. */
  6. #define VOLT_REF        (3300)

  7. /** The maximal digital value */
  8. #define MAX_DIGITAL     (4095UL)

  9. #define STRING_EOL    "\r"
  10. #define STRING_HEADER "-- AFEC Automatic Comparison Example --\r\n" \
  11.                 "-- "BOARD_NAME" --\r\n" \
  12.                 "-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL

  13. /** Low threshold */
  14. static uint16_t gs_us_low_threshold = 0;
  15. /** High threshold */
  16. static uint16_t gs_us_high_threshold = 0;

  17. /**
  18. * \brief Configure UART console.
  19. */
  20. static void configure_console(void)
  21. {
  22.         const usart_serial_options_t uart_serial_options = {
  23.                 .baudrate = CONF_UART_BAUDRATE,
  24.         #ifdef CONF_UART_CHAR_LENGTH
  25.                 .charlength = CONF_UART_CHAR_LENGTH,
  26.         #endif
  27.                 .paritytype = CONF_UART_PARITY,
  28.         #ifdef CONF_UART_STOP_BITS
  29.                 .stopbits = CONF_UART_STOP_BITS,
  30.         #endif
  31.         };

  32.         /* Configure console UART. */
  33.         sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
  34.         stdio_serial_init(CONF_UART, &uart_serial_options);
  35. }

  36. /**
  37. * \brief Callback function for AFEC enter compasion window interrupt.
  38. */
  39. static void afec_print_comp_result(void)
  40. {
  41.         uint16_t us_adc;

  42.         /* Disable Compare Interrupt. */
  43.         afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_COMP_ERROR);
  44.        
  45.         us_adc = afec_channel_get_value(AFEC0, AFEC_CHANNEL_POTENTIOMETER);

  46.         printf("-ISR-:Potentiometer voltage %d mv is in the comparison "
  47.                         "window:%d -%d mv!\n\r",
  48.                         (int)(us_adc * VOLT_REF / MAX_DIGITAL),
  49.                         (int)(gs_us_low_threshold * VOLT_REF / MAX_DIGITAL),
  50.                         (int)(gs_us_high_threshold * VOLT_REF / MAX_DIGITAL));
  51. }

  52. /**
  53. * \brief Application entry point.
  54. *
  55. * \return Unused (ANSI-C compatibility).
  56. */
  57. int main(void)
  58. {
  59.         /* Initialize the SAM system. */
  60.         sysclk_init();
  61.         board_init();

  62.         configure_console();

  63.         /* Output example information. */
  64.         puts(STRING_HEADER);

  65.         gs_us_low_threshold = 0x0;
  66.         gs_us_high_threshold = 0x7FF;

  67.         afec_enable(AFEC0);

  68.         struct afec_config afec_cfg;

  69.         afec_get_config_defaults(&afec_cfg);
  70.         afec_init(AFEC0, &afec_cfg);

  71.         struct afec_ch_config afec_ch_cfg;
  72.         afec_ch_get_config_defaults(&afec_ch_cfg);
  73.        
  74. #if (SAMV71 || SAMV70 || SAME70 || SAMS70)
  75.         /*
  76.          * Because the internal AFEC offset is 0x200, it should cancel it and shift
  77.          * down to 0.
  78.          */
  79.         afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x200);

  80.         afec_ch_cfg.gain = AFEC_GAINVALUE_0;

  81. #else
  82.         /*
  83.          * Because the internal AFEC offset is 0x800, it should cancel it and shift
  84.          * down to 0.
  85.          */
  86.         afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x800);
  87. #endif

  88.         afec_ch_set_config(AFEC0, AFEC_CHANNEL_POTENTIOMETER, &afec_ch_cfg);
  89.         afec_set_trigger(AFEC0, AFEC_TRIG_SW);

  90.         afec_set_comparison_mode(AFEC0, AFEC_CMP_MODE_2, AFEC_CHANNEL_POTENTIOMETER, 0);
  91.         afec_set_comparison_window(AFEC0, gs_us_low_threshold, gs_us_high_threshold);

  92.         /* Enable channel for potentiometer. */
  93.         afec_channel_enable(AFEC0, AFEC_CHANNEL_POTENTIOMETER);


  94.         afec_set_callback(AFEC0, AFEC_INTERRUPT_COMP_ERROR, afec_print_comp_result, 1);

  95.         while (1) {
  96.                 afec_start_software_conversion(AFEC0);
  97.                 delay_ms(1000);

  98.         }
  99. }
可见该程序是经过配置后,就AFEC的通道及模式,最后还设了回调函数
最后运行结果如下:



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部