卓泰科技 发表于 2014-10-29 00:16:00

树莓派blink,让小灯闪起来

话说,突然想起吃灰长达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内容如下:#!/bin/sh
#
# blink.sh:
#       Standard "blink" program in wiringPi. Blinks an LED connected
#       to the first GPIO pin.
#
# Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
#######################################################################
# This file is part of wiringPi:
#       https://projects.drogon.net/raspberry-pi/wiringpi/
#
#    wiringPi is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    wiringPi is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with wiringPi.If not, see <http://www.gnu.org/licenses/>.
#######################################################################

# LED Pin - wiringPi pin 0 is BCM_GPIO 17.

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

gpio mode $PIN out

while true; do
gpio write $PIN 1
sleep 0.5
gpio write $PIN 0
sleep 0.5
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,内容如下:
/*
* blink.c:
*      Standard "blink" program in wiringPi. Blinks an LED connected
*      to the first GPIO pin.
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* This file is part of wiringPi:
*      https://projects.drogon.net/raspberry-pi/wiringpi/
*
*    wiringPi is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    wiringPi is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with wiringPi.If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/

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

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

#define LED   0

int main (void)
{
printf ("Raspberry Pi blink\n") ;

wiringPiSetup () ;
pinMode (LED, OUTPUT) ;

for (;;)
{
    digitalWrite (LED, HIGH) ;// On
    delay (500) ;               // mS
    digitalWrite (LED, LOW) ;   // Off
    delay (500) ;
}
return 0 ;
}是不是很熟悉,简直和Arduino的一样。

编译并执行:
make blink
sudo ./blink熟悉的小灯闪闪又开始了。

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

结束语

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





二叔科技 发表于 2014-10-31 10:54:22

既没有接线图又没有效果图,强烈BS



页: [1]
查看完整版本: 树莓派blink,让小灯闪起来