當前位置:首頁 » 編程軟體 » 毫秒編程

毫秒編程

發布時間: 2023-06-14 02:11:32

㈠ C語言編程計時器如何工作

秒錶計時器的代碼
#include

#include

#include

#include

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("按回車鍵開始計時!\n");
while(1)
{
m
=
getch();
if(m
!=
『\r』)
//讀入一個輸入,如果是回車,那麼跳出次循環
printf("輸入錯誤,僅能輸入回車鍵!\n");
else
break;
}
printf("已經開始計時,但是你可以按回車鍵以分段計時!\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("輸入錯誤,僅支持輸入回車鍵或者空格鍵!\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("\n總時間:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fprintf(fout,"統計時間:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fclose(fout);
printf("已經保存到當前目錄下的timeout.txt文件中按任意鍵結束!");
getch();
}

㈡ C語言編程 關於計算時間的問題 望高手解答!

希望能夠我的思路可以幫助你:
①如果password="124567"時,歡迎進入!
②如果password != "124567"時,等待15分鍾!
③等待15分鍾後返回重新輸入密碼!

#include <stdio.h>
#include <string.h>
#include<windows.h>
int main()
{
char str[20], password;
int x,i;
//執行4次循環0,1,2,3
for(x=0; x<=3 && strcmp(str,"1234567")!=0; x++)
{
printf("Enter password please:");
scanf("%s",&str);
//當密碼錯誤時提示輸入錯誤!
if(strcmp(str,"1234567")!=0)
{
printf("Input error!\n");
}
//當錯誤了3次時執行等待,並重置x的初值
if(x==2)
{
printf("Please wait another 15 min.");
for(i=0;i<=(15*60);i++)
Sleep(1000); //停滯一秒
//重置x的初值
x=0;
}
else
//密碼輸入正確時跳出循環,執行for循環之外的語句
{
if(strcmp(str,"1234567")==0)
printf("Welcome\n");
break;
}
}
//可以插入驗證後要執行的代碼
return 0;
}

㈢ C# 關於DateTime類型 精確到毫秒

datetime包含毫秒,要格式化輸出,用fff

DateTime t = DateTime.Now;

Console.WriteLine(t.ToString("yyyy-MM-dd hh:mm:ss fff"));

註:mysql裡面的datetime類型的精確度是可以到1/ 10 ^ 6 秒的,某些客戶端(如navicat for mysql)的顯示經常只能看到精確到秒,其實是設計表的時候的配置問題。

(3)毫秒編程擴展閱讀:

mysql中DateTime和Timestamp

DateTime

1、8個位元組儲存(8 bytes storage)

2、實際格式儲存(Just stores what you have stored and retrieves the same thing which you have stored.)

3、與時區無關(It has nothing to deal with the TIMEZONE and Conversion.)

4、存儲的時間范圍為:'1000-01-01 00:00:00.000000' 到 '9999-12-31 23:59:59.999999'

Timestamp

1、4個位元組儲存(Time stamp value is stored in 4 bytes)

2、值以UTC格式保存( it stores the number of milliseconds)

3、時區轉化 ,存儲時對當前的時區進行轉換,檢索時再轉換回當前的時區。

4、存儲的時間范圍為:'1970-01-01 00:00:01.000000' 到 '2038-01-19 03:14:07.999999'

㈣ 如何利用單片機匯編指令編程實現延時10ms利用匯編指令設計多重循環編程實現20ms延時

這是調用一個延時10毫秒的延時子程序 當然,子程序的名字可以自己定 DELY1MS: 這個名字一定要與調用中出現的名字一樣,否則的話,調用的就不是這個子程序了

DELAY10MS:
MOV R2,#20
DELAY10MS1:
MOV R3,#250
DJNZ R3,$
DJNZ R3,DELAY10MS1
RET

這是調用一個延時20毫秒的延時子程序 當然,子程序的名字可以自己定 DELY1MS: 這個名字一定要與調用中出現的名字一樣,否則的話,調用的就不是這個子程序了

DELAY20MS:
MOV R2,#40
DELAY20MS1:
MOV R3,#250
DJNZ R3,$
DJNZ R3,DELAY20MS1
RET

RETRET的作用是子程序返回。調用一個子程序,當單片機執行到RET語句的時候,單片機就會返回到調用的地方並繼續向下執行。

下面給你一個常式:

閃爍燈

ORG 00H

AJMP START

ORG 30H

START:

MOV P0,#00H

LCALL DELY;調用延時子程序

MOV P0,#0FFH

LCALL DELY;調用延時子程序

AJMP START

DELY:

MOV R7,#250

DL1:MOV R6,#250

DJNZ R6,$

DJNZ R7,DL1

RET

㈤ 單片機延時時間程序怎麼編程

舉一個例子來說明吧.比如你要編一個延時50毫秒的子程序,那麼步驟如下:
1、查看一下你的硬體環境,比如晶振大小,CPU型號,不用CPU指令的機器周期是不一樣的。
2、計算延時需要的機器周期。比如採用12M晶振,CPU採用通用8051,那麼一個機器周期為1US,50毫秒為50*1000=50000US,需要的機器周期=50000/1=50000。
3、試編程,如下:

程序代碼 指令時間 總共時間
DELAY50MS: ;2 2
MOV R7,#A ;1 1
DELAY1:
MOV R6,#B ;1 1*A
DJNZ R6,$ ;2 2*B*A
DJNZ R7,DELAY1 ;2 2*A
RET ;2 2
所以總時間=2+1+A+2*A*B+2*A+2=5+3A+2AB
4、湊數求A、B
根據2、3得到如下式子:
50000=5+3A+2AB
可以有很多種結果,不過最好是以A盡可能小,B盡可能大為原則,當然不能大於255.
我現在湊出A=110,B=225;那麼總延時時間=5+3*110+2*110*225=49835。還差165US
5、補齊不夠時間
再加一個小循環就OK了,呵呵如下:
MOV R6,#C
DJNZ R6,$
會算了吧,2*C+1=165;所以C=82。
現在完整的延時程序出來了,如下:
DELAY50MS: ;2 2
MOV R7,#110 ;1 1
DELAY1:
MOV R6,#225 ;1 1*110
DJNZ R6,$ ;2 2*225*110
DJNZ R7,DELAY1 ;2 2*110
MOV R6,#82 ;1 1
DJNZ R6,$ ;2 2*82
RET ;2 2
很圓滿:總的時間50000微妙,也就是50毫秒。這種方式編程,在該硬體環境下可以保證最大誤差為1微妙。

php 輸出時間。格式為:「時:分:秒.毫秒」,例如:19:37:05.380,要用什麼函數怎麼實現

<?php

function udate($format = 'u', $utimestamp = null) {

if (is_null($utimestamp))

$utimestamp = microtime(true);

$timestamp = floor($utimestamp);

$milliseconds = round(($utimestamp - $timestamp) * 1000000);

return date(preg_replace('`(?<!\\)u`', $milliseconds, $format), $timestamp);

}

echo udate('Y-m-d H:i:s.u');

?>

㈦ VC++編程中 如何獲取當前時間(精確到毫秒)

1、直接利用Pentium CPU內部時間戳進行計時的高精度計時手段。
2、在 Intel Pentium以上級別的CPU中,有一個稱為「時間戳(Time Stamp)」的部件,它以64位無符號整型數的格式,記錄了自CPU上電以來所經過的時鍾周期數。由於目前的CPU主頻都非常高,因此這個部件可以達到納秒級的計時精度。
3、因為RDTSC不被C++的內嵌匯編器直接支持,所以要用_emit偽指令直接嵌入該指令的機器碼形式0X0F、0X31,如下:
inline unsigned __int64 GetCycleCount()
{
__asm _emit 0x0F
__asm _emit 0x31
}
4、在需要計數器的場合,可以像使用普通的Win32 API一樣,調用兩次GetCycleCount函數,比較兩個返回值的差,像這樣:
unsigned long t;
t = (unsigned long)GetCycleCount();
//Do Something time-intensive ...
t -= (unsigned long)GetCycleCount();

熱點內容
php樹菜單 發布:2025-02-09 10:04:10 瀏覽:359
linux保存ip 發布:2025-02-09 10:04:10 瀏覽:23
四川霜狼伺服器怎麼樣 發布:2025-02-09 10:02:44 瀏覽:145
Vs中h編譯選項是灰的 發布:2025-02-09 10:01:59 瀏覽:556
安卓43怎麼升級44 發布:2025-02-09 09:51:33 瀏覽:463
美國雲伺服器快還是香港快 發布:2025-02-09 09:34:33 瀏覽:988
怎麼解壓qq文件 發布:2025-02-09 09:18:14 瀏覽:581
安卓最新怎麼調靈敏度更穩 發布:2025-02-09 09:12:44 瀏覽:400
豌豆莢如何用安卓手機下載 發布:2025-02-09 09:11:57 瀏覽:213
吃雞腳本輔助 發布:2025-02-09 09:09:29 瀏覽:6