您的位置:首页 > 其它

seg显示时间——51程序

2016-09-11 10:52 459 查看
#include
#define SEGPORT P0

sbit bit_select = P2 ^ 0;   //p2 ^ 0控制位选锁存器,位选锁存器选定 数码管
sbit seg_select = P2 ^ 1;   //p2 ^ 1控制段选锁存器,段选锁存器选定 数码管的内容(并显示内容)。

unsigned char segdata[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char tempdata[8] = 0x0;
unsigned char shi = 23;
unsigned char fen = 59;
unsigned char miao = 50;
unsigned int count = 0;

void display()          //显示刷新函数
{
static unsigned char i = 0;

SEGPORT = 0x0;      //清屏操作,消除重影
seg_select = 1;
seg_select = 0;
SEGPORT = 0xff;
bit_select = 1;
bit_select = 0;

SEGPORT = bitdata[i];   //选定并显示特定数码管的内容
bit_select = 1;
bit_select = 0;
SEGPORT = tempdata[i];
seg_select = 1;
seg_select = 0;

i++;
if(i == 8)
{
i = 0;
}
}

void timer0_init(void)      //定时器中断0,计算时间,优先级大于中断1
{
EA = 1;
TMOD = 0x01;

TH0 = (65536 - 2000) / 256;
TL0 = (65536 - 2000) % 256;

ET0 = 1;
TR0 = 1;
}

void timer1_init(void)      //定时器中断1,显示时间
{
EA = 1;
TMOD = 0x11;

TH1 = (65536 - 2000) / 256;
TL1 = (65536 - 2000) % 256;

ET1 = 1;
TR1 = 1;
}

void main()
{
timer0_init();
timer1_init();
while(1);
}

void timer0_isr() interrupt 1
{
TH0 = (65536 - 2000) / 256;
TL0 = (65536 - 2000) % 256;

count++;
if(500 == count)
{
count = 0;
miao++;
if(60 == miao)
{
miao = 0;
fen++;
}
if(60 == fen)
{
fen = 0;
shi++;
}
if(24 == shi)
{
shi = 0;
}
}
tempdata[0] = segdata[shi / 10];
tempdata[1] = segdata[shi % 10];
tempdata[2] = 0x40;
tempdata[3] = segdata[fen / 10];
tempdata[4] = segdata[fen % 10];
tempdata[5] = 0x40;
tempdata[6] = segdata[miao / 10];
tempdata[7] = segdata[miao % 10];
}

void timer1_isr() interrupt 3
{
TH1 = (65536 - 2000) / 256;
TL1 = (65536 - 2000) % 256;
display();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: