Skip to content

Instantly share code, notes, and snippets.

@Tiryoh
Last active July 9, 2020 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tiryoh/501f03c230ad915ab8b2a7aa2da962ea to your computer and use it in GitHub Desktop.
Save Tiryoh/501f03c230ad915ab8b2a7aa2da962ea to your computer and use it in GitHub Desktop.
A Python script to read MCP3208 via spi0.0 on Jetson Nano
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# raspimouse_read_mcp3208_via_spidev.py
# (C) 2020 Daisuke Sato@RT CORPORATION
# Released under the MIT License
# special thanks: https://www.denshi.club/pc/raspi/5raspberry-pi-zeroiot8a-d5mcp3208.html
import time
import spidev
import Jetson.GPIO as GPIO
def main():
Vref = 3.3
spi = spidev.SpiDev()
spi.open(0,0) #port 0,cs 0
spi.max_speed_hz = 1000000 # 1MHz
channels = [15, 29, 13, 11] #R, L, RF, LF
GPIO.setmode(GPIO.BOARD)
GPIO.setup(channels, GPIO.OUT)
GPIO.output(channels, GPIO.LOW)
# channel 0, l
GPIO.output(29, GPIO.HIGH)
time.sleep(0.05)
adc0 = spi.xfer2([0x06,0x00,0x00])
GPIO.output(29, GPIO.LOW)
# channel 1, lf
GPIO.output(11, GPIO.HIGH)
time.sleep(0.05)
adc1 = spi.xfer2([0x06,0x40,0x00])
GPIO.output(11, GPIO.LOW)
# channel 2, rf
GPIO.output(13, GPIO.HIGH)
time.sleep(0.05)
adc2 = spi.xfer2([0x06,0x80,0x00])
GPIO.output(13, GPIO.LOW)
# channel 3, r
GPIO.output(15, GPIO.HIGH)
time.sleep(0.05)
adc3 = spi.xfer2([0x06,0xc0,0x00])
GPIO.output(15, GPIO.LOW)
data = ((adc0[1] & 0x0f) << 8) | adc0[2]
print ("ch0(L ) " + str(Vref*data/4096) + "V")
data = ((adc1[1] & 0x0f) << 8) | adc1[2]
print ("ch1(LF) " + str(Vref*data/4096) + "V")
data = ((adc2[1] & 0x0f) << 8) | adc2[2]
print ("ch2(RF) " + str(Vref*data/4096) + "V")
data = ((adc3[1] & 0x0f) << 8) | adc3[2]
print ("ch3(R ) " + str(Vref*data/4096) + "V")
GPIO.cleanup(channels)
spi.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment