为什么我的wsb2812无法正常工作,最后一个LED不工作

时间:2019-01-05 03:45:12

标签: c stm32 stm32f1

我试图顺时针和逆时针点亮我的led灯。 我用MPU6050作为LED方向的控制。我检查了我的mpu6050驱动程序代码,它们还不错。 顺时针工作时效果很好,所有指示灯都以红色点亮。像这样 enter image description here 当它逆时针工作时,发生了一个问题 enter image description here

我尝试了许多方法来解决,但没有一个起作用。 这是我的领导司机代码

#include "../BOARD/ws2812/ws2812.h"
#include "bsp_usart1.h"
#include "delay.h"

uint8_t pixelBuffer[PIXEL_NUM][24] ;


void ws281x_init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  SPI_InitTypeDef  SPI_InitStructure;
  DMA_InitTypeDef DMA_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);    


  /* PA7  SPI1_MOSI */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //SPI-PA-7
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;  
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master; 
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;     
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;    
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;     
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;    
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;  
  SPI_Init(SPI1, &SPI_InitStructure);  

  SPI_Cmd(SPI1, ENABLE); 
  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);

  DMA_DeInit(DMA1_Channel3);   
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(SPI1 -> DR); 
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)pixelBuffer; //cmar
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;  
  DMA_InitStructure.DMA_BufferSize = PIXEL_NUM * 24; //cndtr;  
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; 
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 
  DMA_Init(DMA1_Channel3, &DMA_InitStructure); 

   ws281x_closeAll();  //SHUT DOWN ALL LEDS
   delay_ms(100); //SHUT DOWN DELAY
}

void ws281x_closeAll(void)
{
  uint16_t i;
  uint8_t j;

  for(i = 0; i < PIXEL_NUM; ++i)
  {

    for(j = 0; j < 24; ++j)
    {
      pixelBuffer[i][j] = WS_LOW;
    }
  }
  ws281x_show(); 
}

uint32_t ws281x_color(uint8_t red, uint8_t green, uint8_t blue)
{
  return green << 16 | red << 8 | blue;
}

void ws281x_setPixelColor(uint16_t n ,uint32_t GRBcolor)
{
  uint8_t i;
  if(n < PIXEL_NUM)
  {
    for(i = 0; i < 24; ++i)
    {
      pixelBuffer[n][i] = (((GRBcolor << i) & 0X800000) ? WS_HIGH : WS_LOW);
    }
  }
}

void ws281x_setPixelRGB(uint16_t n ,uint8_t red, uint8_t green, uint8_t blue)
{
  uint8_t i;

  if(n < PIXEL_NUM)
  {
    for(i = 0; i < 24; ++i)
    {
      pixelBuffer[n][i] = (((ws281x_color(red,green,blue) << i) & 0X800000) ? WS_HIGH : WS_LOW);
     }
   }
}

void ws281x_show(void)
{
  DMA_Cmd(DMA1_Channel3, DISABLE );  //shut down USART1 TX DMA1 
  DMA_ClearFlag(DMA1_FLAG_TC3);    
  DMA_SetCurrDataCounter(DMA1_Channel3,24 * PIXEL_NUM );
  DMA_Cmd(DMA1_Channel3, ENABLE);  //enable USART1 TX DMA1 
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t ws281x_wheel(uint8_t wheelPos) {
    wheelPos = 255 - wheelPos;
    if(wheelPos < 85) {
         return ws281x_color(255 - wheelPos * 3, 0, wheelPos * 3);
    }
    if(wheelPos < 170) {
        wheelPos -= 85;
        return ws281x_color(0, wheelPos * 3, 255 - wheelPos * 3);
     }
     wheelPos -= 170;
     return ws281x_color(wheelPos * 3, 255 - wheelPos * 3, 0);
     }

     // Fill the dots one after the other with a color
     void ws281x_colorWipe(uint32_t c, uint8_t wait) {
         for(uint16_t i=0; i<PIXEL_NUM; i++) {
             ws281x_setPixelColor(i, c);
             ws281x_show();
             delay_ms(wait);
         }
     }


         //roll the opposite way 
         //doesnt enable the last led
   void ws281x_colorWipe_rollback(uint32_t c, uint8_t wait){
       uint16_t n;
       for(uint16_t i = PIXEL_NUM ; i > 0; i --) {
           ws281x_setPixelColor(i, c);
           ws281x_show();
           delay_ms(wait);
       }
   }

void ws281x_rainbow(uint8_t wait) {
uint16_t i, j;

for(j=0; j<256; j++) {
    for(i=0; i<PIXEL_NUM; i++) {
        ws281x_setPixelColor(i, ws281x_wheel((i+j) & 255));
        }
    ws281x_show();
    delay_ms(wait);
    }
}

// Slightly different, this makes the rainbow equally distributed throughout
void ws281x_rainbowCycle(uint8_t wait) {
    uint16_t i, j;

    for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
        for(i=0; i< PIXEL_NUM; i++) {
        ws281x_setPixelColor(i,ws281x_wheel(((i * 256 / PIXEL_NUM) + j) & 
 255));
        }
       ws281x_show();
       delay_ms(wait);
     }
}

