
	
	//=================================================================================
	MAX7219.DATA_IN = DATA_IN; MAX7219.DATA_OUT = DATA_OUT; MAX7219.DATA_DIR = DATA_DIR;
	MAX7219.LOCK_IN = LOCK_IN; MAX7219.LOCK_OUT = LOCK_OUT; MAX7219.LOCK_DIR = LOCK_DIR;
	MAX7219.SHIFT_IN = SHIFT_IN; MAX7219.SHIFT_OUT = SHIFT_OUT; MAX7219.SHIFT_DIR = SHIFT_DIR;
	
	unit MAX7219
	{
		const int32 NUMBER = 4;
		
		public link bit DATA_IN; public link bit DATA_OUT; public link bit DATA_DIR;
		public link bit LOCK_IN; public link bit LOCK_OUT; public link bit LOCK_DIR;
		public link bit SHIFT_IN; public link bit SHIFT_OUT; public link bit SHIFT_DIR;
		
		//---------------------------------------------------
		//初始化
		public void init()
		{
			DATA_DIR = 1;
			DATA_OUT = 0;
			SHIFT_DIR = 1;
			SHIFT_OUT = 1;
			LOCK_DIR = 1;
			LOCK_OUT = 1;
			
			//解码格式为非BCD
			send_cmd( 0b0000_1001, 0x00 );
			
			//亮度为最大
			send_cmd( 0b0000_1010, 0x0F );
			
			//扫描数目为8
			send_cmd( 0b0000_1011, 0x07 );
			
			//非掉电模式 - 最低位 0 关断, 1 工作
			send_cmd( 0b0000_1100, 0x01 );
			
			//非测试模式 - 最低位 0 关断, 1 工作
			send_cmd( 0b0000_1111, 0x00 );
		}
		//---------------------------------------------------
		//设置亮度
		public void set_light( uint8 d )
		{
			send_cmd( 0b0000_1010, d );
		}
		//---------------------------------------------------
		//发送一组数据, 1-点亮  0-熄灭
		public void send( [uint8*?] data )
		{
			for( uint8 i = 0; i < 8; i + 1 ) {
				LOCK_OUT = 0;
				for( uint8 n = 0; n < NUMBER; n + 1 ) {
					uint8 nn = (NUMBER - n - 1) * 8;
					uint8 l0 = data[nn];
					uint8 l1 = data[nn + 1];
					uint8 l2 = data[nn + 2];
					uint8 l3 = data[nn + 3];
					uint8 l4 = data[nn + 4];
					uint8 l5 = data[nn + 5];
					uint8 l6 = data[nn + 6];
					uint8 l7 = data[nn + 7];
					l0 <<= i;
					l1 <<= i;
					l2 <<= i;
					l3 <<= i;
					l4 <<= i;
					l5 <<= i;
					l6 <<= i;
					l7 <<= i;
					uint8 d = 0;
					d.0(bit) = l0.7(bit);
					d.1(bit) = l1.7(bit);
					d.2(bit) = l2.7(bit);
					d.3(bit) = l3.7(bit);
					d.4(bit) = l4.7(bit);
					d.5(bit) = l5.7(bit);
					d.6(bit) = l6.7(bit);
					d.7(bit) = l7.7(bit);
					send_word( i + 1, d );
				}
				LOCK_OUT = 1;
			}
		}
		//==============================================
		void send_cmd( uint8 addr, uint8 data )
		{
			LOCK_OUT = 0;
			
			loop( NUMBER ) {
				send_word( addr, data );
			}
			LOCK_OUT = 1;
		}
		//==============================================
		void send_word( uint8 addr, uint8 data )
		{
			uint16 d = data;
			d.8(uint8) = addr;
			loop( 16 ) {
				DATA_OUT = d.15(bit);
				d << 1;
				SHIFT_OUT = 0;
				SHIFT_OUT = 1;
			}
		}
	}
	
	
	
