求一个用c++编写的时钟程序的源代码

求一个用c++编写的时钟程序的源代码,第1张

//

/ 时钟源程序 /

/ 仿WINDOWS界面设计 /

/ Turboc3下调试通过 /

/ 包含两个源文件: /

/ clockcpp wincpp /

//

#include"stdioh"

#include"stdlibh"

#include"conioh"

#include"timeh"

#include"stringh"

#include"mathh"

#include"dosh"

#include"biosh"

#include"wincpp" //调用窗体显示

#define TRUE 1

#define FALSE 0

#define PI 31415926

void cursor(int x,int y) //光标

{

int count=0;

while(count<=10){

delay(20);

count++;

if(count<=5)

setcolor(9);

else

setcolor(7);

line(x,y,x,y+12);

line(x+1,y,x+1,y+12);

}

}

int keyscancode() //检测按键

{

int key;

while(bioskey(1)==0) return 0;

key=bioskey(0);

key=key&0xff key&0xff:key;

return(key);

}

void message(int x,int y,int n) //状态栏信息

{

char msg[6]={"Press ESC to quit clock",

"Press T or t to set time",

"Press D or d to set date",

"Press ESC to cancel set",

"Press Enter to confirm set",

"Press BackSpace to delete a number"};

setfillstyle(SOLID_FILL,LIGHTGRAY);

bar(x,y,x+508,y+12);

setcolor(10);

outtextxy(x,y+2,msg[n]);

}

class CLOCK:public WIN //定义CLOCK为WIN的继承类

{

int hour,min,sec;

int year,mon,day;

int x,y,radio;

float ak,aj,ai;

int xs,ys,xm,ym,xh,yh;

public:

CLOCK(char ,int,int,int,int);

int isleapyear(int);

int isweek(int,int);

void showclock();

void runclock();

void setclock();

void inittodaydate();

void initnowtime();

void showdate();

void hidedate();

void pip();

void showtime();

void setdates();

};

//构造函数

CLOCK::CLOCK(char n,int a,int b,int c,int d):WIN(n,a,b,c,d){

x=a+130;

y=b+155;

radio=120;

}

int CLOCK::isleapyear(int year) //判断是否闰年

{

if(year%100!=0&&year%4==0)

return TRUE;

if(year%400==0)

return TRUE;

return FALSE;

}

int CLOCK::isweek(int year,int mon) //计算year年mon月1号的星期数

{

int days=0,week=0;

int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};

if(isleapyear(year)) month[1]=29;

else month[1]=28;

for(int i=1;i<year;i++)

{

days=365;

if(isleapyear(i)) days=366;

week=(days+week)%7;

}

for(i=0;i<mon-1;i++)

week=(month[i]+week)%7;

return week;

}

void CLOCK::setdates() //设置曰期

{

struct date set;

int key=0,count=0,index=0;

int textx=x+140,texty=y+42,tcnt=0;

char text[2];

int dateset[10];

setfillstyle(SOLID_FILL,LIGHTGRAY);

bar(textx,texty,textx+150,texty+12);

do{

message(80+6,380-12,index+3);

count++;

index=count/10;

if(count==29) count=0;

runclock();

key=keyscancode();

cursor(textx,texty);

if((key>='0'&&key<='9')&&tcnt!=10)

{ //限制按键,防止误 *** 作

if(tcnt<4||(tcnt==5&&key<='1')||((tcnt==6&&dateset[tcnt-1]==1&&key<='2')||(tcnt==6&&dateset[tcnt-1]==0))||(tcnt==8&&key<='3')||(tcnt==9))

{

sprintf(text,"%c",key);

text[1]='\0';

setcolor(LIGHTBLUE);

outtextxy(textx,texty+2,text);

dateset[tcnt]=key-'0';

textx+=10;

tcnt++;

if((tcnt==4||tcnt==7)&tcnt!=10){

outtextxy(textx,texty+2,"-");

textx+=10;

tcnt++;

}

}

}

if(key==8&&tcnt>0){

if(tcnt==5||tcnt==8)

{

tcnt-=2;

textx-=20;

}

else{

tcnt--;

textx-=10; }

setfillstyle(SOLID_FILL,LIGHTGRAY);

bar(textx,texty,textx+8,texty+12);

}

}while(key!=13&&key!=27);

if(key==27) return; //如果是ESC键,则退出设置

if(key==13) //如果ENTER键,则设置曰期

{

setda_year=dateset[0]1000+dateset[1]100+dateset[2]10+dateset[3];

setda_mon=dateset[5]10+dateset[6];

setda_day=dateset[8]10+dateset[9];

setdate(&set);

}

}

void CLOCK::setclock() //设置时间

