今夜无眠,写此博客!

我们先来看时序图!

#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 误差正负150ns
void 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 误差正负150ns
void 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码转RGB
void 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); // BluetheaterChase(Color(0,255,0),80); // BluetheaterChase(Color(255,0,0),80); // BluetheaterChaseRainbow(40);colorWipe(255,255);}
}


以上是全部代码

更多内容请关注微信公众号

STC15单片机驱动WS2812相关推荐

  1. STC单片机驱动WS2812不同颜色变化显示

    STC单片机驱动WS2812不同颜色变化显示

  2. 【Proteus仿真】STC15单片机+LCD1602驱动显示时间(DEMO)示例

    [Proteus仿真]STC15单片机+LCD1602驱动显示时间(DEMO)示例 Proteus仿真 Proteus加载 STC15单片机说明 在Proteus里面唯一能找到的 STC15一款支持仿 ...

  3. 51系列驱动WS2812

    51驱动WS2812B三色流水灯 WS2812简介 模组选型 关于WS2812的时序解析 发送数据格式解析 部分代码 功能现象 总结 原文链接:https://www.yourcee.com/news ...

  4. STC15单片机实战项目 - 项目需求

    以经典的51内核单片机设计一款产品,功能如下: 1.采用宏晶的STC15L2K32S2-LQFP32,2k SRAM,32k ROM: 目的:选用STC15系列1T的经典51内核单片机,资源丰富. 2 ...

  5. STC15F104W驱动WS2812

    STC15F104W驱动WS2812 提示:学习目标:了解WS2812的驱动原理,写出驱动代码. 内容: 提示:这里可以添加要学的内容 例如: 1. 连接WS2812时序: 2. 基于STC15F10 ...

  6. STC15单片机-GPIO模式介绍以及LED灯闪烁

    GPIO 1.说明 GPIO,英文全称 General-purpose input/output,即通用型输入输出,是单片机中最常用的外设之一. 2.设置模式 ​ STC15系列单片机最多有62个I/ ...

  7. 【STC15单片机】 超声波模块的使用

    目录 1 基于STC15F2K60S2的超声波测距代码 1.1 基本注意事项 1.1.1 跳线帽接法 1.1.2 晶振设置 1.2 板载超声波工作原理 1.2.1 原理总结 1.2.2 超声波代码思路 ...

  8. ② ESP8266 开发学习笔记_By_GYC 【ESP8266 驱动 ws2812 三原色灯(spi方式 稳定灯光)】

    目录 ② ESP8266 开发学习笔记_By_GYC [ESP8266 驱动 ws2812 三原色灯(spi方式 稳定灯光)] 一.驱动ws2812遇到的问题 二.可能的方案 三.具体实现 四.测试程 ...

  9. MS51FB9AE驱动WS2812

    新塘单片机MS51FB9AE驱动WS2812 文章目录 新塘单片机MS51FB9AE驱动WS2812 一.原理图 二.代码讲解 三.资料整理 这个模块可以和所有单片机进行通讯,既可以用iic也可以用串 ...

最新文章

  1. js垃圾回收机制和引起内存泄漏的操作
  2. python如何将生成的随机数存入文件中_用python在excel中读取与生成随机数写入excel中...
  3. MAC OS中的dylib 的@rpath和@loader_path小问题
  4. 线性代数学习资料汇编
  5. [原]HAproxy 代理技术原理探究
  6. python程序的书写特点_不一样的Python代码写法,让你写出一手漂亮的代码
  7. js中执行到一个if就停止的代码_Node 中如何引入一个模块及其细节
  8. 如何用c语言从txt文件中读取数据
  9. 两边填上相同的数_二年级必考题,在括号里填上相同的数~
  10. mysql的几种模式_MYSQL复制的几种模式
  11. windows php apc 安装,php-apc 安装
  12. delete table 和 truncate table
  13. day02.1 爬取豆瓣网电影信息
  14. Spring 概念模型 : PathMatcher 路径匹配器
  15. Linux平台下快速搭建FTP服务器
  16. 【MATLAB】 01 基本操作与数组输入
  17. 将map转换成json字符串
  18. 排毒一年,长寿十年,排出毒素,一身轻松 要美容 先排毒
  19. Open edX数据结构Mysql edxapp
  20. spring如何注入作用域不同的bean

热门文章

  1. 将人工智能整合进区块链
  2. LPDDR5 之Link ECC2
  3. Web流量检测与绕过(基于Snort规则)
  4. 数据库中心服务器配置,海关总署信息中心南方分中心数据库服务器配置.doc
  5. TM4C123-TivaWare中函数名与函数指针在ROM中的映射
  6. canon相机镜头协议
  7. php表单的交互(post方法)
  8. 设置差分对的两种方法
  9. 文言文不好玩,16岁高中生开发粤语编程项目
  10. 线性规划简单理解(LP问题)