通過(guò)Raspberry Pi(樹(shù)莓派)的GPIO接口控制發(fā)光二極管_第1頁(yè)
通過(guò)Raspberry Pi(樹(shù)莓派)的GPIO接口控制發(fā)光二極管_第2頁(yè)
通過(guò)Raspberry Pi(樹(shù)莓派)的GPIO接口控制發(fā)光二極管_第3頁(yè)
通過(guò)Raspberry Pi(樹(shù)莓派)的GPIO接口控制發(fā)光二極管_第4頁(yè)
通過(guò)Raspberry Pi(樹(shù)莓派)的GPIO接口控制發(fā)光二極管_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

通過(guò)RaspberryPi(樹(shù)莓派)的GPIO接口控制發(fā)光二極管簡(jiǎn)單地說(shuō),它就是一個(gè)基于A(yíng)RMCPU的、信用卡那么大的迷你計(jì)算機(jī)。

Inshort,PiisanARM-basedminicomputerwhichhasacreditcardsize.在閱讀本文之前,請(qǐng)確保你已經(jīng)閱讀過(guò)我的另外幾篇關(guān)于Pi的文章,因?yàn)楸疚呐c它們有或多或少的關(guān)系。

Beforereadingthisarticle,pleasemakesurethatyou'veread

someotherarticles

ofmineonPi,becausethisarticleismoreorlessbasedonthem.通過(guò)RaspberryPi的GPIO口來(lái)控制發(fā)光二極管已經(jīng)不是什么新鮮事了,網(wǎng)上有大量的教程和資源可查。為了測(cè)試一下控制GPIO輸出的功能,我也實(shí)踐了一次。

首先聲明,我對(duì)硬件并不了解,所以下文可能有錯(cuò)誤,如果你發(fā)現(xiàn)了,請(qǐng)留言告訴我,非常感謝。

It'snothingnewtocontrolLEDsthroughPi'sGPIO,alotoftutorials&resourcesareavailableontheInternetnow.ButItriedtodoitmyselftooforthesakeoftestingtheGPIoutputfunctionality.

FirstIwanttoremindthereadersthatI'mnotahardwareguysothisarticlemaycontainssomemistakes,andifyoufindthat,pleaseleavecommentstotellme,thanksinadvance.

文章來(lái)源:/

【1】實(shí)驗(yàn)?zāi)康?/p>

/

Experimentpurpose

寫(xiě)一個(gè)簡(jiǎn)單的程序,控制RaspberryPi的GPIO口輸出高、低電平,從而讓接在GPIO口上的發(fā)光二極管閃爍。

WriteasimpleprogramtocontroltheGPIOonPitooutputhighorlowleveltomaketheLEDwhichconnectedtoGPIOblink.

【2】材料準(zhǔn)備

/

Materialpreparing

首先,你當(dāng)然要購(gòu)買(mǎi)一些發(fā)光二極管,電阻,導(dǎo)線(xiàn),面包板之類(lèi)的必需電子元器件??匆幌挛医雍镁€(xiàn)的實(shí)物圖:

FirstofallyoushouldbuysomeessentialelectroniccomponentssuchasLEDs,resistor,wires,breadboard,etc.Let'shavealookatthestateofwiringdone:【3】關(guān)于GPIO

/

AbouttheGPIO

GPIO是通用輸入輸出(GeneralPurposeInputOutput)的簡(jiǎn)稱(chēng),可以通過(guò)軟件控制芯片上的GPIO口來(lái)定義它是輸出還是輸出接口。

GPIO接口沒(méi)有定義特殊的功能,并且通常是不使用的。之所以有GPIO,是因?yàn)橄到y(tǒng)有時(shí)需要一些額外的控制線(xiàn)路以便于提供一些額外的功能。

FromWikipedia:

GeneralPurposeInput/Output(GPIO)isagenericpinonachipwhosebehavior(includingwhetheritisaninputoroutputpin)canbecontrolled(programmed)throughsoftware.

GPIOpinshavenospecialpurposedefined,andgounusedbydefault.Theideaisthatsometimesthesystemintegratorbuildingafullsystemthatusesthechipmightfinditusefultohaveahandfulofadditionaldigitalcontrollines,andhavingtheseavailablefromthechipcansavethehassleofhavingtoarrangeadditionalcircuitrytoprovidethem.RaspberryPi上的GPIO接口在哪里?它是怎么定義的?我在網(wǎng)上找到這樣兩幅圖:

HowaretheGPIOpinsdefinedinRaspberryPi?IfoundtwoimagesontheInternet:

