Cubieboard驅動74HC595擴展Pin口

最近總是愛弄沒人弄過的東西,如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/ShiftOuthttp://www.geek-workshop.com/thread-1778-1-1.html,不過老是工做不了,後來我都把代碼二進制都輸出了才發現是接線問題
【視頻晚點給】
【上圖(touch4拍的,不清楚,湊合着看吧)】
IMG_0795.JPG 
一亮一暗
IMG_0796.JPG 
正面照~
IMG_0798.JPG 
亮4個
IMG_0799.JPG 
全亮
IMG_0800.JPG 
工做圖
【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

  1. #include <stdio.h>
    python

  2. #include <stdlib.h>
    git

  3. #include "gpio_lib.c"
    github

  4. #include <string.h>
    oop


  5. //#define LOW 0
    ui

  6. //#define HIGH 1
    spa


  7. //Pin connected to ST_CP of 74HC595
    code

  8. int latchPin = 2;
    視頻

  9. //Pin connected to SH_CP of 74HC595

  10. int clockPin = 3;

  11. ////Pin connected to DS of 74HC595

  12. int dataPin = 4;

  13. int value = 255;

  14. typedef unsigned char byte;

  15. void pinMode(int pin,int io){

  16. printf("set %d mode to %d \n",pin,io);

  17. if(SETUP_OK!=sunxi_gpio_set_cfgpin(SUNXI_GPD(pin),io)){

  18.     printf("Failed to config GPIO pin\n");

  19.     //return -1;

  20. }

  21. }

  22. void digitalWrite(int pin,int hl){

  23. printf("set %d value to %d \n",pin,hl);

  24. if(sunxi_gpio_output(SUNXI_GPD(pin),hl)){

  25.     printf("Failed to set GPIO pin value\n");

  26.     //return -1;

  27. }

  28. }


  29. void *convert(int n)

  30. {

  31.     int len=sizeof(int)*8;   //int型所佔數據寬度

  32.     int i;

  33.     for(i=0;i<len;i++)

  34.     {

  35.         putchar('0'+((unsigned)(n<<i)>>(len-1)));    //先左移,再邏輯右移,就把二進制從高位到低位輸出了

  36.         // printf("%d",((unsigned)(n<<i)>>(len-1)));  //也能夠這樣輸出

  37.     }

  38.     printf("\n");

  39.     //printf("\n%d\n",n);

  40. }


  41. void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {

  42.   // This shifts 8 bits out MSB first, 

  43.   //on the rising edge of the clock,

  44.   //clock idles low


  45.   //internal function setup

  46.   int i=0;

  47.   int pinState;

  48.   pinMode(myClockPin, OUTPUT);

  49.   pinMode(myDataPin, OUTPUT);


  50.   //clear everything out just in case to

  51.   //prepare shift register for bit shifting

  52.   digitalWrite(myDataPin, 0);

  53.   digitalWrite(myClockPin, 0);


  54.   //for each bit in the byte myDataOut

  55.   //NOTICE THAT WE ARE COUNTING DOWN in our for loop

  56.   //This means that %00000001 or "1" will go through such

  57.   //that it will be pin Q0 that lights. 

  58.   for (i=7; i>=0; i--)  {

  59.     digitalWrite(myClockPin, 0);


  60.     //if the value passed to myDataOut and a bitmask result 

  61.     // true then... so if we are at i=6 and our value is

  62.     // %11010100 it would the code compares it to %01000000 

  63.     // and proceeds to set pinState to 1.

  64.     int ia = 1<<i;

  65.     int ips = myDataOut & ia;

  66.     if (ips) {

  67.       pinState= 1;

  68.     }

  69.     else {        

  70.       pinState= 0;

  71.     }

  72.     printf("DATA: %d\n",myDataOut);

  73.     convert(ia);

  74.     convert(myDataOut);

  75.     convert(ips);

  76.     //Sets the pin to HIGH or LOW depending on pinState

  77.     digitalWrite(myDataPin, pinState);

  78.     //register shifts bits on upstroke of clock pin

  79.     digitalWrite(myClockPin, 1);

  80.     //zero the data pin after shift to prevent bleed through

  81.     digitalWrite(myDataPin, 0);

  82.   }

  83.   //stop shifting

  84.   digitalWrite(myClockPin, 0);

  85. }

  86. void init_gpio(){

  87. if(SETUP_OK!=sunxi_gpio_init()){

  88.         printf("Failed to initialize GPIO\n");

  89.         //return -1;

  90. }

  91. }

  92. int main(int argc,char **argv){

  93.     if(argc > 1){

  94.         value = atoi(argv[1]);

  95.         printf("Value:%d\n",value);

  96.     }else{

  97.         printf("No value argument(argc=%d)!Using 255!\n",argc);

  98.     }

  99. init_gpio();

  100. pinMode(latchPin, OUTPUT);

  101. digitalWrite(latchPin, 0);


  102. shiftOut(dataPin, clockPin, value); 

  103. digitalWrite(latchPin, 1);

  104. return 0;

  105. }


複製代碼

使用方法:
執行「./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

相關文章
相關標籤/搜索