當前位置:首頁 » 編程語言 » c語言秒錶計時器實現
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言秒錶計時器實現

發布時間: 2022-12-26 00:02:43

A. 用c語言編寫秒錶的程序(單片機控制的時鍾設計)

這個程序可以實現秒的計時,按鍵控制開始、暫停、清零功能,更多功能自己在看清程序的基礎上進行改進。
#include <reg51.H>
sbit P3_5 =P3^5;
unsigned char code dispcode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x00};
unsigned char second;
unsigned char keycnt;
unsigned int tcnt;
void main(void)
{
unsigned char i,j;
TMOD=0x02;
ET0=1;
EA=1;
second=0;
P1=dispcode[second/10];
P2=dispcode[second%10];
while(1)
{
if(P3_5==0)
{
for(i=20;i>0;i--)
for(j=248;j>0;j--);
if(P3_5==0)
{
keycnt++;
switch(keycnt)
{
case 1:
TH0=0x06;
TL0=0x06;
TR0=1;
break;
case 2:
TR0=0;
break;
case 3:
keycnt=0;
second=0;
P1=dispcode[second/10];
P2=dispcode[second%10];
break;
}
while(P3_5==0);
}
}
}
}
void t0(void) interrupt 1 using 0
{
tcnt++;
if(tcnt==4000)
{
tcnt=0;
second++;
if(second==100)
{
second=0;
}
P1=dispcode[second/10];
P2=dispcode[second%10];
}
}

B. 秒錶計時程序要求(需要用C語言實現)

這里的分段計時,我使用空格鍵實現的,F2比較麻煩。程序開始,輸入回車開始計時,中途輸入空格可以開始新的計時,最後輸入回車完成計時。

文件存在程序目錄下的timeout.txt

真麻煩,下次這種求助才給10分,絕對不做。。。

//////////////////////////

我的代碼就是在VS2010下寫的。。。怎麼會無法編譯。。。你要建一個空工程,然後加入C++源文件。

/////////////////////////////

請新建一個空工程,不要新建Win32Console那個工程!

#include<stdio.h>

#include<conio.h>

#include<windows.h>

#include<stdlib.h>

structtm//定義時間結構體,包括時分秒和10毫秒

{

inthours,minutes,seconds;

inthscd;

}time,tmp,total;//time用以計時顯示,tmp用以存儲上一階段時間,total記總時間

intcnt;

FILE*fout;

//每次調用update函數,相當於時間過了10ms

voipdate(structtm*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

}

voiddisplay(structtm*t)

{

//此處輸出計時結果, 為回車不換行,既一直在同一行更新時間

printf("%d:",(*t).hours);

printf("%d:",(*t).minutes);

printf("%d:",(*t).seconds);

printf("%d ",(*t).hscd);

//printf("Now,press'e'keytostoptheclock...");

}

voidtime_init()//初始化時間

{

time.hours=time.minutes=time.seconds=time.hscd=0;

}

voidget_total()//計算總時間

{

total.hscd=cnt%100;

cnt/=100;

total.seconds=cnt%60;

cnt/=60;

total.minutes=cnt%60;

cnt/=60;

total.hours=cnt;

}

intmain()

{

charm;

time_init();

cnt=0;

fout=fopen("timeout.txt","w");

printf("Now,pressEnterkeytobegintheclock... ");

while(1)

{

m=getch();

if(m!=' ')//讀入一個輸入,如果是回車,那麼跳出次循環

printf("InputError! ");

else

break;

}

printf("Whilecounting,! ");

while(1)

{

if(kbhit())//此處檢查是否有鍵盤輸入

{

m=getch();

if(m==' ')//如果等於回車,那麼計時結束,跳出循環

break;

elseif(m=='')//如果等於空格,顯示此次計時,初始化計時器

{

tmp=time;//記錄上一段計時器結果

fprintf(fout,"%d:%d:%d:%d ",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);//寫入文件

time_init();

printf(" ");

}

else

{

printf("InputError! ");

}

}

update(&time);//更新計時器

display(&time);//顯示計時器時間

}

tmp=time;//輸出最後一次即使結果,寫入文件

fprintf(fout,"%d:%d:%d:%d ",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);

get_total();//計算總的時間,顯示,並寫入文件

printf(" totaltime:%d:%d:%d:%d ",total.hours,total.minutes,total.seconds,total.hscd);

fprintf(fout,"totaltime:%d:%d:%d:%d ",total.hours,total.minutes,total.seconds,total.hscd);

fclose(fout);

getch();

}

