<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:wfw="http://wellformedweb.org/CommentAPI/">
<channel>
<title>小夏的猪窝 - MicroPython</title>
<link>https://blog.x-tools.top/category/micropython/</link>
<atom:link href="https://blog.x-tools.top/feed/category/micropython/" rel="self" type="application/rss+xml" />
<language>zh-CN</language>
<description>MicroPython  ，是Python3编程语言的一个完整软件实现，用C语言编写，被优化于运行在微控制器之上。</description>
<lastBuildDate>Sun, 05 Jun 2022 13:59:00 +0000</lastBuildDate>
<pubDate>Sun, 05 Jun 2022 13:59:00 +0000</pubDate>
<item>
<title>MicroPython - 代码整合</title>
<link>https://blog.x-tools.top/archives/28/</link>
<guid>https://blog.x-tools.top/archives/28/</guid>
<pubDate>Sun, 05 Jun 2022 13:59:00 +0000</pubDate>
<dc:creator>小夏</dc:creator>
<description><![CDATA[&lt;!-- [toc] --&gt;本文仅针对与STM32F405芯片有用OLED显示导入引用头文件，使用压缩包中的ssd1306.py文件、font.py文件初始化oled= SSD130...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>&lt;!-- [toc] --&gt;</p><blockquote>本文仅针对与STM32F405芯片有用</blockquote><h1>OLED显示</h1><ol><li>导入引用头文件，使用压缩包中的<code>ssd1306.py</code>文件、<code>font.py</code>文件</li><li><p>初始化</p><pre><code class="lang-python">oled= SSD1306(pinout={'dc': 'Y9','res': 'Y10'},
               height=64,
               external_vcc=False)</code></pre></li><li><p>启动OLED，并且初始化</p><pre><code class="lang-python">oled.poweron()        #启动OLED
oled.init_display()    #显示函数初始化</code></pre></li><li><p>向OLED写字符串</p><blockquote>定义<code>def draw_text(self, x, y, string, size=1, space=1):</code></blockquote><pre><code class="lang-python">oled.draw_text(100,1,'hello world',size=1,space=1)  # 显示hello world</code></pre></li><li>刷新显示<code>oled.display()</code></li><li>清屏<code>oled.clear()</code></li></ol><h1>光照数值检测</h1><ol><li><p>初始化引脚配置</p><pre><code class="lang-python">Light = pyb.ADC('Y11')  #初始化ADC,Pin='Y11'</code></pre></li><li><p>读取ADC值</p><pre><code class="lang-python">value = Light.read()  #获取ADC数值</code></pre></li><li><p>写入计算的电压值，获得的数据0-4095相当于0-3V</p><pre><code class="lang-python">string='{:.2f}V'.format(value/4095*3.3)  # 字符串格式化
oled.draw_text(1, 20, string, size=2, space=1)  # OLED上显示</code></pre></li><li><p>判断光照强度，分3档写入显示函数</p><pre><code class="lang-python"> #判断光照强度，分3档写入显示函数
 if 0&lt;value&lt;=1365:
  oled.draw_text(80, 1, 'Bright', size=1, space=1)
 if 1365 &lt; value &lt;= 2730:
  oled.draw_text(80, 1, 'Normal', size=1, space=1)
 if 2730 &lt; value &lt;= 4095:
  oled.draw_text(80, 1, 'Weak', size=1, space=1)</code></pre></li></ol><h1>温湿度获取</h1><ol><li>导入温湿度驱动<code>from dht import DHT11</code>在附件中有此文件</li><li><p>创建DHT11对象dt</p><pre><code class="lang-python">dt = DHT11(Pin('X5'))</code></pre></li><li><p>发送一次数值获取信号，等待接受数值</p><blockquote>此函数方法如果未成功会抛出异常，这里需要将异常抛出</blockquote><pre><code class="lang-python">try:
 dt.measure()  # 检查异常
except Exception as e:
 print(e)</code></pre></li><li><p>读取温湿度的值，存放到te、dh中即可</p><pre><code class="lang-python">te = dt.temperature()  # 获取温度值
dh = dt.humidity()  # 获取湿度值</code></pre></li></ol><h1>串口的接收</h1><ol><li>引用pyb库中的UART <code>from pyb import UART</code></li><li>初始化一个串口 波特率为9600 <code>uart = UART(1,baudrate=9600)</code></li><li><p>接收函数：</p><pre><code class="lang-python"> # 判断是否有接收到任何数据
 if(uart.any()):
     text = uart.readline()  # 读取一行数据
     cmd = text.decode('ascii')  # 转换编码
     if('202' in cmd):  # 202是不是在刚刚读取的一行中
         print('OK'+' '+cmd)  # 是 就输出'OK '+cmd的一行
     else:
         print('ERROR')  #不 就输出ERROR</code></pre></li></ol><h1>所有整合例子</h1><p><br></p><h2>时间日期显示</h2><pre><code class="lang-python">#时间、日期、星期的显示
import pyb
from ssd1306 import SSD1306

#创建ssd1306的SPI方式的实体
oled= SSD1306(pinout={'dc': 'Y9','res': 'Y10'},
                  height=64,
                  external_vcc=False)

oled.poweron()        #启动OLED
oled.init_display()    #显示函数初始化

rtc = pyb.RTC()  #实例化RTC
rtc.datetime((2021, 4, 6, 2, 13, 4, 30, 250))  #初始化时钟

week = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']    #建立星期列表
supp = ['']*7

while True:
    temp = rtc.datetime()   #获取时间值

    for i in range(7):
        if(temp[i] &lt; 10):
            supp[i]='0'
        else:
            supp[i]=''

    oled.draw_text(1,1,supp[0]+str(temp[0])+'-'+supp[1]+str(temp[1])+'-'+supp[2]+str(temp[2]),
                    size=1,space=1)  #显示日期

    oled.draw_text(100,1,week[temp[3]-1],size=1,space=1) #显示星期
    oled.draw_text(1,35,supp[4]+str(temp[4])+':'+supp[5]+str(temp[5])
                    +':'+supp[6]+str(temp[6]),size=3,space=1)
    oled.display()
    #延时一段时间后清屏
    pyb.delay(500)
    oled.clear()</code></pre><h2>光照强度OLED显示</h2><pre><code class="lang-python">import pyb
from ssd1306 import SSD1306

#创建ssd1306的SPI方式的实体
oled = SSD1306(pinout={'dc': 'Y9',
                          'res': 'Y10'},
                  height=64,
                  external_vcc=False)

#启动OLED屏并初始化
oled.poweron()
oled.init_display()

rtc = pyb.RTC()  #实例化RTC
rtc.datetime((2021, 5, 9, 1, 11, 10, 30, 250))  #初始化时钟

Light = pyb.ADC('Y11')  #初始化ADC,Pin='Y11'
supp = ['']*7
while True:
    temp = rtc.datetime()   #获取时间值
    for i in range(7):
        if(temp[i] &lt; 10):
            supp[i]='0'
        else:
            supp[i]=''
    oled.draw_text(1,44,supp[0]+str(temp[0])+'-'+supp[1]+str(temp[1])+'-'+supp[2]+str(temp[2]),
                size=1,space=1)  #显示日期
    oled.draw_text(70,44,supp[4]+str(temp[4])+':'+supp[5]+str(temp[5])
                    +':'+supp[6]+str(temp[6]),size=1,space=1)

    value = Light.read()  #获取ADC数值

    #写入读取到的ADC数值
    oled.draw_text(1, 1, str(value)+'/'+'4095', size=1, space=1)

    #写入计算的电压值，获得的数据0-4095相当于0-3V
    string='{:.2f}V'.format(value/4095*3.3)  #字符串格式化
    oled.draw_text(1, 20, string, size=2, space=1)

    #判断光照强度，分3档写入显示函数
    if 0&lt;value&lt;=1365:
        oled.draw_text(80, 1, 'Bright', size=1, space=1)
    if 1365 &lt; value &lt;= 2730:
        oled.draw_text(80, 1, 'Normal', size=1, space=1)
    if 2730 &lt; value &lt;= 4095:
        oled.draw_text(80, 1, 'Weak', size=1, space=1)

    #先将写入的内容显示出来，然后清除一边进入下一次数据刷新
    oled.display()
    oled.clear()</code></pre><h2>温湿度OLED显示</h2><pre><code class="lang-python">import pyb
from ssd1306 import SSD1306

# 导入温湿模块
from pyb import Pin
from dht import DHT11

# 创建ssd1306的SPI方式的实体
oled = SSD1306(pinout={'dc': 'Y9',
                       'res': 'Y10'},
               height=64,
               external_vcc=False)

# 启动OLED屏并初始化
oled.poweron()
oled.init_display()

# 创建DHT11对象dt
dt = DHT11(Pin('X5'))

while True:
    try:
        dt.measure()  # 检查异常
    except Exception as e:
        print(e)

    te = dt.temperature()  # 获取温度值
    dh = dt.humidity()  # 获取湿度值

    # 写入读取到的数据
    oled.draw_text(1, 1, str(te) + ' C', size=2, space=1)
    oled.draw_text(1, 40, str(dh) + ' %', size=2, space=1)

    oled.display()  # 在OLED屏上显示出写入的内容

    # 延时一段时间后清屏
    pyb.delay(500)
    oled.clear()</code></pre><h2>温湿度光照信息串口输出</h2><pre><code class="lang-python">from pyb import RTC, delay, ADC, Timer, UART
from ssd1306 import SSD1306
import dht

dht = dht.DHT11(pin='X5')

oled = SSD1306(pinout={'dc': 'Y9',
                       'res': 'Y10'},
               height=64,
               external_vcc=False)

Light = ADC('Y11')  #初始化ADC,Pin='Y11'

rtc = RTC()  #实例化RTC
rtc.datetime((2021, 5, 23, 1, 10, 20, 30, 250))         #初始化时钟
ls = []
l0 = &quot;&quot;
l1 = &quot;&quot;
l2 = &quot;&quot;
# 启动OLED屏并初始化
oled.poweron()
oled.init_display()

# 串口输出定时器
uart = UART(1, 9600)
tim1 = Timer(1, freq=1)

def timer_fun(T):
    global l0, l1, l2
    uart.write(l0)
    uart.write(l1)
    uart.write(l2)
    uart.write('\r\n')
    print(l0)

tim1.callback(timer_fun)
# tim1.deinit()
# tim2 = Timer(2, freq=1)
while True:
    temp = 0                         # 温度
    hum = 0                          # 湿度
    light = 0                        # 光照度

    # 时间
    dtDict = ['year','month','day','week','hour','minute','second','subsecond']
    dttime = rtc.datetime()
    dtDict = {x:y for x,y in zip(dtDict,list(dttime))}  # 合并字典
    for x,y in dtDict.items():       # 遍历补齐两位
        dtDict[x] = str(y) if y &gt; 9 else &quot;0&quot;+str(y)

    # 温湿度
    try:
        dht.measure()                # 检查异常
    except Exception as e:
        pass
    temp = dht.temperature()         # 获取温度值
    hum = dht.humidity()             # 获取湿度值
    
    # 光照度
    value = Light.read()
    light = str(value)+'/4095'      # 获取ADC数值


    # 整合字符串
    l0 = dtDict['year']+'.'+dtDict['month']+'.'+dtDict['day']+' '+dtDict['hour']+':'+dtDict['minute']+':'+dtDict['second']
    l1 = &quot;temp:&quot;+str(temp)+&quot;C  hum:&quot;+str(hum)+&quot;%&quot;
    l2 = light+&quot;V&quot;

    if 0&lt;value&lt;=1365:            # 判断光照强度，分3档写入显示函数
        l2 = light+&quot;V   Bright&quot;
    if 1365 &lt; value &lt;= 2730:
        l2 = light+&quot;V   Normal&quot;
    if 2730 &lt; value &lt;= 4095:
        l2 = light+&quot;V   Weak&quot;

    # print(l0)
    # print(l1)
    # print(l2)
    # print(&quot;&quot;)

    # 显示
    oled.draw_text(1, 1, l0, size=1, space=1)
    oled.draw_text(1, 26, l1, size=1, space=1)
    oled.draw_text(1, 50, l2, size=1, space=1)

    oled.display()             # 在OLED屏上显示出写入的内容</code></pre><h2>串口接受例子</h2><pre><code class="lang-python">from pyb import UART, LED
uart = UART(1,baudrate=9600)
led1 = LED(1)
while True:
    if(uart.any()):
        text = uart.readline()
        cmd = text.decode('ascii')
        if('202' in cmd):
            print('OK'+' '+cmd)
        else:
            print('ERROR')</code></pre>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://blog.x-tools.top/archives/28/#comments</comments>
<wfw:commentRss>https://blog.x-tools.top/feed/category/micropython/</wfw:commentRss>
</item>
</channel>
</rss>