树莓派软硬件控制GPIO接口控制发光二极管

Raspberry Pi是什么?
引用维基百科的一句话:

The Raspberry Pi is a credit card sized single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools.

简单地说,它就是一个基于ARM CPU的、信用卡那么大的迷你计算机。
In short, Pi is an ARM-based mini computer which has a credit card size.

在阅读本文之前,请确保你已经阅读过我的另外几篇关于Pi的文章,因为本文与它们有或多或少的关系。
Before reading this article, please make sure that you've read some other articles of mine on Pi, because this article is more or less based on them.

通过Raspberry Pi的GPIO口来控制发光二极管已经不是什么新鲜事了,网上有大量的教程和资源可查。为了测试一下控制GPIO输出的功能,我也实践了一次。
首先声明,我对硬件并不了解,所以下文可能有错误,如果你发现了,请留言告诉我,非常感谢。
It's nothing new to control LEDs through Pi's GPIO, a lot of tutorials & resources are available on the Internet now. But I tried to do it myself too for the sake of testing the GPI output functionality.
First I want to remind the readers that I'm not a hardware guy so this article may contains some mistakes, and if you find that, please leave comments to tell me, thanks in advance.

文章来源:http://www.codelast.com/
【1】实验目的 / Experiment purpose
写一个简单的程序,控制Raspberry Pi的GPIO口输出高、低电平,从而让接在GPIO口上的发光二极管闪烁。
Write a simple program to control the GPIO on Pi to output high or low level to make the LED which connected to GPIO blink.

【2】材料准备 / Material preparing
首先,你当然要购买一些发光二极管,电阻,导线,面包板之类的必需电子元器件。看一下我接好线的实物图:
First of all you should buy some essential electronic components such as LEDs, resistor, wires, bread board, etc. Let's have a look at the state of wiring done:

【3】关于GPIO / About the GPIO
GPIO是通用输入输出(General Purpose Input Output)的简称,可以通过软件控制芯片上的GPIO口来定义它是输出还是输出接口。
GPIO接口没有定义特殊的功能,并且通常是不使用的。之所以有GPIO,是因为系统有时需要一些额外的控制线路以便于提供一些额外的功能。
From Wikipedia:

General Purpose Input/Output (GPIO) is a generic pin on a chip whose behavior (including whether it is an input or output pin) can be controlled (programmed) through software.
GPIO pins have no special purpose defined, and go unused by default. The idea is that sometimes the system integrator building a full system that uses the chip might find it useful to have a handful of additional digital control lines, and having these available from the chip can save the hassle of having to arrange additional circuitry to provide them.

Raspberry Pi上的GPIO接口在哪里?它是怎么定义的?我在网上找到这样两幅图:
How are the GPIO pins defined in Raspberry Pi? I found two images on the Internet:

  

以及另外一幅稍有不同的图:

特别要注意的是:尽管你在网上找到的大多数是类似上面两幅图的接口分布图,但是树莓派的GPIO口有两种不同的命名方式,最后这幅图就提供了一个对比。
You should pay special attention to that although most of what you found in the Internet are the pinouts like the first two images, it actually exists two kinds of names and the last image provides a comparison to them.
文章来源:http://www.codelast.com/
然后我们再来看看Pi上的接口实物图(红框中):
Then we check the real photo of Pi's GPIO(the red box):