狗趴(GodPub),开源硬件学习与实践

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 8024|回复: 1
打印 上一主题 下一主题

树莓派blink,让小灯闪起来

[复制链接]

84

主题

143

帖子

725

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
725
QQ
跳转到指定楼层
楼主
发表于 2014-10-29 00:16:00 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
话说,突然想起吃灰长达7个月之久的树莓派。
当初购买回来之后,折腾装系统,折腾联网,折腾无线网卡,折腾打印服务器以及文件服务器等。
现在回想起来,甚至都忘记了当时具体做了些什么,如何做的。看来写日志还是有必要的,像雷锋同志学习。发到狗趴上(Godpub.com) 没准能吸引点人气,不能让二叔孤军作战是吧。

好了,言归正传。
硬件接线

GPIO 0(编号11的排针)串220欧电阻后LED正极。
0V (编号6的排针)接LED负极。
GPIO图暂略。

软件部分

因为之前无线网卡啥的都是设置好的,所以拿来上电,putty登录。
登录目录下有个wiringPi/目录(说实话,我忘记了这个是否是自带的,还是我装上的,权且当作自带的吧)

进入到示例目录:
cd wiringPi/examples/
里边保存有一些代码示例:

blink.sh 这个是shell脚本控制小灯的例子。
blink.c   这个是C语言代码控制小灯的例子。
(其它还有8个小灯,串口等例子,这里就不多说了)


blink.sh内容如下:
  1. #!/bin/sh
  2. #
  3. # blink.sh:
  4. #       Standard "blink" program in wiringPi. Blinks an LED connected
  5. #       to the first GPIO pin.
  6. #
  7. # Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  8. #######################################################################
  9. # This file is part of wiringPi:
  10. #       https://projects.drogon.net/raspberry-pi/wiringpi/
  11. #
  12. #    wiringPi is free software: you can redistribute it and/or modify
  13. #    it under the terms of the GNU Lesser General Public License as published by
  14. #    the Free Software Foundation, either version 3 of the License, or
  15. #    (at your option) any later version.
  16. #
  17. #    wiringPi is distributed in the hope that it will be useful,
  18. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. #    GNU Lesser General Public License for more details.
  21. #
  22. #    You should have received a copy of the GNU Lesser General Public License
  23. #    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
  24. #######################################################################

  25. # LED Pin - wiringPi pin 0 is BCM_GPIO 17.

  26. <b><font color="#ff0000">LED=0</font></b>

  27. gpio mode $PIN out

  28. while true; do
  29.   gpio write $PIN 1
  30.   sleep 0.5
  31.   gpio write $PIN 0
  32.   sleep 0.5
  33. done
复制代码

执行之(sh blink.sh),我们会得到提示的错误信息
Usage: gpio mode pin mode
Usage: gpio write pin value
Usage: gpio write pin value
...

为什么会这样呢?回头看文件中我们红色加粗的那行,里边定义了一个变量LED,但是调用命令的时候,使用的变量却是PIN!

话说,写这个例子的人也太不负责了,强烈谴责!
把LED=0改成PIN=0,保存执行。

小灯是不是闪了起来?简单吧?

接下来看blink.c,内容如下:
  1. /*
  2. * blink.c:
  3. *      Standard "blink" program in wiringPi. Blinks an LED connected
  4. *      to the first GPIO pin.
  5. *
  6. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. *      https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. *    wiringPi is free software: you can redistribute it and/or modify
  12. *    it under the terms of the GNU Lesser General Public License as published by
  13. *    the Free Software Foundation, either version 3 of the License, or
  14. *    (at your option) any later version.
  15. *
  16. *    wiringPi is distributed in the hope that it will be useful,
  17. *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. *    GNU Lesser General Public License for more details.
  20. *
  21. *    You should have received a copy of the GNU Lesser General Public License
  22. *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */

  25. #include <stdio.h>
  26. #include <wiringPi.h>

  27. // LED Pin - wiringPi pin 0 is BCM_GPIO 17.

  28. #define LED     0

  29. int main (void)
  30. {
  31.   printf ("Raspberry Pi blink\n") ;

  32.   wiringPiSetup () ;
  33.   pinMode (LED, OUTPUT) ;

  34.   for (;;)
  35.   {
  36.     digitalWrite (LED, HIGH) ;  // On
  37.     delay (500) ;               // mS
  38.     digitalWrite (LED, LOW) ;   // Off
  39.     delay (500) ;
  40.   }
  41.   return 0 ;
  42. }
复制代码
是不是很熟悉,简直和Arduino的一样。

编译并执行:
  1. make blink
  2. sudo ./blink
复制代码
熟悉的小灯闪闪又开始了。

PS:
Ctrl+c 停掉程序。根据停掉的时机,小灯会处于常亮或者常灭状态。

结束语

太晚了,就写这么多了。
原创啊,转载请保留狗趴(Godpub.com)链接。





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。

13

主题

33

帖子

155

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
155
沙发
发表于 2014-10-31 10:54:22 | 只看该作者
既没有接线图又没有效果图,强烈BS



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

本版积分规则

QQ|Archiver|手机版|狗趴(GodPub) Arduino&Raspberry Pi开源硬件学习与实践[QQ群:20085629]  

GMT+8, 2024-5-6 20:04 , Processed in 0.100615 second(s), 41 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表