c语言怎么样编写一个时钟程序

c语言怎么样编写一个时钟程序,第1张

c语言时钟程序代码如下:

#include<windows.h>

#include<math.h>

#define ID_TIMER 1//计时器ID

#define TWOPI (2*3.14159)

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM)

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)

{

static TCHAR szAppName[]=TEXT("Clock")

HWND hwnd

MSG msg

WNDCLASS wndclass

wndclass.cbClsExtra=0

wndclass.cbWndExtra=0

wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH)

wndclass.hCursor=LoadCursor(NULL,IDC_ARROW)

wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION)

wndclass.hInstance=hInstance

wndclass.lpfnWndProc=WndProc

wndclass.lpszClassName=szAppName

wndclass.lpszMenuName=NULL

wndclass.style=CS_HREDRAW|CS_VREDRAW

if(!RegisterClass(&wndclass))

{

MessageBox(NULL,TEXT("This program requires Windows

T"),szAppName,MB_ICONERROR)

return 0

}

hwnd=CreateWindow(szAppName,TEXT("Analog Clock"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL)

ShowWindow(hwnd,iCmdShow)

UpdateWindow(hwnd)

while(GetMessage(&msg,NULL,0,0))

{

TranslateMessage(&msg)

DispatchMessage(&msg)

}

return msg.wParam

}

void Setsotropic(HDC hdc,int cxClient,int cyClient)

{

SetMapMode(hdc,MM_ISOTROPIC)

SetWindowExtEx(hdc,1000,1000,NULL)

SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL)

SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL)

}

void RotatePoint(POINT pt[],int iNum,int iAngle)

{

int i

POINT ptTemp

for(i=0i<iNumi++)

{

ptTemp.x=(int)(pt[i].x*cos(TWOPI*iAngle/360)+pt[i].y*sin(TWOPI*iAngle/360))

ptTemp.y=(int)(pt[i].y*cos(TWOPI*iAngle/360)+pt[i].x*sin(TWOPI*iAngle/360))

pt[i]=ptTemp

}

}

void DrawClock(HDC hdc)

{

int iAngle

POINT pt[3]

for(iAngle=0iAngle<360iAngle+=6)

{

pt[0].x=0

pt[0].y=900

RotatePoint(pt,1,iAngle)

pt[2].x=pt[2].y=iAngle%5?33:100

pt[0].x-=pt[2].x/2

pt[0].y-=pt[2].y/2

pt[1].x=pt[0].x+pt[2].x

pt[1].y=pt[0].y+pt[2].y

SelectObject(hdc,GetStockObject(BLACK_BRUSH))

Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y )

}

}

void DrawHands(HDC hdc,SYSTEMTIME *pst,BOOL fChange)

{

static POINT pt[3][5]={0,-150,100,0,0,600,-100,0,0,-150, 0,-200,50,0,0,800,-50,0,0,-200, 0,0,0,0,0,0,0,0,0,800 }

int i,iAngle[3]

POINT ptTemp[3][5]

iAngle[0]=(pst->wHour*30)%360+pst->wMinute/2

iAngle[1]=pst->wMinute*6

iAngle[2]=pst->wSecond*6

memcpy(ptTemp,pt,sizeof(pt))

for(i=fChange?0:2i<3i++)

{

RotatePoint(ptTemp[i],5,iAngle[i])

Polyline(hdc,ptTemp[i],5)

}

}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)

{

static int cxClient,cyClient

static SYSTEMTIME stPrevious

BOOL fChange

HDC hdc

PAINTSTRUCT ps

SYSTEMTIME st

switch(message)

{

case WM_CREATE:

SetTimer(hwnd,ID_TIMER,1000,NULL)

GetLocalTime(&st)

stPrevious=st

return 0

case WM_SIZE:

cxClient=LOWORD(lParam)

cyClient=HIWORD(lParam)

return 0

case WM_TIMER:

GetLocalTime(&st)

fChange=st.wHour!=stPrevious.wHour||st.wMinute!=stPrevious.wMinute

hdc=GetDC(hwnd)

Setsotropic(hdc,cxClient,cyClient)

SelectObject(hdc,GetStockObject(WHITE_PEN))

DrawHands(hdc,&stPrevious,fChange)

SelectObject(hdc,GetStockObject(BLACK_PEN))

DrawHands(hdc,&st,TRUE)

stPrevious=st

return 0

case WM_PAINT:

hdc=BeginPaint(hwnd,&ps)

Setsotropic(hdc,cxClient,cyClient)

DrawClock(hdc)

DrawHands(hdc,&stPrevious,TRUE)

EndPaint(hwnd,&ps)

return 0

case WM_DESTROY:

KillTimer(hwnd,ID_TIMER)

PostQuitMessage(0)

return 0

}

return DefWindowProc(hwnd,message,wParam,lParam)

}

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

