【得捷电子Follow me第1期】任务2:驱动外设
[复制链接]
驱动led
led闪烁
from machine import Pin
import time
led = Pin('LED',Pin.OUT)
while True:
led.toggle()
time.sleep(0.5)
驱动OLED
使用ssd1306库
from machine import Pin, I2C
i2c1 = I2C(1,sda=Pin(6), scl=Pin(7))
from ssd1306 import SSD1306_I2C
oled = SSD1306_I2C(128, 64, i2c1)
import time
oled.fill(1)
oled.show()
time.sleep(1.0)
oled.fill(0)
oled.show()
time.sleep(1.0)
oled.text('Hello', 0, 0)
oled.text('World', 0, 10)
oled.show()
驱动蜂鸣器
使用gpio16引脚
from machine import Pin, PWM
import utime
pwm0 = PWM(Pin(16)) # create PWM object from a pin
pwm0.freq() # get current frequency
pwm0.freq(1000) # set frequency
pwm0.duty_u16() # get current duty cycle, range 0-65535
pwm0.duty_u16(1000) # set duty cycle, range 0-65535
utime.sleep(1.0)
pwm0.deinit() # turn off PWM on the pin
|