//Theatre-style crawling lights.
void ws281x_theaterChase(uint32_t c, uint8_t wait) {
      for (int j=0; j<10; j++) {  //do 10 cycles of chasing
            for (int q=0; q < 3; q++) {
                  for (uint16_t i=0; i < PIXEL_NUM; i=i+3) {
                        ws281x_setPixelColor(i+q, c);    //turn every third             pixel on
                   }
                  ws281x_show();
                  delay_ms(wait);

                  for (uint16_t i=0; i < PIXEL_NUM; i=i+3) {
                        ws281x_setPixelColor(i+q, 0);        //turn every third pixel off
                  }
            }
      }
}

//Theatre-style crawling lights with rainbow effect
void ws281x_theaterChaseRainbow(uint8_t wait) {
      for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
           for (int q=0; q < 3; q++) {
                for (uint16_t i=0; i < PIXEL_NUM; i=i+3) {
                    ws281x_setPixelColor(i+q, ws281x_wheel( (i+j) % 255));            
                    //turn every third pixel on
                  }
                ws281x_show();
                delay_ms(wait);
                for (uint16_t i=0; i < PIXEL_NUM; i=i+3) {
                    ws281x_setPixelColor(i+q, 0);        //turn every third pixel off
                 }
          }
    }
}

让我不高兴的拖车功能是这些

         // Fill the dots one after the other with a color
     void ws281x_colorWipe(uint32_t c, uint8_t wait) {
         for(uint16_t i=0; i<PIXEL_NUM; i++) {
         ws281x_setPixelColor(i, c);
         ws281x_show();
         delay_ms(wait);
         }
     }


         //roll the opposite way 
         //doesnt enable the last led
   void ws281x_colorWipe_rollback(uint32_t c, uint8_t wait){
       uint16_t n;
       for(uint16_t i = PIXEL_NUM ; i > 0; i --) {
       ws281x_setPixelColor(i, c);
       ws281x_show();
       delay_ms(wait);
       }
   }

mcu:stm32f103c8 led:带有24个led的wsb2812

2 个答案:

答案 0 :(得分:0)

您在CREATE PROCEDURE dbo.[Learning] ( @StartDate DATE, @EndDate DATE, @Data VARCHAR(30), @Cond_CID INT, @Cond_PID INT, @Cond_SSC VARCHAR(3) ) AS BEGIN IF (@Cond_CID IS NULL OR @Cond_PID IS NULL OR @Cond_SSC IS NULL OR @StartDate IS NULL OR @EndDate IS NULL OR @Data IS NULL ) BEGIN RAISERROR('Null values not allowed for the 3 last parameters!', 16, 1) END ELSE BEGIN UPDATE temp.dbo.Table6 SET StartDate = ISNULL(StartDate,@StartDate) ,EndDate = ISNULL(EndDate,@EndDate) ,Data = ISNULL(Data,@Data) WHERE Table6.CID = @Cond_CID AND TABLE6.PID = @Cond_PID AND TABLE6.SSC = @Cond_SSc END END 函数中从24循环到1,但是您希望它从23下降到0:

ws281x_colorWipe_rollback

答案 1 :(得分:0)

void ws281x_colorWipe_rollback(uint32_t c, uint8_t wait){
    for(int i = PIXEL_NUM ; i > (-1); --i) {

    ws281x_setPixelColor(i, c);

    ws281x_show();
    delay_ms(wait);
   }
}

问题已解决