在本教程的带领下,你将在十分钟内学会在PYNQ-Z2板卡上使用传感器和LED开发自己的IoT应用!
初始化板卡
from pynq.overlays.base import BaseOverlay
base = BaseOverlay("base.bit")
初始化LED Bar
Grove LED bar上有10个LED灯,8个绿色的LED,一个橘黄色的LED和一个红色的LED。每个LED的亮度都可以独立被控制。我们将会在Z2板卡上外接一块Arduino Shield扩展板,LED bar连接在扩展板的D6接口上。
from pynq.lib.arduino import Grove_LEDbar
from pynq.lib.arduino import ARDUINO_GROVE_G4
# Instantiate Grove LED Bar on Arduino shield G4
ledbar = Grove_LEDbar(base.ARDUINO, ARDUINO_GROVE_G4)
ledbar.reset()
初始化超声波传感器
Pmod-grove扩展板连接在PMODA接口上,超声波传感器连接Pmod-Grove转接器的G1接口上的
%%microblaze base.PMODA
#include "xparameters.h" #define TCSR0 0x00 void create_10us_pulse(gpio usranger){ void configure_as_input(gpio usranger){ unsigned int capture_duration(gpio usranger){ unsigned int read_raw(){ 使用python即可调用超声波传感器 def read_distance_cm(): 案例展示:超声波测距仪 按BTN3结束 btns = [base.buttons[index] for index in range(4)] done = False distance = read_distance_cm() 点击查看原代码: https://github.com/xupsh/pynq-hands-on-demos/tree/master/InternetOfThings 文章转载自: Pynq
#include "xtmrctr.h"
#include "gpio.h"
#include "timer.h"
#include
#define TLR0 0x04
#define TCR0 0x08
#define TCSR1 0x10
#define TLR1 0x14
#define TCR1 0x18
#define MAX_COUNT 0xFFFFFFFF
gpio_set_direction(usranger, GPIO_OUT);
gpio_write(usranger, 0);
delay_us(2);
gpio_write(usranger, 1);
delay_us(10);
gpio_write(usranger, 0);
}
gpio_set_direction(usranger, GPIO_IN);
}
unsigned int count1, count2;
count1=0;
count2=0;
XTmrCtr_WriteReg(XPAR_TMRCTR_0_BASEADDR, 0, TLR0, 0x0);
XTmrCtr_WriteReg(XPAR_TMRCTR_0_BASEADDR, 0, TCSR0, 0x190);
while(!gpio_read(usranger));
count1=XTmrCtr_ReadReg(XPAR_TMRCTR_0_BASEADDR, 0, TCR0);
while(gpio_read(usranger));
count2=XTmrCtr_ReadReg(XPAR_TMRCTR_0_BASEADDR, 0, TCR0);
if(count2 > count1) {
return (count2 - count1);
} else {
return((MAX_COUNT - count1) + count2);
}
}
gpio usranger;
usranger = gpio_open(PMOD_G1_A);
create_10us_pulse(usranger);
configure_as_input(usranger);
return capture_duration(usranger);
}
from pynq import Clocks
raw_value = read_raw()
clk_period_ns = int(1000 / Clocks.fclk0_mhz)
num_microseconds = raw_value * clk_period_ns * 0.001
if num_microseconds * 0.001 > 30:
return 500
else:
return num_microseconds/58
靠近测距仪50cm以内,LED每近5cm多亮一格
import time
import math
ledbar.reset()
while not done:
if (btns[3].read()==1):
ledbar.reset()
done = True
if distance < 50.0:
ledbar.write_level(10-math.floor(distance/5), 3, 1)
else:
ledbar.write_level(0, 3, 1)
time.sleep(0.1)