单片机中断定时

来源:互联网 发布:梦幻花园无法加载 网络 编辑:程序博客网 时间:2024/04/30 21:15
#include<reg52.h>
#define Dataport P0
sbit SPK=P1^2;    //定义喇叭端口
unsigned char frq; 


sbit   Latch_seg = P2^2; //段
sbit   Latch_bit = P2^3; //位
unsigned char arr_bit[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char arr_seg[10] = {0x3F,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f/*,0x77,0x7C,0x39,0x5E,0x79,0x71*/};
//                             0   1   2     3     4    5   6    7    8    9     
unsigned char arr_tel[16] = {0x06,0x07,0x07,0x7d,0x7f,0x06,0x5b,0x66,0x06,0x7f,0x4f,0x00,0x00,0x00,0x00,0x00};
//                              1   7    7     6   8    1    2    4    1    8   3
unsigned long int j,k;


void DelayUs2x(unsigned char t)
{   
 while(--t);
}
/*------------------------------------------------
 mS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
     
 while(t--)
 {
     //大致延时1mS
     DelayUs2x(245);
DelayUs2x(245);
 }
}
void isr_r0(void) interrupt 1
{

TH0 = 0xd8;
TL0 = 0xf0;
j++;
if(j == 100)
{
j = 0;
k++;
DelayUs2x(200); 
 DelayUs2x(200); 
 SPK=!SPK;
}
}
void delay(unsigned int i)
{
while(--i);
}
void tel()
{
unsigned int i;
while(1)
{
Dataport = arr_bit[i]; 
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_tel[(i+k)%16];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
i++;
if(i == 8)
i = 0;
}
}
void clock()
{
unsigned int hour,min,sec = 0;


hour = k/3600;
  Dataport = arr_bit[0];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[hour/10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
Dataport = arr_bit[1];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[hour%10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
if(hour == 24)
hour = 0;


Dataport = arr_bit[2];
Latch_bit = 1;
Latch_bit = 0;
Dataport = 0x40;
Latch_seg = 1;
Latch_seg = 0;


min = k%3600/60;
  Dataport = arr_bit[3];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[min/10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
Dataport = arr_bit[4];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[min%10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);


Dataport = arr_bit[5];
Latch_bit = 1;
Latch_bit = 0;
Dataport = 0x40 ;
Latch_seg = 1;
Latch_seg = 0;


sec = k%60;
  Dataport = arr_bit[6];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[sec/10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
Dataport = arr_bit[7];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[sec%10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);




}


void miaobiao()
{


unsigned int min,sec,msec = 0;


min = k%3600/60;
  Dataport = arr_bit[0];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[min/10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
Dataport = arr_bit[1];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[min%10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);


Dataport = arr_bit[2];
Latch_bit = 1;
Latch_bit = 0;
Dataport = 0x40;
Latch_seg = 1;
Latch_seg = 0;


sec = k%60;
  Dataport = arr_bit[3];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[sec/10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
Dataport = arr_bit[4];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[sec%10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);





Dataport = arr_bit[5];
Latch_bit = 1;
Latch_bit = 0;
Dataport = 0x40 ;
Latch_seg = 1;
Latch_seg = 0;


msec = j;
  Dataport = arr_bit[6];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[msec/10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
Dataport = arr_bit[7];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[msec%10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);

}


void boom()
{
unsigned int i,sec,msec = 0;
sec = k;
  Dataport = arr_bit[3];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[sec/10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
Dataport = arr_bit[4];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[9-sec];
Latch_seg = 1;
Latch_seg = 0;
delay(200);





Dataport = arr_bit[5];
Latch_bit = 1;
Latch_bit = 0;
Dataport = 0x40 ;
Latch_seg = 1;
Latch_seg = 0;
  delay(200);
msec = j;
  Dataport = arr_bit[6];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[9-msec/10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);
Dataport = arr_bit[7];
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[9-msec%10];
Latch_seg = 1;
Latch_seg = 0;
delay(200);


if(k == 10)
{
k = 0;
for(i=0;i<500;i++)
      {
      DelayMs(1);  
 SPK=!SPK;
 }
}
}




void main()
{
 

TMOD = 0x01;
TH0 = 0xd8;
TL0 = 0xf0;
EA = 1;
ET0 = 1;
TR0 = 1;


while(1)
{

/*Dataport = 0xfe;
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[k/10];
Latch_seg = 1;
Latch_seg = 0;
delay(100);
Dataport = 0xfd;
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[k%10];
Latch_seg = 1;
Latch_seg = 0;
delay(100);
if(k == 60)
k = 0; */
//tel();
//miaobiao();
//boom();
clock();
}
}
0 0
原创粉丝点击