TM1668 Led 驱动芯片源程序 (一)

2014-11-23 21:42:25 · 作者: · 浏览: 18
#define	P_1668DAT_In	RA0   //数据输入端口
#define	P_1668DAT		LATA0 //数据输出端口
#define	P_1668CLK		LATA1
#define	P_1668CS		LATC0


#define		TM1668_CS_HIGH	P_1668CS = 1
#define		TM1668_CS_LOW	P_1668CS = 0

#define		TM1668_DAT_HIGH	P_1668DAT = 1
#define		TM1668_DAT_LOW	P_1668DAT = 0

#define		TM1668_CLK_HIGH	P_1668CLK = 1
#define		TM1668_CLK_LOW	P_1668CLK = 0


//显示模式设置命令
#define		V_MDSP1		0x00 //4位13段
#define		V_MDSP2		0x01 //5位12段	
#define		V_MDSP3		0x02 //6位11段	
#define		V_MDSP4		0x03 //7位10段

//数据命令设置
#define		V_MDAT1		0x40 //写数据到显示区
#define		V_MDAT2		0x42 //读键扫数据
//#define		V_MDAT3		0x40 //自动地址增加	
#define		V_MDAT4		0x44 //固定地址	

//地址命令设置
#define		V_ADDR0			0xC0 //地址0
#define		V_ADDR1			0xC1 //地址1	
#define		V_ADDR2			0xC2 //地址2
#define		V_ADDR3			0xC3 //地址3
#define		V_ADDR4			0xC4 //地址4
#define		V_ADDR5			0xC5 //地址5
#define		V_ADDR6			0xC6 //地址6	
#define		V_ADDR7			0xC7 //地址7
#define		V_ADDR8			0xC8 //地址8
#define		V_ADDR9			0xC9 //地址9
#define		V_ADDR10		0xCA //地址10
#define		V_ADDR11		0xCB //地址11
#define		V_ADDR12		0xCC //地址12
#define		V_ADDR13		0xCD //地址13

//显示控制 - 亮度调节
#define		V_DIS16_01		0x80 //显示宽度1/16
#define		V_DIS16_02		0x81 //显示宽度2/16
#define		V_DIS16_03		0x82 //显示宽度3/16	
#define		V_DIS16_10		0x83 //显示宽度10/16
#define		V_DIS16_11		0x84 //显示宽度11/16
#define		V_DIS16_12		0x85 //显示宽度12/16
#define		V_DIS16_13		0x86 //显示宽度13/16	
#define		V_DIS16_14		0x87 //显示宽度14/16

#define		V_DIS16_OFF		0x80 //显示宽度14/16
#define		V_DIS16_ON		0x88 //显示宽度14/16

//---------------------------------------------
#define		V_LED_LIGHT		(V_DIS16_10|V_DIS16_ON)  //显示亮度设置


//----------------------------
#define		V_NOP		3//5
//*************************************
// 函数名称:Nop1668
// 函数功能:延时函数
// 入口参数:延时时间
// 出口参数:无
//***************************************
void Nop1668(uint8 T_Dly)
{	
	while(T_Dly--);			
	return ;
}
//**************************************
// 函数名称:TM1668_WriteByteData
// 函数功能:TM1668发送一字节数据
// 入口参数:要发送的数据
// 出口参数:
//***************************************
void TM1668_WriteByteData(uint8 Data)   
{   
 	uint8 i;  

 	Nop1668(V_NOP) ;
	for(i=8;i>
0;i--) { TM1668_CLK_LOW ; if((Data & 0x01) == 0) { TM1668_DAT_LOW ; } else { TM1668_DAT_HIGH ; } Data >>= 1 ; Nop1668(V_NOP) ; TM1668_CLK_HIGH ; Nop1668(V_NOP) ; } } //************************************** // 函数名称:TM1668_ReadByteData // 函数功能:读TM1668一字节数据 // 入口参数:无 // 出口参数: // 返回值 : 所读的数据 //*************************************** uint8 TM1668_ReadByteData(void) { uint8 i; uint8 RetValue = 0 ; TM1668_CLK_LOW ; for(i=0;i<8;i++) { Nop1668(V_NOP) ; TM1668_CLK_HIGH ; RetValue >>= 1 ; //先读出的是低位 if(P_1668DAT_In) { RetValue |= 0x80 ; } TM1668_CLK_LOW ; } return (RetValue); } //************************************** // 函数名称:TM1668_WriteCommand // 函数功能:写设置命令 // 入口参数:设置命令参数 // 出口参数:无 //*************************************** void TM1668_WriteCommand(uint8 Comm) { TM1668_CS_LOW ; Nop1668(V_NOP) ; TM1668_WriteByteData(Comm); TM1668_CS_HIGH ; } //************************************** // 函数名称:TM1668_WriteAddrData