-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadc.py
More file actions
64 lines (48 loc) · 1.79 KB
/
adc.py
File metadata and controls
64 lines (48 loc) · 1.79 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
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
#import spidev library
import spidev
import time
#Create spi objects
spi0 = spidev.SpiDev()
spi1 = spidev.SpiDev()
def spi_setup(spiObject, port, device, max_speed):
#Enables SPI ports for Raspberry Pi to SPI device communication
spiObject.open(port, device)
#Sets the max speed for spi communication
spiObject.max_speed_hz = max_speed
spi_setup(spi0, 1, 0, 488000)
spi_setup(spi1, 1, 1, 488000)
#Sets up the first 8 bits that will be sent to the ADC
#Five leading zeros Start Bit SGL/DIFF Bit D2
# 00000 1 1 0
primary_reg = 0x07 & 0x06
#Sets up the second set of 8 bits that will be sent to the ADC
# D1 D0 Don't Care Bits
#(2 bits to determine the XXXXXX
#channel of the ADC)
secondary_reg0 = 0 << 6
secondary_reg1 = 1 << 6
#Sets both of the chip selects to high
spi0.cshigh = True
spi1.cshigh = True
try:
while(1):
#Sets chip select 0 low to enable communication to channel 0
spi0.cshigh = False
#Performs SPI transaction for channel 0
adc0 = spi0.xfer2([primary_reg, secondary_reg0, 0x00])
#Sets chip select 0 to high and chip select 1 to low to enable communnication to channel 1
spi0.cshigh = True
spi1.cshigh = False
#Performs SPI transaction for channel 1
adc1 = spi1.xfer2([primary_reg, secondary_reg1, 0x00])
#Sets chip select 1 to high
spi1.cshigh = True
#Extracts the 12-bit values received from SPI transactions for channels 0 and 1
data0 = ((adc0[1]&0xF) << 8) | adc0[2]
data1 = ((adc1[1]&0xF) << 8) | adc1[2]
print("channel 0: ", data0, " channel 1: ", data1)
time.sleep(0.5)
except KeyboardInterrupt:
spi0.close()
spi1.close()