//芯片1
sbit TM_DAT1 = P0^7; //数据
sbit TM_CLK1 = P0^6; //时钟
//芯片2
sbit TM_DAT2 = P1^1; //数据
sbit TM_CLK2 = P0^0; //时钟
//芯片3
sbit TM_DAT3 = P0^4; //数据
sbit TM_CLK3 = P0^3; //时钟
//芯片4
sbit TM_DAT4 = P1^4; //数据
sbit TM_CLK4 = P1^3; //时钟
#define TM1640_DELAY 0 /* 640ns */
/*
段码 7 6 5 4 3 2 1 0
数字0 1 1 1 1 1 0 1 0 ==0xFA
数字1 0 0 1 0 0 0 1 0 ==0x22
数字2 1 0 1 1 1 0 0 1 ==0xB9
数字3 1 0 1 0 1 0 1 1 ==0xAB
数字4 0 1 1 0 0 0 1 1 ==0x63
数字5 1 1 0 0 1 0 1 1 ==0xCB
数字6 1 1 0 1 1 0 1 1 ==0xDB
数字7 1 0 1 0 0 0 1 0 ==0xA2
数字8 1 1 1 1 1 0 1 1 ==0xFB
数字9 1 1 1 0 0 0 1 1 ==0xE3
*/
const u8 SegNumTable[11]={0xFA,0x22,0xB9,0xAB,0x63,0xCB,0xDB,0xA2,0xFB,0xE3,0x00};//数字编码(0~9)+关闭显示
static const u8 SegAddr[14]={5,4,3,2,1,0,10,9,8,11,7,6,13,12}; //顺序地址(0~13位)
//初始化
static volatile u8 SegDisDat[4][16]={
0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,
0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,
0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,
0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,
};
//CLK输出
static void TM1640_CLK(u8 select,u8 dat)
{
if(select==0){ TM_CLK1 = dat?1:0;
}else if(select==1){TM_CLK2 = dat?1:0;
}else if(select==2){TM_CLK3 = dat?1:0;
}else if(select==3){TM_CLK4 = dat?1:0;
}
}
//DAT输出
static void TM1640_Dat(u8 select,u8 dat)
{
if(select==0){ TM_DAT1 = dat?1:0;
}else if(select==1){TM_DAT2 = dat?1:0;
}else if(select==2){TM_DAT3 = dat?1:0;
}else if(select==3){TM_DAT4 = dat?1:0;
}
}
void TM1640_Init(void)
{
u8 i = 0;
for ( i = 0; i < 4; i++)
{
TM1640_CLK(i,1);
TM1640_Dat(i,1);
}
P07_PushPull_Mode;
P06_PushPull_Mode;
P00_PushPull_Mode;
P11_PushPull_Mode;
P04_PushPull_Mode;
P03_PushPull_Mode;
P13_PushPull_Mode;
P14_PushPull_Mode;
}
//开始信号
static void TM1640_Start(u8 select)
{
u16 i =0;
TM1640_CLK(select,1);
for (i = 0; i < TM1640_DELAY; i++){};
TM1640_Dat(select,1);
for (i = 0; i < TM1640_DELAY; i++){};
TM1640_Dat(select,0);
for (i = 0; i < TM1640_DELAY; i++){};
}
//结束信号
static void TM1640_Stop(u8 select)
{
u16 i = 0;
TM1640_CLK(select,0);
for (i = 0; i < TM1640_DELAY; i++){};
TM1640_Dat(select,0);
for (i = 0; i < TM1640_DELAY; i++){};
TM1640_CLK(select,1);
for (i = 0; i < TM1640_DELAY; i++){};
TM1640_Dat(select,1);
}
//发送一字节数据
static void TM1640_SendByte(u8 select,u8 dat)
{
u8 n = 0;
u16 i = 0;
for (n = 0; n < 8; n++)
{
TM1640_CLK(select,0);
for (i = 0; i < TM1640_DELAY; i++){};
TM1640_Dat(select,dat&0x01);
for (i = 0; i < TM1640_DELAY; i++){};
TM1640_CLK(select,1);
dat>>=1;
for (i = 0; i < TM1640_DELAY; i++){};
}
}
//数据命令模式枚举
typedef enum TM1640_Mode
{
Mode_Fixed=0,
Mode_Auto,
}TM_ModeENUM;
//数据命令1
static void TM1640_Command1(u8 select,TM_ModeENUM mode)
{
if(mode>1)mode=1;
TM1640_Start(select);
TM1640_SendByte(select,0x40|(mode<<2));// 地址模式,固定/自动加一
TM1640_Stop(select);
}
//地址命令2
static void TM1640_Command2(u8 select,u8 startAddr,u8*dat,u8 len)
{
u8 i = 0;
startAddr &=0x0F;
TM1640_Start(select);
TM1640_SendByte(select,0xC0|startAddr);// 地址位置(0~16)
for ( i = 0; i < len; i++)
{
TM1640_SendByte(select,dat[i]); //显示数据
}
TM1640_Stop(select);
}
//显示控制命令3
static void TM1640_Command3(u8 select,u8 lightLevel,u8 Switch)
{
lightLevel &=0x07 ;
if(Switch)Switch=1;
TM1640_Start(select);
TM1640_SendByte(select,0x80|(Switch<<3)|lightLevel);// 亮度(0~7)+开关(0/1)
TM1640_Stop(select);
}
//地址自动加一
// 数据命令+ (设置地址+显示数据+显示数据+ .... + 显示数据) + 控制显示
void TM1640_AutoMode(u8 select,u8 startAddr,u8 *dat,u8 len)
{
TM1640_Command1(select,Mode_Auto); //数据命令-->地址自动加一
TM1640_Command2(select,startAddr,dat,len); //显示的数据
TM1640_Command3(select,LightLevel ,1); //显示亮度+开关
}
//固定地址写数据
// 设置数据+ (设置地址+显示数据)+(设置地址+显示数据)+ .... +(设置地址+显示数据) + 控制显示
void TM1640_FixedMode(u8 select,u8 startAddr,u8 dat)
{
TM1640_Command1(select,Mode_Auto); //数据命令-->地址自动加一
TM1640_Command2(select,startAddr,&dat,1); //显示的数据
TM1640_Command3(select,LightLevel,1); //显示亮度+开关
}
/*
使用Demo
================================
TM1640_AutoMode(0,0,SegDisDat[0],16); //整个芯片更新
TM1640_AutoMode(1,0,SegDisDat[1],16);
TM1640_AutoMode(2,0,SegDisDat[2],16);
TM1640_AutoMode(3,0,SegDisDat[3],16);
TM1640_FixedMode(0,0,SegNumTable[0]); //芯片0,位0,显示数字0
*/
记录-使用TM1640 数码管驱动芯片
最新推荐文章于 2024-06-05 11:30:31 发布