单片机中断定时

该博客介绍了一种使用单片机中断技术进行精确计时的方法,通过中断定时函数更新显示,并展示了如何在单片机中设置中断、计数以及驱动数码管显示时间。文中提供了中断服务子程序、延时函数以及主程序的详细代码实现。
摘要由CSDN通过智能技术生成
#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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值