以及另外一幅稍有不同的圖:特別要注意的是:盡管你在網(wǎng)上找到的大多數(shù)是類(lèi)似上面兩幅圖的接口分布圖,但是樹(shù)莓派的GPIO口有兩種不同的命名方式,最后這幅圖就提供了一個(gè)對(duì)比。

YoushouldpayspecialattentiontothatalthoughmostofwhatyoufoundintheInternetarethepinoutslikethefirsttwoimages,itactuallyexiststwokindsofnamesandthelastimageprovidesacomparisontothem.

文章來(lái)源:/

然后我們?cè)賮?lái)看看Pi上的接口實(shí)物圖(紅框中):

ThenwechecktherealphotoofPi'sGPIO(theredbox):但是,這有一個(gè)問(wèn)題:我怎么知道上面兩幅圖對(duì)應(yīng)的方向是一致的?也就是說(shuō),我如何確定哪個(gè)針腳是1,哪個(gè)針腳是2,等等?我請(qǐng)教了一個(gè)做嵌入式開(kāi)發(fā)的朋友,他告訴我,將Pi的電路板翻到反面,在26個(gè)針腳中,焊點(diǎn)為方形的那個(gè)針腳就是“1”,在它旁邊同一排(兩個(gè)針腳一排)的那一個(gè)就是“2”,依此類(lèi)推。

But,thereisaproblemhere:howcanIknowthesequenceofthepins?Thatis,whichoneis1,andwhichoneis2,etc.Iaskaengineerfriendofmineandhetoldmeamethodtoidentifythesequenceofthepins:onthereversesideofPi'scircuitboard,checkwhichweldpointofthe26pinshasasquareshape,andtheonewhohasisjustpin"1",andtheonenexttoitinthesamerow(eachrowhas2pins)ispin"2",andsoforth.

文章來(lái)源:/

在此,我特意拍攝了一張反面焊點(diǎn)的圖來(lái)說(shuō)明:

ToillustratethatItookaphotoforthecircuitboard:文章來(lái)源:/

根據(jù)這個(gè)判斷,我得到了Pi的GPIO接口順序圖:

AccordingtothejudgementIgotthesequenceoftheports:文章來(lái)源:/

同時(shí),為了保險(xiǎn),還需要用萬(wàn)用表測(cè)量一下我們的判斷對(duì)不對(duì):在RaspberryPi的工作狀態(tài)下,將萬(wàn)用表的“+”接到“1”口上,萬(wàn)用表的“-”接到“6”口上,在看看萬(wàn)用表的顯示是不是大約為正3.3V,如果不是,那么說(shuō)明我們弄錯(cuò)了針腳順序,需要回頭再檢查!

Andforsaftyreasonweshouldusea

multimetertohaveatest:turnPi'spoweron,andconnectthe"+"ofthemultimetertothe"1"(3.3V)weconsideris,"-"tothe"6"(GROUND)weconsideris,thenthemultimetershoulddisplaya

approximate+3.3V.Ifit'snot,thenwemadeawrong

judgementonthepinsequenceandshouldgobacktocheckagain!下圖是我在樹(shù)莓派的工作狀態(tài)下,用萬(wàn)用表測(cè)量的實(shí)物圖:

BelowisthephotoofusingamultimetertomeasurethevoltageoftwopinswhenPiispoweron:順便說(shuō)一句,Youtube上有一個(gè)關(guān)于GPIO的視頻教程,看完它你會(huì)覺(jué)得非常有幫助。

Bytheway,thereisaveryhelpful

tutorialonusingGPIO

onYoutube,youcanhavealook.在確認(rèn)了接口序號(hào)無(wú)誤后,我們就可以開(kāi)始接線(xiàn)了。在這里,我隨意選了一個(gè)GPIO口——GPIO23(即端口16)來(lái)接LED。電路圖如下:

Afterbesurewiththesequenceoftheports,wecanstarttoconnectalltheelectroniccomponentstogether.I

chosetheGPIO23(namelyport16)toconnecttotheLEDandthecircuitdiagramisasfollows:文章來(lái)源:/

