//SPI的时钟定为2.5MHZ,也就是400NS,一个数据位用3位SPI波形来表示;
//那么就把一个数据字节扩展成了24位的SPI波形字长;
//把逻辑0转化成二进制100就是0x04,就是高400nS,低800nS;
//把逻辑1转化成二进制110就是0x06,就是高800nS,低400nS;
//这样就能用SPI的MOSI发数据来驱动TM1812的DIN。
源码:
/*---------------------------------------------------------------------------------------------------------*/
/* @file tm1812.c */
/* @version V1.00 */
/* $Date: 15/07/01 11:44a $ */
/* @brief M051 Series UART for M35 GPRS Sample Code */
/* Copyright(c) PM9GZY Technology Corp. yuanxihua@21cn.com. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include "main.h"
/* M0516LBN MOSI --> TM1812 DIN */
/*
逻辑0 高电平时间 350-450ns 低电平时间 800-900ns
逻辑1 高电平时间 700-999ns 低电平时间 250-550ns
位逻辑 低电平时间 8-24us
逻辑0 高400NS 低800NS
逻辑1 高800NS 低400NS
复位 低2000NS
*/
/* Function prototype declaration */
void SPI0_Init(void);
void spi0_write(uint32_t word)
{
// while(SPI_GET_TX_FIFO_FULL_FLAG(SPI0));
/* Write to TX register */
SPI_WRITE_TX0(SPI0, word);
/* Trigger SPI data transfer */
SPI_TRIGGER(SPI0);
/* Check SPI0 busy status */
while(SPI_IS_BUSY(SPI0));
/* Read received data */
// tmp = SPI_READ_RX0(SPI0);tmp=tmp;
}
//SPI的时钟定为2.5MHZ,也就是400NS,一个数据位用3位SPI波形来表示;
//那么就把一个数据字节扩展成了24位的SPI波形字长;
//把逻辑0转化成二进制100就是0x04,就是高400nS,低800nS;
//把逻辑1转化成二进制110就是0x06,就是高800nS,低400nS;
//这样就能用SPI的MOSI发数据来驱动TM1812的DIN。
void tm1812_write_byte(uint8_t byte)
{
uint32_t tmp,data=0;
for(uint8_t i=0;i<8;i++){tmp=(byte&1)?0x06:0x04;data|=tmp<<(i*3);byte>>=1;}
spi0_write(data);
}
void tm1812_display_lamp(color_t *led)
{
for(uint8_t i=0;i<24;i++)
{
tm1812_write_byte(led.red);
tm1812_write_byte(led.green);
tm1812_write_byte(led.blue);
}
CLK_SysTickDelay(24); //更新显示,复位时间24uS
}
void tm1812_display_all(color_t *color)
{
color_t led[24];for(uint8_t i=0;i<24;i++){led=*color;}
tm1812_display_lamp((color_t*)led);
}
void tm1812_display_off(void){uint32_t color =0;tm1812_display_all((color_t *)&color);}
void tm1812_display_key(uint8_t key_value,color_t *color)
{
color_t led[24];memset((void *)&led[0],0,sizeof(led));
led[0]=*color;for(uint8_t i=0;i<key_value;i++){led[8+i]=*color;}
tm1812_display_lamp((color_t*)led);
}
void spi0_test(void)
{
uint32_t i;
for(i=0;i<254;i++){uint32_t color=i<<16;tm1812_display_all((color_t *)&color);CLK_SysTickDelayMs(4);}
while(i--) {uint32_t color=i<<16;tm1812_display_all((color_t *)&color);CLK_SysTickDelayMs(4);}
for(i=0;i<254;i++){uint32_t color=i<< 8;tm1812_display_all((color_t *)&color);CLK_SysTickDelayMs(4);}
while(i--) {uint32_t color=i<< 8;tm1812_display_all((color_t *)&color);CLK_SysTickDelayMs(4);}
for(i=0;i<254;i++){uint32_t color=i<< 0;tm1812_display_all((color_t *)&color);CLK_SysTickDelayMs(4);}
while(i--) {uint32_t color=i<< 0;tm1812_display_all((color_t *)&color);CLK_SysTickDelayMs(4);}
tm1812_display_off();CLK_SysTickDelayMs(200);
}
void SPI0_Init(void)
{
/* Select HCLK as the clock source of SPI0 */
CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL1_SPI0_S_HCLK, MODULE_NoMsk);
/* Enable SPI0 peripheral clock */
CLK_EnableModuleClock(SPI0_MODULE);
/* Setup SPI0 multi-function pins */
/* Set P1 multi-function pins for SPI0 */
SYS->P1_MFP = (SYS->P1_MFP & (~(SYS_MFP_P14_Msk | SYS_MFP_P15_Msk | SYS_MFP_P16_Msk | SYS_MFP_P17_Msk)))
| (/*SYS_MFP_P14_SPISS0 |*/ SYS_MFP_P15_MOSI_0 /*| SYS_MFP_P16_MISO_0 | SYS_MFP_P17_SPICLK0*/);
/* Set P1。3 pins for GPIO */
SYS->P1_MFP = (SYS->P1_MFP & (~(SYS_MFP_P13_Msk))) | (SYS_MFP_P13_TXD1);
/* Configure P1.3 as Input mode */
GPIO_SetMode(P1, BIT3, GPIO_PMD_INPUT);
/*---------------------------------------------------------------------------------------------------------*/
/* 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_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW);
}
//完毕
最近编辑记录 缥缈九哥 (2020-05-09 19:59:46)
离线