您的位置 首页 汽车

51单片机秒表C程序

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

本程序所用的原理图下载:点这里,单片机芯片运用的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;
}
}
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/254027.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部