找回密码
 立即注册
查看: 1100|回复: 3

15W408AS驱动WS2812全彩LED

[复制链接]
  • 打卡等级:初来乍到
  • 打卡总天数:7
  • 最近打卡:2025-04-24 13:36:20

8

主题

39

回帖

342

积分

中级会员

积分
342
发表于 2023-12-20 08:51:27 | 显示全部楼层 |阅读模式
用15W408AS单片机驱动WS2812 ,使用内部晶振11.0592M.完全没问题。

WS2812.c

6.46 KB, 下载次数: 131

点亮WS2812彩灯_基于STC15W204S单片机.zip

37.29 KB, 下载次数: 166

回复

使用道具 举报 送花

  • 打卡等级:偶尔看看III
  • 打卡总天数:54
  • 最近打卡:2025-05-01 09:07:55

717

主题

1万

回帖

1万

积分

管理员

积分
15613
发表于 2024-1-9 08:46:22 | 显示全部楼层
SPI方案:
只用到MOSI驱动WS2812,但由于SPI的3个信号是捆绑在一起的,MISO、SCLK不能做它用。本例使用P1.3-MOSI输出驱动信号,用户可以切换到别的脚。
===将 MISO 不做其他用途;
===将 MISO管脚 先设置为对外输出0;
===MOSI 发送完成后,对外停止时输出的是 MISO 读到的状态, 0 ,这 STC-R&D 留了这么个绝活支持 WS2812B !!!

回复 支持 反对

使用道具 举报 送花

  • 打卡等级:初来乍到
  • 打卡总天数:1
  • 最近打卡:2024-09-04 14:38:43

0

主题

3

回帖

12

积分

新手上路

积分
12
发表于 2024-9-4 14:38:32 | 显示全部楼层
我是小白,改成了触摸一下,切换一下灯的颜色,但是第二个灯,第三个灯的通讯起来,DO口输出是一直5V,不知道怎么改
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:初来乍到
  • 打卡总天数:1
  • 最近打卡:2024-09-04 14:38:43

0

主题

3

回帖

12

积分

新手上路

