前一篇有介紹使用GPIO使用三隻腳位控制一個74HC595晶片,三隻腳位不只可以控制單一個74HC595晶片輸出,利用串聯的方式可使用更多的74HC595
詳細介紹可以查看最後的參考資料
使用麵包板顯示的結果,紅色燈為前8bit,綠燈為後8bit,也就是如果資料是0000000100000000則綠色燈第一個會亮起
程式碼如下:
package mygpio; 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; /** * * @author Eric */ public class IC74HC595 { public static void main(String args[]) { final GpioController gpio = GpioFactory.getInstance(); System.out.println("start IC"); // 建立控制GPIO_23輸出的物件 final GpioPinDigitalOutput ds = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_23, PinState.LOW); final GpioPinDigitalOutput stcp = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_24, PinState.LOW); final GpioPinDigitalOutput shcp = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_25, PinState.LOW); System.out.println("init "); int delayTime = 500; //while(true) { //for (int i = 128; i < 300; i++) int i =2068; { stcp.setState(PinState.LOW); //System.out.println((byte)(i>> 8)+";"+(((byte)i))+";"+i); //if(i>256) com.pi4j.wiringpi.Shift.shiftOut((byte)ds.getPin().getAddress(), (byte)shcp.getPin().getAddress(), (byte)com.pi4j.wiringpi.Shift.MSBFIRST, (byte)(i>> 8)); com.pi4j.wiringpi.Shift.shiftOut((byte) ds.getPin().getAddress(), (byte) shcp.getPin().getAddress(), (byte) com.pi4j.wiringpi.Shift.MSBFIRST, (byte) i); stcp.setState(PinState.HIGH); delay(delayTime); } } } private static void delay(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { System.out.println(e.toString()); } } }
只跟前一個範例多了而已,輸出的高位元組,74HC959則是把Q7S連接到第二個IC的DS,SHCP及STCP互連就好了。
com.pi4j.wiringpi.Shift.shiftOut((byte)ds.getPin().getAddress(), (byte)shcp.getPin().getAddress(), (byte)com.pi4j.wiringpi.Shift.MSBFIRST, (byte)(i>> 8));
參考資料