以下是改后的程序:就改了端口!TM1638模块能显示了!就是按键值没有!
#ifndef_TM1638_H
#define_TM1638_H
//#defineDATA_COMMAND0X40
//#defineDISP_COMMAND0x80
//#defineADDR_COMMAND0XC0
//引脚定义
//sbit DIO=P3^1;
//sbit CLK=P3^2;
//sbit STB=P3^3;
//sbit DIO=P1^2;
//sbit CLK=P1^4;
//sbit STB=P1^3;
unsigned char const tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
void TM1638_Write(unsigned charDATA)//写数据函数
{
unsigned char i;
PC_DDR = 0x38;
PC_CR1 = 0x00; //
PC_CR2 = 0x00;
for(i=0;i<8;i++)
{
PC_ODR = PC_ODR & 0xDF;//CLK=0;
if(DATA&0X01)
PC_ODR = PC_ODR | (~0xEF);//DIO=1;
else
PC_ODR = PC_ODR & 0xEF;//DIO=0;
DATA>>=1;
PC_ODR = PC_ODR | (~0xDF);//CLK=1;
}
}
unsigned char TM1638_Read(void)//读数据函数
{
unsigned char i;
unsigned char temp=0;
PC_DDR = 0x28;
PC_CR1 = 0x00; //
PC_CR2 = 0x00;
//PC_CR1 = 0xFF; //
//PC_CR2 = 0x00;
//DIO=1;//设置为输入
for(i=0;i<8;i++)
{
temp>>=1;
PC_ODR = PC_ODR & 0xDF;//CLK=0;
if((PC_IDR | 0xEF)==0xEF)//if(DIO)
{temp|=0x80;}
PC_ODR = PC_ODR | (~0xDF);//CLK=1;
}
return temp;
}
void Write_COM(unsigned char cmd)//发送命令字
{
PC_ODR = PC_ODR & 0xF7;//STB=0;
TM1638_Write(cmd);
PC_ODR = PC_ODR | (~0xF7);//STB=1;
}
unsigned char Read_key(void)
{
unsigned char c[4],i,key_value=0;
PC_ODR = PC_ODR & 0xF7;//STB=0;
TM1638_Write(0x42);
for(i=0;i<4;i++)
c[i]=TM1638_Read();
PC_ODR = PC_ODR | (~0xF7);//STB=1;//4个字节数据合成一个字节
for(i=0;i<4;i++)
key_value|=c[i]<<i;
for(i=0;i<8;i++)
if((0x01<<i)==key_value)
break;
return i;
}
void Write_DATA(unsigned char add,unsigned char DATA)//指定地址写入数据
{
Write_COM(0x44);
PC_ODR = PC_ODR & 0xF7;//STB=0;
TM1638_Write(0xc0|add);
TM1638_Write(DATA);
PC_ODR = PC_ODR | (~0xF7);//STB=1;
}
void Write_oneLED(unsigned char num,unsigned char flag)//单独控制一个LED函数,num为需要控制的led序号,flag为0时熄灭,不为0时点亮
{
Write_DATA(2*num+1,flag&0x03);
}
/*void Write_allLED(unsigned char LED_flag)//控制全部LED函数,LED_flag表示各个LED状态
{
unsigned char i;
for(i=0;i<8;i++)
Write_DATA(2*i+1,LED_flag&0x03);
} */
void init_TM1638(void)
{
unsigned char i;
Write_COM(0x88);//亮度调节 取值:0X88---0X8F,0X88最暗,0X8F最亮。
Write_COM(0x40);
PC_ODR = PC_ODR & 0xF7;//STB=0;
TM1638_Write(0xc0);
for(i=0;i<16;i++)
TM1638_Write(0x00);
PC_ODR = PC_ODR | (~0xF7);//STB=1;
}
#endif