« Nucleo-MicroPythonにNeoPixelを接続する | トップページ | Turtorial Examples for ESP8266-MicroPython »

2020年3月 9日 (月)

Turtorial Examples for Nucleo-MicroPython

2020/3/9

Turtorial Examples for Nucleo-MicroPython

Nucleo-F446RE

Nucleo-F446RE pin layout

import pyb while True: # LED(1) // Green LED on board pyb.LED(1).on() pyb.delay(1000) pyb.LED(1).off() pyb.delay(250)

Pin/GPIO

from machine import Pin led = Pin('PA5', Pin.OUT_PP) # Green LED on board sw = Pin('PC13', Pin.IN, Pin.PULL_UP) # USER SW on board while True: led.value(0 == sw.value()) # invert

PWM

from pyb import Timer from time import sleep from machine import Pin a0 = Pin('A0') # TIM2/CH1 tim = Timer(2, freq=1000) ch = tim.channel(1, Timer.PWM, pin=a0) for p in range(100): ch.pulse_width_percent(p) sleep(0.01) for p in range(100): ch.pulse_width_percent(100-p) sleep(0.01)

ADC

from machine import Pin, ADC from time import sleep # use A1 for analog input (Nucleo_F446RE) #a1 = ADC(Pin('A1')) # こっちでも動作する a1 = ADC('A1') while True: print('a1:{0:>8}'.format(a1.read_u16())) sleep(0.1)

以下のようにREPLで実行してみるとA0が使用不可なことが分かる:

>>> from machine import Pin, ADC >>> a0 = ADC(Pin('A0')) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: Pin(A0) does not have ADC capabilities >>> a1 = ADC(Pin('A1')) >>> a2 = ADC(Pin('A2')) >>> a3 = ADC(Pin('A3')) >>> a4 = ADC(Pin('A4')) >>> a5 = ADC(Pin('A5')) >>> Pin('A1') Pin(Pin.cpu.A1, mode=Pin.ANALOG) >>> Pin('A0') Pin(Pin.cpu.A0, mode=Pin.IN) >>> # Pin(Pin.cpu.A0, mode=Pin.IN) # A0 は mode=Pin.ANALOG になっていない。

SPI

TFT_SD_test_Nucleo.py

# TFT Touch Shield(with SD) # (does not support Touch) # setup display module import ili9341 color565 = ili9341.color565 from machine import Pin, SPI spi = SPI(miso=Pin('D12'), mosi=Pin('D11', Pin.OUT), sck=Pin('D13', Pin.OUT)) #spi=SPI(1) # does not work? (error: EIO) display = ili9341.ILI9341(spi, cs=Pin('D5'), dc=Pin('D6'), rst=Pin('D10')) # setup SDCard module import machine, sdcard, os sd = sdcard.SDCard(spi, Pin('D4')) os.mount(sd, '/sd') # setup path import sys sys.path.append('/sd') sys.path.append('/sd/lib') # display test display.fill(color565(0xff, 0x11, 0x22)) display.fill(color565(0xff, 0xff, 0xff)) display.fill(color565(0x00, 0x00, 0x00)) display.pixel(120, 160, color565(0xff,0,0)) display.pixel(121, 161, color565(0,0,0xff)) display.pixel(123, 163, color565(0,0xff,0)) display.text('Hello World!',0,0,color=color565(0,0xff,00)) display.text('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz{}()+*<>?_',100,200,color=color565(0,0,0xff)) display.fill_rectangle(100,150,20, 50, color565(0xff,0,0)) # SD Card test display.fill(color565(0x00, 0x00, 0xff)) display.text(str(sys.path),0,0,color565(0xff,0xff,0xff),clear_eol=True) display.text(str(os.listdir('/sd')),0,0,color565(0xff,0xff,0xff),clear_eol=True)

I2C

Software I2C:

Nucleo_TMP102_softI2C_test.py

# Nucleo import utime from machine import Pin, I2C from tmp102 import Tmp102 i2c = I2C(sda=Pin('D14'),scl=Pin('D15')) sensor = Tmp102(i2c, 0x48) while True: print('Temperature: {0:.1f}'.format(sensor.temperature)) utime.sleep(2)

Hardware I2C:
Nucleo_TMP102_test.py

# Nucleo import utime from machine import Pin, I2C from tmp102 import Tmp102 i2c = I2C(1) sensor = Tmp102(i2c, 0x48) while True: print('Temperature: {0:.1f}'.format(sensor.temperature)) utime.sleep(2)

RTC

from machine import RTC rtc = RTC() rtc.datetime((2020, 3, 9, 1, 8, 53, 0, 0)) # set a specific date and time # The 8-tuple has the following format: # (year, month, day, weekday, hours, minutes, seconds, subseconds) # weekday is 1-7 for Monday through Sunday. # subseconds counts down from 255 to 0 rtc.datetime() # get date and time

NeoPixel

module install:

git clone https://github.com/JanBednarik/micropython-ws2812.git cd micropython-ws2812 ampy put ws2812.py

sample1:

# connect the following pin to DIN of nexpixels # D11 (if spi_bus=1) or D4 (if spi_bus=3) from ws2812 import WS2812 chain = WS2812(spi_bus=3, led_count=4) data = [ (255, 0, 0), # red (0, 255, 0), # greenp (0, 0, 255), # blue (85, 85, 85), # white ] chain.show(data)

sample2:

from time import sleep import math from ws2812 import WS2812 ring = WS2812(spi_bus=3, led_count=16, intensity=0.1) def data_generator(led_count): data = [(0, 0, 0) for i in range(led_count)] step = 0 while True: red = int((1 + math.sin(step * 0.1324)) * 127) green = int((1 + math.sin(step * 0.1654)) * 127) blue = int((1 + math.sin(step * 0.1)) * 127) data[step % led_count] = (red, green, blue) yield data step += 1 for data in data_generator(ring.led_count): ring.show(data) sleep(0.01)

DHT

STM32_demo_DHT11.py

# F446RE from dht import DHT11 from machine import Pin from time import sleep d = DHT11(Pin('D5')) while True: d.measure() tempe=d.temperature() humi=d.humidity() print('tempe: {0:.1f} humi:{1:0.1f}'.format(tempe, humi)) sleep(1)

Networking

N.A.

Performance Test

performanceTest.py

# Peformace Test def performanceTest(): millis = pyb.millis endTime = millis() + 10000 count = 0 while millis() < endTime: count += 1 print("Count: ", count) performanceTest()

以上

|

« Nucleo-MicroPythonにNeoPixelを接続する | トップページ | Turtorial Examples for ESP8266-MicroPython »

linux」カテゴリの記事

MicroPython」カテゴリの記事

Nucleo」カテゴリの記事

コメント

コメントを書く



(ウェブ上には掲載しません)




« Nucleo-MicroPythonにNeoPixelを接続する | トップページ | Turtorial Examples for ESP8266-MicroPython »