📄 main.c
字号:
#define __SRC
#include "ps7219.h"
#undef __SRC
#include <reg52.h>
void main()
{
while(1)
{
InitIO_PS7219();
display_led_n(0x05,1);
display_led_n(0x05,2);
bright_led_n(0x0f,1);
bright_led_n(0x0f,2);
glitter_led_n(0xff,1);
glitter_led_n(0xff,2);
}
}
/*******************************************************************************************/
//function InitIO_PS7219(); initialize the PS7219
/*******************************************************************************************/
void InitIO_PS7219()
{
LOAD=0; //clear the load signal
CLK=1; //set the clk signal high
}
/*******************************************************************************************/
//function Delay5us();Delay 5us
/*******************************************************************************************/
void Delay5us(unsigned char n)
{
unsigned int i;
while(n--)
for(i=0;i<4;i++);
}
/*******************************************************************************************/
//function SpiWrite();
/*******************************************************************************************/
void SpiWrite(unsigned char byte)
{
unsigned char i;
DATA_BUF=byte; // Put function's parameter into a bdata variable
for (i=0;i<8;i++) // Setup byte circulation bits
{
if (flag) // Put DATA_BUF.7 on data line
DO=1;
else
DO=0;
Delay5us(1); // delay 5us
CLK=0; // Set clock line low
Delay5us(1);
DATA_BUF=DATA_BUF<<1; // Shift DATA_BUF
CLK=1; // Set clock line high
}
}
/*******************************************************************************************/
//function send_ps7219();send 1 byte address and 1 byte data to PS7219
/*******************************************************************************************/
void send_ps7219(unsigned char addr,unsigned char mdata)
{
unsigned char j;
SpiWrite(addr);
DATA_BUF=mdata; // Put function's parameter into a bdata variable
for (j=0;j<7;j++) // Setup byte circulation bits
{
if (flag) // Put DATA_BUF.7 on data line
DO=1;
else
DO=0;
Delay5us(1);
CLK=0; // Set clock line low
Delay5us(1);
DATA_BUF=DATA_BUF<<1; // Shift DATA_BUF
LOAD=1; // after the 15th clock's falling edge, set the load signal high
CLK=1; // Set clock line high
}
if (flag) // Put the 16th bit on data line
DO=1;
else
DO=0;
Delay5us(1);
CLK=0; // Set clock line high
Delay5us(1);
CLK=1;
Delay5us(8); // delay 40us because the load line need to be setted high for 50us
LOAD=0; // clear the load line
}
/*******************************************************************************************/
//function send_ps7219_n();send 1 byte address and 1 byte data to Nth PS7219
/*******************************************************************************************/
void send_ps7219_n(unsigned char add,unsigned char mdat,unsigned char n)
{
unsigned char i;
if(n==1)
{
send_ps7219(add,mdat); //send the address and the data]
}
else
{
send_ps7219(add,mdat);
for(i=0;i<n-1;i++)
{
send_ps7219(0xf0,0xff); //send no-op code to the (n-1)th PS7219 in the head
}
}
}
/*******************************************************************************************/
//function glitter_led_n();glitter the led which is controled by the n PS7219
//n: 第N个PS7219
//ledn: 第N个PS7219控制的N个LE
/*******************************************************************************************/
void glitter_led_n(unsigned char ledn,unsigned char n)
{
send_ps7219_n(0x0D,ledn,n);
}
/*******************************************************************************************/
//function display_led_n();glitter the led which is controled by the n PS7219
//n: 第N个PS7219
//ledn: 第N个PS7219控制的显示的LED
/*******************************************************************************************/
void display_led_n(unsigned char ledn,unsigned char n)
{
send_ps7219_n(0x0B,ledn,n);
}
/*******************************************************************************************/
//function bright_led_n();control the brightness of the led
//n: 第N个PS7219
//ledn: 第N个PS72控制的LED的亮度
/*******************************************************************************************/
void bright_led_n(unsigned char ledn,unsigned char n)
{
send_ps7219_n(0x0A,ledn,n);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -