#define
_TM1618_H
#define LEDS_NUM
4
#define uchar unsigned char
#define uint
unsigned int
#include
<./Atmel/REG51.H>
#define BYTE unsigned char
//引脚定义
sbit
DIO=P2^0;
sbit
CLK=P2^1;
sbit
STB=P2^2;
uchar keydone;
uchar keyprocess;
uchar keymark;
uchar blink;
uchar const led_cs[LEDS_NUM] =
{
0xfb,
//11111011
0xf7,
//11110111
0xef,
//11101111
0xdf,
//11011111
};
uchar led_buf[LEDS_NUM] =
{
0x73, 0x81, 0x82, 0x83
};
uchar *pb = &led_buf[1]; //定义指向数码管数据缓冲区的指针
uchar keypre[2] =
{
0x00, 0x00
}; //存放上次功能键和上档键的键值
unsigned char code tab[]={
0x3F
// 0
,0x06
// 1
,0x5B
// 2
,0x4F
// 3
,0x66
// 4
,0x6D
// 5
,0x7D
// 6
,0x07
// 7
,0x7F
// 8
,0x6F
// 9
,0x77
// A
,0x7C
// b
,0x39
// C
,0x5E
// d
,0x79
// E
,0x71
// F
,0x40
// -
,0x80
// .
,0x00
// 全灭
};
void TM1618_WriteBYTE(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_command(unsigned char cmd)
//发送命令字
{
STB=0;
TM1618_WriteBYTE(cmd);
STB=1;
}
uchar KEY[4]={0};
//为存储按键值开辟的数组
void read_key()
//功能:读取按键值并存入KEY[] 数组,从低字节开始,从低位开始
{
uchar i,j;
Write_command(0x42);
//读键盘命令
DIO=1;
//释放DIO数据总线
for(j=0;j<3;j++) //连续读取3个字节
for(i=0;i<8;i++)
{
KEY[j]=KEY[j]>>1;
CLK=0;
CLK=1;
if(DIO)
KEY[j]=KEY[j]|0X80;
}
STB=1;
}
void Write_data(unsigned char add,unsigned char DATA)
//指定地址写入数据
{
Write_command(0x44);//使用固定地址模式
STB=0;
TM1618_WriteBYTE(0xc0|add);
TM1618_WriteBYTE(DATA);
STB=1;
}
void Write_num(BYTE add, BYTE DATA)
{
Write_command(0x40);
STB=0;
TM1618_WriteBYTE(0xc0|add);
TM1618_WriteBYTE(DATA&0x1F);//seg1~seg5
STB=1;
STB=0;
TM1618_WriteBYTE(0xc0|add+1);
TM1618_WriteBYTE((DATA&0xE0)>>2);//seg12~seg14
STB=1;
}
void key1_handler()
{
uchar s = 0;
if (keymark)
{
for(s=0;s<8;s++)
Write_num(s,tab[18]);
}
}
void key2_handler()
{
if (keymark)
{
*pb += 1;
if (*pb >= 16)
*pb = 0;
}
}
void key3_handler()
{
if (keymark)
{
if (*pb <= 0)
*pb = 16;
else
*pb -= 1;
}
}
void key4_handler()
{
blink = 0x00;
led_buf[0] = 0x73;
keymark = 0;
}
void init_TM1618(void)
{
unsigned char i;
Write_command(0x00);//4位8段模式
Write_command(0x8b);//亮度
Write_command(0x40);//地址自动+1模式
STB=0;
TM1618_WriteBYTE(0xc0);
for(i=0;i<16;i++)
TM1618_WriteBYTE(0x00);
STB=1;
}
void key_processing()
//功能:按键数据处理
{
if (keyprocess)
{
keydone = 0;
keyprocess = 0;
switch (keypre[1])
{
case 0x00:
switch (keypre[0])
{
case 0x01:
key1_handler();
break;
case 0x02:
key2_handler();
break;
case 0x03:
key3_handler();
break;
case 0x04:
key4_handler();
break;
}
}
keydone = 1;
}
}
# endif
// 晶振: 11.0592 MHz
#include
<./Atmel/REG51.h>
#include
"TM1618.H"
#define uchar unsigned char
//255
#define uint unsigned int
//65535
void delay_100ms(void)
//误差 0ms
{
unsigned char a,b,c;
for(c=19;c>0;c--)
for(b=20;b>0;b--)
for(a=130;a>0;a--);
}
main()
{
uchar i = 0;
uchar num[4] = {0};
init_TM1618();
for(i=0;i<18;i++)
{
Write_num(0,tab[0]-tab[17]);
Write_num(2,tab[0]);
Write_num(4,tab[0]);
Write_num(6,tab[0]);
delay_100ms();
}
while(1)
{
read_key();
//读按键值
key_processing();
//按键处理
num[i]++;
if(num[i] >15)
{
num[i]=0;
}
Write_num(i*2,tab[num[i]]);
}
}
看下有什么问题