Hallo liebe Forumsmitglieder,
ich möchte einen ADC mit integrierter SPI Schnittstelle am RPI anschließen und Daten auslesen. Auf der pi4j Library gibt es ein Beispielprogramm, weiter unten angehängt. Beschaltet habe ich den MCP3008 so:
http://www.raspberrypi-spy.co.uk/2013/10/analog…/mcp3008_tmp36/
Hier
http://wiringpi.com/reference/spi-library/
steht
"Data that was in your buffer is overwritten by data returned from the SPI bus."
Also müsste doch in packet die Daten gespeichert werden. Das Programm zeigt mit überhaupt nicht die Werte, die am ADC0 Eingang angelegt werden. Ich benutze einen Spannungsteiler und anstelle eines LDR wie auf der Webseite, benutze ich halt einfach einen Poti.
Gibt es noch andere Beispiele wie man SPI mit Java (pi4j) ansprechen kann?
Danke für all eure Unterstützung.
lg Ethernut
/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: Java Examples
* FILENAME : example.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: http://www.pi4j.com/
* **********************************************************************
* %%
* Copyright (C) 2012 - 2013 Pi4J
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.pi4j.wiringpi.Spi;
public class example
{
// SPI operations
public static byte INIT_CMD = (byte) 0xD0;
public static byte SPI_CHANNEL = 0x0;
public static void main(String args[]) throws InterruptedException
{
//
// This SPI example is using the WiringPi native library to communicate
// with
// the SPI hardware interface connected to a MCP23S17 I/O Expander.
//
// Please note the following command are required to enable the SPI
// driver on
// your Raspberry Pi:
// > sudo modprobe spi_bcm2708
// > sudo chown `id -u`.`id -g` /dev/spidev0.*
//
// this source code was adapted from:
// https://github.com/thomasmacpherson/piface/blob/master/python/piface/pfio.py
//
// see this blog post for additional details on SPI and WiringPi
// https://projects.drogon.net/understanding-spi-on-the-raspberry-pi/
//
// see the link below for the data sheet on the MCP23S17 chip:
// http://ww1.microchip.com/downloads/en/devicedoc/21952b.pdf
System.out.println("<--Pi4J--> SPI test program using MCP3002 AtoD Chip");
// setup SPI for communication
int fd = Spi.wiringPiSPISetup(SPI_CHANNEL, 1000000);
System.out.println("fd = " + fd + "\n");
if (fd <= -1)
{
System.out.println(" ==>> SPI SETUP FAILED");
return;
}
// infinite loop
while (true)
{
read();
Thread.sleep(1000);
}
}
public static void read()
{
// send test ASCII message
byte packet[] = new byte[2];
packet[0] = INIT_CMD; // address byte
packet[1] = 0x00; // dummy
System.out.println("-----------------------------------------------");
System.out.println("[TX] " + bytesToHex(packet));
Spi.wiringPiSPIDataRW(SPI_CHANNEL, packet, 2);
System.out.println("[RX] " + bytesToHex(packet));
System.out.println("-----------------------------------------------");
// System.out.println(( (packet[0]<<7) | (packet[1]>>1) ) & 0x3FF);
System.out.println("packet[0] = " + packet[0]);
System.out.println("packet[1] = " + packet[1]);
System.out.println(((packet[0] << 8) | packet[1]) & 0x3FF);
}
public static String bytesToBinary(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
int v;
for (int j = 0; j < bytes.length; j++)
{
v = bytes[j];
sb.append(Integer.toBinaryString(v));
}
return sb.toString();
}
public static String bytesToHex(byte[] bytes)
{
final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[] hexChars = new char[bytes.length * 2];
int v;
for (int j = 0; j < bytes.length; j++)
{
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
Alles anzeigen