久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4323|回復: 0
打印 上一主題 下一主題
收起左側

MicroPython實例之TPYBoard v102炫彩跑馬燈WS2812B

[復制鏈接]
跳轉到指定樓層
樓主
    一、實驗目的

    了解ws2812b的工作原理
    學習ws2812b的驅動方法

    二、實驗器材

    TPYBoard v102 1塊
    ws2812b RGB-Ring-8 1個
    micro USB數據線 1條
    杜邦線 若干

    三、WS2812B的介紹

    WS2812B是一個集控制電路與發光電路于一體的智能外控LED光源。 其外型與一個5050LED燈珠相同, 每個元件即為一個像素點。像素點內部包含了智能數字接口數據鎖存信號整形放大驅動電路, 還包含有高精度的內部振蕩器和可編程定電流控制部分, 有效保證了像素點光的顏色高度一致。

    數據協議采用單線歸零碼的通訊方式, 像素點在上電復位以后, DIN端接受從控制器傳輸過來的數據, 首先送過來的24bit數據被第一個像素點提取后, 送到像素點內部的數據鎖存器, 剩余的數據經過內部整形處理電路整形放大后通過DO端口開始轉發輸出給下一個級聯的像素點, 每經過一個像素點的傳輸, 信號減少24bit。像素點采用自動整形轉發技術, 使得該像素點的級聯個數不受信號傳送的限制, 僅僅受限信號傳輸速度要求。

    實物圖
   

    上圖是8個燈珠的。
    WS2812B的引腳說明:


    硬件連接
    將TPYBoard v102與WS2812B的接線示意圖,如下:


    程序源碼如下:
  1. import   pyb
  2. import   math
  3.   
  4. from   ws2812 import WS2812
  5.   
  6.   
  7. ring   = WS2812(spi_bus=1, led_count=8, intensity=0.1)
  8.   
  9.   
  10. def   data_generator(led_count):
  11.     data = [(0, 0, 0) for i in   range(led_count)]
  12.     step = 0
  13.     while True:
  14.         red = int((1 + math.sin(step *   0.1324)) * 127)
  15.         green = int((1 + math.sin(step *   0.1654)) * 127)
  16.         blue = int((1 + math.sin(step * 0.1))   * 127)
  17.         data[step % led_count] = (red, green,   blue)
  18.         yield data
  19.         step += 1
  20.   
  21.   
  22. for   data in data_generator(ring.led_count):
  23.     ring.show(data)
  24.     pyb.delay(100)
