Cubieboard驱动74HC595扩展Pin口

最近老是爱弄没人弄过的东西,如1602,i2c,现在又弄个74HC595
【视频】

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拍的,不清楚,凑合着看吧)】
Cubieboard驱动74HC595扩展Pin口_第1张图片 
一亮一暗
Cubieboard驱动74HC595扩展Pin口_第2张图片 
正面照~
Cubieboard驱动74HC595扩展Pin口_第3张图片 
亮4个
Cubieboard驱动74HC595扩展Pin口_第4张图片 
全亮
Cubieboard驱动74HC595扩展Pin口_第5张图片 
工作图
【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,所以调了一下)
主程序源代码如下,具体的程序压缩包+电路图晚点给(还是这句):

  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #include "gpio_lib.c"

  4. #include <string.h>


  5. //#define LOW 0

  6. //#define HIGH 1


  7. //Pin connected to ST_CP of 74HC595

  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


你可能感兴趣的:(cubieboard,扩展Pin口)