Created
December 11, 2013 22:42
-
-
Save omarowns/7919855 to your computer and use it in GitHub Desktop.
SPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import spidev | |
| import time | |
| def write(value, cs): | |
| """" | |
| #Set chipSelect HIGH | |
| GPIO.output(cs, True) | |
| #Set clock LOW | |
| GPIO.output(clock, False) | |
| #Set chipSelect LOW | |
| GPIO.output(cs, False) | |
| for i in range(12): | |
| if (commandout & 0x08000): | |
| GPIO.output(mosi, True) | |
| else: | |
| GPIO.output(mosi, False) | |
| commandout <<= 1 | |
| GPIO.output(clockpin, True) | |
| GPIO.output(clockpin, True) | |
| """ | |
| commandout = 0b10011 | |
| commandout <<= 10 | |
| commandout ^= value | |
| commandout <<= 2 | |
| #Create spi object | |
| spi = spidev.SpiDev() | |
| #open port | |
| spi.open(0,cs) | |
| #send command | |
| spi.xfer2([commandout]) | |
| for i in range(12): | |
| spi.xfer2([(commandout & 0x08000)]) | |
| commandout <<= 1 | |
| def read(adcnum, cs): | |
| if((adcnum > 7) or (adcnum < 0)): | |
| return -1 | |
| #create spi | |
| spi = spidev.SpiDev() | |
| #open port | |
| spi.open(0,cs) | |
| #send command transfer receive result | |
| r = spi.xfer2([1,(8+adcnum)<<4,0]) | |
| #close connection | |
| spi.close() | |
| #parse result | |
| adcout = ((r[1]&3) << 8) + r[2] | |
| return adcout | |
| try: | |
| #Main logic | |
| while True: | |
| for i in range(254): | |
| write(i,0) | |
| time.sleep(0.1) | |
| for i in range(254): | |
| write(254-i,0) | |
| time.sleep(0.1) | |
| except KeyboardInterrupt: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment