找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8372|回复: 5
收起左侧

那位牛人能把arduino程序翻译成stc51单片机程序(51就可以)

[复制链接]
ID:73366 发表于 2015-2-6 21:57 | 显示全部楼层
  1. //Jeremy Blum's Arduino Tutorial Series - Episode 14 - Lights and Sound Holiday Special
  2. //Sample Code 2 - Driving all 50 LEDs using an array powered by both stereo channels
  3. //WS2801 Library and Helper Functions by Adafruit
  4. /*
  5.   The circuit:
  6. * A0 from Amplififed Left Channel
  7. * A1 from Amplified Right Channel
  8. * Digital 2 to Light Clock Line
  9. * Digital 3 to Light Data Line
  10. * Don't forget to connect all your grounds!
  11. */

  12. //SPI Library
  13. #include "SPI.h"

  14. //RGB LED Library
  15. //You can download it here: [url=https://github.com/adafruit/Adafruit-WS2801-Library/archive/master.zip]https://github.com/adafruit/Adaf ... /archive/master.zip[/url]
  16. //Install it in your library folder and restart the Arduino IDE
  17. #include "Adafruit_WS2801.h"

  18. //Analog Input Pins
  19. int left_channel = 0;
  20. int right_channel = 1;

  21. //Light Control Pins
  22. int light_data = 3;
  23. int light_clk = 2;

  24. //Set Strip Constants
  25. const int length = 50;
  26. const int half = length/2;

  27. //Library Setup
  28. Adafruit_WS2801 strip = Adafruit_WS2801(length, light_data, light_clk);

  29. //Set up arrays for cycling through all the pixels.  I'm assuming you have an even number of lights.
  30. uint32_t left_array[half];
  31. uint32_t right_array[half];

  32. void setup()
  33. {
  34.   //Fill pixel arrays with zeros
  35.   for(int i=0; i<half;i++)
  36.   {
  37.     left_array[i] = 0;
  38.     right_array[i] = 0;
  39.   }

  40.   //Initialize Strip
  41.   strip.begin();
  42.   strip.show();


  43. }

  44. void loop()
  45. {
  46.   //Set the hue (0-255) and 24-bit color depending on left channel value
  47.   byte hue_left = constrain(map(analogRead(left_channel), 0, 400, 0, 255), 0, 255);
  48.   uint32_t color_left = Wheel(hue_left);

  49.   //Set the hue (0-255) and 24-bit color depending on right channel value
  50.   byte hue_right = constrain(map(analogRead(right_channel), 0, 400, 0, 255), 0, 255);
  51.   uint32_t color_right = Wheel(hue_right);

  52.   //Shift the current values.
  53.   for (int i = 0; i<half-1; i++)
  54.   {
  55.     left_array[i] = left_array[i+1];
  56.     right_array[i] = right_array[i+1];
  57.   }

  58.   //Fill in the new value at the end of each array
  59.   left_array[half-1] = color_left;
  60.   right_array[half-1] = color_right;

  61.   //Go through each Pixel on the strip and set its color
  62.   for (int i=0; i<half; i++)
  63.   {
  64.     //set pixel color
  65.     strip.setPixelColor(i, left_array[i]);
  66.     strip.setPixelColor(length-i-1, right_array[i]);
  67.   }


  68.   //Display the new values
  69.   strip.show();

  70.   //sample delay
  71.   delay(40);

  72. }

  73. /* Helper functions */


  74. // Create a 24 bit color value from R,G,B
  75. uint32_t Color(byte r, byte g, byte b)
  76. {
  77.   uint32_t c;
  78.   c = r;
  79.   c <<= 8;
  80.   c |= g;
  81.   c <<= 8;
  82.   c |= b;
  83.   return c;
  84. }

  85. //Input a value 0 to 255 to get a color value.
  86. //The colours are a transition r - g -b - back to r
  87. uint32_t Wheel(byte WheelPos)
  88. {
  89.   if (WheelPos < 85) {
  90.    return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  91.   } else if (WheelPos < 170) {
  92.    WheelPos -= 85;
  93.    return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  94.   } else {
  95.    WheelPos -= 170;
  96.    return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  97.   }
  98. }

复制代码


评分

参与人数 1黑币 +50 收起 理由
admin + 50 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:73366 发表于 2015-2-11 10:47 | 显示全部楼层
看到项目结果有什么想法的都可以说。。。。。。。。。。比如,如何实现
回复

使用道具 举报

ID:62929 发表于 2015-6-13 10:50 来自手机 | 显示全部楼层
期待中
回复

使用道具 举报

ID:358930 发表于 2018-6-27 23:35 来自手机 | 显示全部楼层
简单点可以,这么多代码没时间了。可以重写51
回复

使用道具 举报

ID:62509 发表于 2020-5-9 01:54 | 显示全部楼层
这个确实有些复杂,需要一整套的辅助程序,要占用将近40k。arduino程序只需要改在void loop()前增加一个void main()即可完美运行。后来STC认为占用内存太多,效率降低,帮助AVR放弃51。所以意义不大。
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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