[DemoCode下载] 如何使用SPI接口驱动WS2812B

[复制链接]
1086|0
 楼主| xinpian101 发表于 2024-6-23 21:30 | 显示全部楼层 |阅读模式




  1. #include <stdio.h>
  2. #include "NuMicro.h"

  3. #define SPI_CLK_FREQ    2500000
  4. #define LED_NUMBER 10

  5. void SYS_Init(void);
  6. void SPI_Init(void);
  7. unsigned int data_bit8_to_bit24(uint8_t data);
  8. void send_led_commands(uint8_t Led_number, uint8_t G,uint8_t R,uint8_t B);


  9. void send_array_clear(void){
  10. uint8_t led_counter=0;

  11. for(led_counter=0;led_counter<=LED_NUMBER;led_counter++){
  12. SPI_WRITE_TX(SPI0,0x924924);
  13. SPI_WRITE_TX(SPI0, 0x924924);
  14. SPI_WRITE_TX(SPI0, 0x924924);
  15. while(SPI_IS_BUSY(SPI0));       
  16. }
  17. CLK_SysTickDelay(200);       
  18. }


  19. void send_led_commands(uint8_t Led_number, uint8_t G,uint8_t R,uint8_t B){
  20. uint8_t led_counter=0;
  21. uint32_t dataG,dataR,dataB=0;

  22. dataG=data_bit8_to_bit24(G);  //Transform 8bit green data to 24bit data
  23. dataR=data_bit8_to_bit24(R);  //Transform 8bit red data to 24bit data
  24. dataB=data_bit8_to_bit24(B);  //Transform 8bit blue data to 24bit data

  25. for(led_counter=0;led_counter<Led_number;led_counter++){
  26. SPI_WRITE_TX(SPI0,0x924924);   //Send 0 bit turn off green color
  27. SPI_WRITE_TX(SPI0, 0x924924);  //Send 0 bit turn off red color
  28. SPI_WRITE_TX(SPI0, 0x924924);  //Send 0 bit turn off blue color
  29. while(SPI_IS_BUSY(SPI0));             // wait for communication
  30. }

  31. SPI_WRITE_TX(SPI0, dataG); // Send green color code
  32. SPI_WRITE_TX(SPI0, dataR); // Send red color code
  33. SPI_WRITE_TX(SPI0, dataB); // Send blue color code
  34. while(SPI_IS_BUSY(SPI0));         // wait for communication

  35. CLK_SysTickDelay(200);  //wait for 200us for reset code
  36. }

  37. unsigned int data_bit8_to_bit24(uint8_t data){
  38. uint32_t conv_data=0;
  39. int8_t x=0;
  40. uint32_t bit0=4;  //100
  41. uint32_t bit1=6;  //110

  42. for(x=7;x>=0;x--){
  43. if (data&(1<<x))
  44. conv_data=conv_data|(bit1<<(3*x));
  45. else
  46. conv_data=conv_data|(bit0<<(3*x));
  47. }
  48. return conv_data;
  49. }

  50. int8_t counter=0;
  51. int main(void)
  52. {
  53. /* Unlock protected registers */
  54. SYS_UnlockReg();
  55. /* Init System, IP clock and multi-function I/O. */
  56. SYS_Init();
  57. /* Lock protected registers */
  58. SYS_LockReg();
  59. /* Init SPI */
  60. SPI_Init();
  61. send_array_clear();

  62. while(1){

  63. for(counter=LED_NUMBER-1;counter>=0;counter--){
  64. send_led_commands(counter,255-counter*28,counter*28,0);
  65. CLK_SysTickDelay(100000); //wait 100 miliseconds
  66. }

  67. CLK_SysTickDelay(1000000); //wait 3 seconds
  68. CLK_SysTickDelay(1000000);
  69. CLK_SysTickDelay(1000000);

  70. send_array_clear(); //Turn off all LEDs on strip

  71. CLK_SysTickDelay(1000000); //wait 1 second

  72. }
  73. }


  74. void SYS_Init(void)
  75. {
  76.     /*---------------------------------------------------------------------------------------------------------*/
  77.     /* Init System Clock                                                                                       */
  78.     /*---------------------------------------------------------------------------------------------------------*/
  79.     /* Enable HIRC clock */
  80.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
  81.                 CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HIRC_DIV2);
  82.     /* Waiting for HIRC clock ready */
  83.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  84.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  85.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  86.     /* Select HIRC as the clock source of UART0 */
  87.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  88.     /* Select PCLK1 as the clock source of SPI0 */
  89.     CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL2_SPI0SEL_PCLK1, MODULE_NoMsk);

  90.     /* Enable SPI0 peripheral clock */
  91.     CLK_EnableModuleClock(SPI0_MODULE);

  92.     /*---------------------------------------------------------------------------------------------------------*/
  93.     /* Init I/O Multi-function                                                                                 */
  94.     /*---------------------------------------------------------------------------------------------------------*/
  95.     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
  96.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  97.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  98.     /* Setup SPI0 multi-function pins */
  99.     /* PA.3 is SPI0_SS,   PA.2 is SPI0_CLK,
  100.        PA.1 is SPI0_MISO, PA.0 is SPI0_MOSI*/
  101.     SYS->GPA_MFPL = (SYS->GPA_MFPL & ~(SYS_GPA_MFPL_PA3MFP_Msk |
  102.                                        SYS_GPA_MFPL_PA2MFP_Msk |
  103.                                        SYS_GPA_MFPL_PA1MFP_Msk |
  104.                                        SYS_GPA_MFPL_PA0MFP_Msk)) |
  105.                     (SYS_GPA_MFPL_PA3MFP_SPI0_SS |
  106.                      SYS_GPA_MFPL_PA2MFP_SPI0_CLK |
  107.                      SYS_GPA_MFPL_PA1MFP_SPI0_MISO |
  108.                      SYS_GPA_MFPL_PA0MFP_SPI0_MOSI);

  109.     /* Update System Core Clock */
  110.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
  111.     SystemCoreClockUpdate();
  112. }

  113. void SPI_Init(void)
  114. {
  115.     /*---------------------------------------------------------------------------------------------------------*/
  116.     /* Init SPI                                                                                                */
  117.     /*---------------------------------------------------------------------------------------------------------*/
  118.     /* Configure as a master, clock idle low, 24-bit transaction, drive output on falling clock edge and latch input on rising edge. */
  119.     /* Set IP clock divider. SPI clock rate = 2.5MHz */
  120.     SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0,24, SPI_CLK_FREQ);

  121.     /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
  122.     SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW);
  123. }



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

本版积分规则

129

主题

1650

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部