C语言实现计时范文
- 格式:docx
- 大小:36.65 KB
- 文档页数:2
C语言计时函数(2012-05-16 09:30:18)转载▼分类:c*cpp标签:杂谈1. <time.h>中函数clock(),返回类型clock_t,精确度,毫秒级别实例:#include <stdio.h>#include <time.h>#include <math.h>void test(){int i = 0;int j = 0;double a = 0;while (i++ < 1000000)while (j++ < 1000000){a = sqrt(2.0);}}int main(void){clock_t start, finish;double duration = 0.0;start = clock();test();finish = clock();duration = (double)(finish - start);输出单位msduration = (double)(finish - start) / CLOCKS_PER_SEC; //输出单位为妙,精确到毫秒级//#define CLOCKS_PER_SEC 1000printf("%f seconds\n", duration);return 0;}2、<time.h>中函数time(&t),精确度,秒级别功能:取以秒为单位的,从1970年1月1日格林威治时间00:00:00算起的当前时间,并把它存在长整形变量t中,函数返回如前所述的时间秒值。
测试程序如下:#include "stdio.h "#include "time.h "#include "stdlib.h "main(){long start,end;time(&start);delay(50000);time(&end);printf( "end-start=%ld\n ",end-start);getch();}3. 最精确的计时:QueryPerformanceCounter来查询定时器的计数值,如果硬件里有定时器,它就会启动这个定时器,并且不断获取定时器的值,这样的定时器精度,就跟硬件时钟的晶振一样精确的。
定时器1秒代码c语言1. 概述在计算机编程中,定时器是一个非常常见的工具。
它可以帮助程序员实现一些特定的功能,比如定时执行某个任务或者控制代码执行时间。
本文将介绍如何使用C语言实现一个简单的定时器。
2. 定时器基本原理在计算机编程中,定时器的基本原理是利用系统的时钟来计算经过的时间。
操作系统会维护一个内部的时钟计数器,每隔一段时间就会进行一次计数。
通过判断计数器的值,程序可以知道当前经过的时间。
3. 实现定时器在C语言中,可以使用time.h头文件中的函数实现定时器功能。
以下是一个简单的定时器实现的代码示例:```cinclude <stdio.h>include <time.h>int main(){int count = 0;time_t start_time = time(NULL);while (1){time_t current_time = time(NULL);int diff_time = current_time - start_time;if (diff_time >= 1){count++;start_time = current_time;printf("count = %d\n", count);}}return 0;}```在上述代码中,我们使用了time.h头文件中的函数time()获取当前的系统时间。
程序会在while循环中不断地获取当前时间并计算时间差,当时间差达到1秒时,程序会对计数器进行自增,并输出计数器的值。
由于每隔1秒钟会触发一次输出,所以计数器count的值就成为了一个简单的定时器。
4. 定时器的应用在实际应用中,定时器有着广泛的应用场景。
下面我们简单介绍几个常见的例子。
4.1. 利用定时器实现闹钟功能我们可以通过设置定时器来实现闹钟的功能。
具体步骤如下:1. 获取当前系统时间;2. 计算目标时间和当前时间的差值,得到定时器时长;3. 开启定时器;4. 在定时器结束时触发替换铃声的操作。
湖南人文科技学院课程设计报告课程名称:单片机原理及应用课程设计设计题目:电子时钟的设计系别:通信与控制工程系专业:通信工程班级:09级通信二班学生姓名: 袁琦黄文付学号: 09416230 09416227起止日期:2011年12月20日~2011年12月30日指导教师:王善伟姚毅谢四莲教研室主任:刘建闽摘要时钟是人类日常生活必不可少的工具,本设计从日常生活中常见的事物入手,通过对电子时钟的设计,让我们认识到单片机已经深入到我们生活的每个领域,该设计不仅可以锻炼我们的动手能力,而且可以加深我们对单片机的认识和激发我们对未知科学领域的探索。
本文利用单片机实现数字时钟计时功能的主要内容。
它体积小,成本低、功能强、使用方便、可靠性高等一系列优点,广泛应用于智能产业和工业自动化上。
本次设计采用独立式按键进行时间调整,其中STC89C52是核心元件,同时采用数码管LED动态显示“时”,“分”,“秒”的现代计时装置。
与传统机械表相比,它具有走时精确,显示直观等特点。
它的计时周期为24小时,显满刻度为“23时59分59秒”,另外利用DS1302具有校时功能,断电后有记忆功能,恢复供电时可实现计时同步等特点。
该系统同时具有硬件设计简单、工作稳定性高、价格低廉等优点。
关键词:STC89C52 ;LED数码管;8255芯片;DS1302芯片;目录设计要求 (1)1.方案论证与对比 (1)1.1方案一 (1)1.2方案二 (2)1.3方案对比 (2)2.系统硬件电路的设计 (3)2.1单片机的选择及引脚功能介绍 (3)2.2 8255A芯片的结构及引脚功能介绍 (4)2.3 DS1302芯片的结构及引脚功能介绍 (6)2.4显示电路设计 (7)2.5电源电路设计 (8)2.6键盘动态扫描电路设计 (8)3.控制系统的软件设计 (10)3.1主程序流程图 (10)3.2显示子程序 (11)3.3闹钟时间设定功能程序 (11)3.4键盘扫描程序 (12)4.系统功能调试与整体指标 (13)4.1硬件调试与分析 (13)4.2软件调试与分析 (13)4.3性能分析 (13)5.详细仪器清单 (14)6.总结与思考及致谢 (15)参考文献 (16)附录一:程序(方案一) (17)附录二:程序(方案二) (25)电子时钟的设计设计要求利用单片机作为控制核心,完成一个时钟计时器。
时钟计时器设计目录一、功能要求 (3)二、方案论证 (3)三、系统硬件电路设计 (3)四、系统程序设计 (4)1.主程序 (4)2.显示子程序 (5)3.定时器T0中断服务程序 (5)4.定时器T1中断服务程序 (6)5.调时功能程序 (6)6.整点响程序 (6)7.时钟/秒表功能程序 (6)8.闹钟时间设定功能程序 (6)五、调试及性能分析 (6)1、硬件测试 (6)2、软件测试……………………………………………………………………6.3、性能分析 (6)六.控制源程序清单……………………………………………………………….7.1.汇编源程序清单 (7)2. C源程序清单 (23)3.实物照片 (26)4.元器件清单 (27)七.Proteus仿真 (28)八.设计制作要点 (28)九.实训心得 (29)参考文献 (31)时钟计时器的设计一、功能要求:时钟计时器用单片机和6位LED数码管显示时、分、秒,以24小时计时方式运行。
能整点提醒,使用按键开关可实现时、分调整,秒表/时钟功能的转换,闹钟等功能。
二、方案论证:为了实现LED显示器的数字显示,可采用静态显示和动态显示,由于静态显示法需要数据锁存器等硬件,接口较复杂,又考虑到时钟显示只有6位,且系统没有其他复杂的处理任务,所以决定采用动态扫描法实现LED的显示。
单片机采用STC59C52单片机,这种单片机具有足够的空余硬件资源,可以实现其他的扩充任务。
时钟计时器电路系统的总体设计框图如下。
硬件系统总体设计框图三、系统硬件电路的设计:该电路采用STC89C52单片机最小化应用设计,采用共阳8段LED显示,P0口输出段码数据,P2.0~P2.5作为列扫描输出,P1.0~P1.3接4个按键开关,用于调时及功能设置,P1.7口接5V蜂鸣器,用于按键发音和定时提醒、整点报时提醒。
为了提供共阳LED数码管的列扫描驱动电压,用三极管9012作电源驱动输出,采用12MHz晶振,提高秒计时的精准度。
用C语言写的一个PIC16F877的时闹钟程序单片机用16F877,主时钟用20MHz,用32768作定时时间。
可以实现2路定闹,每一路都可分别设置和开关,采用4x4键盘,16x2的字符型LCD显示。
连线在程序开头有说明。
程序的功能:(1)上电后LCD背光打开,并显示倒计时5秒,然后时钟开始工作。
(2)用模式键(*)切换模式,如显示时间、日期、闹钟1、闹钟2等,并且可以用上、下键控制加1、减1或是闹钟的On、Off。
(3)原程序有16个键,包括0~9数字键,可以直接输入要设置的时间值,但后来将数字键取消了,你仍然可以通过修改程序的部分注释恢复此功能。
(4)闹钟有2路,时间到后闹2分钟,可按任意键取消本次闹钟。
闹钟响时有2种音调,是用PIC的PWM实现的。
(5)按任意键可打开背光,1分钟后自动关闭背光。
(6)RA0~RA3为按键扫描输入,应接下拉电阻。
主程序// FileName: Main.c// MCU: Microchip PIC16F877// Tool: CCS-C compiler// Author: KingEDA, MSN:kingeda@, skype:kingeda, E-mail:kingeda@// Website: // Description:// A timer program// Ver 0.1: 2003-03-31, all clock function with date display, 2 way alarm.// Ver 0.2: 2003-05-05, (1) Alarm default is on,modify alarm1 time to 7:00:00,// and alarm2 to 13:30:00.// (2) Backlight will be enabled when alarming.// (3) Automatic adjust day(28,30,31).// (4) Automatic move cursor to next location when set item.// PIN Connection:// RC0~1 : 32768Hz crystal// RC2 : Buzzer// RC3 : LCD Back Light,drive a PNP BJT// RD0~RD7 : to LCD DB0~DB7// RA0~RA3 : keypad col in// RC4~RC7 : keypad line out// 7 8 9 #// 4 5 6 ↑// 1 2 3 ↓// 0 ←→*// RE0 : LCD RS// RE1 : LCD RW// RE2 : LCD E#include "my16f877.h"#device ICD=true//#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT #use delay(clock = 24000000)//#use fast_io(C)#use fast_io(E)#define lcd_busy (lcd_read_addr()&0x80) == 0x80#define time_start_addr 0x80+0x04#define time_hourh_addr time_start_addr#define time_hourl_addr time_start_addr+1#define time_minuteh_addr time_start_addr+3#define time_minutel_addr time_start_addr+4#define time_secondh_addr time_start_addr+6#define time_secondl_addr time_start_addr+7#define key_0 0x11#define key_1 0x21#define key_2 0x22#define key_3 0x24#define key_4 0x41#define key_5 0x42#define key_6 0x44#define key_7 0x81#define key_8 0x82#define key_9 0x84#define key_left 0x12#define key_right 0x14#define key_up 0x48#define key_down 0x28#define key_mode 0x18#define key_cancel 0x88char StrPower1[] = " * Power on * ";char StrSetTime[] = " * Adjust time* ";char StrSetDate[] = " * Adjust date* ";char StrAlarm1[] = " * Set alarm 1* ";char StrAlarm2[] = " * Set alarm 2* ";unsigned char PORTC_MAP;#bit BackLightEn = PORTC_MAP.3unsigned char BackLightTimer;int1 led;#bit lcd_rs = PORTE.0#bit lcd_rw = PORTE.1#bit lcd_e = PORTE.2#byte lcd_bus = PORTD#byte lcd_dir = TRISD#define PWM_on 0x0c#define PWM_off 0x00#define PWM_period 200#define PWM_DC 100unsigned char lcd_addr;unsigned char KeyLine;unsigned char KeyOld;unsigned char KeyNew;struct mTime {unsigned char hourh; // hour,0~23unsigned char hourl;unsigned char minuteh; // minute,0~59unsigned char minutel;unsigned char secondh; // second,0~59unsigned char secondl;};struct mTime CurrentTime = {1,2,0,0,0,0};struct mTime AlarmTime1 = {0,7,0,0,0,0}; // 07:00:00 struct mTime AlarmTime2 = {1,3,3,0,0,0}; // 13:30:00 unsigned char AlarmStatus;#bit Alarm1Enable = AlarmStatus.0#bit Alarm2Enable = AlarmStatus.1#bit Alarm1Alarm = AlarmStatus.2#bit Alarm2Alarm = AlarmStatus.3unsigned char Alarm1Cnt; // alarm1 second count unsigned char Alarm2Cnt;unsigned char CurrentMode;#define mode_time 0#define mode_set_time 1#define mode_set_date 2#define mode_set_alarm1 3#define mode_set_alarm2 4unsigned char adjust_item;struct mDate {unsigned char year1; //unsigned char year2;unsigned char year3;unsigned char year4;unsigned char monthh;unsigned char monthl;unsigned char dayh;unsigned char dayl;};struct mDate CurrentDate = {2,0,0,3,0,1,0,1}; unsigned char *pStr;// ------------------------------------------------------- unsigned char lcd_read_addr(){unsigned char ch;lcd_dir = 0xff; // read from lcdlcd_rs = 0;lcd_rw = 1; // instlcd_e = 1;#asmnopnopnop#endasmch = lcd_bus;lcd_e = 0;lcd_dir = 0x00; // set write to lcdreturn ch;}// ------------------------------------------------------- unsigned char lcd_write_data(unsigned char ch) {while (lcd_busy){ restart_wdt(); }lcd_rs = 1; // datalcd_rw = 0; // writelcd_bus = ch; // write outlcd_e = 1;#asmnopnopnop#endasmlcd_e = 0;return 'Y';}// ------------------------------------------------------- unsigned char lcd_write_inst(unsigned char ch) {while (lcd_busy){ restart_wdt(); }lcd_rs = 0; // instlcd_rw = 0; // writelcd_bus = ch;lcd_e = 1;#asmnopnopnop#endasmlcd_e = 0;return 'Y';}// ------------------------------------------------------- unsigned char lcd_read_data(){unsigned char ch;while (lcd_busy){ restart_wdt(); }lcd_dir = 0xff; // read from lcdlcd_rs = 1; // datalcd_rw = 1; // readlcd_e = 1;#asmnopnopnop#endasmch = lcd_bus; // read inlcd_e = 0;lcd_dir = 0x00; // set write to lcdreturn ch;}// ------------------------------------------------------- void lcd_init(){unsigned char Tempch;lcd_addr = 0;delay_ms(100);Tempch = 0x38; // 1-line mode,5x8 dotslcd_write_inst(Tempch); // Function setTempch = 0x0f; // lcd on,cursor on,blink onlcd_write_inst(Tempch); // Display on/offTempch = 0x06; // Increment mode,Entire shift offlcd_write_inst(Tempch);Tempch = 0x01; // clear displaylcd_write_inst(Tempch);delay_ms(3);}// -------------------------------------------------------//#int_timer1//void timer1_interrupt(void)#int_ccp2void ccp2_interrupt(void){//TMR1H = 0x80;if (CurrentTime.secondl==9){CurrentTime.secondl=0;if (CurrentTime.secondh==5){CurrentTime.secondh=0;if (CurrentTime.minutel==9){CurrentTime.minutel=0;if (CurrentTime.minuteh==5){CurrentTime.minuteh=0;if (CurrentTime.hourl==9){CurrentTime.hourl=0;CurrentTime.hourh++;}else if((CurrentTime.hourl==3) && (CurrentTime.hourh==2)){CurrentTime.hourl=0;CurrentTime.hourh=0;if ((((CurrentDate.dayl == 8) || (CurrentDate.dayl == 9)) && (CurrentDate.dayh == 2) && (CurrentDate.monthl == 2) && (CurrentDate.monthh == 0)) ||((CurrentDate.dayl == 0) && (CurrentDate.dayh == 3) && ((((CurrentDate.monthl == 4) || (CurrentDate.monthl == 6)|| (CurrentDate.monthl == 9)) && (CurrentDate.monthh == 0)) || ((CurrentDate.monthl == 1) && (CurrentDate.monthh == 1)))) ||((CurrentDate.dayl == 1) && (CurrentDate.dayh == 3))){CurrentDate.dayl=1;CurrentDate.dayh=0;if ((CurrentDate.monthl == 2) && (CurrentDate.monthh == 1)){CurrentDate.monthl = 1;CurrentDate.monthh = 0;if (CurrentDate.year4 == 9){CurrentDate.year4 = 0;if (CurrentDate.year3 == 9){CurrentDate.year3 = 0;if (CurrentDate.year2 == 9){CurrentDate.year2 = 0;CurrentDate.year1++;}elseCurrentDate.year2++;}elseCurrentDate.year3++;}elseCurrentDate.year4++;}else if(CurrentDate.monthl == 9){CurrentDate.monthl = 0;CurrentDate.monthh++;}elseCurrentDate.monthl++;}else if(CurrentDate.dayl == 9){CurrentDate.dayl=0;CurrentDate.dayh++;}elseCurrentDate.dayl++;}elseCurrentTime.hourl++;}elseCurrentTime.minuteh++;}elseCurrentTime.minutel++;}elseCurrentTime.secondh++;}elseCurrentTime.secondl++;if ((Alarm1Alarm == false) & (Alarm2Alarm == false)){led = 0;CCP1CON = PWM_off;}else{if (led == 1){led = 0;PR2 = PWM_period; // set pwm periodCCPR1L = PWM_DC; // set pwm duty cycle//CCP1CON = PWM_on;}else{led = 1;PR2 = PWM_period/2; // set pwm periodCCPR1L = PWM_DC/2; // set pwm duty cycle//CCP1CON = PWM_off;}}Alarm1Cnt++;Alarm2Cnt++;if (BackLightEn == 0)if (((BackLightTimer++)>=60) & (Alarm1Alarm == false) & (Alarm1Alarm == false))BackLightEn = 1; // disable backlight PORTC = PORTC_MAP;//TMR1IF = 0;//PIR1 = PIR2 = 0x00;CCP2IF = 0;}// ------------------------------------------------------- unsigned char get_key(void){unsigned char key_in,tmp;TRISC = 0x03;KeyLine = 0xf0;PORTC = KeyLine | PORTC_MAP;#asmnopnopnop#endasmif ((PORTA & 0x0f) != 0){tmp = 0x10;for (KeyLine = tmp;KeyLine!=0;KeyLine = tmp){PORTC = KeyLine | PORTC_MAP;tmp = KeyLine <<1;#asmnopnopnop#endasmkey_in = PORTA & 0x0f;if (key_in != 0){return (key_in | KeyLine);}}return 0;}elsereturn 0;}// -------------------------------------------------------void set_mode(void){if (CurrentMode == mode_set_alarm2)CurrentMode = mode_time;else{CurrentMode++;adjust_item = 0;}lcd_write_inst(0x01); // clear LCD displaylcd_write_inst(time_start_addr); // set LCD line1 if (CurrentMode == mode_set_time){lcd_write_data(CurrentTime.hourh + '0');lcd_write_data(CurrentTime.hourl + '0');lcd_write_data(':');lcd_write_data(CurrentTime.minuteh + '0');lcd_write_data(CurrentTime.minutel + '0');lcd_write_data(':');lcd_write_data(CurrentTime.secondh + '0');lcd_write_data(CurrentTime.secondl + '0');pStr = StrSetTime;}else if(CurrentMode == mode_set_date){lcd_write_data(CurrentDate.year1 + '0');lcd_write_data(CurrentDate.year2 + '0');lcd_write_data(CurrentDate.year3 + '0');lcd_write_data(CurrentDate.year4 + '0');lcd_write_data('/');lcd_write_data(CurrentDate.monthh + '0');lcd_write_data(CurrentDate.monthl + '0');lcd_write_data('/');lcd_write_data(CurrentDate.dayh + '0');lcd_write_data(CurrentDate.dayl + '0');pStr = StrSetDate;}else if(CurrentMode == mode_set_alarm1){lcd_write_data(AlarmTime1.hourh + '0');lcd_write_data(AlarmTime1.hourl + '0');lcd_write_data(':');lcd_write_data(AlarmTime1.minuteh + '0');lcd_write_data(AlarmTime1.minutel + '0');lcd_write_data(':');lcd_write_data(AlarmTime1.secondh + '0');lcd_write_data(AlarmTime1.secondl + '0');lcd_write_data(' ');lcd_write_data('O');if (Alarm1Enable){lcd_write_data('n');}else{lcd_write_data('f');lcd_write_data('f');}pStr = StrAlarm1;Alarm1Cnt =0;}else if(CurrentMode == mode_set_alarm2) {lcd_write_data(AlarmTime2.hourh + '0');lcd_write_data(AlarmTime2.hourl + '0');lcd_write_data(':');lcd_write_data(AlarmTime2.minuteh + '0');lcd_write_data(AlarmTime2.minutel + '0');lcd_write_data(':');lcd_write_data(AlarmTime2.secondh + '0');lcd_write_data(AlarmTime2.secondl + '0');lcd_write_data(' ');lcd_write_data('O');if (Alarm2Enable){lcd_write_data('n');}else{lcd_write_data('f');lcd_write_data('f');}pStr = StrAlarm2;Alarm2Cnt = 0;}lcd_write_inst(0xc0); // set LCD line2 if (CurrentMode != mode_time){for (;*pStr!=0;pStr++){ // write hint messagelcd_write_data(*pStr);}lcd_write_inst(0x0f); // LCD cursor onlcd_write_inst(time_start_addr); // move cursor to start }else // time mode,write date to second line{lcd_write_inst(0x0c); // LCD sursor off/* lcd_write_inst(0xc0 + 3); // set date start address lcd_write_data(CurrentDate.year1 + '0');lcd_write_data(CurrentDate.year2 + '0');lcd_write_data(CurrentDate.year3 + '0');lcd_write_data(CurrentDate.year4 + '0');lcd_write_data('/');lcd_write_data(CurrentDate.monthh + '0');lcd_write_data(CurrentDate.monthl + '0');lcd_write_data('/');lcd_write_data(CurrentDate.dayh + '0');lcd_write_data(CurrentDate.dayl + '0');*/ }if (CurrentMode == mode_set_time){lcd_write_inst(time_start_addr); // move cursor to start }else if (CurrentMode == mode_set_date){lcd_write_inst(time_start_addr); // move cursor to start }else if (CurrentMode == mode_set_alarm1){lcd_write_inst(time_secondl_addr+3);adjust_item = 6;}else if (CurrentMode == mode_set_alarm2){lcd_write_inst(time_secondl_addr+3);adjust_item = 6;}else{lcd_write_inst(0x0c); // LCD cursor off}}// ------------------------------------------------------- void set_date(void){if (adjust_item == 0) // adjust year{if ((KeyNew >=0) & (KeyNew <= 9)){CurrentDate.year1 = KeyNew;lcd_write_data(CurrentDate.year1 + '0');//lcd_write_inst(time_start_addr);adjust_item ++;}else if (KeyNew == key_left){adjust_item = 7;lcd_write_inst(time_start_addr + 9);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 1);}}else if(adjust_item == 1){if ((KeyNew >=0) & (KeyNew <= 9)){CurrentDate.year2 = KeyNew;lcd_write_data(CurrentDate.year2 + '0');//lcd_write_inst(time_start_addr + 1);adjust_item ++;}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 0);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 2);}}else if(adjust_item == 2){if ((KeyNew >=0) & (KeyNew <= 9)){CurrentDate.year3 = KeyNew;lcd_write_data(CurrentDate.year3 + '0');//lcd_write_inst(time_start_addr + 2);adjust_item ++;}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 1);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 3);}}else if(adjust_item == 3){if ((KeyNew >=0) & (KeyNew <= 9)){CurrentDate.year4 = KeyNew;lcd_write_data(CurrentDate.year4 + '0');//lcd_write_inst(time_start_addr + 3);adjust_item ++;lcd_write_inst(time_start_addr + 5);}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 2);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 5);}}else if(adjust_item == 4)if (((CurrentDate.monthl>2) & (KeyNew == 0)) | ((CurrentDate.monthl == 0) & (KeyNew == 1))| (((CurrentDate.monthl == 1) | (CurrentDate.monthl == 2)) & (KeyNew <2))) {CurrentDate.monthh = KeyNew;lcd_write_data(CurrentDate.monthh + '0');//lcd_write_inst(time_start_addr + 5);adjust_item ++;}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 3);}else if (KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 6);}}else if(adjust_item == 5){if (((CurrentDate.monthh == 3) & (KeyNew <2)) | ((CurrentDate.monthh != 3) & (KeyNew >=0) & (KeyNew <=9))){CurrentDate.monthl = KeyNew;lcd_write_data(CurrentDate.monthl + '0');//lcd_write_inst(time_start_addr + 6);adjust_item ++;lcd_write_inst(time_start_addr + 8);}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 5);}else if (KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 8);}}else if(adjust_item == 6)if (((CurrentDate.dayl>1) & ((KeyNew == 1) | (KeyNew == 2))) | ((CurrentDate.dayl == 0) & (KeyNew >0) & (KeyNew<4))| ((CurrentDate.dayl == 1) & (KeyNew <4))){CurrentDate.dayh = KeyNew;lcd_write_data(CurrentDate.dayh + '0');//lcd_write_inst(time_start_addr + 8);adjust_item ++;}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 6);}else if (KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 9);}}else if(adjust_item == 7){if (((CurrentDate.dayh == 3) & (KeyNew <2)) | ((CurrentDate.dayh != 3) & (KeyNew >=0) & (KeyNew <=9))){CurrentDate.dayl = KeyNew;lcd_write_data(CurrentDate.dayl + '0');//lcd_write_inst(time_start_addr + 9);adjust_item ++;lcd_write_inst(time_start_addr + 0);}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 8);}else if (KeyNew == key_right){adjust_item = 0;lcd_write_inst(time_start_addr + 0);}}}// -------------------------------------------------------void set_time(void){if (adjust_item == 0) // set hourh{if (((CurrentTime.hourl <4) & (KeyNew < 3)) | ((CurrentTime.hourl >3) & (KeyNew <2))){CurrentTime.hourh = KeyNew;lcd_write_data(CurrentTime.hourh + '0'); // refresh hourh//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item = 5;lcd_write_inst(time_secondl_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_hourl_addr);}}else if (adjust_item == 1) // set hourl{if (((CurrentTime.hourh == 2) & (KeyNew < 4)) | ((CurrentTime.hourh < 2) & (KeyNew <=9))){CurrentTime.hourl = KeyNew;lcd_write_data(CurrentTime.hourl + '0'); // refresh hourl//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minuteh_addr);}}else if (adjust_item == 2) // set minuteh{if (KeyNew <6){CurrentTime.minuteh = KeyNew;lcd_write_data(CurrentTime.minuteh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourl_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minutel_addr);}}else if (adjust_item == 3) // set minutel{if ((KeyNew >=0) & (KeyNew <=9)){CurrentTime.minutel = KeyNew;lcd_write_data(CurrentTime.minutel + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondh_addr);}}else if (adjust_item == 4) // set secondh{if (KeyNew <6){CurrentTime.secondh = KeyNew;lcd_write_data(CurrentTime.secondh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;&nb, sp; lcd_write_inst(time_minutel_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr);}}else if (adjust_item == 5) // set secondl{if ((KeyNew >=0) & (KeyNew <=9)){CurrentTime.secondl = KeyNew;lcd_write_data(CurrentTime.secondl + '0');//lcd_write_inst(0x10); // move cursor backadjust_item = 0;lcd_write_inst(time_hourh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_right){adjust_item = 0;lcd_write_inst(time_hourh_addr);}}}// -------------------------------------------------------void set_alarm1(void){if (adjust_item == 0) // set hourh{if (((AlarmTime1.hourl <4) & (KeyNew < 3)) | ((AlarmTime1.hourl >3) & (KeyNew <2))){AlarmTime1.hourh = KeyNew;lcd_write_data(AlarmTime1.hourh + '0'); // refresh hourh//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item = 6;lcd_write_inst(time_secondl_addr + 3);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_hourl_addr);}}else if (adjust_item == 1) // set hourl{if (((AlarmTime1.hourh == 2) & (KeyNew < 4)) | ((AlarmTime1.hourh < 2) & (KeyNew <=9))){AlarmTime1.hourl = KeyNew;lcd_write_data(AlarmTime1.hourl + '0'); // refresh hourl//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minuteh_addr);}}else if (adjust_item == 2) // set minuteh{if (KeyNew <6){AlarmTime1.minuteh = KeyNew;lcd_write_data(AlarmTime1.minuteh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourl_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minutel_addr);}}else if (adjust_item == 3) // set minutel{if ((KeyNew >=0) & (KeyNew <=9)){AlarmTime1.minutel = KeyNew;lcd_write_data(AlarmTime1.minutel + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondh_addr);}}else if (adjust_item == 4) // set secondh{if (KeyNew <6){AlarmTime1.secondh = KeyNew;lcd_write_data(AlarmTime1.secondh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minutel_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr);}}else if (adjust_item == 5) // set secondl{if ((KeyNew >=0) & (KeyNew <=9)){AlarmTime1.secondl = KeyNew;lcd_write_data(AlarmTime1.secondl + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondl_addr+3);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr+3);}}else if (adjust_item == 6) // set on/off{if ((KeyNew == key_up) | (KeyNew == key_down)){if (Alarm1Enable){Alarm1Enable =false; // disable alarm1lcd_write_data('f');lcd_write_data('f');}else{Alarm1Enable =true; // enable alarm1lcd_write_data('n');lcd_write_data(' ');}//lcd_write_inst(time_secondl_addr+3);adjust_item = 0;lcd_write_inst(time_hourh_addr);Alarm1Cnt = 0;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondl_addr);}else if(KeyNew == key_right){adjust_item = 0;lcd_write_inst(time_hourh_addr);}}}// -------------------------------------------------------void set_alarm2(void){if (adjust_item == 0) // set hourh{if (((AlarmTime2.hourl <4) & (KeyNew < 3)) | ((AlarmTime2.hourl >3) & (KeyNew <2))){AlarmTime2.hourh = KeyNew;lcd_write_data(AlarmTime2.hourh + '0'); // refresh hourh//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item = 6;lcd_write_inst(time_secondl_addr+3);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_hourl_addr);}}else if (adjust_item == 1) // set hourl{if (((AlarmTime2.hourh == 2) & (KeyNew < 4)) | ((AlarmTime2.hourh < 2) & (KeyNew <=9))){AlarmTime2.hourl = KeyNew;lcd_write_data(AlarmTime2.hourl + '0'); // refresh hourl//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minuteh_addr);}}else if (adjust_item == 2) // set minuteh{if (KeyNew <6){AlarmTime2.minuteh = KeyNew;lcd_write_data(AlarmTime2.minuteh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourl_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minutel_addr);}}else if (adjust_item == 3) // set minutel{if ((KeyNew >=0) & (KeyNew <=9)){AlarmTime2.minutel = KeyNew;lcd_write_data(AlarmTime2.minutel + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondh_addr);}}else if (adjust_item == 4) // set secondh{if (KeyNew <6){AlarmTime2.secondh = KeyNew;lcd_write_data(AlarmTime2.secondh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minutel_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr);}}else if (adjust_item == 5) // set secondl{if ((KeyNew >=0) & (KeyNew <=9)){AlarmTime2.secondl = KeyNew;lcd_write_data(AlarmTime2.secondl + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondl_addr+3);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr+3);}}else if (adjust_item == 6) // set on/off{if ((KeyNew == key_up) | (KeyNew == key_down)){if (Alarm2Enable){Alarm2Enable =false; // disable alarm2lcd_write_data('f');lcd_write_data('f');}else{Alarm2Enable =true; // enable alarm2lcd_write_data('n');lcd_write_data(' ');}//lcd_write_inst(time_secondl_addr+3);adjust_item = 0;lcd_write_inst(time_hourh_addr);Alarm2Cnt = 0;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondl_addr);}else if(KeyNew == key_right){adjust_item = 0;lcd_write_inst(time_hourh_addr);}}}// -------------------------------------------------------void main(void){unsigned char cnt;TRISC = 0x03; // PORTC.3 drive led,low activePORTC_MAP = 0x00;led = 0;BackLightEn = 0;BackLightTimer = 0;PORTC = PORTC_MAP;TRISA = 0xff; // low half byte as keyscan inTRISE = 0x00;ADCON0 = 0x00;ADCON1 = 0x06; // all digital I/Oslcd_init();INTCON = 0x00;lcd_write_inst(0x80); // set lcd ddram addressfor (pStr = StrPower1;*pStr!=0;pStr++){lcd_write_data(*pStr);}lcd_write_inst(0x0c); // LCD cursor offPIR1 = PIR2 = 0x00;T1CON = 0x0f; // T1CON: -- T1CKPS1 T1CPS0 T1OSCEN /T1SYNC TMR1CS TMR1ONTMR1H = 0x80;TMR1L = 0x00;。
c语言定时控制代码
在C语言中,可以使用``库中的`sleep()`函数来暂停程序的执行一段时间。
下面是一个简单的示例代码,演示如何使用`sleep()`函数实现定时控制:
```c
include <>
include <>
int main() {
// 输出提示信息
printf("开始倒计时...\n");
// 倒计时10秒
for (int i = 10; i >= 0; i--) {
// 输出当前时间
printf("%d\n", i);
// 暂停1秒
sleep(1);
}
// 倒计时结束,输出提示信息
printf("倒计时结束!\n");
return 0;
}
```
在上面的代码中,我们使用了`sleep()`函数来实现每秒钟输出一次当前时间,并且每次输出后暂停1秒钟。
通过循环控制,实现了倒计时10秒的功能。
当倒计时结束后,程序会输出提示信息。
用C语言实现60秒倒计时
以下是用C语言实现60秒倒计时的代码:
```c
#include <stdio.h>
#include <stdlib.h>
void sleep(int milliseconds)
clock_t startTime = clock(;
while (clock( < startTime + milliseconds);
int mai
int seconds = 60;
while (seconds > 0)
printf("%02d:%02d\n", seconds/60, seconds%60); sleep(1000); // 延时1秒
seconds--;
}
printf("倒计时结束!\n");
return 0;
```
这段代码的核心思想是通过循环,每隔一秒打印剩余的秒数,并通过
`sleep`函数实现延时一秒的效果。
当倒计时结束后,会输出"倒计时结束!"。
注意:在不同的操作系统或编译器中,`sleep`函数的实现可能有所
不同。
上述代码中的`sleep`函数是一个简单的实现,可以用于一般的情况,但如果有特殊要求,可能需要使用操作系统提供的更高级的延时函数。
C单片机秒表计时全集文档(可以直接使用,可编辑实用优质文档,欢迎下载)南开大学滨海学院C51嵌入式软件设计(C语言)题目:计时秒表●功能描述:本设计实现在99秒内的秒表计时,一个按键实现开始、暂停、复位。
●原理概述:P1接四位七段数码管,P3.2接一按键产生外部中断0,P3.4-P3.7控制扫描显示。
计时使用定时器0产生10ms 中断累计。
按键不同次序决定了对应的控制功能,因为第一次按键必定为开始计时,所以第二次按键判断为暂停,依次第三次为置零。
主程序调用显示程序,显示程序实时显示计时时间。
●效果显示●图一(电路总图)●图二(效果显示)注:第四位显示为单位:S ●程序清单#include<reg51.h>#include<stdio.h>unsigned char Tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F }; sbit P37=P3^7;sbit P36=P3^6;sbit P35=P3^5;sbit P34=P3^4;unsigned int a=0,cout=0,mm=0;x,y,p,q;/*********延时*********************/void delay(){int g;for(g=70;g>0;g--);}/*********显示程序*****************/void display(){x=cout/10;//秒十位P34=0;P1=Tab[x]; delay();P34=1;y=cout-x*10; //秒各位P35=0;P1=Tab[y]; delay();P1=0x80; delay();P35=1;p=mm/10; //ms的高位P36=0;P1=Tab[p];delay();P36=1;P37=0; //显示单位:SP1=Tab[5]; delay();P37=1;}/*********主程序********************/ void main(){IT0=1;EX0=1;ET0=1;TMOD=0x01;TH0=0xD8; //装初值,10msTL0=0xF0;EA=1;while(1){ display(); };}/*********外部按键中断子程序*********/ void int0 ()interrupt 0{if(a==0) //开始计时{ TR0=1;mm=0;a++; }else if(a==1) //暂停计时{ TR0=0;a++;}else//置零{ a=0;mm=0;cout=0;}}/*********定时器子程序****************/ void time0() interrupt 1{TH0=0xD8;TL0=0xF0;mm++;if(mm==80) //考虑其它损耗,调整后约为1S { cout++;mm=0;}}沈阳理工大学应用技术学院毕业设计(论文) 题目:基于单片机的数字电子秒表的设计与实现系别: 信息工程系专业: 自动化学生姓名:胡培指导教师: 唐朝仁年月日摘要近年来随着科技的飞速发展,单片机的应用正在不断的走向深入.本文阐述了基于单片机的数字电子秒表设计.本设计主要特点是计时精度达到0.001s,解决了传统的由于计时精度不够造成的误差和不公平性,是各种体育竞赛的必备设备之一。
C语⾔Windows程序设计——使⽤计时器传统意义上的计时器是指利⽤特定的原理来测量时间的装置, 在古代, 常⽤沙漏、点燃⼀炷⾹等⽅式进⾏粗略的计时, 在现代科技的带动下, 计时⽔平越来越⾼, 也越来越精确, 之所以需要进⾏计时是在很多情况下我们需要知道时间已经过去了多少, 举例说, 上课下课的打铃、考试时的计时、车站按时间间隔进⾏发车等。
不仅在⽇常⽣活中会应⽤到计时, 在⼀些电⼦设备中计时的普遍存在, 如⼿机⾥的闹钟、电⼦秒表、电⼦设备的定时关机等, 这些计时的⽬的都是相同的, 当达到⼀定时间后执⾏某件事, 计时器相当于提醒作⽤, 当达到某个时间后提醒⼈们或者机器该做某件事了。
在Windows系统中, 计时器作为⼀种输⼊设备存在于系统中, 当每到⼀个设定的时间间隔后它都会向应⽤程序发出⼀个 WM_TIMER 的消息, 以提醒程序规定的间隔时间已经过去了, 计时器在程序中的应⽤⼗分⼴泛, 举些我们容易想到的⽰例:1>. 游戏这控制物体的移动速度, ⽐如说某个物体每100毫秒移动某个单位距离;2>. ⽂件的⾃动保存, 当⽤户编辑某些⽂件时5分钟⾃动保存⼀次, 避免因意外情况造成编辑的成果全部丢失;3>. 实现程序的⾃动退出, 当程序达到某个设定的时间后程序⾃动退出;⼀、使⽤计时器计时器的使⽤主要分为创建、处理、销毁三个部分。
①. 创建: 创建⼀个计时器并设定其定计时器的任务周期, 例如每5秒向程序发送⼀条 WM_TIMER 消息 ;②. 处理: 根据接收到的 WM_TIMER 消息让程序作出响应的处理 ;③. 销毁: Windows的计时器属于系统资源, 在使⽤完毕后应及时销毁。
1>. 计时器的创建要创建⼀个计时器可以使⽤ SetTimer 函数, SetTimer函数的原型: UINT_PTR SetTimer( HWND hWnd, //窗⼝句柄 UINT_PTR nIDEvent, //定时器的ID UINT uElapse, //间隔时间, 单位为毫秒 TIMERPROC lpTimerFunc //所使⽤的回调函数 ); 参数说明:参数⼀窗⼝句柄即为接收 WM_TIMER 消息的窗⼝句柄;参数⼆为设置该计时器的ID, ⽤于与其他的计时器进⾏区分;参数三为计时器发送 WM_TIMER 消息的时间间隔, 单位为毫秒, 最⼤可设置的时间间隔为⼀个 unsigned long int 型所能容下的数据⼤⼩,为 4 294 967 295 毫秒(约合49.7天), 当设定的时间间隔到了后Windows就会向应⽤程序的消息队列放⼊⼀个 WM_TIMER 消息 ;参数四为定时器所使⽤的回调函数, 当使⽤回调函数时, 所产⽣的 WM_TIMER 消息⾃动调⽤回调函数进⾏处理。
C语言实现计时范文
一、实现思路
1、确定计时器的功能要求
本文实现的计时器的功能是:能实现从0~59秒的计时,一分钟为一
个计时周期。
2、实现程序计时
(1)首先定义一个全局变量,即用来保存计时的变量count,赋值
为0,以此来开始新的计时周期;
(2)使用while循环来实现不断循环计时,其中使用Sleep(函数来
实现延时,其中延时的时间可以自定义,此处设置为1秒;
(3)在while循环执行中,使用printf函数来打印计时结果;
(4)将count自增,同时判断count与59比较,若count大于59,则将count置为0,即开始新的计时周期。
二、具体实现
#include <stdio.h>
#include <windows.h>
int count = 0;
void main
while(1)
printf("计时器: %d\n", count);
count++;
Sleep(1000);
if(count == 60)
count = 0;
三、总结
本文介绍了如何使用C语言实现计时器,其中使用while循环、Sleep(函数、printf函数和count这几个函数就可以实现上述的功能要求,使得计时器的功能有效实现,从而达到预期的效果。