最近總是愛弄沒人弄過的東西,如1602,i2c,如今又弄個74HC595
【視頻】php
http://v.youku.com/v_show/id_XNTkyNTk0NjAw.html
74HC595是能夠把3個針腳擴展成無數針腳的芯片(須要級聯),若是不級聯就是3轉8,雖然說Cubieboard有96Pin針腳,可是能用的很少,並且除去i2c等等,只剩下PD0~27個針腳能給咱們使用,這28個針腳還不如個人Arduino mega多,但arduino mega要70多塊(仍是山寨的),一個595只要3毛8,和mega通訊明顯不合算,並且595能夠無限擴展,買10個來擴展到80個口也才3塊8,比mega便宜多了。
前面1602抄了樹梅派的python程序(雖然我把RPi GPIO移植到了cb),i2c也是,把樹梅派的logo都給抄紅了,此次就不抄了(雖然我在github上發現了使用RPi GPIO驅動),改抄Arduino的了,參考:http://arduino.cc/en/Tutorial/ShiftOut和http://www.geek-workshop.com/thread-1778-1-1.html,不過老是工做不了,後來我都把代碼二進制都輸出了才發現是接線問題
【視頻晚點給】
【上圖(touch4拍的,不清楚,湊合着看吧)】
一亮一暗
正面照~
亮4個
全亮
工做圖
【END OF PICTURES】
在這裏感謝一些人:
1.windland,告訴我怎麼在arduino上弄595
2.hipboi,開發出如此好玩的產品——Cubieboard而且弄出了個pySUNXI寫寄存器操做GPIO
3.soloforce,把pySUNXI改編了,直接把pySUNXI的C代碼弄出來控制
4.忘了叫啥,Arduino開發者,要不是這我的,我代碼都不知道哪來的
5.全體支持Cb的人
電路其實沒啥好說的,按照arduino的程序圖接,latchPin接到PD2,clockPin接到PD3,dataPin接到PD4(由於有一些口我接了1602,因此調了一下)
主程序源代碼以下,具體的程序壓縮包+電路圖晚點給(仍是這句):html
#include <stdio.h>
python
#include <stdlib.h>
git
#include "gpio_lib.c"
github
#include <string.h>
oop
//#define LOW 0
ui
//#define HIGH 1
spa
//Pin connected to ST_CP of 74HC595
code
int latchPin = 2;
視頻
//Pin connected to SH_CP of 74HC595
int clockPin = 3;
////Pin connected to DS of 74HC595
int dataPin = 4;
int value = 255;
typedef unsigned char byte;
void pinMode(int pin,int io){
printf("set %d mode to %d \n",pin,io);
if(SETUP_OK!=sunxi_gpio_set_cfgpin(SUNXI_GPD(pin),io)){
printf("Failed to config GPIO pin\n");
//return -1;
}
}
void digitalWrite(int pin,int hl){
printf("set %d value to %d \n",pin,hl);
if(sunxi_gpio_output(SUNXI_GPD(pin),hl)){
printf("Failed to set GPIO pin value\n");
//return -1;
}
}
void *convert(int n)
{
int len=sizeof(int)*8; //int型所佔數據寬度
int i;
for(i=0;i<len;i++)
{
putchar('0'+((unsigned)(n<<i)>>(len-1))); //先左移,再邏輯右移,就把二進制從高位到低位輸出了
// printf("%d",((unsigned)(n<<i)>>(len-1))); //也能夠這樣輸出
}
printf("\n");
//printf("\n%d\n",n);
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
int ia = 1<<i;
int ips = myDataOut & ia;
if (ips) {
pinState= 1;
}
else {
pinState= 0;
}
printf("DATA: %d\n",myDataOut);
convert(ia);
convert(myDataOut);
convert(ips);
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}
void init_gpio(){
if(SETUP_OK!=sunxi_gpio_init()){
printf("Failed to initialize GPIO\n");
//return -1;
}
}
int main(int argc,char **argv){
if(argc > 1){
value = atoi(argv[1]);
printf("Value:%d\n",value);
}else{
printf("No value argument(argc=%d)!Using 255!\n",argc);
}
init_gpio();
pinMode(latchPin, OUTPUT);
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, value);
digitalWrite(latchPin, 1);
return 0;
}
複製代碼
使用方法:
執行「./595 數字」,數字是十進制數,轉換成2進制就明白了
經常使用:
255 = 11111111
170 = 10101010
240 = 11110000
0 = 00000000
右邊二進制,左邊十進制,1就是高電平,0是低電平
提示:這個程序是從低到高位輸出,新手可能不知道,意思是:從右到左看,即最右邊那個是595的輸出口0,右數第二個是輸出口1,以此類推,多試幾回就知道了,至於個人燈的順序爲何是正的,那是由於我把燈反着接了,595的輸出口0接到燈8,輸出口1接到等7,依次類推
原文做者:tll
原文連接:http://forum.cubietech.com/forum.php?mod=viewthread&tid=896&extra=page%3D1