⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ps7219.c

📁 Keil下的ps7219完整编程
💻 C
字号:
/*******************************************************************************************/
//     the copyright is hold by yanxu  
//	   the company of hunan huasheng 
//	   2006.9.22
//	   its purpose is drivering the ps7219 which is the led driver 
/*******************************************************************************************/

#define __SRC
  #include "PS7219.h"
#undef __SRC

#include <reg52.h>

/*******************************************************************************************/
//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
//addr: 0x01 - digital_1 ; 0x02 - digital_2;....so 0x08 - digital8;
//madt: 0x00 - num_0; 0x01 - num_1;....0x09 - num_9;
/*******************************************************************************************/
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控制礜LED
//ledn: xxxx xxxx 8个LED对应位为1时闪烁
/*******************************************************************************************/
void glitter_led_n(unsigned char ledn,unsigned char n)
{
	send_ps7219_n(0x0D,ledn,n);
}
/*******************************************************************************************/
//function decode_led_n();glitter the led which is controled by the n PS7219
//n:	第N个PS7219
//ledn: 第N个PS7219控制礜LED
//ledn: xxxx xxxx 8个LED在其对应位为1时进行BCD译码
/*******************************************************************************************/
void decode_led_n(unsigned char ledn,unsigned char n)
{
	send_ps7219_n(0x09,ledn,n);
}

/*******************************************************************************************/
//function display_led_n();display the led which is controled by the n PS7219
//n:	第N个PS7219
//ledn: 第N个PS7219控制的显示的L
//ledn: 0x00 显示第一个LED and 0x01显示第一和第二个LEDs...so 0x显示全部8个leds(most)
/*******************************************************************************************/
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的亮

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -