xiaohuihui183 2015-05-27 05:03 采纳率: 0%
浏览 1994

树莓派大神求救,源码看不懂 啊

import com.pi4j.wiringpi.Spi;

public class WiringPiSPIExample {

// SPI operations
public static byte WRITE_CMD = 0x40;
public static byte READ_CMD  = 0x41;

@SuppressWarnings("unused")
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
    // http://wiringpi.com/reference/spi-library/
    //
    // 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 MCP23S17 I/O Expander Chip");

    // configuration
    byte IODIRA = 0x00; // I/O direction A
    byte IODIRB = 0x01; // I/O direction B
    byte IOCON  = 0x0A; // I/O config
    byte GPIOA  = 0x12; // port A
    byte GPIOB  = 0x13; // port B
    byte GPPUA  = 0x0C; // port A pullups
    byte GPPUB  = 0x0D; // port B pullups
    byte OUTPUT_PORT = GPIOA;
    byte INPUT_PORT  = GPIOB;
    byte INPUT_PULLUPS = GPPUB;        

    // setup SPI for communication
    int fd = Spi.wiringPiSPISetup(0, 10000000);
    if (fd <= -1) {
        System.out.println(" ==>> SPI SETUP FAILED");
        return;
    }

    // initialize
    write(IOCON,  0x08);  // enable hardware addressing
    write(GPIOA,  0x00);  // set port A off
    write(IODIRA, 0);     // set port A as outputs
    write(IODIRB, 0xFF);  // set port B as inputs
    write(GPPUB,  0xFF);  // set port B pullups on

    int pins = 1;

    // infinite loop
    while(true) {

        // shift the bit to the left in the A register
        // this will cause the next LED to light up and 
        // the current LED to turn off.
        if(pins >= 255)
            pins=1;
        write(GPIOA,  (byte)pins);  
        pins = pins << 1;
        Thread.sleep(1000);

        // read for input changes 
        //read(INPUT_PORT);
    }
}

public static void write(byte register, int data){

    // send test ASCII message
    byte packet[] = new byte[3];
    packet[0] = WRITE_CMD;  // address byte
    packet[1] = register;  // register byte
    packet[2] = (byte)data;  // data byte

    System.out.println("-----------------------------------------------");
    System.out.println("[TX] " + bytesToHex(packet));
    Spi.wiringPiSPIDataRW(0, packet, 3);        
    System.out.println("[RX] " + bytesToHex(packet));
    System.out.println("-----------------------------------------------");
}

public static void read(byte register){

    // send test ASCII message
    byte packet[] = new byte[3];
    packet[0] = READ_CMD;    // address byte
    packet[1] = register;    // register byte
    packet[2] = 0b00000000;  // data byte

    System.out.println("-----------------------------------------------");
    System.out.println("[TX] " + bytesToHex(packet));
    Spi.wiringPiSPIDataRW(0, packet, 3);        
    System.out.println("[RX] " + bytesToHex(packet));
    System.out.println("-----------------------------------------------");
}

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);
}    

}

 代码神码意思
  • 写回答

1条回答

  • threenewbee 2015-05-27 05:52
    关注

    目测是读写gpio端口,至于控制的硬件怎么理解,这个要看它连接了什么设备。那个设备需要什么数据。

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!