打印
[DemoCode下载]

使用M0516来驱动TM1812 LED灯条

[复制链接]
557|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wanduzi|  楼主 | 2024-2-25 19:38 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
* $Revision: 7 $
* $Date: 19/09/02 3:01p $
* [url=home.php?mod=space&uid=247401]@brief[/url]    SPI0 Master driver TM1812 thought MOSI pin.
*
* @note
* Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "M051Series.h"
#include "tm1812.h"


#define PLL_CLOCK           50000000

/* Function prototype declaration */
void SYS_Init(void);
void SPI_Init(void);

/* ------------- */
/* Main function */
/* ------------- */
int main(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();
    /* Init System, IP clock and multi-function I/O. */
    SYS_Init();
    /* Lock protected registers */
    SYS_LockReg();

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

    /* Init SPI */
    SPI_Init();

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

    while (1)
    {
        LED_Test();
    }
}

void SYS_Init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/

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

    /* Waiting for clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

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

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

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

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

    /* Enable UART peripheral clock */
    CLK_EnableModuleClock(UART0_MODULE);
    /* Enable SPI0 peripheral clock */
    CLK_EnableModuleClock(SPI0_MODULE);

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

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

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

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

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

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

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



使用特权

评论回复
沙发
wanduzi|  楼主 | 2024-2-25 19:39 | 只看该作者
/**************************************************************************//**
* @file    TM1812.c
* @brief   TM1812 Driver
*
* @note
* Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*
*****************************************************************************/
#ifndef __TIM1812_DRV_H__
#define __TIM1812_DRV_H__

#include "M051Series.h"
#include "tm1812.h"
/*   M0516LBN MOSI -->  TM1812  DIN   */
/*
logic 0       high level time        350-450ns        low level time        800-900ns
logic 1       high level time        700-999ns        low level time        250-550ns
bit logic       low level time         8-24us
logic 0       high level 400NS       low level800NS
logic 1       high level 800NS           low level400NS
reset time    low level time                 2000NS
*/

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


void spi0_write(uint32_t word)
{
    SPI_WRITE_TX0(SPI0, word);
    /* Trigger SPI data transfer */
    SPI_TRIGGER(SPI0);

    /* Check SPI0 busy status */
    while (SPI_IS_BUSY(SPI0));
}


void tm1812_write_byte(uint8_t byte)
{
    uint32_t tmp, data = 0;
    uint8_t i;

    for (i = 0; i < 8; i++)
    {
        tmp = (byte & 1) ? 0x06 : 0x04;
        data |= tmp << (i * 3);
        byte >>= 1;
    }

    spi0_write(data);
}

void tm1812_display_lamp(uint32_t *led)
{
    uint8_t i;
    uint8_t red_ch, green_ch, blue_ch;

    for (i = 0; i < TM1812_CHANNEL; i++)
    {
        red_ch = (uint8_t)(led[i] >> 16);
        green_ch = (uint8_t)(led[i] >> 8);
        blue_ch = (uint8_t)(led[i]);
        tm1812_write_byte(red_ch);
        tm1812_write_byte(green_ch);
        tm1812_write_byte(blue_ch);
    }

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

void tm1812_display_all(uint32_t ucolor)
{
    uint32_t led[TM1812_CHANNEL + 1];
    uint8_t i;

    for (i = 0; i < (TM1812_CHANNEL); i++)
    {
        led[i] = ucolor;
    }

    tm1812_display_lamp(led);
}

void tm1812_display_off(void)
{
    uint32_t color = 0;
    tm1812_display_all(color);
}

void LED_Test(void)
{
    uint32_t i;
    uint32_t color;

    for (i = 0; i < 254; i++)
    {
        color = i << 16;
        tm1812_display_all(color);
        CLK_SysTickDelay(4000);
    }

    while (i--)
    {
        color = i << 16;
        tm1812_display_all(color);
        CLK_SysTickDelay(4000);
    }

    for (i = 0; i < 254; i++)
    {
        color = i << 8;
        tm1812_display_all(color);
        CLK_SysTickDelay(4000);
    }

    while (i--)
    {
        color = i << 8;
        tm1812_display_all(color);
        CLK_SysTickDelay(4000);
    }

    for (i = 0; i < 254; i++)
    {
        color = i << 0;
        tm1812_display_all(color);
        CLK_SysTickDelay(4000);
    }

    while (i--)
    {
        color = i << 0;
        tm1812_display_all(color);
        CLK_SysTickDelay(4000);
    }

    tm1812_display_off();
    CLK_SysTickDelay(200000);
    CLK_SysTickDelay(200000);
    CLK_SysTickDelay(200000);
    CLK_SysTickDelay(200000);
    CLK_SysTickDelay(200000);
    CLK_SysTickDelay(200000);
    CLK_SysTickDelay(200000);
}

#endif

使用特权

评论回复
板凳
wanduzi|  楼主 | 2024-2-25 19:39 | 只看该作者
/**************************************************************************//**
* @file    TM1812.h
* @brief   TM1812 Driver
*
* @note
* Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
*
*****************************************************************************/
#ifndef __TIM1812_DRV_H__
#define __TIM1812_DRV_H__

void LED_Test(void);

#endif
/*** (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口转发数据。

使用特权

评论回复
5
wanduzi|  楼主 | 2024-2-25 19:40 | 只看该作者

使用特权

评论回复
6
zhuotuzi| | 2024-2-26 22:56 | 只看该作者
类似WS2812吗?看这个数据是很像。

使用特权

评论回复
7
zero115| | 2024-4-10 17:14 | 只看该作者
楼主,可以看看波形吗,发完一帧数据之后可以一直拉低吗,直到要改变才发数据

使用特权

评论回复
8
ClarkLLOTP| | 2024-4-15 15:16 | 只看该作者
单线传输吗

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

129

主题

1655

帖子

3

粉丝