【Beetle ESP32-C3】几个micropython的例子
<div class='showpostmsg'><p>下载了micropython固件,就可以轻松开始测试。先通过几个小程序测试一下:</p><p> </p>
<p>闪灯</p>
<p> </p>
<pre>
<code class="language-python">from machine import Pin
from time import sleep_ms
LED = Pin(10, Pin.OUT)
while 1:
LED(not LED())
sleep_ms(500)</code></pre>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> 本帖最后由 dcexpert 于 2022-8-4 11:00 编辑
<p>呼吸灯(通过定时器改变PWM占空比)</p>
<p><br />
</p>
<pre>
<code class="language-python">from machine import Pin, Timer, PWM
LED = PWM(Pin(10))
n = 0
def breathing(t):
global n
LED.duty(abs(1023- n*32))
n = (n + 1) % 64
T0 = Timer(0)
T0.init(period=20, mode=Timer.PERIODIC, callback=breathing)</code></pre>
<p> </p>
本帖最后由 dcexpert 于 2022-8-4 11:00 编辑
<p>引脚电平中断(改变引脚上的电平,触发中断,下面使用IO0)</p>
<p><br />
</p>
<pre>
<code class="language-python">from machine import Pin
LED = Pin(10, Pin.OUT)
def pin_irq(t):
LED(not LED())
p0 = Pin(0, Pin.IN, pull=Pin.PULL_UP)
p0.irq(trigger = Pin.IRQ_FALLING, handler=pin_irq)</code></pre>
<p> </p>
本帖最后由 dcexpert 于 2022-8-4 10:59 编辑
<p>定时器</p>
<p><br />
</p>
<pre>
<code class="language-python">from machine import Pin, Timer
LED = Pin(10, Pin.OUT)
tm =Timer(0)
tm.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:LED(not LED()))</code></pre>
<p> </p>
<p>目前的micropython固件在esp32c3上存在bug,定时器的时间是实际时间2倍。</p>
本帖最后由 dcexpert 于 2022-8-4 10:59 编辑
<p>ADC</p>
<p><br />
</p>
<pre>
<code class="language-python">from machine import ADC, Pin
from time import sleep_ms
led = Pin(10, Pin.OUT)
adc = ADC(Pin(3))
cnt = 0
while 1:
print(cnt, adc.read_uv())
led(not led())
cnt+=1
sleep_ms(500)</code></pre>
<p> </p>
本帖最后由 dcexpert 于 2022-8-4 10:58 编辑
<p>串口(串口可以使用任意IO,下面程序使用IO5发送,IO7接收)</p>
<p><br />
</p>
<pre>
<code class="language-python">from machine import Pin, UART
from time import sleep_ms
led = Pin(10, Pin.OUT)
uart = UART(1, tx=5, rx=7)
cnt = 1
while 1:
uart.write(str(cnt))
if uart.any():
print('>', uart.read())
cnt+=1
led(not led())
sleep_ms(500)</code></pre>
<p> </p>
本帖最后由 dcexpert 于 2022-8-4 10:58 编辑
<p>RTC</p>
<p><br />
</p>
<pre>
<code class="language-python">from machine import RTC
from time import sleep_ms
rtc = RTC()
while 1:
print(rtc.datetime())
sleep_ms(1000)</code></pre>
<p> </p>
<p>异步(多任务)</p>
<p> </p>
<pre>
<code class="language-python">import uasyncio as asyncio
from machine import Pin
import time
LED = Pin(10, Pin.OUT)
async def blink():
while True:
LED(not LED())
await asyncio.sleep(1)
async def show():
cnt = 1
T1 = time.ticks_us()
while True:
T2 = time.ticks_us()
print(cnt, time.ticks_diff(T2, T1))
T1 = T2
cnt += 1
await asyncio.sleep(2)
asyncio.create_task(blink())
asyncio.create_task(show())
asyncio.Loop.run_forever()
</code></pre>
<p>
<p> </p>
</p>
<p>SHT30温湿度传感器</p>
<p> </p>
<p>为了省事,直接将SHT30传感器插到开发板。因为GND引脚顺序同,因此使用IO0做GND,此外 SDA=IO1,SCL=IO4。使用SoftI2C模式。</p>
<p> </p>
<p> </p>
<p> </p>
<pre>
<code class="language-python">from machine import Pin, SoftI2C
Pin(0, Pin.OUT, value=0)
from time import sleep_ms
class SHT3x():
def __init__(self, i2c, addr = 68):
self.i2c = i2c
self._addr = addr
self.ver = '1.0'
self.info = 'SHT3x I2C drive for micropython'
self._mode = 1
self._cmd = 0x240b
self._delay = 6
self._decimal = 1
self._tb = bytearray(2)
self._rb = bytearray(3)
self._ht = bytearray(6)
self._T = 0
self._H = 0
self.reset()
def status(self):
self.write(0xf32d)
self.i2c.readfrom_into(self._addr, self._rb)
return self._rb*256 + self._rb
def clear_status(self):
self.write(0x3041)
def write(self, cmd):
self._tb = cmd>>8
self._tb = cmd
self.i2c.writeto(self._addr, self._tb)
def reset(self):
self.write(0x30a2)
def heater(self, on=0):
if on: self.write(0x306d)
else:self.write(0x3066)
def config(self, mode = 0x240b, delay = 6, decimal = 1):
t = mode >> 8
self._mode = 1 if t == 0x24 or t == 0x2C else 0
self._cmd = mode
self._delay = delay
self._decimal = decimal
self.write(mode)
def measure(self):
if self._mode:
self.write(self._cmd)
sleep_ms(self._delay)
self.write(0xe000)
self.i2c.readfrom_into(self._addr, self._ht)
self._T = self._ht*256+self._ht
self._H = self._ht*256+self._ht
def humidity(self):
return round(100*self._H/65535, self._decimal)
def temperature(self):
return round(175*self._T/65535 - 45, self._decimal)
def ht(self):
return self.humidity(), self.temperature()
i2c= SoftI2C(sda=Pin(1), scl=Pin(4))
sh = SHT3x(i2c)
sleep_ms(10)
while 1:
sh.measure()
print(sh.ht())
sleep_ms(1000)</code></pre>
<p> </p>
页:
[1]