数码管的共阴极和共阳极:
c 图中:阴极连在一起,阳极独立,阳极置高便亮。
b 图中:阳极连在一起,用上拉电阻,使得led变化。
//下面的数组都需要在myhead.h内申明,然后在这儿定义
//注意只要在第一次使用的.c文件中定义
xdata unsigned char segdata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//段码
xdata unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位码
xdata unsigned char tempdata[8] = {0};
xdata unsigned char hour = 20;
xdata unsigned char min = 39;
xdata unsigned char sec = 45;
void display()
{
static unsigned char i = 0;
//1为打开,0为关闭
PORT = 0xff;//消重影
bit_select = 1;//bit_select在myhead中定义
bit_select = 0;
PORT = 0x0;
seg_select = 1;//seg_select在myhead.h中定义
seg_select = 0;
PORT = tempdata[i];
seg_select = 1;
seg_select = 0;
PORT = bitdata[i];
bit_select = 1;
bit_select = 0;
i++;
if(8 == i)
{
i = 0;
}
}
void time0_init(void)//定时器0初始化
{
EA = 1;//打开总开关
TMOD |= 0x01;//设置工作方式,|= 不影响定时器1
TH0 = (65536 - 20000) / 256;
TL0 = (65536 - 20000) % 256;//设置定时器初值,20ms产生中断
ET0 = 1;//打开定时器中断0
TR0 = 1;//打开定时器0
}
void time0_isr() interrupt 1//定时器0的中断服务函数
{
static unsigned char j = 0;
TH0 = (65536 - 20000) / 256;
TL0 = (65536 - 20000) % 256;
j++;
if(50 == j)//50是根据设置的初值得来,初值是20ms刷新一次,当j = 50 就过了一秒
{
j = 0;
sec++;
if(60 == sec)
{
sec = 0;
min++;
if(60 == min)
{
min = 0;
hour++;
if(24 == hour)
{
hour = 0;
}
}
}
}
/*将时间值分离成十位和各位*/
tempdata[0] = segdata[hour / 10];
tempdata[1] = segdata[hour % 10];
tempdata[2] = 0x40;
tempdata[3] = segdata[min / 10];
tempdata[4] = segdata[min % 10];
tempdata[5] = 0x40;
tempdata[6] = segdata[sec / 10];
tempdata[7] = segdata[sec % 10];
}<pre name="code" class="html">#ifndef _DIGITAL_H
#define _DIGITAL_H
extern void display();
extern void time0_init(void);
//这里不能加interrupt 1(中断序列号)
extern void time0_isr();
extern void digital();
#endif
void digital()
{
unsigned char flag = 1;
unsigned char key;
time0_init();
while(flag)
{
display();
key = key_scan();
if(0xe7 == key)
{
lcd_over();
flag = 0;
PORT = 0xff;
}
}
}1