C语言是由UNIX的研制者丹尼斯·里奇(Dennis Ritchie)于1970年 由 肯·汤普逊(Ken Thompson)所研制出的B语言的基础上发展和完善起来的。目前,C语言编译器普遍存在于各种不同的 *** 作系统中,例如UNIX、MS-DOS、Microsoft Windows及Linux等。C语言的设计影响了许多后来的编程语言,例如C++、Objective-C、Java、C#等。

朋友以下是本人历时一个月殚精竭虑自主开发的一个8位51单片机时钟程序,望吾之兄弟姐妹习之,此程序乃采用一键控制,通过感知按下键持续时间的长短来进行调整时间,闹铃,6只共阳数码管输出显示,此外还包括整点铃声功能,在看程序之前我先交代一下运行该程序的硬件要求:12M晶振,P0端口驱动显示,P2.0到P2.5控制显示扫描,P2.7输出铃声,注意:铃声功放三极管应采用pnp管。以下是该程序:

ORG 00H

SETB TR0

JMP START

ORG 000BH

LJMP T0

START: MOV IE,#82H

MOV TMOD,#01H

MOV DPTR,#TAB

MOV SP,#60H

MOV 20H,#0

MOV R0,#0

MOV R1,#0

MOV R2,#0

MOV R3,#0

MOV R4,#0

MOV R5,#0

MOV R6,#0

MOV R7,#0

MOV 50H,#0

MOV 51H,#0

MOV 52H,#0

MOV 53H,#0

MOV 54H,#0

MOV 55H,#0

MOV A,TL0

ADD A,0B5H

MOV TL0,A

MOV TH0,#3CH

LOOP: ANL 20H,#08H

UNSV: MOV R1,#0

LCALL DISPLAY

JB 03H,LOP1

LCALL SOUND

LCALL HDLY

LOP1: MOV P3,#0FEH 按键监视

MOV A,P3

CJNE A,#0FEH,LOP2

MOV R1,#0

JMP LOOP

LOP2: MOV R1,#2 误按判断

LOP3: LCALL HD

DJNZ R1,LOP3

JNB 04H,LOOP

CLR 04H

LOP4: INC R1

MOV C,00H 调秒

CPL C

MOV 00H,C

LCALL HDLY

CJNE R1,#40,LOP5

JMP LOOP

LOP5: JNB 05H,LOP4

CLR 05H

MOV R1,#0

LCALL HD

CLR 00H

JB 04H,TF

MOV R2,#0

MOV R3,#0

JMP LOP4

TF: CLR 04H

LOP6: INC R1

MOV C,01H 调分

CPL C

MOV 01H,C

LCALL HDLY

CJNE R1,#40,LOP7

JMP LOOP

LOP7: JNB 05H,LOP6

CLR 05H

MOV R1,#0

LCALL HD

CLR 01H

JB 04H,TS

INC R4

MOV R1,#0

CJNE R4,#10,LOP6

MOV R4,#0

INC R5

CJNE R5,#6,LOP6

MOV R5,#0

JMP LOP6

TS: CLR 04H

LOP8: INC R1

MOV C,02H 调时

CPL C

MOV 02H,C

LCALL HDLY

CJNE R1,#40,LOP9

JMP LOOP

LOP9: JNB 05H,LOP8

CLR 05H

MOV R1,#0

LCALL HD

CLR 02H

JB 04H,TN

INC R6

CJNE R7,#2,NO2

CJNE R6,#4,LOP8

MOV R7,#0

MOV R6,#0

NO2: CJNE R6,#10,LOP8

MOV R6,#0

INC R7

JMP LOP8

TN: CLR 04H 调闹铃

SETB RS0

CLR RS1

MOV R2,#0

MOV R3,#0

MOV R4,#0

MOV R5,#0

MOV R6,#0

MOV R7,#0