积分
12
发表于 2024-9-4 14:48:18 | 显示全部楼层
代码是参考论坛另外一个人的。
  1. #include <stc15.h>
  2. #include <intrins.h>
  3. typedef struct
  4. {
  5.         unsigned char Value;
  6.         unsigned char Press;
  7.         unsigned short Count;
  8. }KEY_Type;
  9. void send_color(unsigned char r, unsigned char g, unsigned char b);
  10. void send_byte(unsigned char byte_val);
  11. void send_bit(unsigned char bit_val);
  12. void sw(unsigned char y);
  13. void update_led_strip();
  14. void Delay350us();
  15. void Delay1ms();
  16. void ledaa();      
  17. void ledbb();//红光闪烁
  18. void ledcc();
  19. void leddd();//绿光闪烁
  20. void ledee();//蓝光闪烁
  21. void ledObb();//红光常亮
  22. void ledOdd();//绿光常亮
  23. void ledOee();//蓝光常亮
  24. void ledOyy();//黄光常亮
  25. void Key_Read(void);
  26. void Key_Task(void);
  27. // 定义端口
  28. sbit LED = P3^4;
  29. sbit KEYP = P3^2;// 按键引脚
  30. unsigned int y;
  31. unsigned char Trg;
  32. unsigned char Cont;
  33. unsigned char flag = 0;
  34. unsigned char count = 0; // 按键计数器
  35. KEY_Type Key;
  36. unsigned char code led[]={
  37.         //7种颜色的光亮
  38.         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  39.         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  40.         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  41.         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  42.         0x00, 0x01, 0x02, 0x03,                   //32
  43.       
  44.         //红色的光亮
  45.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49.         0x00, 0x00, 0x00, 0x00,                  //64
  50.       
  51.         //绿色的光亮
  52.         0x07, 0x07, 0x01, 0x01, 0x07, 0x07, 0x01,
  53.         0x01, 0x07, 0x07, 0x01, 0x01, 0x07, 0x07,
  54.         0x01, 0x01, 0x07, 0x07, 0x01, 0x01, 0x07,
  55.         0x07, 0x01, 0x01, 0x07, 0x07, 0x01, 0x01,
  56.         0x07, 0x07, 0x07, 0x07,                  //96
  57.       
  58.         //蓝色的光亮
  59.         0x02, 0x02, 0x02, 0x02, 0x07, 0x07, 0x07,
  60.         0x07, 0x02, 0x02, 0x02, 0x02, 0x07, 0x07,
  61.         0x07, 0x07, 0x02, 0x02, 0x02, 0x02, 0x07,
  62.         0x07, 0x07, 0x07, 0x02, 0x02, 0x02, 0x02,
  63.         0x07, 0x07, 0x07, 0x07,                  //128
  64.       
  65.         //黄色的光亮
  66.         0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  67.         0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  68.         0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  69.         0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  70.         0x03, 0x03, 0x03, 0x03,                                                                                //160
  71.       
  72.         //洋红色的光亮
  73.         0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  74.         0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  75.         0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  76.         0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  77.         0x04, 0x04, 0x04, 0x04,                                                                         //192
  78.       
  79.         //青色的光亮
  80.         0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  81.         0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  82.         0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  83.         0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  84.         0x05, 0x05, 0x05, 0x05,                                                                         //224
  85.       
  86.         //白色的光亮
  87.         0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  88.         0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  89.         0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  90.         0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  91.         0x06, 0x06, 0x06, 0x06,                                                                         //256
  92.       
  93.         //不亮
  94.         0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  95.         0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  96.         0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  97.         0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  98.         0x07, 0x07, 0x07, 0x07                                                                                //288
  99. };
  100. // 主函数
  101. void main()
  102. {
  103.           P3M1 &= ~0x04;  // 设置P3.2为准双向口模式(输入模式)
  104.     P3M0 &= ~0x04;  // 确保P3.2不是开漏输出模式
  105. while(1) // 进入无限循环
  106.   {
  107.         // 读取按键状态
  108.         if (KEYP == 1) {  // 如果按键被按下
  109.             flag = 1;     // 设置标志变量为1
  110.             count = (count + 1) % 3; // 按键按下时增加计数器,并取模3以实现颜色切换
  111.         } else {
  112.             flag = 0;     // 如果按键未被按下,设置标志变量为0
  113.         }
  114.         
  115.         // 根据计数器的值决定LED灯带颜色
  116.         switch(count) {
  117.             case 0:
  118.                 ledObb(); // 红光常亮
  119.                 break;
  120.             case 1:
  121.                 ledOee(); // 蓝光常亮
  122.                 break;
  123.             case 2:
  124.                 ledOdd(); // 绿光常亮
  125.                 break;
  126.                                                 case 3:
  127.                 ledOyy(); // 黄光常亮
  128.                 break;
  129.         }
  130.     // 更新LED灯带
  131.     update_led_strip();
  132.   }
  133. }
  134. //遍历七彩,红,绿,蓝,黄,洋红,靑,白,不亮
  135. void ledaa()
  136. {
  137.     unsigned int n, i, j;
  138.                 for(n=0; n<256; n++)
  139.                 {
  140.                                  // 遍历每个LED灯珠
  141.                                 for(j=0; j<64; j++)
  142.                                 {
  143.                                                 //指定颜色值
  144.                                                 y=led[n+j];
  145.                                           sw(y);
  146.                                 }
  147.                                 // 更新LED灯带
  148.                                 update_led_strip();
  149.                                 // 延时
  150.                                 for(i=0; i<100; i++)
  151.                                 {
  152.                                         Delay1ms();
  153.                                 }
  154.                               
  155.                 }
  156. }
  157. //颜色值
  158. void sw(unsigned char y)
  159. {
  160.                         switch(y)
  161.                 {
  162.                         case 0: send_color(0xff, 0x00, 0x00); break;  //红
  163.                         case 1: send_color(0x00, 0xff, 0x00); break;  //绿
  164.                         case 2: send_color(0x00, 0x00, 0xff); break;  //蓝
  165.                         case 3: send_color(0xff, 0xff, 0x00); break;  //黄
  166.                         case 4: send_color(0xff, 0x00, 0xff); break;  //洋红
  167.                         case 5: send_color(0x00, 0xff, 0xff); break;  //靑
  168.                         case 6: send_color(0xff, 0xff, 0xff); break;  //白
  169.                         case 7: send_color(0x00, 0x00, 0x00); break;  //不亮
  170.                         default : break;
  171.                        
  172.                 }
  173. }
  174. //红光闪烁
  175. void ledbb()
  176. {
  177.         unsigned int a, b, c, i;      
  178.         for(a=0; a<10; a++)
  179.         {
  180.                         for(b=32; b<64; b++)
  181.                         {
  182.                                 y=led;
  183.                                 sw(y);
  184.                         }                       
  185.                         // 更新LED灯带
  186.                         update_led_strip();
  187.                         // 延时
  188.                         for(i=0; i<200; i++)
  189.                         {
  190.                                 Delay1ms();
  191.                         }                       
  192.                         for(c=256; c<289; c++)
  193.                         {
  194.                                 y=led[c];
  195.                                 sw(y);
  196.                         }               
  197.                         // 更新LED灯带
  198.                         update_led_strip();
  199.                         // 延时
  200.                         for(i=0; i<200; i++)
  201.                         {
  202.                                 Delay1ms();
  203.                         }
  204.         }
  205. }
  206. //蓝光闪烁
  207. void ledee()
  208. {
  209.         unsigned int a, b, c, i;      
  210.         for(a=0; a<10; a++)
  211.         {
  212.                         for(b=64; b<96; b++)
  213.                         {
  214.                                 y=led;
  215.                                 sw(y);
  216.                         }                       
  217.                         // 更新LED灯带
  218.                         update_led_strip();
  219.                         // 延时
  220.                         for(i=0; i<150; i++)
  221.                         {
  222.                                 Delay1ms();
  223.                         }                       
  224.                         for(c=256; c<289; c++)
  225.                         {
  226.                                 y=led[c];
  227.                                 sw(y);
  228.                         }               
  229.                         // 更新LED灯带
  230.                         update_led_strip();
  231.                         // 延时
  232.                         for(i=0; i<150; i++)
  233.                         {
  234.                                 Delay1ms();
  235.                         }
  236.         }
  237. }
  238. //绿光闪烁
  239. void leddd()
  240. {
  241.         unsigned int a, b, c, i;      
  242.         for(a=0; a<10; a++)
  243.         {
  244.                         for(b=96; b<128; b++)
  245.                         {
  246.                                 y=led;
  247.                                 sw(y);
  248.                         }                       
  249.                         // 更新LED灯带
  250.                         update_led_strip();
  251.                         // 延时
  252.                         for(i=0; i<200; i++)
  253.                         {
  254.                                 Delay1ms();
  255.                         }                       
  256.                         for(c=256; c<289; c++)
  257.                         {
  258.                                 y=led[c];
  259.                                 sw(y);
  260.                         }               
  261.                         // 更新LED灯带
  262.                         update_led_strip();
  263.                         // 延时
  264.                         for(i=0; i<200; i++)
  265.                         {
  266.                                 Delay1ms();
  267.                         }
  268.         }
  269. }
  270. // 红光常亮
  271. void ledObb()
  272. {
  273.     unsigned int b;
  274.    
  275.     // 点亮LED灯带的红光部分
  276.     for(b = 32; b < 64; b++)
  277.     {
  278.         y = led;
  279.         sw(y);
  280.     }
  281.     // 更新LED灯带
  282.     update_led_strip();
  283. }
  284. // 蓝光常亮
  285. void ledOee()
  286. {
  287.     unsigned int b;
  288.    
  289.     // 点亮LED灯带的绿光部分
  290.     for(b = 64; b < 96; b++)
  291.     {
  292.         y = led;
  293.         sw(y);
  294.     }
  295.     // 更新LED灯带
  296.     update_led_strip();
  297. }
  298. // 另一个绿光常亮(假设这部分代码也是用于常亮)
  299. void ledOdd()
  300. {
  301.     unsigned int b;
  302.    
  303.     // 点亮LED灯带的另一部分绿光
  304.     for(b = 96; b < 128; b++)
  305.     {
  306.         y = led;
  307.         sw(y);
  308.     }
  309.     // 更新LED灯带
  310.     update_led_strip();
  311. }
  312. // 黄光常亮(假设这部分代码也是用于常亮)
  313. void ledOyy()
  314. {
  315.     unsigned int b;
  316.    
  317.     // 点亮LED灯带的另一部分绿光
  318.     for(b = 128; b < 160; b++)
  319.     {
  320.         y = led;
  321.         sw(y);
  322.     }
  323.     // 更新LED灯带
  324.     update_led_strip();
  325. }
  326. // 洋红常亮(假设这部分代码也是用于常亮)
  327. void ledOyr()
  328. {
  329.     unsigned int b;
  330.    
  331.     // 点亮LED灯带的另一部分绿光
  332.     for(b = 160; b < 192; b++)
  333.     {
  334.         y = led;
  335.         sw(y);
  336.     }
  337.     // 更新LED灯带
  338.     update_led_strip();
  339. }
  340. //遍历不亮,白,靑,洋红,黄,蓝,绿,红,七彩
  341. void ledcc()
  342. {
  343.       unsigned int n, i, j;
  344.                 for(n=256; n>=0; n--)
  345.                 {
  346.                                                          // 遍历每个LED灯珠
  347.                                 //for(j=0; j<64; j++)
  348.                         for(j=0; j<96; j++)
  349.                                 {
  350.                                                 //指定颜色值
  351.                                                 y=led[n+j];
  352.                                           sw(y);
  353.                                 }
  354.                                 // 更新LED灯带
  355.                                 update_led_strip();
  356.                                 // 延时一秒
  357.                                 for(i=0; i<100; i++)
  358.                                 {
  359.                                         Delay1ms();
  360.                                 }                              
  361.                 }
  362. }
  363. // 发送一个比特
  364. void send_bit(unsigned char bit_val)
  365. {
  366.         if(bit_val)
  367.         {
  368.         // 发送逻辑1
  369.         LED = 1;
  370.         _nop_();_nop_();_nop_();
  371.         LED = 0;
  372.         _nop_();
  373.         }
  374.         else
  375.         {
  376.         // 发送逻辑0
  377.         LED = 1;
  378.         _nop_();
  379.         LED = 0;
  380.         _nop_();_nop_();_nop_();
  381.         }
  382. }
  383. // 发送一个字节
  384. void send_byte(unsigned char byte_val)
  385. {
  386.         unsigned char i;
  387.         for(i=0; i<8; i++)
  388.         {
  389.                 send_bit(byte_val & (0x80 >> i)) ;
  390.         }
  391. }
  392. // 发送颜色值
  393. void send_color(unsigned char r, unsigned char g, unsigned char b)
  394. {
  395.         send_byte(g);
  396.         send_byte(r);
  397.         send_byte(b);
  398. }
  399. // 更新LED灯带
  400. void update_led_strip()
  401. {
  402.         LED = 0;
  403.         Delay350us();
  404. }
  405. void Delay350us()                //@11.0592MHz
  406. {
  407.         unsigned char data i, j;
  408.         _nop_();
  409.         _nop_();
  410.         _nop_();
  411.         i = 4;
  412.         j = 192;
  413.         do
  414.         {
  415.                 while (--j);
  416.         } while (--i);
  417. }
  418. void Delay1ms()                //@11.0592MHz
  419. {
  420.         unsigned char data i, j;
  421.         _nop_();
  422.         _nop_();
  423.         _nop_();
  424.         i = 11;
  425.         j = 190;
  426.         do
  427.         {
  428.                 while (--j);
  429.         } while (--i);
  430. }
复制代码

回复 支持 反对

使用道具 举报 送花

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2025-5-2 04:13 , Processed in 0.179199 second(s), 69 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表