就算是這么簡(jiǎn)單的一個(gè)電路,像我一樣忘了曾經(jīng)學(xué)過(guò)的模擬電路知識(shí)的人也會(huì)問(wèn)一句:樹(shù)莓派的輸出電流可以驅(qū)動(dòng)得了發(fā)光二極管嗎?首先,我在這個(gè)鏈接查到樹(shù)莓派的3.3V輸出口可以提供的最大電流是50mA,并且從這個(gè)鏈接我知道不應(yīng)讓樹(shù)莓派的單針腳的輸出電流>10mA,然后我又查到發(fā)光二極管的驅(qū)動(dòng)電流一般是5~20mA。因此,驅(qū)動(dòng)發(fā)光二極管是不成問(wèn)題的。在電流為10mA時(shí),可計(jì)算得電阻應(yīng)為3.3V/0.01A=330Ω,考慮到工作狀態(tài)下的發(fā)光二極管的正向電阻一般為幾十到幾百歐姆,因此我們拿發(fā)光二極管串聯(lián)一個(gè)270Ω的電阻就差不多了。

Evenit's

suchasimplecircuitdiagram,peoplewhohavegiventheiranalogcircuitknowledgebacktotheirteacherlikememayaskthatwhethertheoutputcurrentofPicandrivetheLED,soImadesomeinvestigation...First,from

thislink

Iknowthatthe3.3Vpincanoutputamax50mAcurrent,andfrom

thislink

Irealizethatweshouldlimittheoutputcurrentofeachpintolessthan10mA.Second,IgettheinfothatthecurrenttodriveaLEDisabout5~20mA,sothecurrentprovidedbyPiisenoughtodriveit.Supposethecurrentis10mA,wecancalculatethattheresistanceis3.3V/0.01A=330Ω,andconsiderthattheforwardresistanceofaworkingLEDisaboutafewtensoftohundredsofohms,it'sappropriatetoconnecta270Ωresistanceinseries.

補(bǔ)充一句,我使用了一個(gè)非常好用的開(kāi)源軟件來(lái)畫(huà)上面的電路圖:Fritzing。感謝開(kāi)源世界的開(kāi)發(fā)者們!

Bytheway,Iused

Fritzing(anexcellentopen-sourcesoftware)todrawtheabovecircuitdiagramandthankforthecontributionofthedevelopers!那么按照我們的猜測(cè),在接好線(xiàn)的情況下,只要我們用軟件控制GPIO23輸出高/低電平,就可以讓發(fā)光二級(jí)管閃爍了,那么,軟件如何編寫(xiě)呢?

Afterthe

wiringisdone,itseemsthataslongasweuseaprogramtocontrolGPIO23tooutputhigh/lowlevel,we'llmaketheLEDblink.Sohowtowritesuchaprogram?

文章來(lái)源:/

【4】編寫(xiě)控制GPIO的程序/WriteaprogramtocontrolGPIO

我使用C來(lái)寫(xiě)這個(gè)程序。在這里要非常感謝

@drogon

為樹(shù)莓派的用戶(hù)提供了一個(gè)非常好用的庫(kù):WiringPi。通過(guò)這個(gè)庫(kù),我們可以很容易地控制GPIO。

IdecidetouseCtowritetheprogram.Thanksto

@drogon

thatheprovideallthePiusersaverygoodlibrary:

WiringPi,andbyusingthelibwecaneasilycontroltheGPIO.WiringPi的安裝方法:

FollowthesestepstoinstallWiringPi:

①首先確保你已經(jīng)安裝了make,如果沒(méi)裝,很簡(jiǎn)單:

Besuretohave[make]commandinstalledonyoursystem,ifitdoesn't,justinstallitbyusing:1pacman-Smake②下載安裝包,解壓,編譯,安裝:

Downloadtheinstallationpackage,unpack,compile&install:1234567mkdirtempcdtempwget/files/wiringPi.tgztarxfwiringPi.tgzcdwiringPi/wiringPi/makemakeinstall這樣就算安裝完了。

Sotheinstallationisdone.③下面寫(xiě)程序測(cè)試:

Thenwriteatestprogram:0102030405060708091011121314151617181920212223242526272829//led.c

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

intmain(intargc,char*argv[]){

if(argc<2){

printf("Usageexample:./led4\n");

return1;

}

intpinNumber=atoi(argv[1]);

if(-1==wiringPiSetup()){

printf("SetupwiringPifailed!");

return1;

}

pinMode(pinNumber,OUTPUT);//setmodetooutput

while(1){

digitalWrite(pinNumber,1);//outputahighlevel

delay(800);

digitalWrite(pinNumber,0);//outputalowlevel

delay(800);

}

return0;}需要注意的是,一旦你用digitalWrite()函數(shù)置了高電平,那么只要你不把它置為低電平,它將一直維持在高電平。

NotethatonceyouusefunctiondigitalWrite()tosetapintohighlevel,aslongasyoudon'tsetittolow

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論