Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.61 KB)

1
/*
2
 * gertboard.c:
3
 *        Simple test for the SPI bus on the Gertboard
4
 *
5
 *        Hardware setup:
6
 *                D/A port 0 jumpered to A/D port 0.
7
 *
8
 *        We output a sine wave on D/A port 0 and sample A/D port 0. We then
9
 *        plot the input value on the terminal as a sort of vertical scrolling
10
 *        oscilloscipe.
11
 *
12
 * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
13
 ***********************************************************************
14
 * This file is part of wiringPi:
15
 *        https://projects.drogon.net/raspberry-pi/wiringpi/
16
 *
17
 *    wiringPi is free software: you can redistribute it and/or modify
18
 *    it under the terms of the GNU Lesser General Public License as published by
19
 *    the Free Software Foundation, either version 3 of the License, or
20
 *    (at your option) any later version.
21
 *
22
 *    wiringPi is distributed in the hope that it will be useful,
23
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 *    GNU Lesser General Public License for more details.
26
 *
27
 *    You should have received a copy of the GNU Lesser General Public License
28
 *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
29
 ***********************************************************************
30
 */
31

    
32
#include <stdio.h>
33
#include <sys/ioctl.h>
34
#include <stdlib.h>
35
#include <math.h>
36

    
37
// Gertboard D to A is an 8-bit unit.
38

    
39
#define        B_SIZE        256
40

    
41
#include <wiringPi.h>
42
#include <gertboard.h>
43

    
44
int main (void)
45
{
46
  double angle ;
47
  int i, inputValue ;
48
  int  buffer [B_SIZE] ;
49
  int   cols ;
50
  struct winsize w ;
51

    
52

    
53
  printf ("Raspberry Pi Gertboard SPI test program\n") ;
54
  printf ("=======================================\n") ;
55

    
56
  ioctl (fileno (stdin), TIOCGWINSZ, &w);
57
  cols = w.ws_col - 2 ;
58

    
59
// Always initialise wiringPi. Use wiringPiSys() if you don't need
60
//        (or want) to run as root
61

    
62
  wiringPiSetupSys () ;
63

    
64
// Initialise the Gertboard analog hardware at pin 100
65

    
66
  gertboardAnalogSetup (100) ;
67

    
68
// Generate a Sine Wave and store in our buffer
69

    
70
  for (i = 0 ; i < B_SIZE ; ++i)
71
  {
72
    angle = ((double)i / (double)B_SIZE) * M_PI * 2.0 ;
73
    buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ;
74
  }
75

    
76
// Loop, output the sine wave on analog out port 0, read it into A-D port 0
77
//        and display it on the screen
78

    
79
  for (;;)
80
  {
81
    for (i = 0 ; i < B_SIZE ; ++i)
82
    {
83
      analogWrite (100, buffer [i]) ;
84

    
85
      inputValue = analogRead (100) ;
86

    
87
// We don't need to wory about the scale or sign - the analog hardware is
88
//        a 10-bit value, so 0-1023. Just scale this to our terminal
89

    
90
      printf ("%*s\n", (inputValue * cols) / 1023, "*") ;
91
      delay (2) ;
92
    }
93
  }
94

    
95
  return 0 ;
96
}