復制代碼




    里面還需要引入一個ws2812.py 文件。內容如下:
  1. import   gc
  2. import   pyb
  3.   
  4.   
  5. class   WS2812:
  6.     """
  7.     Driver for WS2812 RGB LEDs. May be used   for controlling single LED or chain
  8.     of LEDs.
  9.   
  10.     Example of use:
  11.   
  12.         chain = WS2812(spi_bus=1,   led_count=4)
  13.         data = [
  14.             (255, 0, 0),    # red
  15.             (0, 255, 0),    # green
  16.             (0, 0, 255),    # blue
  17.             (85, 85, 85),   # white
  18.         ]
  19.         chain.show(data)
  20.   
  21.     Version: 1.0
  22.     """
  23.     buf_bytes = (0x11, 0x13, 0x31, 0x33)
  24.   
  25.     def __init__(self, spi_bus=1,   led_count=1, intensity=1):
  26.         """
  27.         Params:
  28.         * spi_bus = SPI bus ID (1 or 2)
  29.         * led_count = count of LEDs
  30.         * intensity = light intensity (float   up to 1)
  31.         """
  32.         self.led_count = led_count
  33.         self.intensity = intensity
  34.   
  35.         # prepare SPI data buffer (4 bytes   for each color)
  36.         self.buf_length = self.led_count * 3   * 4
  37.         self.buf = bytearray(self.buf_length)
  38.   
  39.         # SPI init
  40.         self.spi = pyb.SPI(spi_bus,   pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=1)
  41.   
  42.         # turn LEDs off
  43.         self.show([])
  44.   
  45.     def show(self, data):
  46.         """
  47.         Show RGB data on LEDs. Expected data   = [(R, G, B), ...] where R, G and B
  48.         are intensities of colors in range   from 0 to 255. One RGB tuple for each
  49.         LED. Count of tuples may be less than   count of connected LEDs.
  50.         """
  51.         self.fill_buf(data)
  52.         self.send_buf()
  53.   
  54.     def send_buf(self):
  55.         """
  56.         Send buffer over SPI.
  57.         """
  58.         self.spi.send(self.buf)
  59.         gc.collect()
  60.   
  61.     def update_buf(self, data, start=0):
  62.         """
  63.         Fill a part of the buffer with RGB   data.
  64.   
  65.         Order of colors in buffer is changed   from RGB to GRB because WS2812 LED
  66.         has GRB order of colors. Each color   is represented by 4 bytes in buffer
  67.         (1 byte for each 2 bits).
  68.   
  69.         Returns the index of the first unfilled   LED
  70.   
  71.         Note: If you find this function ugly,   it's because speed optimisations
  72.         beated purity of code.
  73.         """
  74.   
  75.         buf = self.buf
  76.         buf_bytes = self.buf_bytes
  77.         intensity = self.intensity
  78.   
  79.         mask = 0x03
  80.         index = start * 12
  81.         for red, green, blue in data:
  82.             red = int(red * intensity)
  83.             green = int(green * intensity)
  84.             blue = int(blue * intensity)
  85.   
  86.             buf[index] = buf_bytes[green   >> 6 & mask]
  87.             buf[index+1] = buf_bytes[green   >> 4 & mask]
  88.             buf[index+2] = buf_bytes[green   >> 2 & mask]
  89.             buf[index+3] = buf_bytes[green   & mask]
  90.   
  91.             buf[index+4] = buf_bytes[red   >> 6 & mask]
  92.             buf[index+5] = buf_bytes[red   >> 4 & mask]
  93.             buf[index+6] = buf_bytes[red   >> 2 & mask]
  94.             buf[index+7] = buf_bytes[red   & mask]
  95.   
  96.             buf[index+8] = buf_bytes[blue   >> 6 & mask]
  97.             buf[index+9] = buf_bytes[blue   >> 4 & mask]
  98.             buf[index+10] = buf_bytes[blue   >> 2 & mask]
  99.             buf[index+11] = buf_bytes[blue   & mask]
  100.   
  101.             index += 12
  102.   
  103.         return index // 12
  104.   
  105.     def fill_buf(self, data):
  106.         """
  107.         Fill buffer with RGB data.
  108.   
  109.         All LEDs after the data are turned   off.
  110.         """
  111.         end = self.update_buf(data)
  112.   
  113.         # turn off the rest of the LEDs
  114.         buf = self.buf
  115.         off = self.buf_bytes[0]
  116.         for index in range(end * 12,   self.buf_length):
  117.             buf[index] = off
  118.             index += 1
復制代碼




    本次參考的github上的一個項目。項目地址:
    https://github.com/JanBednarik/micropython-ws2812
    給大家看一下效果(額 最后一個燈珠壞了 大家可以自行忽略……)
    https://v.qq.com/x/page/d05297wxo1b.html

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 成年人精品视频在线观看 | 一区二区三区国产精品 | 亚洲a视频 | 精品视频一区二区三区四区 | 亚洲国产福利视频 | 中文字幕不卡视频在线观看 | 久久久久久国产精品免费免费狐狸 | 国内精品视频 | 国产成人区 | 综合另类| 亚洲一区中文字幕 | 亚洲协和影视 | 网站黄色在线免费观看 | 国产精品免费福利 | 国产精品久久久久久久久污网站 | 激情免费视频 | 久久久精品视频免费 | 视频一区二区中文字幕 | 99精品视频在线 | 欧美二区三区 | 国产99久久精品一区二区永久免费 | 九色 在线 | 日韩成人精品在线 | 国产色婷婷| 日韩精品成人在线 | 一区二区国产精品 | 精品视频一区二区 | 国产精品视频网 | 毛片视频免费观看 | 欧美午夜精品 | 99精品免费视频 | www国产成人免费观看视频,深夜成人网 | 日韩精品中文字幕一区二区三区 | 国产色网站 | 亚洲欧美日韩在线不卡 | 国产95在线 | 欧美一区二区三区在线观看 | 国产亚洲日本精品 | 久草在线 | 国产精品99视频 | 天堂综合网久久 |