毫秒编程
㈠ 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();