Blame view

RIOT/tests/periph_adc/main.c 1.56 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  /*
   * Copyright (C) 2014-2015 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     tests
   * @{
   *
   * @file
   * @brief       Test application for peripheral ADC drivers
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  
  #include "xtimer.h"
  #include "timex.h"
  #include "periph/adc.h"
  
  
  #define RES             ADC_RES_10BIT
  #define DELAY           (100LU * US_PER_MS) /* 100 ms */
  
  
  int main(void)
  {
      xtimer_ticks32_t last = xtimer_now();
      int sample = 0;
  
      puts("\nRIOT ADC peripheral driver test\n");
      puts("This test will sample all available ADC lines once every 100ms with\n"
           "a 10-bit resolution and print the sampled results to STDIO\n\n");
  
      /* initialize all available ADC lines */
      for (int i = 0; i < ADC_NUMOF; i++) {
          if (adc_init(ADC_LINE(i)) < 0) {
              printf("Initialization of ADC_LINE(%i) failed\n", i);
              return 1;
          } else {
              printf("Successfully initialized ADC_LINE(%i)\n", i);
          }
      }
  
      while (1) {
          for (int i = 0; i < ADC_NUMOF; i++) {
              sample = adc_sample(ADC_LINE(i), RES);
              if (sample < 0) {
                  printf("ADC_LINE(%i): 10-bit resolution not applicable\n", i);
              } else {
                  printf("ADC_LINE(%i): %i\n", i, sample);
              }
          }
          xtimer_periodic_wakeup(&last, DELAY);
      }
  
      return 0;
  }