Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 9 months ago.
SPI Nucleo and raspberry pi
Hi I am trying to use spi for Nucleo f401re I am using this code :
- include "mbed.h" SPISlave device(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); int main() {
device.format(8, 3); device.frequency(1000000);
device.reply(0x00);
int reply = 1;
while (1) { if (device.receive()) { int valueFromMaster = device.read(); device.reply(reply); reply++; } } }
I connect it to raspberry pi as master with this code :
import time import sys import spidev
spi = spidev.SpiDev() spi.open(0,0)
def buildReadCommand(channel): startBit = 0x01 singleEnded = 0x08
- Return python list of 3 bytes
- Build a python list using [1, 2, 3]
- First byte is the start bit
- Second byte contains single ended along with channel #
- 3rd byte is 0 return []
def processAdcValue(result): '''Take in result as array of three bytes. Return the two lowest bits of the 2nd byte and all of the third byte''' pass
def readAdc(channel): if ((channel > 7) or (channel < 0)): return -1 r = spi.xfer2(buildReadCommand(channel)) return processAdcValue(r)
if name == 'main': try: while True: val = readAdc(0) print "ADC Result: ", str(val) time.sleep(5) except KeyboardInterrupt: spi.close() sys.exit(0)
I supposed to get some value as reply in raspberry. I am confused what is wrong because I got nothing. I am sure my wiring is right. Thank you
Please repost using
posted by Andy A 05 Feb 2015<<code>>
and <</code> so that your code is readable.