c - 为什么我的 wsb2812 led 没有按我的预期工作,最后一个 led 不工作

标签 c stm32 stm32f1

我试图顺时针和逆时针方向点亮我的 LED 灯。 我使用mpu6050作为LED的方向控制。我检查了我的 mpu6050 驱动代码,它们没问题。 顺时针方向工作时效果很好,所有 LED 灯均呈红色亮起。像这样 enter image description here 当它逆时针工作时,出现了问题 enter image description here

我尝试了很多方法来解决,但没有一个有效。 这是我的 LED 驱动器代码

#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);
       }
   }

单片机:stm32f103c8 LED:WSB2812,带24个LED

最佳答案

您在 ws281x_colorWipe_rollback 函数中从 24 循环到 1,但您希望它从 23 下降到 0:

for(int16_t i = PIXEL_NUM-1 ; i >= 0; i --)

关于c - 为什么我的 wsb2812 led 没有按我的预期工作,最后一个 led 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54048792/

相关文章:

c - *((int*)&f) 在 C 中做什么?

c - 为什么 strcmp() 在代码块(特别是在 gcc 中)和其他编译器中的工作方式不同?

stm32 - 无法使用 SPI 和 STM32F303 初始化 NRF24L01+ 寄存器

c - 为什么 USART2 发送垃圾值?

c - 文件写入C困惑

使用 LuaJIT 调用标准打开对话框会导致访问冲突

STM32 DMA 第一次接收 UART 数据后顺序错误

c - 找到两个最差的值并删除总和

c - STM32 µC : SysTick delay doesn't work inside interrupt handler

stm32 - 如何在CubeMX STM32中使用LL(低级)驱动程序?