我用的STC的
单片机,共阳极的数码管,用的驱动
芯片TM1638,你们看看我的程序,哪里出了问题,数码管不亮啊。帮忙指点下谢谢了。在线等~~~
电路图在下面。
程序:
#include <stc12c5a60.h>
#define DATA_COMMAND 0X40
#define DISP_COMMAND 0x80
#define ADDR_COMMAND 0XC0
#define uchar unsigned char
#define uint unsigned int
//TM1638模块引脚定义
sbit DIO=P2^1;
sbit CLK=P2^0;
sbit STB=P4^0;
void display1638();
//uchar data DisBuffer[8]={0,0,0,0,0,0,0,0,0,0}; /*显示缓存区*/ //各个数码管显示的值
//uchar code tab[]={0xc0,0xf9,0xa4,0xb0,
//0x99,0x92,0x82,0xf8,
//0x80,0x90,0x88,0x83,
//0xc6,0xa1,0x86,0x8e};
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;
}
}
/*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_COM(unsigned char cmd) //发送命令
{
STB=0;
TM1638_Write(cmd);
STB=1;
}
void Write_DATA(unsigned char add,unsigned char DATA) //指定地址写入数据
{
Write_COM(0x44); //设置固定地址
STB=0;
TM1638_Write(0xc0|add);
TM1638_Write(DATA);
STB=1;
}
void init_TM1638(void)
{
unsigned char i;
Write_COM(0x8a);//亮度
Write_COM(0x40); //写 数据命令
//Write_COM(0x88);//开显示
STB=0;
TM1638_Write(0xc0); //设置起始地址
for(i=0;i<16;i++)
TM1638_Write(0x00);
STB=1;
}
void main()
{
init_TM1638();
while(1)
{
display1638();
}
}
void display1638()
{
// CLK=0;
Write_DATA(0xc0,0x01);
Write_DATA(0xc2,0x01);
Write_DATA(0xc4,0x01);
Write_DATA(0xc6,0x01);
Write_DATA(0xc8,0x01);
Write_DATA(0xca,0x01);
Write_DATA(0xcc,0x00);
Write_DATA(0xce,0x00);
// CLK=1;
}
-
电路图
#include"tm1628_1.h"
uchar code tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};
//引脚定义
sbit DIO=P0^4;
sbit CLK=P0^5;
sbit STB_1=P0^6;
sbit 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]<<i;
for(i=0;i<8;i++)
{
if(key_value&(1<<i))
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]<<i;
for(i=0;i<8;i++)
{
if(key_value1&(1<<i))
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;
}
一周热门 更多>