[DemoCode下载] 使用M0516来驱动TM1812 LED灯条

[复制链接]
 楼主| wanduzi 发表于 2024-2-25 19:38 | 显示全部楼层 |阅读模式
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 7 $
  5. * $Date: 19/09/02 3:01p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    SPI0 Master driver TM1812 thought MOSI pin.
  7. *
  8. * @note
  9. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"
  13. #include "tm1812.h"


  14. #define PLL_CLOCK           50000000

  15. /* Function prototype declaration */
  16. void SYS_Init(void);
  17. void SPI_Init(void);

  18. /* ------------- */
  19. /* Main function */
  20. /* ------------- */
  21. int main(void)
  22. {
  23.     /* Unlock protected registers */
  24.     SYS_UnlockReg();
  25.     /* Init System, IP clock and multi-function I/O. */
  26.     SYS_Init();
  27.     /* Lock protected registers */
  28.     SYS_LockReg();

  29.     /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
  30.     UART_Open(UART0, 115200);

  31.     /* Init SPI */
  32.     SPI_Init();

  33.     printf("\n\n");
  34.     printf("+--------------------------------------------------------------------+\n");
  35.     printf("|                   M0516 SPI Driver TM1812 Sample Code                      |\n");
  36.     printf("+--------------------------------------------------------------------+\n");
  37.     printf("\n");
  38.     printf("\nThis sample code demonstrates SPI0 Driver TM1812 thought MOSI.\n");
  39.     printf(" SPI0 configuration:\n");
  40.     printf("     Master mode; data width 24 bits.\n");

  41.     while (1)
  42.     {
  43.         LED_Test();
  44.     }
  45. }

  46. void SYS_Init(void)
  47. {
  48.     /*---------------------------------------------------------------------------------------------------------*/
  49.     /* Init System Clock                                                                                       */
  50.     /*---------------------------------------------------------------------------------------------------------*/

  51.     /* Enable external 12MHz XTAL */
  52.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  53.     /* Waiting for clock ready */
  54.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  55.     /* Switch HCLK clock source to HXT and HCLK source divide 1 */
  56.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HXT, CLK_CLKDIV_HCLK(1));

  57.     /* Set core clock as PLL_CLOCK from PLL */
  58.     CLK_SetCoreClock(PLL_CLOCK);

  59.     /* Select HXT as the clock source of UART0 */
  60.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  61.     /* Select HCLK as the clock source of SPI0 */
  62.     CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL1_SPI0_S_HCLK, MODULE_NoMsk);

  63.     /* Enable UART peripheral clock */
  64.     CLK_EnableModuleClock(UART0_MODULE);
  65.     /* Enable SPI0 peripheral clock */
  66.     CLK_EnableModuleClock(SPI0_MODULE);

  67.     /*---------------------------------------------------------------------------------------------------------*/
  68.     /* Init I/O Multi-function                                                                                 */
  69.     /*---------------------------------------------------------------------------------------------------------*/

  70.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  71.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  72.     SYS->P1_MFP &=  ~(SYS_MFP_P14_Msk | SYS_MFP_P15_Msk | SYS_MFP_P16_Msk | SYS_MFP_P17_Msk);
  73.     SYS->P1_MFP |=  SYS_MFP_P15_MOSI_0 ;

  74.     /* Update System Core Clock */
  75.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
  76.     SystemCoreClockUpdate();
  77. }

  78. void SPI_Init(void)
  79. {
  80.     /*---------------------------------------------------------------------------------------------------------*/
  81.     /* Init SPI                                                                                                */
  82.     /*---------------------------------------------------------------------------------------------------------*/
  83.     /* Configure as a master, clock idle low, 24-bit transaction, drive output on falling clock edge and latch input on rising edge. */
  84.     /* Set IP clock divider. SPI clock rate = 2.5MHz */
  85.     SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 24, 2500000);

  86.     /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
  87.     SPI_DisableAutoSS(SPI0);
  88. }

  89. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/



 楼主| wanduzi 发表于 2024-2-25 19:39 | 显示全部楼层
  1. /**************************************************************************//**
  2. * @file    TM1812.c
  3. * @brief   TM1812 Driver
  4. *
  5. * @note
  6. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  7. *
  8. *****************************************************************************/
  9. #ifndef __TIM1812_DRV_H__
  10. #define __TIM1812_DRV_H__

  11. #include "M051Series.h"
  12. #include "tm1812.h"
  13. /*   M0516LBN MOSI -->  TM1812  DIN   */
  14. /*
  15. logic 0       high level time        350-450ns        low level time        800-900ns
  16. logic 1       high level time        700-999ns        low level time        250-550ns
  17. bit logic       low level time         8-24us
  18. logic 0       high level 400NS       low level800NS
  19. logic 1       high level 800NS           low level400NS
  20. reset time    low level time                 2000NS
  21. */

  22. /*
  23. The SPI clock is set at 2.5mhz, or 400NS, and a data bit is represented by a 3-bit SPI waveform
  24. This expands a data byte to a 24-bit SPI waveform word length
  25. Converting logic 0 into binary 100 is 0x04, which means 400nS high and 800nS low
  26. Converting logic 1 into binary 110 is 0x06, which means 800nS high and 400nS low
  27. so SPI MOSI data to drive TM1812 DIN
  28. */
  29. #define TM1812_NUMBER   10
  30. #define TM1812_CHANNEL  (TM1812_NUMBER*4)


  31. void spi0_write(uint32_t word)
  32. {
  33.     SPI_WRITE_TX0(SPI0, word);
  34.     /* Trigger SPI data transfer */
  35.     SPI_TRIGGER(SPI0);

  36.     /* Check SPI0 busy status */
  37.     while (SPI_IS_BUSY(SPI0));
  38. }


  39. void tm1812_write_byte(uint8_t byte)
  40. {
  41.     uint32_t tmp, data = 0;
  42.     uint8_t i;

  43.     for (i = 0; i < 8; i++)
  44.     {
  45.         tmp = (byte & 1) ? 0x06 : 0x04;
  46.         data |= tmp << (i * 3);
  47.         byte >>= 1;
  48.     }

  49.     spi0_write(data);
  50. }

  51. void tm1812_display_lamp(uint32_t *led)
  52. {
  53.     uint8_t i;
  54.     uint8_t red_ch, green_ch, blue_ch;

  55.     for (i = 0; i < TM1812_CHANNEL; i++)
  56.     {
  57.         red_ch = (uint8_t)(led[i] >> 16);
  58.         green_ch = (uint8_t)(led[i] >> 8);
  59.         blue_ch = (uint8_t)(led[i]);
  60.         tm1812_write_byte(red_ch);
  61.         tm1812_write_byte(green_ch);
  62.         tm1812_write_byte(blue_ch);
  63.     }

  64.     CLK_SysTickDelay(24); //update display, reset time is 24us
  65. }

  66. void tm1812_display_all(uint32_t ucolor)
  67. {
  68.     uint32_t led[TM1812_CHANNEL + 1];
  69.     uint8_t i;

  70.     for (i = 0; i < (TM1812_CHANNEL); i++)
  71.     {
  72.         led[i] = ucolor;
  73.     }

  74.     tm1812_display_lamp(led);
  75. }

  76. void tm1812_display_off(void)
  77. {
  78.     uint32_t color = 0;
  79.     tm1812_display_all(color);
  80. }

  81. void LED_Test(void)
  82. {
  83.     uint32_t i;
  84.     uint32_t color;

  85.     for (i = 0; i < 254; i++)
  86.     {
  87.         color = i << 16;
  88.         tm1812_display_all(color);
  89.         CLK_SysTickDelay(4000);
  90.     }

  91.     while (i--)
  92.     {
  93.         color = i << 16;
  94.         tm1812_display_all(color);
  95.         CLK_SysTickDelay(4000);
  96.     }

  97.     for (i = 0; i < 254; i++)
  98.     {
  99.         color = i << 8;
  100.         tm1812_display_all(color);
  101.         CLK_SysTickDelay(4000);
  102.     }

  103.     while (i--)
  104.     {
  105.         color = i << 8;
  106.         tm1812_display_all(color);
  107.         CLK_SysTickDelay(4000);
  108.     }

  109.     for (i = 0; i < 254; i++)
  110.     {
  111.         color = i << 0;
  112.         tm1812_display_all(color);
  113.         CLK_SysTickDelay(4000);
  114.     }

  115.     while (i--)
  116.     {
  117.         color = i << 0;
  118.         tm1812_display_all(color);
  119.         CLK_SysTickDelay(4000);
  120.     }

  121.     tm1812_display_off();
  122.     CLK_SysTickDelay(200000);
  123.     CLK_SysTickDelay(200000);
  124.     CLK_SysTickDelay(200000);
  125.     CLK_SysTickDelay(200000);
  126.     CLK_SysTickDelay(200000);
  127.     CLK_SysTickDelay(200000);
  128.     CLK_SysTickDelay(200000);
  129. }

  130. #endif

 楼主| wanduzi 发表于 2024-2-25 19:39 | 显示全部楼层
  1. /**************************************************************************//**
  2. * @file    TM1812.h
  3. * @brief   TM1812 Driver
  4. *
  5. * @note
  6. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  7. *
  8. *****************************************************************************/
  9. #ifndef __TIM1812_DRV_H__
  10. #define __TIM1812_DRV_H__

  11. void LED_Test(void);

  12. #endif
  13. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/
 楼主| wanduzi 发表于 2024-2-25 19:39 | 显示全部楼层
TM1812采用单线通讯方式,采用归零码的方式发送信号。芯片在上电复位以后,接收DIN端接收的数据,接收够4组24位后,DO端口开始转发数据,为下一个芯片提供输入数据。在转发之前,DOUT口一直拉低。此时芯片将不接收新的数据,芯片4组OUTR、OUTG、OUTB输出口根据接收到的数据,发出相应的不同占空比的信号,该信号周期为1.3毫秒。如果DIN端输入信号为RESET信号,芯片将接收到的数据送显示,芯片将在该信号结束后重新接收新的数据,在接收完开始的4组24位数据后,通过DOUT口转发数据。
 楼主| wanduzi 发表于 2024-2-25 19:40 | 显示全部楼层
zhuotuzi 发表于 2024-2-26 22:56 | 显示全部楼层
类似WS2812吗?看这个数据是很像。
zero115 发表于 2024-4-10 17:14 | 显示全部楼层
楼主,可以看看波形吗,发完一帧数据之后可以一直拉低吗,直到要改变才发数据
ClarkLLOTP 发表于 2024-4-15 15:16 来自手机 | 显示全部楼层
单线传输吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

154

主题

1857

帖子

3

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