定时器时钟程序
- 格式:doc
- 大小:54.00 KB
- 文档页数:4


实验名称:SUMA & BUZZER实验描述:一个带有闹钟的数码时钟,加三个键,一个调小时键一个调分钟键,一个设置闹钟时间键实验方法:TIMER0中断用来计时,控制数码时钟的时间显示还可设置半秒或四分之一秒,用来控制音调TIMER1用来控制音普,,timer0用MODE2自动加载模式*/# include<reg52.h>sbit speaker=P2^3 ;sbit AA=P2^2 ; //调时用sbit BB=P2^1 ; //调分用sbit CC=P2^0 ; // 设置闹钟用sbit P1_7=P1^7; //小数点// int code seven_reg[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//0123456789int code seven_reg[]={0x40,0x79,0x24,0x30,0x19,0x12,0x2,0x78,0x00,0x10};// int code scan[]={0x1F,0x2F,0x4F,0x8F}; //1110,1101,1011,0111int code scan[]={0x1,0x2,0x4,0x8}; //0001,0010,0100,1000unsigned int timer0_times;unsigned int timer1_times;unsigned int timer0_times_AA; //按纽AA用unsigned int timer0_times_BB; //按纽BB用unsigned int timer0_times_CC; // 半秒计时用unsigned int timer0_times_DD; //四分之一秒用unsigned int timer0_times_EE; // 闹钟用typedef struct{unsigned char second;unsigned char minute;unsigned char hour;unsigned char half_second;unsigned char alarm_hour;unsigned char alarm_minute;unsigned char quarter_second ;} time; //此处是固定格式,不能改time now;char mode=0;int i=0,j=0,k=0;int code tone[]={1012,956,852,759,716,638,568,506,478,426,379};//7()低音)1234567(中音)123(高音)int code song[22][2]={ {6,2},{6,2},{7,4},{6,2},{6,2}, {7,4},{6,2},{7,2},{8,2},{7,2},{6,2},{7,1},{6,1},{4,4},{3,2},{1,2},{3,2},{4,2},{3,2},{3,1},{1,1},{0,4}} ;/********************************************************************/void timer1_isr() interrupt TF1_VECTOR using 2{ TR1=0;TL1=(65536-tone[(song[j][0])])%256;TH1=(65536-tone[(song[j][0])])/256;TR1=1;speaker=~speaker;}/*******************************************************************/void timer0_isr() interrupt TF0_VECTOR using 1{/***************************************************/if(CC!=0) timer0_times_EE=0;else{ timer0_times_EE++;if(CC==0&&timer0_times_EE==4000) //按1S进入设置闹钟模式{mode++;//500 ~0.125sif(mode==2) mode=0;}}/**=调时键设置=*/if(AA!=0) timer0_times_AA=0;else{ timer0_times_AA++;if(AA==0&&timer0_times_AA==500&&mode==0)//500*0.25ms=0.125s{ now.hour++;// timer0_times_AA=0;if (now.hour==24) now.hour=0;//后边代码不会达到此效果}if(AA==0&timer0_times_AA==500&&mode==1) //设置闹钟时间HOUR{ now.alarm_hour++;if(now.alarm_hour==24) now.alarm_hour=0;}}/***=调分键设置=***/if (BB!=0) timer0_times_BB=0;else{ timer0_times_BB++;if(BB==0&&timer0_times_BB==500&&mode==0) //0.125s{ //timer0_times_BB=0;now.minute++;if(now.minute==60) now.minute=0;}if(BB==0&&mode==0){ if(timer0_times_BB==2000) //0.5s{ now.minute++;timer0_times_BB=1000;if(now.minute==60) now.minute=0;}}if(BB==0&&timer0_times_BB==500&&mode==1) //设置闹钟时间MINUTE { now.alarm_minute++;if(now.alarm_minute==60) now.alarm_minute=0;}if(BB==0&&mode==1){ if(timer0_times_BB==2000) //0.5s{ now.alarm_minute++;timer0_times_BB=1000;if(now.minute==60) now.minute=0;}}}/*=自然时间设置=*/timer0_times_DD++; //四分之一秒if(timer0_times_DD==1000){ now.quarter_second++;timer0_times_DD=0;if(now.quarter_second==60) now.quarter_second=0;} //二分之timer0_times_CC++;if(timer0_times_CC==2000){now.half_second++;timer0_times_CC=0;if(now.half_second==60) now.half_second=0;}timer0_times++; //一秒一分一时if (timer0_times==4000){ now.second++;timer0_times=0;if(now.second==60){ now.minute++;now.second=0;if(now.minute==60){ now.hour++;now.minute=0;if(now.hour==24) now.hour=0;} } }/******************扫描显示******************************/switch(mode){case 0 :{switch(i){ /*0.005秒选一次*/ case 0:P1=seven_reg[now.minute%10] ;if(now.half_second%2==0)P1_7=1; /*实现让它0.5秒闪一次*/break;case 1:P1=seven_reg[now.minute/10];//小数点不亮同,P1_7=1if(now.half_second%2==0)P1_7=1; /*为什么不能放在上一句前面昵????*/ break;case 2:P1=seven_reg[now.hour %10];break;case 3:P1=seven_reg[now.hour /10];break;}} break;case 1:{switch(i){case 0:P1=seven_reg[now.alarm_minute%10] ;break;case 1:P1=seven_reg[now.alarm_minute/10];break;case 2: P1=seven_reg[now.alarm_hour %10];break;case 3: P1=seven_reg[now.alarm_hour /10];break; }} break;}P3=scan[i];i++;if (i==4) i=0;if(now.quarter_second%2==0){ k++;if(k==(song[j][1]*4)){ j++;k=0;if(j==22) j=0;} } }/****************************************************************/void timer0_initialize(){ EA=0;TR0=0;TMOD=0X12;TL0=(256-250); //0.025ms 自动加载模式0.025*4000=1sTH0=(256-250);ET0=1;TR0=1;EA=1;}void timer1_initialize(){TR1=0;TL1=(65536-tone[song[j][0]])%256;TH1=(65536-tone[song[j][0]])/256;TMOD=0X12;ET1=1;}main(){ unsigned char m1=0;speaker=0;now.alarm_minute=1;timer0_initialize();timer1_initialize();while(1){if(now.alarm_minute!=0)//将闹钟设置为0时,相当于取消闹钟,不会响{ if(now.hour==now.alarm_hour&&now.minute==now.alarm_minute) { if(CC==0&&timer0_times_EE==500) m1=1;switch (m1){ case 0:TR1=1 ;break;case 1 :TR1=0;speaker=0;break;}}else{TR1=0;speaker=0;m1=0;} } } }#include<reg52.h>sbit P10=P1^0; //第0位数码管sbit P11=P1^1;sbit P12=P1^2;sbit P13=P1^3;#define THCO 0xee#define THLO 0x00unsigned char miao=0,fen=0,shi=0;unsigned char code duan[]={0x3F, 0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; main(void){TMOD=0x11;TH0=THCO;TL0=THLO;EA=1;ET0=1;TR0=1;while(1);}void timw0() interrupt 1{static unsigned char c=0,k=0;TH0=THCO;TL0=THLO;P1|=0xff;c++;if(c>200) {miao++;if(miao>=60){miao=0;fen++;}if(fen>=60){shi++;fen=0;}c=0;}if(k>3){k=0;}k++;switch(k-1){case(0):P10=0;P0=duan[shi/10];break;case(1):P11=0;P0=duan[shi%10];break;case(2):P12=0;P0=duan[fen/10];break;case(3):P13=0;P0=duan[fen%10];break;}}这是时钟程序,可以运行,无小数点,显示时分,小数点断码是0x80,其余的只有靠你自己了。
定时器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. 在定时器结束时触发替换铃声的操作。
简述基本定时器的工作流程下载温馨提示:该文档是我店铺精心编制而成,希望大家下载以后,能够帮助大家解决实际的问题。
文档下载后可定制随意修改,请根据实际需要进行相应的调整和使用,谢谢!并且,本店铺为大家提供各种各样类型的实用资料,如教育随笔、日记赏析、句子摘抄、古诗大全、经典美文、话题作文、工作总结、词语解析、文案摘录、其他资料等等,如想了解不同资料格式和写法,敬请关注!Download tips: This document is carefully compiled by theeditor. I hope that after you download them,they can help yousolve practical problems. The document can be customized andmodified after downloading,please adjust and use it according toactual needs, thank you!In addition, our shop provides you with various types ofpractical materials,such as educational essays, diaryappreciation,sentence excerpts,ancient poems,classic articles,topic composition,work summary,word parsing,copy excerpts,other materials and so on,want to know different data formats andwriting methods,please pay attention!1. 时钟源选择:基本定时器通常可以选择内部时钟源或外部时钟源。
#include <reg51.h>#include <intrins.h>#define uint unsigned int#define uchar unsigned charsbit LS138A = P2^2; //定义138译码器的输入A脚由P2.2控制sbit LS138B = P2^3; //定义138译码器的输入脚B由P2.3控制sbit LS138C = P2^4; //定义138译码器的输入脚C由sbit k1=P2^0;sbit k2=P2^1 ;sbit k3=P2^5;bit flag;uchar sec=0,min=0,hour=12;uchar count_10ms, DelayCNT;int m=1;//此表为LED 的字模, 共阴数码管0-9 -unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};void delay(uint z){int x,y;for(x=z;x>0;x--)for(y=20;y>0;y--) ;}void timer(){TMOD=0x01;TH0=0xdc;TL0=0x00;EA=1;ET0=1;TR0=1;}void key(){ int t;if(k1==0){delay(30);if(k1==0){ while(!k1);t++;m=t%2;}}}/************主函数**********************/ main(){ unsigned int i ;unsigned int LedOut[10];timer();while(1) //进入循环状态{if(m==0){if(k2==0){delay(30);if(k2==0)while(!k2);hour++;}if(hour>=24)hour=0;if(k3==0){delay(30);if(k3==0)while(!k3);min++;}if(min>=60)min=0;}LedOut[0]=Disp_Tab[hour/10];LedOut[1]=Disp_Tab[hour%10];LedOut[2]=Disp_Tab[10];LedOut[3]=Disp_Tab[min/10];LedOut[4]=Disp_Tab[min%10];LedOut[5]=Disp_Tab[10];LedOut[6]=Disp_Tab[sec/10];LedOut[7]=Disp_Tab[sec%10];for( i=0; i<9; i++) //实现8位动态扫描循环{P0 = LedOut[i]; //将字模送到P0口显示switch(i) //使用switch 语句控制位选也可以是用查表的方式学员可以试着自己修改{case 0:LS138A=0; LS138B=0; LS138C=0; break;case 1:LS138A=1; LS138B=0; LS138C=0; break;case 2:LS138A=0; LS138B=1; LS138C=0; break;case 3:LS138A=1; LS138B=1; LS138C=0; break;case 4:LS138A=0; LS138B=0; LS138C=1; break;case 5:LS138A=1; LS138B=0; LS138C=1; break;case 6:LS138A=0; LS138B=1; LS138C=1; break;case 7:LS138A=1; LS138B=1; LS138C=1; break;}delay(10);}}}void timer1() interrupt 1 // 中断函数{TH0=0xdc;TH0=0x00;key();if(m){{count_10ms++;}if(count_10ms==10){count_10ms=0;sec++;if(sec>=60){sec=0;min++;if(min>=60){min=0;hour++;if(hour>=24){hour=0;sec=0;min=0;}}}}}}。