2024年10月6日 星期日

用Raspberry Pi Pico學Python

教材:Getting started with Raspberry Pi Pico

MicroPython for Pico:Quick reference for the RP2

範例一:LED閃爍控制

1
2
3
4
5
6
7
from machine import Pin
import time
led = Pin(26, Pin.OUT)

while True:
    led.toggle()
    time.sleep(1)

範例二:採用硬體計時器

1
2
3
4
5
6
7
8
from machine import Pin, Timer
led = Pin(26, Pin.OUT)
timer = Timer()

def blink(timer):
    led.toggle()

timer.init(freq=1, mode=Timer.PERIODIC, callback=blink)

範例三、輸入輸出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from machine import Pin
import time

led = Pin(26, Pin.OUT)
button = Pin(27, Pin.IN, Pin.PULL_DOWN)

while True:
    if button.value():
        led.toggle()
        time.sleep(0.5)


沒有留言:

張貼留言