TM1668 Led 驱动芯片源程序

时间:2021-12-17 14:22:25
#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
// 函数功能:向固定地址写一个数据
// 入口参数:地址 数据
// 出口参数:无
//***************************************
void TM1668_WriteAddrData(uint8 Addr,uint8 Data)
{
TM1668_CS_LOW ;
TM1668_WriteByteData(Addr); //写地址
TM1668_WriteByteData(Data); //写数据
TM1668_CS_HIGH ;
} //----应用实例---------
//**************************************
// 函数名称:TM1668_ReadKey
// 函数功能:TM1668 读按键值
// 入口参数:显示数据缓存区
// 出口参数:
// 备注:
//***************************************
void TM1668_WriteDat(uint8 *InDat)
{
uint8 i ;
uint8 Addr,Tmp2 ; TM1668_WriteCommand(V_MDSP4) ;//7位10段
TM1668_WriteCommand(V_MDAT1) ; //写数据到1668 固定模式
//-----
Addr = V_ADDR2 ;
for(i=0;i<5;i++) //刷显数据
{
if((*InDat) & 0x01)
{
Tmp2 = 0x01 ;
}
else
{
Tmp2 = 0x00 ;
}
TM1668_WriteAddrData(Addr,Tmp2) ;
Addr += 2 ; //地址加2
(*InDat) >>= 1 ;
}
//-----
TM1668_WriteCommand(V_LED_LIGHT) ; //设置亮度 开启显示
}
//**************************************
// 函数名称:TM1668_ReadKey
// 函数功能:TM1668 读按键值
// 入口参数:无
// 出口参数:
// 返回值 : 返回按键值
//***************************************
void TM1668_ReadKey(uint8 *OutDat)
{
uint8 i ; TM1668_CS_LOW ;
Nop1668(V_NOP) ;
TM1668_WriteByteData(V_MDAT2) ; //读按键值 Nop1668(V_NOP) ;
Nop1668(V_NOP) ; TM1668_DAT_HIGH ; //读前数据置高
*OutDat = TM1668_ReadByteData() ;// 只读一个字节 /* for(i=5;i>0;i--) //读所有按键的值
{
*OutDat = TM1668_ReadByteData() ;
OutDat ++ ;
}*/
TM1668_CS_HIGH ;
}