Monday, November 24, 2014

External ADC with Raspberry Pi.



           Really I tried a lot with MCP3208 external ADC to interface with the Raspberry pi. Searched for various codes but nothing was available in JAVA. So after some research i got some python code and modified that to JAVA.  in this i have used the Pi4J library.

/****
*Author : Bikash Narayan Panda
*Use : This is used to read ADC data foem the MCP3208 SPI based ADC.
*Date :19/Nov/2014
*Tags: Please Include the Pi4J Library.
*
*
**/
import java.io.IOException;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;

public class SensorTest {

    public static int readadc(int adcnum, GpioPinDigitalOutput clockpin,
            GpioPinDigitalOutput mosipin, GpioPinDigitalInput misopin,
            GpioPinDigitalOutput cspin, GpioController gpio) {
        if ((adcnum > 7) || (adcnum < 0))
            return -1;

        cspin.high();
        clockpin.low();
        cspin.low();

        int commandout = adcnum;
        commandout |= 0x18;
        commandout <<= 3;
        for (int i = 0; i < 5; i++) {
            if ((commandout & 0x80) != 0) {
                mosipin.high();
            }

            else {
                mosipin.low();
            }
            commandout <<= 1;
            clockpin.high();
            clockpin.low();
        }

        int adcout = 0;
        for (int i = 0; i < 14; i++) {
            clockpin.high();
            clockpin.low();
            adcout <<= 1;
            if (misopin.getState() == PinState.HIGH) {
                adcout |= 0x1;
            }

        }

        cspin.high();

        adcout >>= 1;
        return adcout;

    }

    public static void main(String[] args) throws InterruptedException,
            IOException {

        System.out.println("<--Pi4J--> GPIO Listen Example ... started.");

        // create gpio controller
        GpioController gpio = GpioFactory.getInstance();

        GpioPinDigitalOutput SPICLK = gpio.provisionDigitalOutputPin(
                RaspiPin.GPIO_14, "SPICLK", PinState.LOW);
        GpioPinDigitalInput SPIMISO = gpio.provisionDigitalInputPin(
                RaspiPin.GPIO_13, "SPIMISO", PinPullResistance.PULL_DOWN);
        GpioPinDigitalOutput SPIMOSI = gpio.provisionDigitalOutputPin(
                RaspiPin.GPIO_12, "SPIMOSI", PinState.LOW);
        GpioPinDigitalOutput SPICS = gpio.provisionDigitalOutputPin(
                RaspiPin.GPIO_11, "SPICS", PinState.LOW);


       
        for (int i=0;i <= 20;i++) {
            float value = readadc(0, SPICLK, SPIMOSI, SPIMISO, SPICS, gpio);
            System.out.println("sensor 0value= " + value);
            value = readadc(1, SPICLK, SPIMOSI, SPIMISO, SPICS, gpio);
            System.out.println("sensor 1value= " + value);
            Thread.sleep(500);
            value = readadc(2, SPICLK, SPIMOSI, SPIMISO, SPICS, gpio);
            System.out.println("sensor 2value= " + value);
            Thread.sleep(500);
            value = readadc(3, SPICLK, SPIMOSI, SPIMISO, SPICS, gpio);
            System.out.println("sensor3 value= " + value);
            Thread.sleep(500);
            value = readadc(4, SPICLK, SPIMOSI, SPIMISO, SPICS, gpio);
            System.out.println("sensor 4value= " + value);
            Thread.sleep(500);
            value = readadc(5, SPICLK, SPIMOSI, SPIMISO, SPICS, gpio);
            System.out.println("sensor 5value= " + value);
            Thread.sleep(500);
            value = readadc(6, SPICLK, SPIMOSI, SPIMISO, SPICS, gpio);
            System.out.println("sensor 6value= " + value);
            Thread.sleep(500);
            value = readadc(7, SPICLK, SPIMOSI, SPIMISO, SPICS, gpio);
            System.out.println("sensor7 value= " + value);
            Thread.sleep(500);
            Thread.sleep(500);
            
            Thread.sleep(500);
        }
       
       
        gpio.shutdown();

    }
}

 

No comments:

Post a Comment