CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 19103|回复: 13

Cubieboard驱动74HC595扩展Pin口

[复制链接]
发表于 2013-8-6 22:21:36 | 显示全部楼层 |阅读模式
本帖最后由 tll 于 2013-8-7 06:51 编辑

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

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,所以调了一下)
主程序源代码如下,具体的程序压缩包+电路图晚点给(还是这句):
  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,依次类推

回复

使用道具 举报

发表于 2013-8-6 22:24:51 | 显示全部楼层
多久好的帖子,支持一个
回复 支持 反对

使用道具 举报

发表于 2013-8-7 10:17:29 | 显示全部楼层
{:soso_e179:}
回复 支持 反对

使用道具 举报

发表于 2013-11-14 21:31:26 | 显示全部楼层
具体的程序压缩包+电路图晚点给....好晚啊
回复 支持 反对

使用道具 举报

发表于 2013-11-14 22:07:38 | 显示全部楼层
soloforce,把pySUNXI改编了,直接把pySUNXI的C代码弄出来控制

不是我改编的,pySUNXI一直都有C的API
要感谢原作者
回复 支持 反对

使用道具 举报

发表于 2013-11-15 11:42:40 | 显示全部楼层
595可以用SPI控制的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-15 17:52:40 | 显示全部楼层
rgwan 发表于 2013-11-15 11:42
595可以用SPI控制的。

这样就浪费了cb的SPI
回复 支持 反对

使用道具 举报

发表于 2013-11-16 17:49:30 | 显示全部楼层
tll 发表于 2013-11-15 17:52
这样就浪费了cb的SPI

SPI有CS脚的……
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-16 23:07:12 | 显示全部楼层
rgwan 发表于 2013-11-16 17:49
SPI有CS脚的……

不是,我的意思是说,cb的SPI还有另外用处,而且配置复杂……
回复 支持 反对

使用道具 举报

发表于 2013-11-17 10:40:55 | 显示全部楼层
本帖最后由 rgwan 于 2013-11-17 10:42 编辑
tll 发表于 2013-11-16 23:07
不是,我的意思是说,cb的SPI还有另外用处,而且配置复杂……


好吧。话说你为啥盯着Arduino不放呢。M8 M328之类的芯片也不贵。而且Arduino的哪个糟糕的framework实在坑人。digitalWrite和analogWrite竟然不是宏定义而是函数!浪费机器周期。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-5-3 08:19 , Processed in 0.027712 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部