{

struct time set;

int key=0,count=0,index=0;

int textx=x+140,texty=y+6,tcnt=0;

char text[2];

int timeset[8];

setfillstyle(SOLID_FILL,LIGHTGRAY);

bar(textx,texty,textx+150,texty+12);

do{

message(80+6,380-12,index+3);

count++;

index=count/10;

if(count==29) count=0;

key=keyscancode();

cursor(textx,texty);

if((key>='0'&&key<='9')&&tcnt!=8)

{ //限制按键,防止误 *** 作

if((tcnt==0&&key<='2')||((tcnt==1&&key<='3'&&timeset[tcnt-1]==2)||(tcnt==1&&timeset[tcnt-1]!=2))||(tcnt==3&&key<='5')||(tcnt==6&&key<='5')||tcnt==4||tcnt==7)

{

sprintf(text,"%c",key);

text[1]='\0';

setcolor(9);

outtextxy(textx,texty+2,text);

timeset[tcnt]=key-'0';

textx+=10;

tcnt++;

if((tcnt==2||tcnt==5)&&tcnt!=8) {

outtextxy(textx,texty+2,":");

textx+=10;

tcnt++;

}

}

}

if(key==8&&tcnt>0){

if(tcnt==3||tcnt==6){

tcnt-=2;

textx-=20;

}

else{

tcnt--;

textx-=10;

}

setfillstyle(SOLID_FILL,LIGHTGRAY);

bar(textx,texty,textx+8,texty+12);

}

}while(key!=13&&key!=27);

if(key==27) return;

if(key==13)

{

setti_hour=timeset[0]10+timeset[1];

setti_min=timeset[3]10+timeset[4];

setti_sec=timeset[6]10+timeset[7];

settime(&set);

}

}

void CLOCK::showdate() //在窗体上显示曰期

