PYBminiDB学习板学出厂例程¶
出厂测试代码说明¶
学习板出厂带有出厂测试代码,出厂测试代码能全面检测学习板硬件功能是否可用,也便于同学们研究参考。
使用前的准备工作¶
检测前将DHT11传感器正确的安装到板子上,注意传感器开窗朝外,将温度传感器18B20安装的对于的插槽中,注意平面朝外。注意不正确的安装有可能损坏硬件硬件设备。确保对应跳帽插针上都有跳帽确保安装正确,OLED显示屏幕安装正确,使用usb口进行供电。
出厂测试代码正常表现情况¶
在usb口供电后,2s 后再OLED显示屏幕上打印对应器件的状态或传感器采集到的状态。这里的传感器有,温湿度DHT11、温度传感器18B20和加速度传感器。同时继电器和无源蜂鸣器会周期性的响起,按下两个按键2秒钟后,对应的继电器和蜂鸣器会停止动作。
main.py文件¶
import pyb
from pyb import Pin, ADC, Timer, ExtInt,I2C,SPI
from ssd1306 import SSD1306
import _thread
import time,onewire,ds18x20
from machine import Pin
import dht
accel = pyb.Accel()
adc = ADC(Pin('PA0'))
p_out = Pin('PA8', Pin.OUT_PP)
p_out.low()
time.sleep(1)
p_out.high()
display = SSD1306(pinout={'sda': 'PB7','scl': 'PB6'},height=64,external_vcc=False)
ow=onewire.OneWire(Pin('PB0'))
ds=ds18x20.DS18X20(ow)
dht = dht.DHT11(Pin("PB1"))
KEY1 = Pin('PB9', Pin.IN, Pin.PULL_UP)
KEY2 = Pin('PB8', Pin.IN, Pin.PULL_UP)
relay = Pin('PB10', Pin.OUT_PP)
buzzer = Pin('PA1')
tim = Timer(2, freq=1000)
ch = tim.channel(2, Timer.PWM, pin=buzzer)
ch.pulse_width_percent(0)
def test1():
pyb.LED(4).toggle()
relay.high()
time.sleep(1)
relay.low()
time.sleep(1)
def test2():
pyb.LED(3).toggle()
ch.pulse_width_percent(50)
time.sleep(1)
ch.pulse_width_percent(0)
time.sleep(1)
def sw_callback():
pyb.LED(2).toggle()
print("ButtonPressed")
def funcB(sec):
time.sleep(sec)
while True:
if KEY1.value():
test1()
else:
_thread.exit()
def funcC(sec):
time.sleep(sec)
while True:
if KEY2.value():
test2()
else:
_thread.exit()
def funcA(sec):
time.sleep(sec)
i2c = I2C(1, I2C.MASTER, baudrate=100000)
print(i2c.scan())
time.sleep_ms(50)
i2c.mem_write('xy', 80, 0x10)
time.sleep_ms(50)
if i2c.mem_read(2, 80, 0x10)==b'xy':
abc=1
else:
abc=0
p_out = Pin('PB12', Pin.OUT_PP)
p_out.low()
spi = SPI(2, SPI.MASTER, baudrate=2000, polarity=0, phase=0,crc=0x7,bits=8,firstbit=SPI.MSB)
spi.send(0x9f) #发送固定指令,读取指令
data=spi.recv(2) #接收id号
p_out.high()
print (data)
if data==b'\xef0':
print("SPI FLASH OK")
spiflashok=1
else:
print("SPI FLASH ERROR")
spiflashok=0
try:
display.poweron()
display.init_display()
if abc==1:
display.draw_text(1,49,'24C08 OK')
else:
display.draw_text(1,49,'24C08 ERROR')
if spiflashok==1:
display.draw_text(55,49,' 25x40 OK')
else:
display.draw_text(55,49,'25x40 ERROR')
for x in range(1,48):
display.set_pixel(x,11,True)
display.set_pixel(x,46,True)
for y in range(11,46):
display.set_pixel(1, y,True)
display.set_pixel(48, y,True)
display.draw_text(90,13,'X')
display.draw_text(90,25,'Y')
display.draw_text(90,37,'Z')
display.draw_text(82,1,'MMA7660')
display.draw_text(1,1,'DHT11Tem')
display.draw_text(1,13,'DHT11Hum')
display.draw_text(1,25,'18b20Tem')
display.draw_text(1,37,'ADCvol')
#display.set_pixel(1,1,True)
while True:
display.draw_text(55,37,str(round(adc.read()/4096,1)*3.3))
try:
display.draw_text(105,13,str(accel.x()))
display.draw_text(105,25,str(accel.y()))
display.draw_text(105,37,str(accel.z()))
except :
display.draw_text(105,13,'NC')
display.draw_text(105,25,'NC')
display.draw_text(105,37,'NC')
try:
roms = ds.scan()#扫描总线上的设备
ds.convert_temp()#获取采样温度
for rom in roms:
tem1=ds.read_temp(rom)
display.draw_text(55,25,str(round(tem1,1)))
except :
display.draw_text(55,25,'NC')
try:
dht.measure()
#print("temperature:",dht.temperature())
#print("humidity:",dht.humidity())
display.draw_text(55,1,str(round(dht.temperature(),1)))
display.draw_text(55,13,str(round(dht.humidity(),1)))
except :
display.draw_text(55,1,'NC')
display.draw_text(55,13,'NC')
display.display()
time.sleep_ms(750)
except Exception as ex:
print('Unexpected error: {0}'.format(ex))
#pyb.hard_reset()
#display.poweroff()
if __name__ == '__main__':
_thread.start_new_thread(funcA, (1,))
_thread.start_new_thread(funcB, (2,))
_thread.start_new_thread(funcC, (3,))