找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 9413|回復(fù): 14
打印 上一主題 下一主題
收起左側(cè)

STM32F0通過SPI方式驅(qū)動WS2811彩燈源代碼

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:79631 發(fā)表于 2018-4-20 15:37 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
附件是STM32F0單片機(jī)通過SPI方式驅(qū)動WS2811芯片進(jìn)行彩燈控制的源代碼

單片機(jī)源程序如下:
  1. #include "stm32f0xx.h"
  2. #include "LED_SPI.h"

  3. uint16_t PixelBuffer[404] = {0};
  4. uint16_t PixelPointer = 0;

  5. void LED_SPI_LowLevel_Init(void)
  6. {
  7.     uint16_t i = 0;

  8.     GPIO_InitTypeDef  GPIO_InitStructure;
  9.     SPI_InitTypeDef   SPI_InitStructure;
  10.     DMA_InitTypeDef   DMA_InitStructure;

  11.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_DMA1, ENABLE);
  12.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

  13.     DMA_DeInit(DMA1_Channel3);
  14.     DMA_InitStructure.DMA_BufferSize = 0;
  15.     DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (SPI1->DR);
  16.     DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)PixelBuffer;
  17.     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  18.     DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
  19.     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  20.     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  21.     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  22.     DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  23.     DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  24.     DMA_Init(DMA1_Channel3, &DMA_InitStructure); /* DMA1 CH3 = MEM -> DR */

  25.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  26.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  27.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  28.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  29.     GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
  30.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  31.                 /*
  32.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  33.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  34.                 */

  35.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);
  36.     /* GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0); */

  37.     SPI_I2S_DeInit(SPI1);

  38.     SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
  39.     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  40.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_15b;
  41.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  42.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  43.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  44.     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; /* 48MHz / 8 = 6MHz */
  45.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  46.     SPI_InitStructure.SPI_CRCPolynomial = 7;
  47.     SPI_Init(SPI1, &SPI_InitStructure);

  48.     SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);

  49.     SPI_Cmd(SPI1, ENABLE);

  50.     for (i = 0; i < 404; i++)
  51.     {
  52.         PixelBuffer[i] = 0xAAAA;
  53.     }

  54.     PixelPointer = 0;

  55. }

  56. void LED_SPI_WriteByte(uint16_t Data)
  57. {
  58.     /* Wait until the transmit buffer is empty */
  59.     /*
  60.     while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
  61.     {
  62.     }
  63.     */

  64.     PixelBuffer[PixelPointer] = Data;
  65.     PixelPointer++;

  66.     /* Send the byte */
  67.     /* SPI_I2S_SendData16(SPI1, Data); */
  68. }

  69. void LED_SPI_SendBits(uint8_t bits)
  70. {
  71.     int zero = 0x7000;  //111000000000000
  72.     int one = 0x7F00;  //111111100000000
  73.     int i = 0x00;

  74.     for (i = 0x80; i >= 0x01; i >>= 1)
  75.     {
  76.         LED_SPI_WriteByte((bits & i) ? one : zero);
  77.     }
  78. }

  79. void LED_SPI_SendPixel(struct Pixel pixel)
  80. {
  81.     /*
  82.      r7,r6,r5,r4,r3,r2,r1,r0,g7,g6,g5,g4,g3,g2,g1,g0,b7,b6,b5,b4,b3,b2,b1,b0
  83.      \_____________________________________________________________________/
  84.                                |      _________________...
  85.                                |     /   __________________...
  86.                                |    /   /   ___________________...
  87.                                |   /   /   /
  88.                               RGB,RGB,RGB,RGB,...,STOP
  89.     */

  90.     /*
  91.             BUG Fix : Actual is GRB,datasheet is something wrong.
  92.     */
  93.     LED_SPI_SendBits(pixel.green);
  94.     LED_SPI_SendBits(pixel.red);
  95.     LED_SPI_SendBits(pixel.blue);
  96. }

  97. ErrorStatus LED_SPI_Update(struct Pixel buffer[], uint32_t length)
  98. {
  99.     uint8_t i = 0;
  100.     uint8_t m = 0;
  101.     if(DMA_GetCurrDataCounter(DMA1_Channel3) == 0)
  102.     {

  103.         for (i = 0; i < length; i++)
  104.         {
  105.             LED_SPI_SendPixel(buffer[i]);
  106.         }

  107.         if(length < 16)
  108.         {
  109.             for(i = 16 - length; i < length; i++)
  110.             {
  111.                 for(m = 0; m < 3; m++)
  112.                 {
  113.                     LED_SPI_SendBits(0x00);
  114.                 }
  115.             }
  116.         }

  117.         for (i = 0; i < 20; i++)   
  118. ……………………

  119. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
[STM32]WS281x_STM32F051 (1).zip (312.45 KB, 下載次數(shù): 213)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏5 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:270200 發(fā)表于 2018-4-26 10:11 | 只看該作者
謝謝分享。。。。。。。。。。。。。。。。。。。。。。
回復(fù)

使用道具 舉報(bào)

板凳
ID:471251 發(fā)表于 2019-1-21 14:16 | 只看該作者
謝謝分享
回復(fù)

使用道具 舉報(bào)

地板
ID:477792 發(fā)表于 2019-2-18 16:33 | 只看該作者
謝謝分享
回復(fù)

使用道具 舉報(bào)

5#
ID:441813 發(fā)表于 2019-5-5 22:48 | 只看該作者
謝謝分享,學(xué)習(xí)了
回復(fù)

使用道具 舉報(bào)

6#
ID:373011 發(fā)表于 2019-7-31 14:47 | 只看該作者
大于16燈珠好像不好用
回復(fù)

使用道具 舉報(bào)

7#
ID:75737 發(fā)表于 2019-12-10 17:42 | 只看該作者
試了下程序,燈可以亮,不會變色,有時(shí)間看看源代碼
回復(fù)

使用道具 舉報(bào)

8#
ID:698557 發(fā)表于 2020-3-7 21:07 | 只看該作者

謝謝分享
回復(fù)

使用道具 舉報(bào)

9#
ID:56962 發(fā)表于 2020-6-13 16:38 | 只看該作者
該方法不適合控制數(shù)量多的LED  適合驅(qū)動100個(gè)燈以內(nèi)
回復(fù)

使用道具 舉報(bào)

10#
ID:63058 發(fā)表于 2020-8-14 17:36 | 只看該作者
LED數(shù)量多怎么處理
回復(fù)

使用道具 舉報(bào)

11#
ID:818164 發(fā)表于 2020-9-16 12:00 | 只看該作者
vince 發(fā)表于 2019-12-10 17:42
試了下程序,燈可以亮,不會變色,有時(shí)間看看源代碼

可以變色了嗎
回復(fù)

使用道具 舉報(bào)

12#
ID:766282 發(fā)表于 2021-7-6 09:09 | 只看該作者
浪費(fèi)感情
回復(fù)

使用道具 舉報(bào)

13#
ID:888021 發(fā)表于 2021-9-2 15:20 | 只看該作者
honghu886 發(fā)表于 2020-6-13 16:38
該方法不適合控制數(shù)量多的LED  適合驅(qū)動100個(gè)燈以內(nèi)

求分享哥們
回復(fù)

使用道具 舉報(bào)

14#
ID:226821 發(fā)表于 2021-9-6 08:55 | 只看該作者
Great thanks share ARGB Control
回復(fù)

使用道具 舉報(bào)

15#
ID:436885 發(fā)表于 2021-9-14 18:16 | 只看該作者
這個(gè)是用32F0 的多少M(fèi)頻率
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表