NS: INC R1 闹铃秒调整

MOV C,00H

CPL C

MOV 00H,C

LCALL HDLY

CJNE R1,#40,NS1

JMP NH2

NS1: JNB 05H,NS

CLR 05H

MOV R1,#0

LCALL HD

CLR 00H

JB 04H,NF

INC R2

CJNE R2,#10,NS

MOV R2,#0

INC R3

CJNE R3,#6,NS

MOV R3,#0

JMP NS

NF: CLR 04H 闹铃分

NF1: INC R1

MOV C,01H

CPL C

MOV 01H,C

LCALL HDLY

CJNE R1,#40,NF2

LJMP NH2

NF2: JNB 05H,NF1

CLR 05H

MOV R1,#0

LCALL HD

CLR 01H

JB 04H,NH

INC R4

CJNE R4,#10,NF1

MOV R4,#0

INC R5

CJNE R5,#6,NF1

MOV R5,#0

JMP NF1

NH: CLR 04H 闹铃时调整

NH1: INC R1

MOV C,02H

CPL C

MOV 02H,C

LCALL HDLY

CJNE R1,#40,NH3

NH2: MOV 50H,R2

MOV 51H,R3

MOV 52H,R4

MOV 53H,R5

MOV 54H,R6

MOV 55H,R7

CLR RS0

CLR RS1

LJMP LOOP

NH3: JNB 05H,NH1

CLR 05H

MOV R1,#0

LCALL HD

CLR 02H

JB 04H,NH2

INC R6

CJNE R7,#2,NOT2

CJNE R6,#4,NH1

MOV R6,#0

MOV R7,#0

JMP NH1

NOT2: CJNE R6,#10,NH1

MOV R6,#0

INC R7

JMP NH1

子程序区

TIMES: CJNE R0,#20,LOOP16

MOV R0,#0

INC R2

CJNE R2,#10,LOOP16

MOV R2,#0

INC R3

CJNE R3,#6,LOOP16

MOV R3,#0

ADMINI: INC R4

CJNE R4,#10,LOOP16

MOV R4,#0

INC R5

CJNE R5,#6,LOOP16

MOV R5,#0

HOUR: INC R6

LCALL SOUND 整点铃声

CJNE R7,#2,LOOP14

CJNE R6,#4,LOOP16

MOV R6,#0

MOV R7,#0

JMP LOOP16

LOOP14: CJNE R6,#10,LOOP16

MOV R6,#0

INC R7

LOOP16: RET

DISPLAY:MOV P2,#0FFH

MOV A,R2

MOVC A,@A+DPTR

MOV P0,A

MOV C,00H

MOV P2.0,C

MOV P2,#0FFH

MOV A,R3

MOVC A,@A+DPTR

MOV P0,A

MOV P2.1,C

MOV P2,#0FFH

MOV A,R4

MOVC A,@A+DPTR

MOV P0,A

MOV C,01H

MOV P2.2,C

MOV P2,#0FFH

MOV A,R5

MOVC A,@A+DPTR

MOV P0,A

MOV P2.3,C

MOV P2,#0FFH

MOV A,R6

MOVC A,@A+DPTR

MOV P0,A

MOV C,02H

MOV P2.4,C

MOV P2,#0FFH

MOV A,R7

MOVC A,@A+DPTR

MOV P0,A

MOV P2.5,C

MOV P2,#0FFH

RET

HDLY: MOV 40H,#2

HDL1: MOV 41H,#25

HDL2: MOV 42H,#100

HDL3: DJNZ 42H,HDL4

DJNZ 41H,HDL2

DJNZ 40H,HDL1

JMP HDOT

SOUT: SETB 05H

HDOT: RET

HDL4: MOV P3,#0FEH

MOV A,P3

CJNE A,#0FEH,SOUT

CLR 05H

LCALL DISPLAY

JMP HDL3

HD: MOV 30H,#5

HD1: MOV 31H,#50

HD2: MOV 32H,#100

HD3: LCALL DISPLAY

DJNZ 32H,HD4

DJNZ 31H,HD2

DJNZ 30H,HD1

SETB 04H 计满标志位

JMP HDOUT

HD4: MOV P3,#0FEH

MOV A,P3

CJNE A,#0FEH,HD3

MOV 33H,#50

HD5: MOV 34H,#100

DJNZ 34H,$