{

char nowdate[15];

char days[4];

char week[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int dayx=x+135,dayy=y-88,todayweek=0,control;

struct date d;

getdate(&d);

setcolor(12);

for(int i=0;i<7;i++)

outtextxy(x+i84+135,y-100,week[i]);

todayweek=isweek(dda_year,dda_mon); // 得到本月1号的星期数

if(todayweek+1!=7)

for(i=1;i<=todayweek+1;i++) dayx=dayx+48;

control=todayweek+1;

if(isleapyear(dda_year)) month[1]=29;

else month[1]=28;

setcolor(11);

for(i=1;i<=month[dda_mon-1];i++)

{

control++;

sprintf(days,"%3d",i);

if(i==dda_day) setcolor(LIGHTMAGENTA);

outtextxy(dayx,dayy,days);

setcolor(11);

dayx=dayx+48;

if(control%7==0) { dayx=x+135; dayy=dayy+12; }

}

todayweek=(todayweek+day)%7; //得到今天的星期数

sprintf(nowdate,"%04d-%02d-%02d %s",dda_year,dda_mon,dda_day,week[todayweek]);

nowdate[14]='\0';

setcolor(LIGHTBLUE);

outtextxy(x+185,y-120,nowdate);

if(dda_year!=year||dda_mon!=mon||dda_day!=day) //如果曰期改变了,则先隐藏曰期,再显示

hidedate();

}

void CLOCK::hidedate() //隐藏曰期

{

inittodaydate();

setfillstyle(SOLID_FILL,LIGHTGRAY); //抹去曰期,如果曰期改变了

bar(x+135,y-90,x+135+684+38,y-90+126);

bar(x+165,y-122,x+170+168+10,y-112);

}

void CLOCK::showtime() //显示时间

{

char nowtime[9];

sprintf(nowtime,"%02d:%02d:%02d",hour,min,sec);

nowtime[8]='\0';

setcolor(14);

outtextxy(x-30,y+82,nowtime);

ak=(90+30(12-hour)-min5/606)PI/180; //时针角度

aj=(90+6(60-min))PI/180; //分针角度

ai=(90+6(60-sec))PI/180; //秒针角度

xs=x+cos(ai)(radio-10);

ys=y-sin(ai)(radio-10);

xm=x+cos(aj)(radio-25);

ym=y-sin(aj)(radio-25);

xh=x+cos(ak)(radio-45);

yh=y-sin(ak)(radio-45);

setcolor(RED); //画时针

setlinestyle(0,0,THICK_WIDTH);

line(x-cos(ak)(radio-118),y+sin(ak)(radio-118),xh,yh);

setlinestyle(0,0,NORM_WIDTH); //画时针在钟面上指示位置

circle(x+cos(ak)radio,y-sin(ak)radio,2);

setcolor(LIGHTGREEN);

circle(x+cos(aj)radio,y-sin(aj)radio,2); //画分针在钟面上的指示位置

line(x-cos(aj)(radio-115),y+sin(aj)(radio-115),xm,ym); //画分针

setcolor(LIGHTBLUE);

line(x-cos(ai)(radio-110),y+sin(ai)(radio-110),xs,ys); //画秒针

circle(x+cos(ai)radio,y-sin(ai)radio,2); //画秒针在钟面上的指示位置

}

void CLOCK::runclock() //运行时钟

{

int h,m,s;

float ah;

struct time tim;

gettime(&tim);

s=timti_sec;

m=timti_min;

h=timti_hour;

ah=(90+30(12-h)-m5/606)PI/180;

showdate();

showtime();

delay(200);

nosound();

if(ah!=ak) {

setcolor(LIGHTGRAY);

setlinestyle(0,0,THICK_WIDTH);

line(x-cos(ak)(radio-118),y+sin(ak)(radio-118),xh,yh);

setlinestyle(0,0,NORM_WIDTH);

if(hour%5==0) setcolor(YELLOW);

circle(x+cos(ak)radio,y-sin(ak)radio,2);

setcolor(LIGHTGRAY);

hour=h;

}

if(m!=min) {

setcolor(LIGHTGRAY);

line(x-cos(aj)(radio-115),y+sin(aj)(radio-115),xm,ym);

if(min%5==0) setcolor(YELLOW);

circle(x+cos(aj)radio,y-sin(aj)radio,2);

min=m;

}

if(s!=sec) {

setfillstyle(SOLID_FILL,LIGHTGRAY);

bar(x-30,y+80,x-30+88,y+80+8);

setcolor(LIGHTGRAY);

if(sec%5==0) setcolor(YELLOW);

circle(x+cos(ai)radio,y-sin(ai)radio,2);

setcolor(LIGHTGRAY);

line(x-cos(ai)(radio-110),y+sin(ai)(radio-110),xs,ys);

sec=s;

if(hour>=12) //整点报时

h=hour-12;

else

h=hour;

if(min==59&&sec>=60-h)

sound(392);

if(min==00&&sec==00)

sound(784);

}

showclock();

}

void CLOCK::inittodaydate() //初始化今天的曰期

{

struct date initdate;

getdate(&initdate);

year=initdateda_year;

mon=initdateda_mon;

day=initdateda_day;

}

void CLOCK::initnowtime() //初始化时间

{

struct time inittime;

gettime(&inittime);

hour=inittimeti_hour;

min=inittimeti_min;

sec=inittimeti_sec;

}

void CLOCK::showclock() //显示时钟

{

float alpha;

int x0,y0,i;

char s[3];

for(i=60;i>=1;i--) //显示钟面上数字

{

alpha=(90+6(60-i))PI/180;

x0=x+cos(alpha)radio;

y0=y-sin(alpha)radio;

setcolor(14);

if(i%5==0){

circle(x0,y0,2);

sprintf(s,"%d",i/5);

outtextxy(x+cos(alpha)(radio-10)-4,y-sin(alpha)(radio-10)-2,s);

}

else

circle(x0,y0,1);

}

}

void about(int x,int y,int index) //关于与帮助信息

{

int i=0,t=0;

char abouttext[]={"You can get message from",

" the status bar at the ",

"bottom",

"When you set time,you just",

"input like this:080819",

"The symbol ':' isn't needed",

"Set date is the same",

"This clock is designed by",

" Nie Shiqiu(C99102-07)",

"I'm very interested in C/C++",

"This is my course design"};

setfillstyle(SOLID_FILL,LIGHTGRAY);

bar(x,y,x+278+6,y+412);

t=index-3;

if(index<4){

y=y+(3-index)12;

t=0;

}

for(i=t;i<=index;i++)

{

if(i<11){

if(i==8) setcolor(RED); //名字着重显示

else setcolor(LIGHTBLUE);

outtextxy(x+2,y+2,abouttext[i]);

}

y+=12;

}

}

void main() //主程序

{

int driver=DETECT,mode;

int exitflag=FALSE,key,index=0,count=0,aboutindex=0,acnt=0;

WIN winCLK("Clock",80,80,570,385); //创建窗体

CLOCK c1("Clock",80,80,570,385);

initgraph(&driver,&mode,"");

winCLKForm(FALSE);

c1showclock();

c1initnowtime();

do{

about(342,80+185+44,aboutindex);

acnt++;

aboutindex=acnt/10;

if(acnt==139) acnt=0;

message(80+6,380-12,index);

count++;

index=count/10;

if(count==29) count=0;

key=keyscancode();

c1runclock();

if(key==27) exitflag=TRUE; //按ESC退出

if(key=='T'||key=='t') {

c1showtime();

c1setclock();

}

if(key=='D'||key=='d') {

c1setdates();

}

}while(exitflag==FALSE);

winCLKdraw_closebutton(570-18,80+4,BLACK,TRUE);

delay(300);

closegraph();

}

//

/ 窗体文件 wincpp/

//

#include"graphicsh"

#define TRUE 1

#define FALSE 0

class WIN

{

char title[20];

protected:

int left,top,right,bottom;

public:

WIN(char n,int a,int b,int c,int d){strcpy(title,n);left=a;top=b;right=c;bottom=d;}

void draw_minbutton(int left,int top,int color,int state);

void draw_maxbutton(int left,int top,int color,int state);

void draw_closebutton(int left,int top,int color,int state);

void Form(int);

void Button(int,int,int,int,int);

void line3d(int,int,int,int,int);

void Frame(int,int,int,int,char );

};

void WIN::Frame(int left,int top,int right,int bottom,char s)

{

line3d(left,top+2,right,top+2,FALSE);

line3d(left,top+3,left,bottom,FALSE);

line3d(left,bottom,right,bottom,FALSE);

line3d(right,top+2,right,bottom,FALSE);

bar(left+4,top,left+4+8strlen(s)+4,top+8);

setcolor(10);

outtextxy(left+5,top,s);

}

void WIN::line3d(int left,int top,int right,int bottom,int state) /画有凹凸感的线条函数/

{

if(state==FALSE){

setcolor(0);

line(left,top,right,bottom);

setcolor(15);

if(top==bottom){

line(left,top+1,right,bottom+1);

}

if(left==right){

line(left+1,top,right+1,bottom);

} }

else

{ setcolor(15);

line(left,top,right,bottom);

setcolor(0);

if(top==bottom){

line(left,top+1,right,bottom+1);

}

if(left==right){

line(left+1,top,right+1,bottom);

} }

}

void WIN::Form(int state)

{

Button(left,top,right,bottom,state);

setfillstyle(1,1);

bar(left+2,top+2,right-2,top+20);

setcolor(YELLOW);

circle(left+12,top+12,8);

line(left+12,top+12,left+12,top+4);

line(left+12,top+12,left+16,top+12);

outtextxy(left+16+10,top+8,title);

draw_minbutton(right-163-4,top+4,BLACK,FALSE);

draw_maxbutton(right-162-3,top+4,BLACK,FALSE);

draw_closebutton(right-18,top+4,BLACK,FALSE);

Frame(left+5,top+25,left+255,top+278,"Time");

Frame(left+260,top+25,right-5,top+25+206,"Date");

line3d(left+280,top+25+20,right-25,top+25+20,FALSE);

Frame(left+260,top+150,right-5,top+150+30,"SetTime(T)");

Button(left+265,top+160,right-8,top+150+25,TRUE);

Frame(left+260,top+185,right-5,top+185+30,"SetDate(D)");

Button(left+265,top+195,right-8,top+185+25,TRUE);

Button(left+4,bottom-20,right-4,bottom-4,TRUE);

Frame(left+260,top+185+35,right-5,bottom-27,"About");

}

void WIN::Button(int left,int top,int right,int bottom,int state)

{

if(state==-1){

setfillstyle(1,7);

bar(left,top,right,bottom);}

setfillstyle(1,7);

bar(left,top,right,bottom);

if(state==FALSE)

{

setcolor(15);

line(left,top,right,top);

line(left,top,left,bottom);

setcolor(0);

line(left,bottom,right,bottom);

line(right,top,right,bottom);

setcolor(8);

line(left+1,bottom-1,right-1,bottom-1);

line(right-1,top+1,right-1,bottom-1);

}

if(state==TRUE)

{

setcolor(8);

line(left,top,right,top);

line(left,top,left,bottom);

setcolor(15);

line(left,bottom,right,bottom);

line(right,top,right,bottom);

}

}

void WIN::draw_minbutton(int left,int top,int color,int state)

{

if(state==FALSE)

{

Button(left,top,left+15,top+15,FALSE);

setcolor(color);

line(left+3,top+11,left+12,top+11);

line(left+3,top+12,left+12,top+12);

}

else

{

Button(left,top,left+15,top+15,TRUE);

setcolor(color);

line(left+3,top+13,left+12,top+13);

line(left+3,top+12,left+12,top+12);

}

}

void WIN::draw_maxbutton(int left,int top,int color,int state)

{

if(state==FALSE)

{

Button(left,top,left+15,top+15,FALSE);

setcolor(color);

rectangle(left+4,top+4,left+12,top+12);

rectangle(left+4,top+5,left+12,top+12);

}

else

{

Button(left,top,left+15,top+15,TRUE);

setcolor(color);

rectangle(left+4,top+4,left+12,top+14);

rectangle(left+4,top+3,left+12,top+14);

}

}

void WIN::draw_closebutton(int left,int top,int color,int state)

{

if(state==FALSE)

{

Button(left,top,left+15,top+15,FALSE);

setcolor(color);

line(left+4,top+4,left+12,top+12);

line(left+5,top+4,left+13,top+12);

line(left+4,top+12,left+12,top+4);

line(left+5,top+12,left+13,top+4);

}

else

{

Button(left,top,left+15,top+15,TRUE);

setcolor(color);

line(left+4,top+4,left+12,top+12);

line(left+5,top+4,left+13,top+12);

line(left+4,top+12,left+12,top+4);

line(left+5,top+12,left+13,top+4);

}

}

/ ch07-5-3c - 计时器实验3 -60秒计数器,每1分钟LED反相1次 /

//==声明区================================

#include // 定义8x51暂存器之标头档,P2-17~19

#define SEG P2 // 定义七节显示器接至Port 2

#define SCANP P1 // 定义扫瞄线接至Port 1

sbit LED=P0^7; // 声明LED接至P07

/声明T0计时相关声明/ // THx TLx 计算参考

#define count_M1 50000 // T0(MODE 1)之计量值,005s

#define TH_M1 (65636-count_M1)/256// T0(MODE 1)计量高8位元

#define TL_M1 (65636-count_M1)%256// T0(MODE 1)计量低8位元

int count_T0=0; // 计算 T0中断次数

/声明T1扫瞄相关声明/

#define count_M2 250 // T1(MODE 2)之计量值,025ms

#define TH_M2 (256-count_M2) // T1(MODE 2)自动载入计量

#define TL_M2 (256-count_M2) // T1(MODE 2)计数量

char count_T1=0; // 计算 T1中断次数

/ 声明七节显示器驱动信号阵列(共阳) /

char code TAB[10]={ 0xc0, 0xf9, 0xa4, 0xb0, 0x99, //数字0-4

0x92, 0x83, 0xf8, 0x80, 0x98 }; //数字5-9

char disp[2]={ 0xc0, 0xc0 }; // 声明显示区阵列初始显示00

/ 声明基本变量 /

char seconds=0; // 秒数

char scan=0; // 扫瞄信号

//==主程序================================

main() // 主程序开始

{ IE=0x8a; // 1000 1010,启用TF0、TF1中断(6-4页)

TMOD=0x21; // 0010 0001,T1采mode 2、T0采mode 1

TH0=TH_M1; TL0=TL_M1;// 设置T0计数量高8位元、低8位元

TR0=1; // 启动T0(7-7页)

TH1=TH_M2; TL1=TL_M2;// 设置T1自动载入值、计数量

TR1=1; // 启动T1

LED=1; // 关闭LED

while(1); // 无穷回圈,程序停滞

} // 主程序结束

//== T0中断子程序- 计算并显示秒数 ==================

void T0_1s(void) interrupt 1 // T0中断子程序开始

{ TH0=TH_M1; TL0=TL_M1; // 设置T0计数量高8位元、低8位元

if (++count_T0==20) // 若中断20次,即005x20=1秒

{ count_T0=0; // 重新计次

seconds++; // 秒数加1

if (seconds==60) // 若超过60秒

{ seconds=0; // 秒数归0,重新开始

LED=~LED; // 切换LED

} // if叙述结束(超过60秒)

} // if叙述结束(中断20次)

disp[1]=TAB[seconds/10]; // 填入十位数显示区

disp[0]=TAB[seconds%10]; // 填入个位数显示区

} // T0中断子程序结束

//===T1中断子程序 - 扫瞄 =========================

void T1_8ms(void) interrupt 3 // T1中断子程序开始

{ if (++count_T1==32) // 若中断32次,即025mx32=8ms

{ count_T1=0; // 重新计次

if (++scan==3) scan=1;// 若超过十位数,显示个位

SEG=0xff; // 关闭7段显示器

SCANP=~scan; // 输出扫瞄信号

SEG=disp[scan-1]; // 输出显示信号

} // 结束if判断(中断32次)

} // T0中断子程序结束

图11

程序如下:

library IEEE;

use IEEESTD_LOGIC_1164ALL;

use IEEESTD_LOGIC_ARITHALL;

use IEEESTD_LOGIC_UNSIGNEDALL;

entity xuan21 is

Port ( alarm,a,b: in std_logic;

y:out std_logic);

end xuan21 ;

architecture one of xuan21 is

begin

process(alarm,a,b)

begin

if alarm='0' then y<=a;else y<=b;

end if;

end process;

end one;

仿真波形如下图12:

图12

(2)三位二选一:

模块图如图13。用以进行正常计时时间与闹铃时间显示的选择,alarm输入为按键。当alarm按键未曾按下时二选一选择器会选择输出显示正常的计时结果,否则当alarm按键按下时选择器将选择输出显示闹铃时间显示。

图13

程序如下:

library IEEE;

use IEEESTD_LOGIC_1164ALL;

use IEEESTD_LOGIC_ARITHALL;

use IEEESTD_LOGIC_UNSIGNEDALL;

entity x213 is

Port ( alarm : in std_logic;

y:out std_logic_vector(3 downto 0);

a,b: in std_logic_vector(3 downto 0));

end x213;

architecture one of x213 is

begin

process(alarm,a,b)

begin

if alarm='0' then y<=a;else y<=b;

end if;

end process;

end one;

仿真结果如下图14:

图14

8、整点报时及闹时:

模块图如图15。在59分51秒、53秒、55秒、57秒给扬声器赋以低音512Hz信号,在59分59秒给扬声器赋以高音1024Hz信号,音响持续1秒钟,在1024Hz音响结束时刻为整点。当系统时间与闹铃时间相同时给扬声器赋以高音1024Hz信号。闹时时间为一分钟。

图15

程序如下:

library IEEE;

use IEEESTD_LOGIC_1164ALL;

use IEEESTD_LOGIC_ARITHALL;

use IEEESTD_LOGIC_UNSIGNEDALL;

entity voice is

Port ( hou1,huo0,min1,min0,sec1,sec0,hh,hl,mh,ml: std_logic_vector(3 downto 0);

in_1000,in_500:in std_logic;

q : out std_logic);

end voice;

architecture one of voice is

begin

process(min1,min0,sec1,sec0)

begin

if min1="0101" and min0="1001" and sec1="0101" then

if sec0="0001" or sec0="0011" or sec0="0101" or sec0="0111"

then q<=in_500;

elsif sec1="0101" and sec0="1001" then q<=in_1000;

else q<='0';

end if;

else q<='0';

end if;

if min1=mh and min0=ml and hou1=hh and huo0=hl then

q<=in_1000;

end if;

end process;

end one;

仿真波形如下图16

图16

9、顶层原理图:

三、感想

通过这次设计,既复习了以前所学的知识,也进一步加深了对EDA的了解,让我对它有了更加浓厚的兴趣。特别是当每一个子模块编写调试成功时,心里特别的开心。但是在画顶层原理图时,遇到了不少问题,最大的问题就是根本没有把各个模块的VHD文件以及生成的器件都全部放在顶层文件的文件夹内,还有就是程序设计的时候考虑的不够全面,没有联系着各个模式以及实验板的情况来编写程序,以至于多考虑编写了译码电路而浪费了很多时间。在波形仿真时,也遇到了一点困难,想要的结果不能在波形上得到正确的显示

:在分频模块中,设定输入的时钟信号后,却只有二分频的结果,其余三个分频始终没反应。后来,在数十次的调试之后,才发现是因为规定的信号量范围太大且信号的初始值随机,从而不能得到所要的结果。还有的仿真图根本就不出波形,怎么调节都不管用,后来才知道原来是路径不正确,路径中不可以有汉字。真是细节决定成败啊!总的来说,这次设计的数字钟还是比较成功的,有点小小的成就感,终于觉得平时所学的知识有了实用的价值,达到了理论与实际相结合的目的,不仅学到了不少知识,而且锻炼了自己的能力,使自己对以后的路有了更加清楚的认识,同时,对未来有了更多的信心。

四、

参考资料:

1、潘松,王国栋,VHDL实用教程〔M〕成都:电子科技大学出版社,2000(1)

2、崔建明主编,电工电子EDA仿真技术北京:高等教育出版社,2004

3、李衍编著,EDA技术入门与提高王行西安:西安电子科技大学出版社,2005

4、侯继红,李向东主编,EDA实用技术教程北京:中国电力出版社,2004

5、沈明山编著,EDA技术及可编程器件应用实训北京:科学出版社,2004

6、侯伯亨等,VHDL硬件描述语言与数字逻辑电路设计西安: 西安电子科技大学出版社,1997

7、辛春艳编著,VHDL硬件描述语言北京:国防工业出版社,2002 就这些

import javaawt;

import javaawtevent;

import javaxswing;

import sunutilcalendarGregorian;

import javautilCalendar;

import javautilGregorianCalendar;

public class ClockPointer extends JFrame{

        int x, y, x0, y0, r, h, olds_x, olds_y, oldm_x, oldm_y, oldh_x, oldh_y, 

        ss,mm, hh, old_m, old_h, ang;

        final double RAD = MathPI/180;

    public ClockPointer(){

        super("Java时钟");

        setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

        Image image = getToolkit()getImage("clockgif");

        setIconImage(image);

        setSize(400,400);

        setBackground(Colorwhite);

        //setLocation(300,150);

        thissetLocationRelativeTo(null);

        setResizable(true);

        int delay = 1000;

        //创建一个监听事件

        setVisible(true);

        ActionListener drawClock = new ActionListener(){

            public void actionPerformed(ActionEvent evt){

                repaint();

            }

        };

        //创建一个时间计数器,每一秒触发一次

        new Timer(delay, drawClock)start();

    }

    

    javatextSimpleDateFormat fmTime = new javatextSimpleDateFormat("HH:mm:ss");

    //绘制图形

    public void paint(Graphics g){

        superpaint(g);

        gsetFont(null);

        Graphics2D g2D = (Graphics2D)g;

        

        Insets insets = getInsets();

        int L = insetsleft/2, T = insetstop/2;

        h = getSize()height;

        gsetColor(Colorwhite);

        //画圆

        g2DsetStroke(new BasicStroke(20f));

        gsetColor(Colorgray);

        gdrawOval(L+40, T+40, h-80, h-80);

        r = h/2 - 40;

        x0 = 40 + r - 5 + L;

        y0 = 40 + r - 5 - T;

        ang = 60;

        //绘制时钟上的12个字

        for(int i = 1;i <= 12;i ++){

            x = (int)((r+10)Mathcos(RADang)+x0);

            y = (int)((r+10)Mathsin(RADang)+y0);

            gsetColor(Colorblack);

            gdrawString(""+i, x, h-y);

            ang -=30;

        }

        //获得现在的时间

        Calendar now = new GregorianCalendar();

        int nowh = nowget(CalendarHOUR_OF_DAY);

        int nowm = nowget(CalendarMINUTE);

        int nows = nowget(CalendarSECOND);

        

        String st=fmTimeformat(nowgetTime());

        //在窗体上显示时间

        gsetColor(Colorpink);

        gfillRect(L, T, 50, 28);

        gsetColor(Colorblue);

        gdrawString(st,L+2,T+26);

        

        //计算时间与度数的关系

        ss = 90 - nows6;

        mm = 90 - nowm6;

        hh = 90 - nowh30 - nowm/2;

        x0 = r+40+L;

        y0 = r+40+T;

        g2DsetStroke(new BasicStroke(12f));

        //擦除秒针

        //if(olds_x > 0){

        //    gsetColor(getBackground());

        //    // gsetColor(Colorgray);

        //    gdrawLine(x0, y0, olds_x, h-olds_y); // ()

        //}

        //绘制秒针

        x = (int)(r09Mathcos(RADss))+x0;

        y = (int)(r09Mathsin(RADss))+y0-2T;

        gsetColor(Coloryellow);

        gdrawLine(x0, y0, x, h-y);

        olds_x = x;

        olds_y = y;

        g2DsetStroke(new BasicStroke(22f));

        //擦除分针

        //if(old_m!=mm){

        //    gsetColor(getBackground());

        //    gdrawLine(x0,y0,oldm_x,h-oldm_y);

        //}

        //绘制分针

        x = (int)(r07Mathcos(RADmm))+x0;

        y = (int)(r07Mathsin(RADmm))+y0-2T;

        gsetColor(Colorgreen);

        gdrawLine(x0,y0,x,h-y);

        oldm_x = x;

        oldm_y = y;

        old_m = mm;

        g2DsetStroke(new BasicStroke(32f));

        //擦除时针

        //if(old_h!=hh){

        //    gsetColor(getBackground());

        //    gdrawLine(x0,y0,oldh_x,h-oldh_y);

        //}

        //绘制时针

        x = (int)(r05Mathcos(RADhh))+x0;

        y = (int)(r05Mathsin(RADhh))+y0-2T;

        gsetColor(Colorred);

        gdrawLine(x0,y0,x,h-y);

        oldh_x = x;

        oldh_y = y;

        old_h = hh;

    }

    public static void main(String[] args){

        new ClockPointer();

    }

}

//整理一下

我以前做的一个6位数码管的时钟程序,你可以看看,其实6位足够了。

KEYVAL EQU 30H

KEYTM EQU 31H

KEYSCAN EQU 32H

DAT EQU 33H

SCANLED EQU 39H

CLK EQU 77H

SEC EQU 78H

MIN EQU 79H

HOUR EQU 7AH

PAUSE BIT 00H

DOT BIT 01H

ORG 0000H

LJMP MAIN

ORG 000BH

LJMP T0ISR ;50ms定时

ORG 001BH

LJMP T1ISR ;扫描显示

ORG 0030H

MAIN:

MOV SP,#5FH

MOV TMOD,#11H

MOV TH0,#03CH

MOV TL0,#0B0H

MOV TH1,#0ECH

MOV TL1,#078H

MOV KEYVAL,#0

MOV SCANLED,#0

MOV 33H,#10H

MOV 34H,#10H

MOV 35H,#10H

MOV 36H,#10H

MOV 37H,#10H

MOV 38H,#10H

MOV SEC,#0

MOV MIN,#0

MOV HOUR,#0

MOV CLK,#0

CLR PAUSE

SETB EA

SETB ET1

SETB TR1

LOOP:

LCALL KEYSEL

MOV A,KEYVAL

CJNE A,#0FFH,LOOP1

SJMP LOOP

LOOP1:

CJNE A,#10,LOOP2 ;“ON”启动

SETB TR0

SETB ET0

SETB PAUSE

SJMP LOOP

LOOP2:

CJNE A,#11,LOOP3 ;“=”清零

MOV SEC,#0

MOV MIN,#0

MOV HOUR,#0

LCALL DISCHG

SJMP LOOP

LOOP3:

CJNE A,#15,LOOP4 ;“+”暂停

CLR TR0

CLR ET0

CLR PAUSE

SJMP LOOP

LOOP4:

CJNE A,#14,LOOP5 ;“-”清显示暂停

MOV 33H,#10H

MOV 34H,#10H

MOV 35H,#10H

MOV 36H,#10H

MOV 37H,#10H

MOV 38H,#10H

CLR TR0

CLR ET0

CLR PAUSE

SJMP LOOP

LOOP5:

CJNE A,#10,LOOP6 ;数字键

LOOP6:

JC LOOP7

LJMP LOOP

LOOP7:

JNB PAUSE,LOOP8 ;暂停状态可以输入数字键

LJMP LOOP

LOOP8:

MOV 33H,34H

MOV 34H,35H

MOV 35H,36H

MOV 36H,37H

MOV 37H,38H

MOV 38H,KEYVAL

MOV A,33H

SWAP A

ORL A,34H

LCALL BCDH

MOV HOUR,A

MOV A,35H

SWAP A

ORL A,36H

LCALL BCDH

MOV MIN,A

MOV A,37H

SWAP A

ORL A,38H

LCALL BCDH

MOV SEC,A

LJMP LOOP

;------------------

;BCD转换为十六进制

BCDH:

MOV B,#10H

DIV AB

MOV R7,B

MOV B,#10

MUL AB

ADD A,R7

RET

;------------------

;十六进制转换为BCD

HBCD:

MOV B,#10

DIV AB

SWAP A

ORL A,B

RET

;------------------

KEYSEL:

MOV KEYVAL,#0

MOV KEYSCAN,#0EFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS1

MOV KEYVAL,A

SJMP KEYRTN

KEYS1:

MOV KEYSCAN,#0DFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS2

CLR C

ADD A,#4

MOV KEYVAL,A

SJMP KEYRTN

KEYS2:

MOV KEYSCAN,#0BFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS3

CLR C

ADD A,#8

MOV KEYVAL,A

SJMP KEYRTN

KEYS3:

MOV KEYSCAN,#7FH

LCALL GETKEY

MOV A,KEYTM

JZ KEYRTN

CLR C

ADD A,#12

MOV KEYVAL,A

KEYRTN:

LCALL CHGKEY

RET

;--------------------

GETKEY:

MOV KEYTM,#0

MOV A,KEYSCAN

MOV P3,A

NOP

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JZ NOKEY

MOV R2,#10

LCALL DELAY

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JZ NOKEY

MOV A,P3

ANL A,#0FH

MOV R7,A

SF:

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JNZ SF

MOV A,R7

CJNE A,#0EH,NK1

MOV KEYTM,#1

SJMP NOKEY

NK1:

CJNE A,#0DH,NK2

MOV KEYTM,#2

SJMP NOKEY

NK2:

CJNE A,#0BH,NK3

MOV KEYTM,#3

SJMP NOKEY

NK3:

CJNE A,#07H,NOKEY

MOV KEYTM,#4

NOKEY: RET

;--------------------

DELAY:

MOV R3,#50

DELAY1:

MOV R4,#100

DJNZ R4,$

DJNZ R3,DELAY1

DJNZ R2,DELAY

RET

;--------------------

T0ISR:

PUSH ACC

CLR TR0

MOV TH0,#3CH

MOV TL0,#0B0H

SETB TR0

INC CLK

MOV A,CLK

CJNE A,#20,T0ISRE

MOV CLK,#0

INC SEC

MOV A,SEC

CJNE A,#60,T0ISRE

MOV SEC,#0

INC MIN

MOV A,MIN

CJNE A,#60,T0ISRE

MOV MIN,#0

INC HOUR

MOV A,HOUR

CJNE A,#24,T0ISRE

MOV SEC,#0

MOV MIN,#0

MOV HOUR,#0

T0ISRE:

LCALL DISCHG

POP ACC

RETI

;--------------------

DISCHG:

MOV A,HOUR

LCALL HBCD

PUSH ACC

ANL A,#0FH

MOV 34H,A

POP ACC

ANL A,#0F0H

SWAP A

MOV 33H,A

MOV A,MIN

LCALL HBCD

PUSH ACC

ANL A,#0FH

MOV 36H,A

POP ACC

ANL A,#0F0H

SWAP A

MOV 35H,A

MOV A,SEC

LCALL HBCD

PUSH ACC

ANL A,#0FH

MOV 38H,A

POP ACC

ANL A,#0F0H

SWAP A

MOV 37H,A

RET

;--------------------

T1ISR:

PUSH ACC

CLR TR1

MOV TH1,#0ECH

MOV TL1,#78H

SETB TR1

MOV DPTR,#LEDTAB

T100:

MOV R0,#DAT

MOV A,SCANLED

ADD A,R0

MOV R0,A

MOV A,SCANLED

JNZ T101

MOV P2,#01H

CLR DOT

SJMP T1DIS

T101:

DEC A

JNZ T102

MOV P2,#02H

SETB DOT

SJMP T1DIS

T102:

DEC A

JNZ T103

MOV P2,#04H

CLR DOT

SJMP T1DIS

T103:

DEC A

JNZ T104

MOV P2,#08H

SETB DOT

SJMP T1DIS

T104:

DEC A

JNZ T105

MOV P2,#10H

CLR DOT

SJMP T1DIS

T105:

MOV P2,#20H

CLR DOT

T1DIS:

MOV A,@R0

MOVC A,@A+DPTR

JNB DOT,T1DIS1

ORL A,#01H

T1DIS1:

CPL A

MOV P0,A

INC SCANLED

MOV A,SCANLED

CJNE A,#6,T1END

MOV SCANLED,#0

T1END:

POP ACC

RETI

;--------------------

CHGKEY:

MOV A,KEYVAL

JZ KV16

DEC A

JNZ KV01

MOV KEYVAL,#7

RET

KV01:

DEC A

JNZ KV02

MOV KEYVAL,#4

RET

KV02:

DEC A

JNZ KV03

MOV KEYVAL,#1

RET

KV03:

DEC A

JNZ KV04

MOV KEYVAL,#10

RET

KV04:

DEC A

JNZ KV05

MOV KEYVAL,#8

RET

KV05:

DEC A

JNZ KV06

MOV KEYVAL,#5

RET

KV06:

DEC A

JNZ KV07

MOV KEYVAL,#2

RET

KV07:

DEC A

JNZ KV08

MOV KEYVAL,#0

RET

KV08:

DEC A

JNZ KV09

MOV KEYVAL,#9

RET

KV09:

DEC A

JNZ KV10

MOV KEYVAL,#6

RET

KV10:

DEC A

JNZ KV11

MOV KEYVAL,#3

RET

KV11:

DEC A

JNZ KV12

MOV KEYVAL,#11

RET

KV12:

DEC A

JNZ KV13

MOV KEYVAL,#12

RET

KV13:

DEC A

JNZ KV14

MOV KEYVAL,#13

RET

KV14:

DEC A

JNZ KV15

MOV KEYVAL,#14

RET

KV15:

DEC A

JNZ KV16

MOV KEYVAL,#15

RET

KV16:

MOV KEYVAL,#0FFH

RET

;--------------------

LEDTAB: DB 0FCH ;"0" 00H

DB 60H ;"1" 01H

DB 0DAH ;"2" 02H

DB 0F2H ;"3" 03H

DB 66H ;"4" 04H

DB 0B6H ;"5" 05H

DB 0BEH ;"6" 06H

DB 0E0H ;"7" 07H

DB 0FEH ;"8" 08H

DB 0F6H ;"9" 09H

DB 0EEH ;"A" 0AH

DB 3EH ;"B" 0BH

DB 9CH ;"C" 0CH

DB 7AH ;"D" 0DH

DB 9EH ;"E" 0EH

DB 8EH ;"F" 0FH

DB 00H ;" " 10H

;--------------------

END

以上就是关于求一个用c++编写的时钟程序的源代码全部的内容,包括:求一个用c++编写的时钟程序的源代码、单片机c语言时钟程序、VHDL数字时钟完整程序代码(要求要有元件例化,并且有按键消抖),谢谢啦啦啦啦等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9267704.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存