#include
#include"tm1628_1.h"
uchar code tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};
//引脚定义
***it DIO=P0^4;
***it CLK=P0^5;
***it STB_1=P0^6;
***it STB=P0^7;
void TM1638_Write(unsigned char DATA) //写数据函数
{
unsigned char i;
for(i=0;i<8;i++)
{
CLK=0;
if(DATA&0X01)
DIO=1;
else
DIO=0;
DATA>>=1;
CLK=1;
}
}
void Write_COM(bit mode,unsigned char cmd) //发送命令字
{
if(mode==1)
STB_1=0;
else
STB=0;
TM1638_Write(cmd);
if(mode==1)
STB_1=1;
else
STB=1;
}
unsigned char TM1638_Read(void) //读数据函数
{
unsigned char i;
unsigned char temp=0;
DIO=1; //设置为输入
for(i=0;i<8;i++)
{
temp>>=1;
CLK=0;
if(DIO)
temp|=0x80;
CLK=1;
}
return temp;
}
void Write_1_DATA(unsigned char add,unsigned char DATA) //指定地址写入数据
{
Write_COM(1,0x44);
STB_1=0;
TM1638_Write(0xc0|add);
TM1638_Write(DATA);
STB_1=1;
}
unsigned int Read_key(void)
{
unsigned int c[4],c1[4],date[16],i,key_value=0,key_value1=0;
STB=0;
TM1638_Write(0x42);
for(i=0;i<4;i++)
c[i]=TM1638_Read();
STB=1; //4个字节数据合成一个字节
for(i=0;i<4;i++)
key_value|=c[i]<
for(i=0;i<8;i++)
{
if(key_value&(1<
date[i]=1;
else
date[i]=0;
}
key_value=date[0]+(date[2]<<4)+(date[4]<<1)+(date[6]<<5)+(date[1]<<2)+(date[3]<<6)+(date[5]<<3)+(date[7]<<7);
STB_1=0;
TM1638_Write(0x42);
for(i=0;i<4;i++)
c1[i]=TM1638_Read();
STB_1=1; //4个字节数据合成一个字节
for(i=0;i<4;i++)
key_value1|=c1[i]<
for(i=0;i<8;i++)
{
if(key_value1&(1<
date[i]=1;
else
date[i]=0;
}
key_value1=date[0]+(date[2]<<4)+(date[4]<<1)+(date[6]<<5)+(date[1]<<2)+(date[3]<<6)+(date[5]<<3)+(date[7]<<7);
key_value=(key_value1<<8)+key_value;
return key_value;
}
void Write_DATA(unsigned char add,unsigned char DATA) //指定地址写入数据
{
Write_COM(0,0x44);
STB=0;
TM1638_Write(0xc0|add);
TM1638_Write(DATA);
STB=1;
}
void init_TM1638(void)
{
unsigned char i;
Write_COM(0,0x8f);//亮度
Write_COM(0,0x40); //写 数据命令
STB=0;
TM1638_Write(0xc0); //写地址命令
for(i=0;i<16;i++)
TM1638_Write(0x00);
STB=1;
Write_COM(1,0x8f);//亮度
Write_COM(1,0x40); //写 数据命令
STB_1=0;
TM1638_Write(0xc0); //写地址命令
for(i=0;i<16;i++)
TM1638_Write(0x00);
STB_1=1;
}
#include
#include"tm1628_1.h"
uchar code tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};
//引脚定义
***it DIO=P0^4;
***it CLK=P0^5;
***it STB_1=P0^6;
***it STB=P0^7;
void TM1638_Write(unsigned char DATA) //写数据函数
{
unsigned char i;
for(i=0;i<8;i++)
{
CLK=0;
if(DATA&0X01)
DIO=1;
else
DIO=0;
DATA>>=1;
CLK=1;
}
}
void Write_COM(bit mode,unsigned char cmd) //发送命令字
{
if(mode==1)
STB_1=0;
else
STB=0;
TM1638_Write(cmd);
if(mode==1)
STB_1=1;
else
STB=1;
}
unsigned char TM1638_Read(void) //读数据函数
{
unsigned char i;
unsigned char temp=0;
DIO=1; //设置为输入
for(i=0;i<8;i++)
{
temp>>=1;
CLK=0;
if(DIO)
temp|=0x80;
CLK=1;
}
return temp;
}
void Write_1_DATA(unsigned char add,unsigned char DATA) //指定地址写入数据
{
Write_COM(1,0x44);
STB_1=0;
TM1638_Write(0xc0|add);
TM1638_Write(DATA);
STB_1=1;
}
unsigned int Read_key(void)
{
unsigned int c[4],c1[4],date[16],i,key_value=0,key_value1=0;
STB=0;
TM1638_Write(0x42);
for(i=0;i<4;i++)
c[i]=TM1638_Read();
STB=1; //4个字节数据合成一个字节
for(i=0;i<4;i++)
key_value|=c[i]<
for(i=0;i<8;i++)
{
if(key_value&(1<
date[i]=1;
else
date[i]=0;
}
key_value=date[0]+(date[2]<<4)+(date[4]<<1)+(date[6]<<5)+(date[1]<<2)+(date[3]<<6)+(date[5]<<3)+(date[7]<<7);
STB_1=0;
TM1638_Write(0x42);
for(i=0;i<4;i++)
c1[i]=TM1638_Read();
STB_1=1; //4个字节数据合成一个字节
for(i=0;i<4;i++)
key_value1|=c1[i]<
for(i=0;i<8;i++)
{
if(key_value1&(1<
date[i]=1;
else
date[i]=0;
}
key_value1=date[0]+(date[2]<<4)+(date[4]<<1)+(date[6]<<5)+(date[1]<<2)+(date[3]<<6)+(date[5]<<3)+(date[7]<<7);
key_value=(key_value1<<8)+key_value;
return key_value;
}
void Write_DATA(unsigned char add,unsigned char DATA) //指定地址写入数据
{
Write_COM(0,0x44);
STB=0;
TM1638_Write(0xc0|add);
TM1638_Write(DATA);
STB=1;
}
void init_TM1638(void)
{
unsigned char i;
Write_COM(0,0x8f);//亮度
Write_COM(0,0x40); //写 数据命令
STB=0;
TM1638_Write(0xc0); //写地址命令
for(i=0;i<16;i++)
TM1638_Write(0x00);
STB=1;
Write_COM(1,0x8f);//亮度
Write_COM(1,0x40); //写 数据命令
STB_1=0;
TM1638_Write(0xc0); //写地址命令
for(i=0;i<16;i++)
TM1638_Write(0x00);
STB_1=1;
}
举报