单片机之秒表

                 用单片机控制数码管实现秒表功能

要求:秒表跑到10s时停止,蜂鸣器响一声,秒表精度为0.01s


代码张贴:

#include


#define SEGPORT P0

sbit dula=P1^1;

sbit wela=P1^0;

sbit deep = P1^2;

  

unsigned char code wei_table[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

unsigned char  tempdata[4];

unsigned char  p_table[10] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};

unsigned char code table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

 

unsigned char miao,m_miao;

void display_seg()

{

static unsigned char i = 0; 

  

SEGPORT=0xff;

wela = 1;

wela = 0;

    

SEGPORT =0x0;

dula = 1;

dula = 0;

 

    SEGPORT=wei_table[i];

wela = 1;

wela = 0;

 

SEGPORT = tempdata[i];  

dula = 1;

dula = 0;

i++;

if(4 == i)

{

i = 0;

}

}

 

void timer0_init()

{

EA = 1;

TMOD = 0x01;

TH0 = (65536 - 3000)/256;

TL0 = (65536 - 3000)%256;

ET0 = 1;

TR0 = 1;    

}

void timer1_init()

{

EA = 1;

TMOD |= 0x10;

TH1 = (65536 - 10000)/256;

TL1 = (65536 - 10000)%256;

ET1 = 1;

TR1 = 1;     

}    

void  timer0_isr() interrupt 1

{

TH0 = (65536 - 3000)/256;

TL0 = (65536 - 3000)%256; 

display_seg(); 

}

void  timer1_isr() interrupt 3

{

unsigned char i,j;

TH1 = (65536 - 10000)/256;

TL1 = (65536 - 10000)%256; 

m_miao++;

if(100 == m_miao)

{

m_miao = 0;

miao++;

if(10 == miao)

{

ET1 = 0;

tempdata[0] = table[miao/10];

tempdata[1] = p_table[miao%10];

tempdata[2] = table[m_miao/10];

tempdata[3] = table[m_miao%10];

for(j=0;j<200;j++)

{

i=50;

deep = ~deep;

while(i--);

}

}

}

 

tempdata[0] = table[miao/10];

tempdata[1] = p_table[miao%10];

tempdata[2] = table[m_miao/10];

tempdata[3] = table[m_miao%10];

}

 

void main()

{

   timer0_init();

   timer1_init();

   while(1);

}


你可能感兴趣的:(单片机)