完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
先甩锅 做完后才发现最后一个灯坏了,就坏了一个灯也不好意思去找淘宝店家,大家视频凑合着看把。不过并不影响实验效果。因为这个发光二极管白天不是很明显 晚上炫彩效果就能出来了。本次实验用的是8个灯珠,大家可以尝试更多用更多灯珠的,或者买灯带,那样呈现的效果会更酷、更炫!视频地址见评论。 实验器材(来自某宝,可惜没有实体店不然省很多麻烦) TPYBoard v102 1块 ws2812b RGB-Ring-8 1个 micro USB数据线 1条 杜邦线 若干 WS2812B是一个集控制电路与发光电路于一体的智能外控LED光源。 其外型与一个5050LED灯珠相同, 每个元件即为一个像素点。像素点内部包含了智能数字接口数据锁存信号整形放大驱动电路, 还包含有高精度的内部振荡器和可编程定电流控制部分, 有效保证了像素点光的颜色高度一致。 数据协议采用单线归零码的通讯方式, 像素点在上电复位以后, DIN端接受从控制器传输过来的数据, 首先送过来的24bit数据被第一个像素点提取后, 送到像素点内部的数据锁存器, 剩余的数据经过内部整形处理电路整形放大后通过DO端口开始转发输出给下一个级联的像素点, 每经过一个像素点的传输, 信号减少24bit。像素点采用自动整形转发技术, 使得该像素点的级联个数不受信号传送的限制, 仅仅受限信号传输速度要求。 实物图 上图是8个灯珠的。 WS2812B的引脚说明:
硬件连接 将TPYBoard v102与WS2812B的接线示意图,如下:
程序源码如下:
里面还需要引入一个ws2812.py 文件。内容如下:
|
|
相关推荐
5 个讨论
|
|
本次参考的github上的一个项目。项目地址:
/github.com/JanBednarik/micropython-ws2812[/url] 给大家看一下效果 /v.qq.com/x/page/d05297wxo1b.html[/url] ws2812.py 文件怎么变乱码了 import gc import pyb class WS2812: """ Driver for WS2812 RGB LEDs. May be used for controlling single LED or chain of LEDs. Example of use: chain = WS2812(spi_bus=1, led_count=4) data = [ (255, 0, 0), # red (0, 255, 0), # green (0, 0, 255), # blue (85, 85, 85), # white ] chain.show(data) Version: 1.0 """ buf_bytes = (0x11, 0x13, 0x31, 0x33) def __init__(self, spi_bus=1, led_count=1, intensity=1): """ Params: * spi_bus = SPI bus ID (1 or 2) * led_count = count of LEDs * intensity = light intensity (float up to 1) """ self.led_count = led_count self.intensity = intensity # prepare SPI data buffer (4 bytes for each color) self.buf_length = self.led_count * 3 * 4 self.buf = bytearray(self.buf_length) # SPI init self.spi = pyb.SPI(spi_bus, pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=1) # turn LEDs off self.show([]) def show(self, data): """ Show RGB data on LEDs. Expected data = [(R, G, B), ...] where R, G and B are intensities of colors in range from 0 to 255. One RGB tuple for each LED. Count of tuples may be less than count of connected LEDs. """ self.fill_buf(data) self.send_buf() def send_buf(self): """ Send buffer over SPI. """ self.spi.send(self.buf) gc.collect() def update_buf(self, data, start=0): """ Fill a part of the buffer with RGB data. Order of colors in buffer is changed from RGB to GRB because WS2812 LED has GRB order of colors. Each color is represented by 4 bytes in buffer (1 byte for each 2 bits). Returns the index of the first unfilled LED Note: If you find this function ugly, it's because speed optimisations beated purity of code. """ buf = self.buf buf_bytes = self.buf_bytes intensity = self.intensity mask = 0x03 index = start * 12 for red, green, blue in data: red = int(red * intensity) green = int(green * intensity) blue = int(blue * intensity) buf[index] = buf_bytes[green >> 6 & mask] buf[index+1] = buf_bytes[green >> 4 & mask] buf[index+2] = buf_bytes[green >> 2 & mask] buf[index+3] = buf_bytes[green & mask] buf[index+4] = buf_bytes[red >> 6 & mask] buf[index+5] = buf_bytes[red >> 4 & mask] buf[index+6] = buf_bytes[red >> 2 & mask] buf[index+7] = buf_bytes[red & mask] buf[index+8] = buf_bytes[blue >> 6 & mask] buf[index+9] = buf_bytes[blue >> 4 & mask] buf[index+10] = buf_bytes[blue >> 2 & mask] buf[index+11] = buf_bytes[blue & mask] index += 12 return index // 12 def fill_buf(self, data): """ Fill buffer with RGB data. All LEDs after the data are turned off. """ end = self.update_buf(data) # turn off the rest of the LEDs buf = self.buf off = self.buf_bytes[0] for index in range(end * 12, self.buf_length): buf[index] = off index += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
你正在撰写讨论
如果你是对讨论或其他讨论精选点评或询问,请使用“评论”功能。
991 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
970 浏览 2 评论
2080 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1177 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1599 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 16:17 , Processed in 0.803593 second(s), Total 54, Slave 44 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号