[Atmel] 用SAM-BA或JLINK跑ATSAM4E16的程序(10)ADC

[复制链接]
 楼主| ddllxxrr 发表于 2015-11-12 21:19 | 显示全部楼层 |阅读模式

本程序,有两种ADC模式可选择,一种 正常模式,一种 增加模式,本例可通过串口显示结果,也可通过串口切换ADC模式。

而ADC是通过不断查询ADC中断来完成的。然后把结果显示到串口。

以下是运行的截图:


以下是运行的程序:

  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_12_BIT     (4095UL)

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

  13. #define MENU_HEADER "\n\r-- press a key to change the resolution mode--\n\r" \
  14.                 "-- n: Normal Resolution Mode--\n\r" \
  15.                 "-- e: Enhanced Resolution Mode--\n\r" \
  16.                 "-- q: Quit Configuration--\n\r"

  17. /** AFEC sample data */
  18. struct {
  19.         uint32_t value;
  20.         bool is_done;
  21. } g_afec_sample_data;

  22. /** The maximal digital value */
  23. static uint32_t g_max_digital;

  24. /**
  25. * \brief Configure UART console.
  26. */
  27. static void configure_console(void)
  28. {
  29.         const usart_serial_options_t uart_serial_options = {
  30.                 .baudrate = CONF_UART_BAUDRATE,
  31.                 .paritytype = CONF_UART_PARITY
  32.         };

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

  37. /**
  38. * \brief Display main menu.
  39. */
  40. static void display_menu(void)
  41. {
  42.         puts(MENU_HEADER);
  43. }

  44. /**
  45. * \brief Set AFEC resolution mode.
  46. */
  47. static void set_afec_resolution(void)
  48. {
  49.         uint8_t uc_key;
  50.         uint8_t uc_done = 0;

  51.         display_menu();

  52.         while (!uc_done) {
  53.                 while (uart_read(CONF_UART, &uc_key));

  54.                 switch (uc_key) {
  55.                 case 'n':
  56.                         g_max_digital = MAX_DIGITAL_12_BIT;
  57.                         afec_set_resolution(AFEC0, AFEC_12_BITS);
  58.                         puts(" Set Resolution to Normal \n\r");
  59.                         break;
  60.                 case 'e':
  61.                         g_max_digital = MAX_DIGITAL_12_BIT * 16;
  62.                         afec_set_resolution(AFEC0, AFEC_16_BITS);
  63.                         puts(" Set Resolution to Enhanced \n\r");
  64.                         break;
  65.                 case 'q':
  66.                         uc_done = 1;
  67.                         puts(" Quit Configuration \n\r");
  68.                         break;
  69.                 default:
  70.                         break;
  71.                 }
  72.         }
  73. }

  74. /**
  75. * \brief AFEC interrupt callback function.
  76. */
  77. static void afec_data_ready(void)
  78. {
  79.         g_afec_sample_data.value = afec_get_latest_value(AFEC0);
  80.         g_afec_sample_data.is_done = true;
  81. }

  82. /**
  83. * \brief Application entry point.
  84. *
  85. * \return Unused (ANSI-C compatibility).
  86. */
  87. int main(void)
  88. {
  89.         uint8_t uc_key;

  90.         /* Initialize the SAM system. */
  91.         sysclk_init();
  92.         board_init();

  93.         configure_console();

  94.         /* Output example information. */
  95.         puts(STRING_HEADER);

  96.         g_afec_sample_data.value = 0;
  97.         g_afec_sample_data.is_done = false;
  98.         g_max_digital = MAX_DIGITAL_12_BIT;

  99.         afec_enable(AFEC0);

  100.         struct afec_config afec_cfg;

  101.         afec_get_config_defaults(&afec_cfg);
  102.         afec_init(AFEC0, &afec_cfg);

  103.         struct afec_ch_config afec_ch_cfg;
  104.         afec_ch_get_config_defaults(&afec_ch_cfg);
  105.         afec_ch_set_config(AFEC0, AFEC_CHANNEL_POTENTIOMETER, &afec_ch_cfg);

  106.         /*
  107.          * Because the internal ADC offset is 0x800, it should cancel it and shift
  108.          * down to 0.
  109.          */
  110.         afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x800);

  111.         afec_set_trigger(AFEC0, AFEC_TRIG_SW);

  112.         /* Enable channel for potentiometer. */
  113.         afec_channel_enable(AFEC0, AFEC_CHANNEL_POTENTIOMETER);

  114.         afec_set_callback(AFEC0, AFEC_INTERRUPT_DATA_READY, afec_data_ready, 1);

  115.         display_menu();

  116.         afec_start_calibration(AFEC0);
  117.         while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL);

  118.         while (1) {
  119.                 afec_start_software_conversion(AFEC0);
  120.                 delay_ms(1000);

  121.                 /* Check if AFEC sample is done. */
  122.                 if (g_afec_sample_data.is_done == true) {
  123.                         printf("Potentiometer Voltage: %4d mv.",
  124.                                         (int)(g_afec_sample_data.value * VOLT_REF
  125.                                         / g_max_digital));
  126.                         puts("\r");
  127.                         g_afec_sample_data.is_done = false;
  128.                 }

  129.                 /* Check if the user enters a key. */
  130.                 if (!uart_read(CONF_UART, &uc_key)) {
  131.                         /* Disable all afec interrupt. */
  132.                         afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_ALL);
  133.                         set_afec_resolution();
  134.                         afec_enable_interrupt(AFEC0, AFEC_INTERRUPT_DATA_READY);
  135.                 }
  136.         }
  137. }



本帖子中包含更多资源

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

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

本版积分规则

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

2403

主题

6994

帖子

68

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

2403

主题

6994

帖子

68

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