|
1.實(shí)驗(yàn)?zāi)康?br />
1. 學(xué)習(xí)在PC機(jī)系統(tǒng)中擴(kuò)展簡(jiǎn)單I/O?接口的方法。
2. 進(jìn)一步學(xué)習(xí)編制數(shù)據(jù)輸出程序的設(shè)計(jì)方法。
3. 學(xué)習(xí)超聲波模塊的測(cè)距原理。
4. 學(xué)習(xí)LCD5110接線方法
5. 學(xué)習(xí)TPYboard控制超聲波模塊測(cè)距。(蘿卜學(xué)科編程教育tpyboard。com)
2.所需元器件
超聲波模塊一個(gè)
TPYBoard板子一塊
5110LCD顯示屏一個(gè)
數(shù)據(jù)線一條
杜邦線若干
3.超聲波模塊工作原理
(1)采用IO口TRIG觸發(fā)測(cè)距,給最少10us的高電平信呈。
(2)模塊自動(dòng)發(fā)送 8 個(gè) 40khz 的方波,自動(dòng)檢測(cè)是否有信號(hào)返回。
(3)有信號(hào)返回,通過 IO 口 ECHO 輸出一個(gè)高電平,高電平持續(xù)的時(shí)間就是超聲波從發(fā)射到返回的時(shí)間。測(cè)試距離=(高電平時(shí)間*聲速(340M/S))/2。
如下圖接線,VCC 供 5V電源, GND 為地線,TRIG 觸發(fā)控制信號(hào)輸入,ECHO 回響信號(hào)輸出等四個(gè)接口端。。(蘿卜學(xué)科編程教育tpyboard。com)
4.控制5110顯示屏顯示6x8字符
先看一下LCD5110針腳含義吧(注意:LCD5110的針腳有些不一樣的)
TPYBoard的針腳與5110的針腳對(duì)應(yīng)關(guān)系如下:
TPYBoard LCD5110 memo
————————————————————————————
# any Pin => RST Reset pin (0=reset, 1=normal)
# any Pin => CE Chip Enable (0=listen for input, 1=ignore input)
# any Pin => DC Data/Command (0=commands, 1=data)
# MOSI => DIN data flow (Master out, Slave in)
# SCK => CLK SPI clock
# 3V3 or any Pin => VCC 3.3V logic voltage (0=off, 1=on)
# any Pin => LIGHT Light (0=on, 1=off)
# GND => GND
還是看不明白的話,直接上針腳編號(hào)吧
TPYBoard LCD5110 memo
————————————————————————————
Y10 => RST Reset pin (0=reset, 1=normal)
Y11 => CE Chip Enable (0=listen for input, 1=ignore input)
Y9 => DC Data/Command (0=commands, 1=data)
X8 => DIN data flow (Master out, Slave in)
X6 => CLK SPI clock
VCC
Y12 => LIGHT Light (0=on, 1=off)
GND
接線ok后,并且導(dǎo)入font.py文件和upcd8544.py文件,編寫main.py將測(cè)到的距離顯示在5110顯示屏上,運(yùn)行main.py就ok了。(font.py和upcd8544.py可以從官網(wǎng)上下載,最后會(huì)告訴下載地址)。。(蘿卜學(xué)科編程教育tpyboard。com)
5.源代碼
- import pyb
- from pyb import Pin
- from pyb import Timer
- import upcd8544
- from machine import SPI,Pin
- Trig = Pin('X2',Pin.OUT_PP)
- Echo = Pin('X1',Pin.IN)
- num=0
- flag=0
- run=1
- def start(t):
- global flag
- global num
- if(flag==0):
- num=0
- else:
- num=num+1
- def stop(t):
- global run
- if(run==0):
- run=1
- start1=Timer(1,freq=10000,callback=start)
- stop1=Timer(4,freq=2,callback=stop)
- while True:
- if(run==1):
- SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK
- #DIN =>SPI(1).MOSI 'X8' data flow (Master out, Slave in)
- #CLK =>SPI(1).SCK 'X6' SPI clock
- RST = pyb.Pin('Y10')
- CE = pyb.Pin('Y11')
- DC = pyb.Pin('Y9')
- LIGHT = pyb.Pin('Y12')
- lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)
- Trig.value(1)
- pyb.udelay(100)
- Trig.value(0)
- while(Echo.value()==0):
- Trig.value(1)
- pyb.udelay(100)
- Trig.value(0)
- flag=0
- if(Echo.value()==1):
- flag=1
- while(Echo.value()==1):
- flag=1
- if(num!=0):
- #print('num:',num)
- distance=num/10000*34000/2
- print('Distance')
- print(distance,'cm')
- lcd_5110.lcd_write_string('Distance',0,0)
- lcd_5110.lcd_write_string(str(distance),6,1)
- lcd_5110.lcd_write_string('cm',58,1)
- lcd_5110.lcd_write_string('This is a test of Distance',0,2)
- flag=0
- run=0
復(fù)制代碼
|
|