DJNZ 33H,HD5

MOV P3,#0FEH

MOV A,P3

CJNE A,#0FEH,HD3

CLR 04H

HDOUT: RET

SOUND: MOV 30H,#10

SO1: MOV 31H,#100

SO2: MOV 32H,#3

MOV C,P2.7

CPL C

MOV P2.7,C

SO3: MOV 33H,#25

DJNZ 33H,$

DJNZ 32H,SO3

DJNZ 31H,SO2

DJNZ 30H,SO1

RET

T0: PUSH PSW

PUSH ACC

CLR RS1

CLR RS0

MOV A,TL0

ADD A,#0B5H

MOV TL0,A

MOV TH0,#3CH

INC R0

LCALL TIMES

MOV A,R2

CJNE A,50H,TO

SETB 03H

MOV A,R3

CJNE A,51H,TO

MOV A,R7

CJNE A,55H,TO

MOV A,R6

CJNE A,54H,TO

MOV A,R5

CJNE A,53H,TO

MOV A,R4

CJNE A,52H,TO

MOV A,R2

ADD A,R3

ADD A,R4

ADD A,R5

ADD A,R6

ADD A,R7

JZ TO

CLR 03H

TO: POP ACC

POP PSW

RETI

TAB: DB 0C0H 0F9H 0A4H 0B0H 099H

DB 092H 082H 0F8H 080H 090H

END

DATA SEGMENT

DAY DB 'The day is $'

TIME DB 'Current time is $'

PRESS DB 'Press any key to exit!$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:

MOV AX,DATA

MOV DS,AX

BEGIN: CALL CLEAR

LEA DX,DAY

MOV AH,09H

INT 21H

MOV AH,2AH

INT 21H

MOV AX,CX

PUSH DX

XOR DX,DX

MOV BX,1000

DIV BX

PUSH DX

MOV DL,AL

CALL ZH

POP DX

MOV AX,DX

MOV BL,100

DIV BL

MOV DL,AL

PUSH AX

CALL ZH

POP AX

MOV AL,AH

MOV AH,0

CALL LBR

MOV DL,'-'

MOV AH,02H

INT 21H

POP DX

MOV BX,DX

MOV AX,0

MOV AL,DH

CALL LBR

MOV DL,'-'

MOV AH,02H

INT 21H

MOV AX,0

MOV AL,BL

CALL LBR

CALL HC

LEA DX,TIME

MOV AH,09H

INT 21H

MOV AH,2CH

INT 21H

MOV AX,0

MOV AL,CH

CALL LBR

MOV DL,':'

MOV AH,02H

INT 21H

MOV AX,0

MOV AL,CL

CALL LBR

MOV DL,':'

MOV AH,02H

INT 21H

MOV AX,0

MOV AL,DH

CALL LBR

CALL HC

LEA DX,PRESS

MOV AH,09H

INT 21H

CALL HC

CALL WAITF

MOV AH,0BH

INT 21H

INC AL

JE EXIT

JMP BEGIN

EXIT: MOV AH,4CH

INT 21H

HC PROC

MOV DL,0DH

MOV AH,02H

INT 21H

MOV DL,0AH

MOV AH,02H

INT 21h

RET

HC ENDP

LBR PROC

MOV DL,10

DIV DL

MOV BH,AH

MOV DL,AL

ADD DL,30H

MOV AH,02H

INT 21H

MOV DL,BH

ADD DL,30H

MOV AH,02H

INT 21H

RET

LBR ENDP

ZH PROC

ADD DL,30H

MOV AH,02H

INT 21H

RET

ZH ENDP

CLEAR PROC

MOV AL,0

MOV CX,0

MOV DH,48

MOV DL,79

MOV BH,7

MOV AH,6

INT 10H

RET

CLEAR ENDP

WAITF PROC

MOV CX,65535

PUSH AX

NEXT2: IN AL,61H

AND AH,10H

CMP AL,AH

LOOP NEXT2

POP AX

RET

WAITF ENDP

CODE ENDS

END START

我这个可以在屏幕上显示动态的时间,但不能设置时间,要设置时间的话要调用DOS的2BH号功能.相信你自己能根据这个程序改写成你要求的程序!


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

原文地址: http://outofmemory.cn/yw/10966287.html

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

发表评论

登录后才能评论

评论列表(0条)

保存