找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1133|回复: 3
打印 上一主题 下一主题
收起左侧

这个单片机程序老是说编译错误,哪位大神指点一下

[复制链接]
跳转到指定楼层
楼主
ID:735961 发表于 2020-10-4 20:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include <STC15F2K60S2.H>
  2. #include "intrins.h"

  3. sbit WS2812 = P1^7;

  4. #define numLEDs 8   //灯的个数
  5. unsigned char buf_R[numLEDs] = {0};//颜色缓存
  6. unsigned char buf_G[numLEDs] = {0};
  7. unsigned char buf_B[numLEDs] = {0};

  8. void RGB_Set_Up();  //送0码
  9. void RGB_Set_Down(); //送1码



  10. void HAL_Delay(unsigned int t)
  11. {
  12.                 unsigned int x,y;
  13.           for(x=114;x>0;x--)
  14.           for(y=t;y>0;y--);
  15. }


  16.          
  17. //复位延时
  18. void Delay50us()                //@22.1184MHz
  19. {
  20.         unsigned char i, j;

  21.         _nop_();
  22.         _nop_();
  23.         i = 2;
  24.         j = 15;
  25.         do
  26.         {
  27.                 while (--j);
  28.         } while (--i);
  29. }


  30. //1码,高电平850ns 低电平400ns 误差正负150ns
  31. void RGB_Set_Up()
  32. {
  33.                 WS2812 = 1;
  34.           //经过逻辑分析仪调试的的延时
  35.                 _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
  36.           _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();_nop_();
  37.                 WS2812 = 0;
  38. }

  39. //1码,高电平400ns 低电平850ns 误差正负150ns
  40. void RGB_Set_Down()
  41. {
  42.                 WS2812 = 1;
  43.           //经过逻辑分析仪调试的的延时
  44.                 _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();  
  45.                 WS2812 = 0;
  46. }

  47. //发送24位数据
  48. void Send_2811_24bits(unsigned char G8,unsigned char R8,unsigned char B8)
  49. {
  50.          
  51.           unsigned int n = 0;
  52.           //发送G8位
  53.                 for(n=0;n<8;n++)
  54.                 {
  55.                         G8<<=n;
  56.                         if(G8&0x80 == 0x80)
  57.                         {
  58.                                 RGB_Set_Up();
  59.                         }
  60.                         else  
  61.                         {
  62.                           RGB_Set_Down();
  63.                         }
  64.                 }
  65.                 //发送R8位
  66.                 for(n=0;n<8;n++)
  67.                 {
  68.                         R8<<=n;
  69.                         if(R8&0x80 == 0x80)
  70.                         {
  71.                                 RGB_Set_Up();
  72.                         }
  73.                         else  
  74.                         {
  75.                                 RGB_Set_Down();
  76.                         }
  77.                         
  78.                 }
  79.                 //发送B8位
  80.           for(n=0;n<8;n++)
  81.                 {
  82.                         B8<<=n;
  83.                         if(B8&0x80 == 0x80)
  84.                         {
  85.                                 RGB_Set_Up();
  86.                         }
  87.                         else  
  88.                         {
  89.                           RGB_Set_Down();
  90.                         }
  91.                 }
  92. }
  93. //复位码
  94. void RGB_Rst()
  95. {
  96.                 WS2812 = 0;
  97.                 Delay50us();
  98. }
  99. //把24位数据GRB码转RGB
  100. void Set_Colour(unsigned char r,unsigned char g,unsigned char b)
  101. {
  102.           unsigned char i;
  103.                 for(i=0;i<numLEDs;i++)
  104.           {
  105.                                 buf_R[i] = r; //缓冲
  106.                           buf_G[i] = g;
  107.                           buf_B[i] = b;
  108.                 }
  109.                 for(i=0;i<numLEDs;i++)
  110.                 {
  111.                         Send_2811_24bits(buf_G[i],buf_R[i],buf_B[i]);//发送显示
  112.                 }
  113. }
  114. //某一个点显示的颜色
  115. void SetPointColour(unsigned int num,unsigned char r,unsigned char g,unsigned char b)
  116. {
  117.           unsigned char i;
  118.                 for(i=0;i<numLEDs;i++)
  119.           {
  120.                                 buf_R[num] = r;//缓冲
  121.                           buf_G[num] = g;
  122.                           buf_B[num] = b;
  123.                 }
  124.                 for(i=0;i<numLEDs;i++)
  125.                 {
  126.                         Send_2811_24bits(buf_G[i],buf_R[i],buf_B[i]);//发送显示
  127.                 }
  128. }

  129. //颜色交换24位不拆分发
  130. void SetPixelColor(unsigned char num,unsigned long c)
  131. {
  132.           unsigned char i;
  133.                 for(i=0;i<numLEDs;i++)
  134.           {
  135.                                 buf_R[num] = (unsigned char)(c>>16);
  136.                           buf_G[num] = (unsigned char)(c>>8);
  137.                           buf_B[num] = (unsigned char)(c);
  138.                 }
  139.                 for(i=0;i<numLEDs;i++)
  140.                 {
  141.                         Send_2811_24bits(buf_G[i],buf_R[i],buf_B[i]);
  142.                 }
  143. }

  144. //复位
  145. void PixelUpdate()
  146. {
  147.         RGB_Rst();
  148. }
  149. //颜色
  150. unsigned long Color(unsigned char r, unsigned char g, unsigned char b)
  151. {
  152.   return ((unsigned long)r << 16) | ((unsigned long)g <<  8) | b;
  153. }

  154. //颜色算法
  155. unsigned long Wheel(unsigned char WheelPos)
  156. {
  157.   WheelPos = 255 - WheelPos;
  158.   if(WheelPos < 85)
  159.         {
  160.     return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  161.   }
  162.   if(WheelPos < 170) {
  163.     WheelPos -= 85;
  164.     return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  165.   }
  166.   WheelPos -= 170;
  167.   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  168. }

  169. //彩虹
  170. void rainbow(unsigned int wait)
  171. {
  172.   unsigned int i, j;

  173.   for(j=0; j<256; j++)
  174.         {
  175.     for(i=0; i<numLEDs; i++)
  176.                 {
  177.       SetPixelColor(i, Wheel((i+j) & 255));
  178.     }
  179.                 PixelUpdate();
  180.     HAL_Delay(wait);
  181.   }
  182. }

  183. //稍微不同的是,这使得彩虹均匀分布
  184. void rainbowCycle(unsigned int wait)
  185. {
  186.   unsigned int i, j;

  187.   for(j=0;j<256*5;j++)
  188.         { // 5 cycles of all colors on wheel  车轮上所有颜色的5个循环
  189.     for(i=0;i<numLEDs;i++)
  190.          {
  191.      SetPixelColor(i, Wheel(((i * 256 / numLEDs) + j) & 255));
  192.     }
  193.           PixelUpdate();
  194.     HAL_Delay (wait);
  195.   }
  196. }

  197. //Theatre-style crawling lights.呼吸灯
  198. void theaterChase(unsigned long c, unsigned int wait)
  199. {
  200.         int j,q;
  201.         unsigned int i;
  202.   for (j=0; j<10; j++)
  203.         {  //do 10 cycles of chasing  做10个循环
  204.     for (q=0; q < 3; q++)
  205.                 {
  206.       for (i=0; i<numLEDs; i=i+3)
  207.                         {
  208.         SetPixelColor(i+q, c);    //turn every third pixel on  把每一个第三个像素
  209.       }
  210.                         PixelUpdate();
  211.       HAL_Delay(wait);

  212.       for (i=0; i<numLEDs; i=i+3)
  213.                         {
  214.        SetPixelColor(i+q, 0);        //turn every third pixel off   把每一个第三个像素关掉
  215.       }
  216.                         PixelUpdate();
  217.     }
  218.   }
  219. }

  220. //Theatre-style crawling lights with rainbow effect
  221. //带有彩虹效果的戏剧式爬行灯
  222. void theaterChaseRainbow(unsigned int wait)
  223. {
  224.         int j,q;
  225.         unsigned int i;
  226.   for (j=0; j < 256; j++)
  227.         {     // cycle all 256 colors in the wheel 在轮子上循环所有256色
  228.     for (q=0; q < 3; q++)
  229.                 {
  230.       for (i=0; i < numLEDs; i=i+3)
  231.                         {
  232.         SetPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel off 把每一个第三个像素
  233.       }
  234.       PixelUpdate();

  235.       HAL_Delay(wait);

  236.       for (i=0; i < numLEDs; i=i+3)
  237.                         {
  238.         SetPixelColor(i+q, 0);        //turn every third pixel off  把每一个第三个像素关掉
  239.       }
  240.     }
  241.   }
  242. }

  243. // Fill the dots one after the other with a color
  244. //用一种颜色填充这些圆点
  245. void colorWipe(unsigned long c, unsigned int wait)
  246. {
  247.         unsigned int i=0;
  248.   for( i=0; i<numLEDs; i++)
  249.         {
  250.     SetPixelColor(i, c);
  251.     PixelUpdate();
  252.     HAL_Delay(wait);
  253.   }
  254. }

  255. void main()
  256. {
  257.          
  258.                 while(1)
  259.                 {
  260.                           rainbow(45);
  261.         rainbowCycle(40);
  262.         theaterChase(Color(0,0,255),80); // Blue
  263.                           theaterChase(Color(0,255,0),80); // Blue
  264.                           theaterChase(Color(255,0,0),80); // Blue
  265.                     theaterChaseRainbow(40);
  266.                                 colorWipe(255,255);
  267.                 }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:342822 发表于 2020-10-5 00:17 | 只看该作者
295 行加右花括弧
回复

使用道具 举报

板凳
ID:743654 发表于 2020-10-5 16:24 | 只看该作者
编译提示什么错误?同一楼 貌似主程序少了个}
回复

使用道具 举报

地板
ID:453974 发表于 2020-10-7 08:17 | 只看该作者
#include <reg52.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); // Blue
                          theaterChase(Color(0,255,0),80); // Blue
                          theaterChase(Color(255,0,0),80); // Blue
                    theaterChaseRainbow(40);
                                colorWipe(255,255);
                }
}
确实是缺了},这里面有警告,应该是有程序没调用吧?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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