51单片机写数字钟2

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include

sbit SCLK=P2^4;
sbit SDAI=P2^5;
sbit LCLK=P1^5;
sbit KEY0=P3^2;

unsigned char led_buff[]={0,0,0,0,0,0,0,0};
unsigned char code led_wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char code led_code[]={0x03,0x9F,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0xfd,0xff};

unsigned long int counter;
unsigned char counter_c;

void delay(unsigned int t)
{
while(t--);
}

void write_byte(unsigned d)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(d & 0x1) SDAI=1;
else SDAI=0;
SCLK=0; //一个上升沿
SCLK=1;
d = d>>1;
}
}

void led_disp(void)
{
static unsigned char i=0;
write_byte(led_code[led_buff[i]]);
write_byte(led_wei[i]);
LCLK=1; //上升沿
LCLK=0;
i++;
if(i>7)i=0;
}

void led_init(void)
{
SDAI=0;
SCLK=0;
LCLK=0;
}

void Timer0(void) interrupt 1
{
counter_c++;
}

void Timer1(void) interrupt 3
{
static unsigned int i=0;
TH1=0xE9;
TL1=0x9A;
if(i>318)
{
counter=counter_c*65535+TH0*256+TL0;
TL0=0;
TH0=0;
counter_c=0;
i=0;
}
else i++;
led_disp();
}

main()
{
unsigned char key_flag;
unsigned char m_on;
unsigned char delay_cnt;
led_init();

TMOD=0x15;
TR0=1;
ET0=1;
TR1=1;
ET1=1;
EA=1;

counter=0;
key_flag=0;
m_on=0;
delay_cnt=0;

while(1)
{
led_buff[7] = counter%10;
led_buff[6] = counter/10%10;
led_buff[5] = counter/100%10;
led_buff[4] = counter/1000%10;
led_buff[3] = counter/10000%10;
//counter = counter/10;
//led_buff[2] = counter%10;
//counter = counter/10;
//led_buff[1] = counter%10;
//counter = counter/10;
//led_buff[0] = counter%10;

delay(1);


}

}

相关文档
最新文档