用单片机的定时器T0定时,计时1秒,实现倒计时的秒表的功能。
程序如下
#include<reg51.h>
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}//共阳数码管段码表
unsigned char second//秒倒计时变量
unsigned char t0n//T0中断次数计数
sbit k1=P3^2//定义按键
void delay()
{
unsigned int j
for(j=400j>0j--)
}
void display()//显示子程序
{
P2=0x01
P0=tab[second/10]//显示十位
delay()
P2=0x02
P0=tab[second%10]//显示个位
delay()
}
void main()
{
TMOD=0x01//用T0定时,中断方式
TH0 =0x3C//晶振12M,定时50ms
TL0 =0xB0
IE =0x82
while(1)
{
display()//调显示子程序
if(k1==0)//按键按下
{
if(TR0==0)
{
TR0=1//启动开始计时
second=60//从60秒开始倒计时
}
else
TR0=0
while(k1==0)//等待按键释放
}
}
}
//T0中断程序
void T0_int() interrupt 1
{
TH0 =0x3C
TL0 =0xB0
t0n++
if(t0n>=20)//1秒到
{
t0n=0
second--//减1秒
if(second==0)TR0=0
}
}
这里的分段计时,我使用空格键实现的,F2比较麻烦。程序开始,输入回车开始计时,中途输入空格可以开始新的计时,最后输入回车完成计时。
文件存在程序目录下的timeout.txt
真麻烦,下次这种求助才给10分,绝对不做。。。
//////////////////////////
我的代码就是在VS2010下写的。。。怎么会无法编译。。。你要建一个空工程,然后加入C++源文件。
/////////////////////////////
请新建一个空工程,不要新建Win32 Console那个工程!
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
struct tm //定义时间结构体,包括时分秒和10毫秒
{
int hours,minutes,seconds
int hscd
}time,tmp,total //time用以计时显示,tmp用以存储上一阶段时间,total记总时间
int cnt
FILE* fout
//每次调用update函数,相当于时间过了10ms
void update(struct tm *t)
{
(*t).hscd++ //10ms单位时间加1
cnt++
if ((*t).hscd==100) //计时满1s,进位
{
(*t).hscd=0
(*t).seconds++
}
if ((*t).seconds==60) //计时满一分,进位
{
(*t).seconds=0
(*t).minutes++
}
if ((*t).minutes==60) //计时满一小时,进位
{
(*t).minutes=0
(*t).hours++
}
if((*t).hours==24) (*t).hours=0
//delay()
Sleep(10) //Sleep是windows提供的函数,作用是暂停程序,单位毫秒,所以此处暂停10ms
}
void display(struct tm *t)
{
//此处输出计时结果,\r为回车不换行,既一直在同一行更新时间
printf("%d:",(*t).hours)
printf("%d:",(*t).minutes)
printf("%d:",(*t).seconds)
printf("%d\r",(*t).hscd)
//printf("Now, press 'e' key to stop the clock...")
}
void time_init() //初始化时间
{
time.hours=time.minutes=time.seconds=time.hscd=0
}
void get_total() //计算总时间
{
total.hscd = cnt % 100
cnt /= 100
total.seconds = cnt % 60
cnt /= 60
total.minutes = cnt % 60
cnt /= 60
total.hours = cnt
}
int main()
{
char m
time_init()
cnt = 0
fout = fopen("timeout.txt","w")
printf("Now, press Enter key to begin the clock...\n")
while(1)
{
m = getch()
if(m != '\r') //读入一个输入,如果是回车,那么跳出次循环
printf("Input Error!\n")
else
break
}
printf("While counting, you can press space to start a new time counter!\n")
while(1)
{
if(kbhit()) //此处检查是否有键盘输入
{
m=getch()
if(m == '\r') //如果等于回车,那么计时结束,跳出循环
break
else if(m == ' ') //如果等于空格,显示此次计时,初始化计时器
{
tmp = time //记录上一段计时器结果
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd) //写入文件
time_init()
printf("\n")
}
else
{
printf("Input Error!\n")
}
}
update(&time) //更新计时器
display(&time) //显示计时器时间
}
tmp = time //输出最后一次即使结果,写入文件
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd)
get_total() //计算总的时间,显示,并写入文件
printf("\ntotal time:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd)
fprintf(fout,"total time:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd)
fclose(fout)
getch()
}
1,需建立一个1毫秒的中断定时器如timer0()。2,需建立一个全局变量毫秒变量,在中断里面累计计数,1毫秒加1一次。
3,需建立三个全局变量秒变量,分变量,时变量。
4,需做显示模块的处理。·
秒表开始工作时,毫秒变量在中断里面一直累计,在主循环里面做毫秒变量判断是否大于999 。如果大于999,秒变量加1,毫秒变量清零;如果秒变量大于59,分变量加1,秒变量清零;如果分变量大于59时,时变量加1,分变量清零。如果时变量大于23,时变量清零。秒表停止时,毫秒、秒、时、分变量不在累计。同时将这些变量送到数码管上显示。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)