首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

基于Arduino的LPD6803 LED驱动器(含源码)

基于Arduino的LPD6803 LED驱动器(含源码)

#Include
#Include "LPD6803.H"
Int InByte;
Int DataPin = 2;       // 'Yellow' Wire
Int ClockPin = 3;      // 'Green' Wire
LPD6803 Strip = LPD6803(5, DataPin, ClockPin);
Void Setup() {
  Serial.Begin(9600);
  Strip.SetCPUmax(50);  // Start With 50% CPU Usage. Up This If The Strand Flickers Or Is Slow
  Strip.Begin();
  Strip.Show();
}
Void Loop() {
   If(Serial.Available()){
    InByte = Serial.Read();
    Switch(InByte){
    Case 0x1:
      Runrunrun(Color(31, 0, 0), 50);
      Break;
    Case 0x2:
      Runrunrun(Color(62, 0, 0), 50);
      Break;
    Case 0x3:
      Runrunrun(Color(93, 0, 0), 50);
      Break;
    Case 0x11:
      ColorWipe(Color(63, 0, 0), 50);
      ColorWipe(Color(0, 63, 0), 50);
      ColorWipe(Color(0, 0, 63), 50);
      Break;
    Case 0x22:
      Rainbow(50);
      Break;
    Case 0x33:
      RainbowCycle(50);
      Break;
    }  
  }
}

Void Rainbow(Uint8_t Wait) {
  Int I, J;

  For (J=0; J < 96 * 3; J++) {     // 3 Cycles Of All 96 Colors In The Wheel
    For (I=0; I < Strip.NumPixels(); I++) {
      Strip.SetPixelColor(I, Wheel( (I + J) % 96));
    }  
    Strip.Show();   // Write All The Pixels Out
    Delay(Wait);
  }
}

// Slightly Different, This One Makes The Rainbow Wheel Equally Distributed
// Along The Chain
Void RainbowCycle(Uint8_t Wait) {
  Int I, J;

  For (J=0; J < 96 * 5; J++) {     // 5 Cycles Of All 96 Colors In The Wheel
    For (I=0; I < Strip.NumPixels(); I++) {
      // Tricky Math! We Use Each Pixel As A Fraction Of The Full 96-Color Wheel
      // (Thats The I / Strip.NumPixels() Part)
      // Then Add In J Which Makes The Colors Go Around Per Pixel
      // The % 96 Is To Make The Wheel Cycle Around
      Strip.SetPixelColor(I, Wheel( ((I * 96 / Strip.NumPixels()) + J) % 96) );
    }  
    Strip.Show();   // Write All The Pixels Out
    Delay(Wait);
  }
}

// Fill The Dots One After The Other With Said Color
// Good For Testing Purposes
Void Runrunrun(Uint16_t C, Uint8_t Wait) {

}
Void ColorWipe(Uint16_t C, Uint8_t Wait) {
  Int I;

  For (I=0; I < Strip.NumPixels(); I++) {
      Strip.SetPixelColor(I, C);
      Strip.Show();
      Delay(Wait);
  }
}

/* Helper Functions */

// Create A 15 Bit Color Value From R,G,B
Unsigned Int Color(Byte R, Byte G, Byte B)
{
  //Take The Lowest 5 Bits Of Each Value And Append Them End To End
  Return( ((Unsigned Int)G & 0x1F )<<10 | ((Unsigned Int)B & 0x1F)<<5 | (Unsigned Int)R & 0x1F);
}

//Input A Value 0 To 127 To Get A Color Value.
//The Colours Are A Transition R - G -B - Back To R
Unsigned Int Wheel(Byte WheelPos)
{
  Byte R,G,B;
  Switch(WheelPos >> 5)
  {
    Case 0:
      R=31- WheelPos % 32;   //Red Down
      G=WheelPos % 32;      // Green Up
      B=0;                  //Blue Off
      Break;
    Case 1:
      G=31- WheelPos % 32;  //Green Down
      B=WheelPos % 32;      //Blue Up
      R=0;                  //Red Off
      Break;
    Case 2:
      B=31- WheelPos % 32;  //Blue Down
      R=WheelPos % 32;      //Red Up
      G=0;                  //Green Off
      Break;
  }
  Return(Color(R,G,B));
}
Terminal Software: Coolterm [Link]

Reference:

Timer Page [Link]

All LPD6803 Lib Files [Link]

返回列表