单片机 中断秒表 c语言程序,51单片机秒表C程序

本程序所用的原理图下载: 点这里 ,单片机芯片使用的stc89c52;电路找到相应部分即可.这是一整个单片机开发板的电路图其他的忽略.

本程序的keil工程下载: http://www.51hei.com/f/miaobiao.rar   以下是通过测试的源代码:

/*

*功能:用定时器0实现秒表,实现60秒定时,精确度为1毫秒

*   利用key1独立按键实现定时器的启动和停止,

*   利用key2独立按键实现秒表的清零;

*日期:2013-03-24

*作者:徐冉

*注意事项:若打开两个定时器时,必须使用两个定时器,

*     否则两个定时器都不工作!!!

**/

/**********stc89C52-RC 51hei单片机实验板**********/

/**********51hei-5开发板**********/

#include

typedef unsigned int uint;

typedef unsigned char uchar;

sbit wela = P2^7;

sbit dula = P2^6;

sbit key1 = P3^4;

sbit key2 = P3^5;

sbit FM   = P2^3;//蜂鸣器

uchar code table[] = {

0x3F,  //"0"

0x06,  //"1"

0x5B,  //"2"

0x4F,  //"3"

0x66,  //"4"

0x6D,  //"5"

0x7D,  //"6"

0x07,  //"7"

0x7F,  //"8"

0x6F,  //"9"

0x77,  //"A"

0x7C,  //"B"

0x39,  //"C"

0x5E,  //"D"

0x79,  //"E"

0x71,  //"F"

0x76,  //"H"

0x38,  //"L"

0x37,  //"n"

0x3E,  //"u"

0x73,  //"P"

0x5C,  //"o"

0x40,  //"-"

0x00,  //熄灭

0x40  //自定义

};

uint counter, num_h, num_m;

void delay(uint xms);

void display_h(uint num_h);

void init();

void display_m(uint num_m);

void keyscan_D();

void keyscan_Q();

//主函数

void main()

{

init();

while(1)

{

dula = 1;

P0 = table[22];

dula = 0;

P0 = 0xff;

wela = 1;

P0 = 0xfb;

wela = 0;

keyscan_D();

keyscan_Q();

display_h(num_h);

display_m(num_m);

}

}

//定时器0初始化函数

void init()

{

TMOD = 0x01;

TH0 = 0xFC;

TL0 = 0x67;

TR0 = 1;

EA = 1;

ET0 = 1;

}

//毫秒位显示子函数

void display_h(uint num_h)

{

uchar bai,shi,ge;

bai = num_h / 100 % 10;

shi = num_h / 10 % 10;

ge  = num_h % 10;

dula = 1;

P0 = table[bai];

dula = 0;

P0 = 0xff;

wela = 1;

P0 = 0xf7;

wela = 0;

P0 = 0x00;

delay(1);

dula = 1;

P0 = table[shi];

dula = 0;

P0 = 0xff;

wela = 1;

P0 = 0xef;

wela = 0;

P0 = 0x00;

delay(1);

dula = 1;

P0 = table[ge];

dula = 0;

P0 = 0xff;

wela = 1;

P0 = 0xdf;

wela = 0;

P0 = 0x00;

delay(1);

}

//秒位显示子函数

void display_m(uint num_m)

{

uchar shi_, ge_;

shi_ = num_m / 10 % 10;

ge_ = num_m % 10;

dula = 1;

P0 = table[shi_];

dula = 0;

P0 = 0xff;

wela = 1;

P0 = 0xfe;

wela = 0;

P0 = 0x00;

delay(1);

dula = 1;

P0 = table[ge_];

dula = 0;

P0 = 0xff;

wela = 1;

P0 = 0xfd;

wela = 0;

P0 = 0x00;

delay(1);

}

//延时子函数

void delay(uint xms)

{

uint i, j;

for(i = xms; i > 0; i--)

for(j = 125; j > 0; j--);

}

//独立按键1检测子函数 ,开启和关闭

void keyscan_D()

{

if(key1 == 0)

{

delay(10);

if(key1 == 0)

{

TR0 = ~TR0;

FM = 0;

while(!key1);

FM = 1;

}

}

}

//独立按键2检测子函数,清零

void keyscan_Q()

{

if(key2 == 0)

{

delay(10);

if(key2 == 0)

{

counter = 0;

num_h = 0;

num_m = 0;

FM = 0;

while(!key2);

FM = 1;

}

}

}

//定时器0中断子程序

void int_time0() interrupt 1

{

TH0 = 0xFC;

TL0 = 0x67;

counter++;

if(counter == 1)

{

counter = 0;

num_h++; //毫秒自增

if(num_h == 1000)

{

num_h = 0;

num_m++; //秒自增

if(num_m == 60)

{

num_m = 0;

}

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值