forked from FourierFencing/Team8-FFS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADCSetup.cpp
More file actions
51 lines (36 loc) · 1.03 KB
/
ADCSetup.cpp
File metadata and controls
51 lines (36 loc) · 1.03 KB
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
#include <wiringPi.h>
#include <SPISetup.h>
#include "ADCSetup.h"
/*
* adcRead:
* Return the analog value of the given pin
*********************************************************************************
*/
static int adcRead (struct wiringPiNodeStruct *node, int pin)
{
unsigned char spiData [3] ;
unsigned char chanBits ;
int chan = pin - node->pinBase ;
chanBits = 0b10000000 | (chan << 4) ;
spiData [0] = 1 ; // Start bit
spiData [1] = chanBits ;
spiData [2] = 0 ;
SPIDataRW (node->fd, spiData, 3) ;
return ((spiData [1] << 8) | spiData [2]) & 0x3FF ;
}
/*
* mSetup:
* Create a new wiringPi device node for an mcp3004 on the Pi's
* SPI interface.
*********************************************************************************
*/
int adcSetup (const int pinBase, int spiChannel)
{
struct wiringPiNodeStruct *node ;
if (SPISetup (spiChannel, 1000000) < 0)
return FALSE ;
node = wiringPiNewNode (pinBase, 8) ;
node->fd = spiChannel ;
node->analogRead = adcRead ;
return TRUE ;
}