STC15单片机驱动WS2812
来源:互联网 发布:家装erp软件 编辑:程序博客网 时间:2024/06/06 00:28
今夜无眠,写此博客!
我们先来看时序图!
#include <STC15F2K60S2.H>#include "intrins.h"sbit WS2812 = P1^7;#define numLEDs 8 //灯的个数unsigned char buf_R[numLEDs] = {0};//颜色缓存unsigned char buf_G[numLEDs] = {0};unsigned char buf_B[numLEDs] = {0};void RGB_Set_Up(); //送0码void RGB_Set_Down(); //送1码void HAL_Delay(unsigned int t){unsigned int x,y; for(x=114;x>0;x--) for(y=t;y>0;y--);} //复位延时void Delay50us()//@22.1184MHz{unsigned char i, j;_nop_();_nop_();i = 2;j = 15;do{while (--j);} while (--i);}//1码,高电平850ns 低电平400ns 误差正负150nsvoid RGB_Set_Up(){WS2812 = 1; //经过逻辑分析仪调试的的延时_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();_nop_(); WS2812 = 0;}//1码,高电平400ns 低电平850ns 误差正负150nsvoid RGB_Set_Down(){WS2812 = 1; //经过逻辑分析仪调试的的延时_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); WS2812 = 0;}//发送24位数据void Send_2811_24bits(unsigned char G8,unsigned char R8,unsigned char B8){ unsigned int n = 0; //发送G8位for(n=0;n<8;n++){G8<<=n;if(G8&0x80 == 0x80){RGB_Set_Up();}else { RGB_Set_Down();}}//发送R8位for(n=0;n<8;n++){R8<<=n;if(R8&0x80 == 0x80){RGB_Set_Up();}else {RGB_Set_Down();}}//发送B8位 for(n=0;n<8;n++){B8<<=n;if(B8&0x80 == 0x80){RGB_Set_Up();}else { RGB_Set_Down();}}}//复位码void RGB_Rst(){WS2812 = 0;Delay50us();}//把24位数据GRB码转RGBvoid Set_Colour(unsigned char r,unsigned char g,unsigned char b){ unsigned char i;for(i=0;i<numLEDs;i++) {buf_R[i] = r; //缓冲 buf_G[i] = g; buf_B[i] = b;}for(i=0;i<numLEDs;i++){Send_2811_24bits(buf_G[i],buf_R[i],buf_B[i]);//发送显示}}//某一个点显示的颜色void SetPointColour(unsigned int num,unsigned char r,unsigned char g,unsigned char b){ unsigned char i;for(i=0;i<numLEDs;i++) {buf_R[num] = r;//缓冲 buf_G[num] = g; buf_B[num] = b;}for(i=0;i<numLEDs;i++){Send_2811_24bits(buf_G[i],buf_R[i],buf_B[i]);//发送显示}}//颜色交换24位不拆分发void SetPixelColor(unsigned char num,unsigned long c){ unsigned char i;for(i=0;i<numLEDs;i++) {buf_R[num] = (unsigned char)(c>>16); buf_G[num] = (unsigned char)(c>>8); buf_B[num] = (unsigned char)(c);}for(i=0;i<numLEDs;i++){Send_2811_24bits(buf_G[i],buf_R[i],buf_B[i]);}}//复位void PixelUpdate(){RGB_Rst();}//颜色unsigned long Color(unsigned char r, unsigned char g, unsigned char b){ return ((unsigned long)r << 16) | ((unsigned long)g << 8) | b;}//颜色算法unsigned long Wheel(unsigned char WheelPos){ WheelPos = 255 - WheelPos; if(WheelPos < 85) { return Color(255 - WheelPos * 3, 0, WheelPos * 3); } if(WheelPos < 170) { WheelPos -= 85; return Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return Color(WheelPos * 3, 255 - WheelPos * 3, 0);}//彩虹void rainbow(unsigned int wait){ unsigned int i, j; for(j=0; j<256; j++) { for(i=0; i<numLEDs; i++){ SetPixelColor(i, Wheel((i+j) & 255)); }PixelUpdate(); HAL_Delay(wait); }}//稍微不同的是,这使得彩虹均匀分布void rainbowCycle(unsigned int wait) { unsigned int i, j; for(j=0;j<256*5;j++) { // 5 cycles of all colors on wheel 车轮上所有颜色的5个循环 for(i=0;i<numLEDs;i++) { SetPixelColor(i, Wheel(((i * 256 / numLEDs) + j) & 255)); } PixelUpdate(); HAL_Delay (wait); }}//Theatre-style crawling lights.呼吸灯void theaterChase(unsigned long c, unsigned int wait) {int j,q;unsigned int i; for (j=0; j<10; j++) { //do 10 cycles of chasing 做10个循环 for (q=0; q < 3; q++) { for (i=0; i<numLEDs; i=i+3){ SetPixelColor(i+q, c); //turn every third pixel on 把每一个第三个像素 }PixelUpdate(); HAL_Delay(wait); for (i=0; i<numLEDs; i=i+3) { SetPixelColor(i+q, 0); //turn every third pixel off 把每一个第三个像素关掉 }PixelUpdate(); } }}//Theatre-style crawling lights with rainbow effect//带有彩虹效果的戏剧式爬行灯void theaterChaseRainbow(unsigned int wait) {int j,q;unsigned int i; for (j=0; j < 256; j++) { // cycle all 256 colors in the wheel 在轮子上循环所有256色 for (q=0; q < 3; q++){ for (i=0; i < numLEDs; i=i+3) { SetPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel off 把每一个第三个像素 } PixelUpdate(); HAL_Delay(wait); for (i=0; i < numLEDs; i=i+3){ SetPixelColor(i+q, 0); //turn every third pixel off 把每一个第三个像素关掉 } } }}// Fill the dots one after the other with a color//用一种颜色填充这些圆点void colorWipe(unsigned long c, unsigned int wait) {unsigned int i=0; for( i=0; i<numLEDs; i++) { SetPixelColor(i, c); PixelUpdate(); HAL_Delay(wait); }}void main(){ while(1){ rainbow(45); rainbowCycle(40); theaterChase(Color(0,0,255),80); // Blue theaterChase(Color(0,255,0),80); // Blue theaterChase(Color(255,0,0),80); // Blue theaterChaseRainbow(40);colorWipe(255,255);}}
以上是全部代码
阅读全文
0 0
- STC15单片机驱动WS2812
- stc15系列单片机 timer组件
- STC15系列单片机命名规则
- STC15系列单片机-PWM模块
- STC15系列单片机IAP15F2K61S2 矩阵键盘实验
- STC15系列单片机-环境监控设备
- STC15系列单片机 超声波测距实验
- 基于STC15单片机的DHT11采样
- 《STC15单片机实战指南》_分享
- STC15单片机6路专用PWM
- 对STC15系列单片机EEPROM使用感受
- STC15系列单片机-I/O口小结
- STC15系列单片机检测不到,下载不成功
- NUCLEO-F042K6驱动的彩色灯环(WS2812)
- STC15系列单片机检测不到,下载不成…
- 关于蓝桥杯STC15单片机的18B20程序
- 2015.07.25 STC15单片机学习日记-时钟与复位
- 2015.07.26 STC15单片机学习日记-储存器结构
- #求助#搞到现在凌晨3点,卡到这上面了【error C2248: “CObject::CObject”: 无法访问 private 成员(在“CObject”类中声明)】
- logstash与filebeat收集日志
- 【Python数据分析与展示】(四)pandas库基本操作
- JAVA高并发学习(一)
- 你或许理解错了Android系统权限管理的这两个概念
- STC15单片机驱动WS2812
- 12.12 IE7275-Data Mining笔记
- 多进程、进程池
- 如何成功配置opencv3 sfm模块?
- ios-消息转发机制
- spring boot整合freemarker出现Whitelabel Error Page问题
- 数据库SQL语句
- TensorFlow-抽取模型某一层特征
- 论组件enable属性的妙处——unity