C. 用c語言怎麼寫秒計時器

#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar tt,num;
uchar shi,ge,temp;
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,<br>0x66,0x6d,0x7d,0x07,<br>0x7f,0x6f};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar shi,uchar ge)
{
shi=temp/10;
ge=temp%10; P2=table[shi];
P3=0xfe;
delay(1);
P2=0x00; P2=table[ge];
P3=0xfd;
delay(1);
P2=0x00;
}
void timer0() interrupt 1
{
TH0=-50000/256;
TL0=-50000%256;
tt++;
if(tt>=20)
{
tt=0;
temp++;
if(temp==60)
temp=0;
}
}
void init()
{
TMOD=0x01;
TH0=-50000/256;
TL0=-50000%256;
EA=1;
ET0=1;
TR0=1;
tt=0;
temp=0;
}
void main()
{
init();
while(1)
{
display(shi,ge);
}
} 這個是60秒的,供參考

D. c語言中怎麼設置計時器

C語言計時可以用很多方法。
1.
如果是想使用秒級別的技術,可用使用C語言庫<time.h>自帶的clock()進行計時。如:
#include
<iostream>
#include
<time.h>
using
namespace
std;
int
main()
{
clock_t
start
=
clock();
//do
some
process
here
clock_t
end
=
(clock()
-
start)/CLOCKS_PER_SEC;
cout<<"time
comsumption
is
"<<end<<endl;
}
以上方法只能用於秒級別的計時。
2.如果想使用毫秒級別的計時可以使用2種方法。一種是使用linux的系統庫<sys/time.h>,一種是使用CUDA的cutil的庫。
A.
如果使用linux的系統庫,則可以使用如下方法:
#include
<sys/time.h>
int
main()
{
timeval
starttime,endtime;
gettimeofday(&starttime,0);
//do
some
process
here
gettimeofday(&endtime,0);
double
timeuse
=
1000000*(endtime.tv_sec
-
starttime.tv_sec)
+
endtime.tv_usec
-
startime.tv_usec;
timeuse
/=1000;//除以1000則進行毫秒計時,如果除以1000000則進行秒級別計時,如果除以1則進行微妙級別計時
}
timeval的結構如下:
strut
timeval
{
long
tv_sec;
/*
秒數
*/
long
tv_usec;
/*
微秒數
*/
};
上述方法可以進行微妙級別的計時,當然也可以進行毫秒和秒的計時。
B.
如果可以使用CUDA的話,則可以使用CUDA的sdk裡面的cutil庫裡面的函數。
#include
<cutil.h>
int
main()
{
unsigned
int
timer
=
0;
cutCreatTimer(&timer);//創建計時器
cutStartTimer(&timer);//開始計時
//
do
some
process
here
cutStopTimer(&timer);//停止計時
cout<<"time
is
"<<cutGetTimerValue(&timer)<<endl;//列印時間
}

E. c語言中怎麼設置計時器

#include <iostream>

#include <time.h>

using namespace std;

int main()

{

clock_t start = clock();

//do some process here

clock_t end = (clock() - start)/CLOCKS_PER_SEC;

cout<<"time comsumption is "<<end<<endl;

}

(5)c語言秒錶計時器實現擴展閱讀

使用linux的系統設置計時器

#include <sys/time.h>

int main()

{

timeval starttime,endtime;

gettimeofday(&starttime,0);

//do some process here

gettimeofday(&endtime,0);

double timeuse = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - startime.tv_usec;

timeuse /=1000;//除以1000則進行毫秒計時,如果除以1000000則進行秒級別計時,如果除以1則進行微妙級別計時

}

timeval的結構如下:

strut timeval

{

long tv_sec; /* 秒數 */

long tv_usec; /* 微秒數 */

};

F. 用C語言設計秒錶程序

int main()
{ char m;
struct tm time;
time.hours=time.minutes=time.seconds=0;
printf("Now, press 's' key to begin the clock...\n");
m=getch();
if(m=='s'||m=='S')
while(1)
{
while(!kbhit()) {
update(&time);
display(&time);}
m=getch();
if(m=='s'||m=='S')break;
else{ printf("Only 's' or 'S' can be inputed!!");}
}
}
把main函數改成這樣看看行不行