LED灯渐变色程序框架

1.程序初始化,设定RGB颜色比例。

PwmTarVal[0] = RPwm; 
PwmTarVal[1] = GPwm;
PwmTarVal[2] = BPwm;

2.时序1ms程序渐变色效果,PWM输出程序。

for(i=0;i<3;i++)
{
  if(PwmTarVal[i] != PwmCurVal[i])
  {
    if(PwmTarVal[i] > PwmCurVal[i])
    {
      PwmCurVal[i]++;
    }
    else if(PwmTarVal[i] < PwmCurVal[i])
    {
      PwmCurVal[i]--;
    }
  }
}
Pwm0Init(1000,PwmCurVal[0]);
Pwm1Init(1000,PwmCurVal[1]);
Pwm2Init(1000,PwmCurVal[2]);

3.扩张为7彩渐变色
只需定时更新PwmTarVal[i],RGB颜色比例达到7彩渐变的效果

if(Mode5TimCnt > 200)
{
  Mode5TimCnt = 0;
  Mode5Step++;
  if(Mode5Step >6)
  {
    Mode5Step = 0;
  }
}
if(Mode5Step == 0)
{
  RPwm = 1000;    //红
  GPwm = 0;
  BPwm = 0;
}
else if(Mode5Step == 1)
{
  RPwm = 0;       //绿
  GPwm = 1000;
  BPwm = 0;
}
else if(Mode5Step == 2)
{
  RPwm = 0;       //蓝
  GPwm = 0;
  BPwm = 1000;
}
else if(Mode5Step == 3)
{                   //橙
  RPwm = 1000;       
  GPwm = 647;
  BPwm = 0;
}
else if(Mode5Step == 4)
{                   //黄
  RPwm = 1000;       
  GPwm = 1000;
  BPwm = 0;
}
else if(Mode5Step == 5)
{
  RPwm = 0;       
  GPwm = 1000;
  BPwm = 1000;                  //青
}
else if(Mode5Step == 6)
{
  RPwm = 627;       
  GPwm = 125;
  BPwm = 941;                  //紫
}
posted @ 2023-07-06 15:26  steven_lg  阅读(371)  评论(0编辑  收藏  举报