DAC
from pyb import DAC,Switch
from machine import Pin,I2C
from ssd1306 import SSD1306_I2C
i2c = I2C(sda=Pin("Y8"), scl=Pin("Y6"))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
sw =Switch()
dac = DAC(1)
freq=[1,200,1000,5000]
buf = bytearray(2)
buf[0]=0
buf[1]=255
key_node = 0
i = 0
def key():
global key_node
key_node = 1
sw.callback(key)
oled.fill(0)
oled.text('DAC-Beep', 0, 15)
oled.text('Pls Press USER', 0, 40)
oled.show()
while True:
if key_node==1:
i = i+1
if i == 4:
i = 0
key_node = 0
dac.write_timed(buf, freq[i]*len(buf), mode=DAC.CIRCULAR)
oled.fill(0)
oled.text(str(freq[i]) + 'Hz', 0, 40)
oled.show()
光敏
import pyb
from machine import Pin,I2C
from ssd1306 import SSD1306_I2C
i2c = I2C(sda=Pin("Y8"), scl=Pin("Y6"))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
Light = pyb.ADC('Y11')
while True:
oled.fill(0)
oled.text('Light test:', 0, 15)
value=Light.read()
oled.text(str(value)+' (4095)',0,40)
oled.text(str('%.2f'%(value/4095*3.3))+' V',0,55)
if 0 < value <=1365:
oled.text('Bright', 60, 55)
if 1365 < value <= 2730:
oled.text('Normal', 60, 55)
if 2730 < value <= 4095:
oled.text('Weak ', 60, 55)
oled.show()
pyb.delay(1000)
ADC
import pyb
from machine import Pin,I2C
from ssd1306 import SSD1306_I2C
i2c = I2C(sda=Pin("Y8"), scl=Pin("Y6"))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
adc = pyb.ADC('X7')
while True:
oled.fill(0)
oled.text(str(adc.read()),0,40)
oled.text('(4095)',40,40)
oled.text(str('%.2f'%(adc.read()/4095*3.3)),0,55)
oled.text('V',40,55)
oled.show()
pyb.delay(1000)
显示屏
from machine import I2C,Pin
from ssd1306 import SSD1306_I2C
i2c = I2C(sda=Pin("Y8"), scl=Pin("Y6"))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
oled.text("Hello World!", 0, 0)
oled.text("MicroPython", 0, 20)
oled.text("By 01Studio", 0, 50)
oled.show()