STM32 Code Examples
ตัวอย่างโค้ดสาธิต CircuitPython สำหรับบอร์ดไมโครคอนโทรลเลอร์ STM32
โค้ดตัวอย่างที่ 1 PWM-based RGB LED Dimming
import time
import board
import math
import pwmio
###########################################################
class PWMArray:
def __init__(self,pwm_objects):
self.pwm_objects = pwm_objects
def __iter__(self):
self.index = 0
return self
def __next__(self):
current = self.index
self.index = (self.index+1) % len(pwm_objects)
return pwm_objects[current]
###########################################################
led_pins = [board.A0, board.A1, board.A2]
pwm_objects = [
pwmio.PWMOut(pin=led_pin,duty_cycle=65535,frequency=500)
for led_pin in led_pins ]
# create an iterator for the list of three PWMOut objects
pwm_iter = iter(PWMArray(pwm_objects))
# create a table of (N+1) sine-wave values
N=128
sine_int = lambda i: int(65535*(1-math.sin(math.pi*i/N)))
values = [sine_int(i) for i in range(N+1)]
try:
level = 0 # 0..N
pwm = next(pwm_iter) # select the first PWM output
t = int(1000*time.monotonic()) # time in msec
while True:
now = int(1000*time.monotonic()) # time in msec
if now - t < 15:
continue
t = now # update timestamp
pwm.duty_cycle = values[level]
if level == N:
level = 0
pwm = next(pwm_iter) # select next PWM output
else:
level += 1
except KeyboardInterrupt:
pass
for pwm in pwm_objects:
pwm.deinit()
print('Done')
โค้ดตัวอย่างที่ 2: RGB LED + Push Button

โค้ดตัวอย่างที่ 3: PCF8574 I/O Expander (Output)

โค้ดตัวอย่างที่ 4: PCF8574 I/O Expander (Input)

Last updated