Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / examples / Gertboard / voltmeter.c @ 32:cadb9025f1e0

History | View | Annotate | Download (1.86 KB)

1
/*
2
 * voltmeter.c:
3
 *        Demonstrate use of the Gertboard A to D converter to make
4
 *        a simple voltmeter.
5
 *
6
 * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
7
 ***********************************************************************
8
 * This file is part of wiringPi:
9
 *        https://projects.drogon.net/raspberry-pi/wiringpi/
10
 *
11
 *    wiringPi is free software: you can redistribute it and/or modify
12
 *    it under the terms of the GNU Lesser General Public License as published by
13
 *    the Free Software Foundation, either version 3 of the License, or
14
 *    (at your option) any later version.
15
 *
16
 *    wiringPi is distributed in the hope that it will be useful,
17
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *    GNU Lesser General Public License for more details.
20
 *
21
 *    You should have received a copy of the GNU Lesser General Public License
22
 *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
23
 ***********************************************************************
24
 */
25

    
26
#include <stdio.h>
27

    
28
#include <wiringPi.h>
29
#include <gertboard.h>
30

    
31
int main ()
32
{
33
  int x1, x2 ;
34
  double v1, v2 ;
35

    
36
  printf ("\n") ;
37
  printf ("Gertboard demo: Simple Voltmeters\n") ;
38
  printf ("=================================\n") ;
39

    
40
// Always initialise wiringPi. Use wiringPiSys() if you don't need
41
//        (or want) to run as root
42

    
43
  wiringPiSetupSys () ;
44

    
45
// Initialise the Gertboard analog hardware at pin 100
46

    
47
  gertboardAnalogSetup (100) ;
48

    
49
  printf ("\n") ;
50
  printf ("| Channel 0 | Channel 1 |\n") ;
51

    
52
  for (;;)
53
  {
54

    
55
// Read the 2 channels:
56

    
57
    x1 = analogRead (100) ;
58
    x2 = analogRead (101) ;
59

    
60
// Convert to a voltage:
61

    
62
    v1 = (double)x1 / 1023.0 * 3.3 ;
63
    v2 = (double)x2 / 1023.0 * 3.3 ;
64

    
65
// Print
66

    
67
    printf ("|    %6.3f |    %6.3f |\r", v1, v2) ;
68
    fflush (stdout) ;
69
  }
70

    
71
  return 0 ;
72
}
73