A. 求用單片機c語言做一個電子時鍾,實現調時、顯示、整點報時等功能。
(1)用數字邏輯集成塊實現;
(2)時間以24小時為一個周期,顯示時、分、秒;
(3)計時過程具有報時功能,當時間到達整點前5秒進行蜂鳴報時;
(4)為了保證計時的穩定及准確須由晶體振盪器提供表針時間基準信號。
c51單片機 晶振為11.0592MHz
#include<reg52.h>
#define HOUR1 1
#define HOUR0 0
#define MIN1 2
#define MIN0 8
#define SEC1 2
#define SEC0 0
#define uint unsigned int
#define ulint unsigned long int
#define uchar unsigned char
sbit la=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;
int i;
ulint
sharp,second,count=0,sec0=SEC0,sec1=SEC1,min0=MIN0,min1=MIN1,hour0=HOUR0,hour1=HOUR1;//秒計數全局變數
uchar code segment[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar code time[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delay(uint);//程序毫秒延時
void beeper(uchar);//開蜂鳴器毫秒
void init();//初始化函數
void display();//從數碼管上顯示
void counter();//計算進行過程中的時、分、秒值
void scan();//掃描鍵盤
void main()
{
init();
while(1)
{
scan();//掃描鍵盤看是否有鍵按下
for(i=6;i>0;i--)//動態掃描6位數碼管
{
display();//顯示時、分、秒
}
}
}
void init()
{
second=hour1*36000+hour0*3600+min1*600+min0*60+sec1*10+sec0;
TMOD=0x01;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
EA=1;
ET0=1;
TR0=1;
}
void delay(uint z)//程序毫秒延時
{
uint x=0,y=0;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer0() interrupt 1
{
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
count++;
if(count==20)//判斷是否到1秒
{
counter();//計算進行過程中的時、分、秒值
if(sharp!=hour0) beeper(1000);//判斷小時的值是否改變,變則啟動蜂鳴器
}
}
void beeper(uchar tt)
{
uchar t=tt;
count=0;
beep=0;//開蜂鳴器
delay(t);
beep=1;//關蜂鳴器
}
void display()
{
P0=0xff;//位消影(低電平選擇位)
//送位選信號
wela=1;
P0=segment[i-1];
wela=0;
P0=0x00;//段消影(高電平選擇段)
//送段選信號
la=1;
switch(i)
{
case 6 : P0=time[sec0]; break;
case 5 : P0=time[sec1]; break;
case 4 : P0=time[min0]; break;
case 3 : P0=time[min1]; break;
case 2 : P0=time[hour0]; break;
case 1 : P0=time[hour1]; break;
}
delay(1);
P0=0x00; //配合上面用於消隱
la=0;
}
void counter()
{
second++;
if(second==86400) second=0;
count=0;
sharp=hour0;//設置報時檢測KEY
sec0=second%10;
sec1=(second%60-sec0)/10;
min0=((second%3600-sec1*10-sec0)/60)%10;
min1=((second%3600-sec1*10-sec0)/60-min0)/10;
hour0=(second%36000-min1*600-min0*60-sec1*10-sec0)/3600;
hour1=second/36000;
}
void scan()
{
}
B. 用C語言編一個數字電子時鍾的程序
1.這是用windows api寫的程序。所以要求是純c的話就沒有辦法了
2.其中定時用了兩種方法。一種是用取消息。另一種是延時隊列。這里只使用了取消息的方法。延時隊列由於我機器上是vc6.0,CreateTimerQueue在本人機器上無法使用,需要新的sdk,所以沒有加以驗證,但取消息的方式是可行的。
3.稍稍驗證了下,基本滿足要求。
-------------------------------------------
程序如下:
// DigitalClock.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <winbase.h>
typedef struct _st_time{
int hour;
int min;
int sec;
}ST_TIME;
ST_TIME g_Time; // The struct contain the hour,min and sec.
HANDLE g_hStdout; //
WORD g_cxCenter, g_cyCenter; // Center of the screen.
HANDLE g_DoneEvent; // The program could be over.
BOOL g_ThreadTerminated; // The Thread should be terminated.
#define SECOND_CIRCLE 60
#define MINUTE_CIRCLE 60
#define HOUR_CIRCLE 24
void TimeIncreaseSecond(ST_TIME & st)
{
st.sec ++;
if (st.sec >= SECOND_CIRCLE)
{
st.sec -= SECOND_CIRCLE;
st.min++;
if (st.min >= MINUTE_CIRCLE)
{
st.min -= MINUTE_CIRCLE;
st.hour++;
if (st.hour >= HOUR_CIRCLE)
{
st.hour -= HOUR_CIRCLE;
}
}
}
}
void PrintTimeToScreen(HANDLE hStdout, short cxCenter, short cyCenter, ST_TIME st)
{
char buf[64] = {0};
COORD crdPos;
// make it format to output.
sprintf (buf, "%02d:%02d:%02d", st.hour, st.min, st.sec);
crdPos.X = cxCenter - 4;
crdPos.Y = cyCenter;
SetConsoleCursorPosition(hStdout, crdPos);
printf(buf);
}
#ifdef USE_TIMERQUEUE
// if we use the timer queue function.
// Its procre is in this.
void CALLBACK TimerRoutine (LPVOID lpParam, BOOL TimerOrWaitFired)
{
if (lpParam == NULL)
{
printf ("NULL parameters.\n");
}
else
{
ST_TIME *st = (ST_TIME *)lpParam;
TimeIncreaseSecond(st);
PrintTimeToScreen(g_hStdout, g_cxCenter, g_cyCenter, *st);
}
}
#else
DWORD WINAPI TimerThreadProc(LPVOID lpParam)
{
#define ID_TIMER_SECOND 1
MSG msg;
BOOL ret;
ST_TIME *st = (ST_TIME *)lpParam;
SetTimer(NULL, ID_TIMER_SECOND, 1000, NULL);
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
while (!g_ThreadTerminated && (ret = GetMessage (&msg, NULL, 0, 0)) != 0)
{
if (ret == -1)
{
//process fatal event.
}
else if (msg.message == WM_TIMER)
{
TimeIncreaseSecond(*st);
PrintTimeToScreen(g_hStdout, g_cxCenter, g_cyCenter, *st);
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
return 1;
}
#endif
// If the ctrl+break combined key pressed. call this function.
// It set the g_DoneEvent. this terminate the program.
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
case CTRL_BREAK_EVENT:
// Terminate the program.
printf ("Terminate.\n");
SetEvent(g_DoneEvent);
return TRUE;
default:
return FALSE;
}
}
BOOL InitApplication()
{
// Get the stdin and stdout handle.
HANDLE hStdIn;
hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if (hStdIn == INVALID_HANDLE_VALUE)
return FALSE;
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
// Set the mode, make the input echo.
DWORD fOldMode;
GetConsoleMode(hStdIn, &fOldMode);
fOldMode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
SetConsoleMode(hStdIn, fOldMode);
// Set the window buffer.
// make a line 40 columns.
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(g_hStdout, &csbiInfo);
csbiInfo.srWindow.Right = 40;
// get the center point.
g_cxCenter = csbiInfo.srWindow.Right / 2;
g_cyCenter = csbiInfo.srWindow.Bottom / 2;
// Set the window.
SetConsoleWindowInfo(g_hStdout, TRUE, &csbiInfo.srWindow);
return TRUE;
}
BOOL (HANDLE hStdout, WORD cxCenter, WORD cyCenter, ST_TIME & time)
{
#define GAPS_LEFT_COLON (-2)
#define GAPS_RIGHT_COLON (1)
#define GAPS_LEFT_UNDERLINE_START (-4)
#define GAPS_MIDDLE_UNDERLINE_START (-1)
#define GAPS_RIGHT_UNDERLINE_START (2)
// __:__:__
// So the left ":" center -2
// so the right ":" center + 1
// so the left "_" center - 4;
// so the lfet "_" center - 1;
// so the right "_" center + 2;
COORD crdPos;
crdPos.X = cxCenter + GAPS_LEFT_COLON;
crdPos.Y = cyCenter;
SetConsoleCursorPosition(hStdout, crdPos);
printf (":");
crdPos.X = cxCenter + GAPS_RIGHT_COLON;
SetConsoleCursorPosition(hStdout, crdPos);
printf (":");
crdPos.X = cxCenter + GAPS_LEFT_UNDERLINE_START;
SetConsoleCursorPosition(hStdout, crdPos);
scanf ("%d", &time.hour);
crdPos.X = cxCenter + GAPS_MIDDLE_UNDERLINE_START;
SetConsoleCursorPosition(hStdout, crdPos);
scanf ("%d", &time.min);
crdPos.X = cxCenter + GAPS_RIGHT_UNDERLINE_START;
SetConsoleCursorPosition(hStdout, crdPos);
scanf ("%d", &time.sec);
if (time.hour < 0 || time.hour > HOUR_CIRCLE ||
time.min < 0 || time.min > MINUTE_CIRCLE ||
time.sec < 0 || time.sec > SECOND_CIRCLE)
return FALSE;
return TRUE;
}
int main(int argc, char* argv[])
{
InitApplication();
(g_hStdout, g_cxCenter, g_cyCenter, g_Time);
// create a event to tell the program to terminate.
g_DoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
#ifdef USE_TIMERQUEUE
HANDLE hTimerQueue, hTimer;
hTimerQueue = CreateTimerQueue();
if (!CreateTimerQueueTimer(&hTimer,
hTimerQueue, TimerRoutine, &g_Time, 1000, 0, 0))
{
printf("CreateTimerQueueTimer failed (%d)\\n", GetLastError());
return 3;
}
#else
// create the thread.
HANDLE hThreadTimer;
DWORD dwThreadId;
g_ThreadTerminated = FALSE;
hThreadTimer = CreateThread(NULL, 0,
TimerThreadProc, &g_Time, 0, &dwThreadId);
if (hThreadTimer == NULL)
{
}
#endif
SetConsoleCtrlHandler(CtrlHandler, TRUE);
if (WaitForSingleObject(g_DoneEvent, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\\n", GetLastError());
#ifdef USE_TIMERQUEUE
if (!DeleteTimerQueue(hTimerQueue))
printf("DeleteTimerQueue failed(%d) \\n", GetLastError());
#else
g_ThreadTerminated = TRUE;
if (WaitForSingleObject(hThreadTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\\n", GetLastError());
#endif
return 0;
}
--------------------------------------------
下面是純c的。
有幾個問題:
1.textmode函數在turboc中沒有辦法使用,不知道是什麼問題,而borland c就可以。
2.無論怎麼設置,自己的ctrlbreak函數在上述兩個環境中都不能被調用,非常遺憾。所以不能夠優雅的退出。只能按兩次ctrlbreak。
下面是程序。
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define ABORT 0
int jump_out_loop = -1;
int jump_out(void)
{
jump_out_loop = 1;
printf("Abort ..\n");
return ABORT;
}
int main(void)
{
struct text_info ti;
int center_x, center_y;
int hour, min, sec;
char str_out[64] = {0};
clrscr();
/*textmode(BW40);*/
/*textmode在turbo c下設置會出問題*/
gettextinfo(&ti);
center_x = ti.winright / 2;
center_y = ti.winbottom / 2;
gotoxy(center_x - 4, center_y);
cprintf(" : : ");
gotoxy(center_x - 4, center_y);
cscanf("%d", &hour);
gotoxy(center_x - 1, center_y);
cscanf("%d", &min);
gotoxy(center_x + 2, center_y);
cscanf("%d", &sec);
/* check input valid or not */
{}
setcbrk(1);
ctrlbrk(jump_out);
/*jump_out沒有起到作用,實在不好意思.*/
/*
if (getcbrk())
printf("crtl break is on\n");
else
printf("is off\n");
*/
while (1)
{
delay(1000);
sec++;
if (sec >= 60)
{
sec -= 60;
min++;
if (min >= 60)
{
min -= 60;
hour++;
if (hour >= 24)
{
hour -= 24;
}
}
}
sprintf(str_out, "%02d:%02d:%02d", hour, min, sec);
gotoxy(center_x - 4, center_y);
cprintf(str_out);
}
/* getch();*/
return 0;
}
C. 圖形時鍾用C語言怎麼編
#include<graphics.h>
#include<conio.h>
#include<math.h>
voidDraw(inthour,intminute,intsecond)
{
doublea_hour,a_min,a_sec;//時、分、秒針的弧度值
intx_hour,y_hour,x_min,y_min,x_sec,y_sec;//時、分、秒針的末端位置
intx_hour1,y_hour1,x_min1,y_min1,x_sec1,y_sec1;
//計算時、分、秒針的弧度值
a_sec=second*2*PI/60;
a_min=minute*2*PI/60;
a_hour=hour*2*PI/12+a_min/12;;
//計算時、分、秒針的首末端位置
x_sec=320+(int)(120*sin(a_sec));
y_sec=240-(int)(120*cos(a_sec));
x_min=320+(int)(100*sin(a_min));
y_min=240-(int)(100*cos(a_min));
x_hour=320+(int)(70*sin(a_hour));
y_hour=240-(int)(70*cos(a_hour));
x_sec1=320-(int)(15*sin(a_sec));
y_sec1=240+(int)(15*cos(a_sec));
x_min1=320-(int)(10*sin(a_min));
y_min1=240+(int)(10*cos(a_min));
x_hour1=320-(int)(5*sin(a_hour));
y_hour1=240+(int)(5*cos(a_hour));//畫時針
setlinestyle(PS_SOLID,NULL,7);
setcolor(WHITE);
line(x_hour1,y_hour1,x_hour,y_hour);
//畫分針
setlinestyle(PS_SOLID,NULL,4);
setcolor(LIGHTGRAY);
line(x_min1,y_min1,x_min,y_min);
//畫秒針
setlinestyle(PS_SOLID,NULL,2);
setcolor(RED);
line(x_sec1,y_sec1,x_sec,y_sec);
}
voidmain()
{
initgraph(640,480);//初始化640x480的繪圖窗口
//繪制一個簡單的表盤
circle(320,240,2);
circle(320,240,60);
circle(320,240,160);
outtextxy(296,330,"竹斌");
intx,y;
for(inti=0;i<12;i++)
{
x=320+(int)(140*sin(30*i*2*PI/360));
y=240-(int)(140*cos(30*i*2*PI/360));
switch(i)
{
case0:outtextxy(x-5,y-5,"12");break;
case1:outtextxy(x-5,y-5,"1");break;
case2:outtextxy(x-5,y-5,"2");break;
case3:outtextxy(x-5,y-5,"3");break;
case4:outtextxy(x-5,y-5,"4");break;
case5:outtextxy(x-5,y-5,"5");break;
case6:outtextxy(x-5,y-5,"6");break;
case7:outtextxy(x-5,y-5,"7");break;
case8:outtextxy(x-5,y-5,"8");break;
case9:outtextxy(x-5,y-5,"9");break;
case10:outtextxy(x-5,y-5,"10");break;
case11:outtextxy(x-5,y-5,"11");break;
}
}
//設置XOR繪圖模式
setwritemode(R2_XORPEN);//設置XOR繪圖模式
//畫刻度
inta,b,a1,b1,n=0;
for(n=0;n<60;n++)
{
a=320+(int)(160*sin(n*2*PI/60));
b=240-(int)(160*cos(n*2*PI/60));
a1=320+(int)(150*sin(n*2*PI/60));
b1=240-(int)(150*cos(n*2*PI/60));
if(n%5==0)
setlinestyle(PS_SOLID,NULL,5);
else
setlinestyle(PS_SOLID,NULL,1);
line(a1,b1,a,b);
}
//繪製表針
SYSTEMTIMEti;//定義變數保存當前時間
while(!kbhit())//按任意鍵退出鍾表程序
{
GetLocalTime(&ti);//獲取當前時間
Draw(ti.wHour,ti.wMinute,ti.wSecond);//畫表針
Sleep(1000);//延時1秒
Draw(ti.wHour,ti.wMinute,ti.wSecond);//擦表針(擦表針和畫表針的過程是一樣的)
}
closegraph();//關閉繪圖窗口
}
D. 求一個關於用C語言編寫的電子時鍾的程序,內容要有鬧鍾,可以調整時間
#include<reg52.h>
#include<absacc.h>
#include<intrins.h>
#define unit unsigned int
#define uchar unsigned char
//#define HZ 12
sbit key0=P0^0; // 分鍾調整
sbit key1=P0^1; // 小時調整
sbit P2_0=P2^7; //秒 指示燈
sbit MN_RXD=P3^6;
sbit MN_TXD=P3^7;
uchar data CLOCK[4]={0,0,0,12};//存放時鍾時間(百分秒,秒,分,和時位)
//數碼管顯示表0-f 滅
uchar code TABLE[]={0xBE,0x06,0xEA,0x6E,0x56,0x7C,0xFC,0x0E,0xFE,0x7E,0x00};
//**********************************
//模擬串口發送一個位元組數據 函數
//**********************************
void SendData(unsigned char senddata)
{
unsigned char i;
for(i=0;i<8;i++)
{
if((senddata&0x01)==0)
MN_RXD=0;
else
MN_RXD=1;
_nop_();
MN_TXD=0;
_nop_();
MN_TXD=1;
senddata=senddata>>1;
}
}
//**********************************
//顯示程序函數
//**********************************
void display(void)
{
// unsigned int n;
uchar temp;
temp=CLOCK[1]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[1]; temp=temp/10; SendData(TABLE[temp]);
temp=CLOCK[2]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[2]; temp=temp/10; SendData(TABLE[temp]);
temp=CLOCK[3]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[3]; temp=temp/10; SendData(TABLE[temp]);
/*
for(n=0;n<5000;n++);
for(n=0;n<6;n++)
{
SendData(TABLE[10]);
}
*/
}
//**********************************
//按鍵控制函數
//**********************************
void keycan()
{
unsigned int n;
EA=0;
if(key0==0) // 分鍾調整
{
for(n=0;n<10000;n++); //延時去抖動
while(key0==0);
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60) //到一時
{
CLOCK[2]=0;
}
display();
}
if(key1==0) // 小時調整
{
for(n=0;n<10000;n++); //延時去抖動
while(key1==0);
CLOCK[3]=CLOCK[3]+1;
if(CLOCK[3]==24)
{
CLOCK[3]=0;
}
display();
}
EA=1;
}
//**********************************
//T0中斷服務函數
//**********************************
void time0() interrupt 1 //using 1
{
TH0=0xD8; TL0=0xF0; //重置初值
// TH0=0xB1; TL0=0xE0;
//時鍾處理
CLOCK[0]=CLOCK[0]+1;
}
//**********************************
//主函數
//**********************************
void main()
{
EA=1;
ET0=1;
TMOD=0x01; //T0方式1定時
TH0=0xD8; TL0=0xF0; //D8F0 定時10ms
// TH0=0xB1; TL0=0xE0; //定時 20ms
TR0=1;
for(;;)
{
if(CLOCK[0]==100) //到一秒 10ms*100
{
CLOCK[0]=0;
P2_0=~P2_0;
CLOCK[1]=CLOCK[1]+1;
if(CLOCK[1]==60) //到一分
{
CLOCK[1]=0;
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60) //到一時
{
CLOCK[2]=0;
CLOCK[3]=CLOCK[3]+1;
if(CLOCK[3]==24)
{
CLOCK[3]=0;
}
}
}
display();
}
keycan();
}
}
E. 用C語言編寫AT89C51單片機程序,設計一個智能數字鍾。
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit QB1=P1^0;
sbit QB2=P1^1; //數碼管段選
sbit QB3=P1^2;
sbit QB4=P1^3;
sbit QB5=P1^4;
sbit QB6=P1^5;
sbit fm=P1^6; //蜂鳴器
sbit s1=P2^4; //s5按鍵,切換顯示
sbit s2=P2^3; //s2按鍵,設置調時
sbit s3=P2^2; //s3按鍵,加1
sbit s4=P2^1; //s4按鍵,減1
sbit led1=P0^0;
sbit led2=P0^1;
sbit led3=P0^2;
uchar count;
uchar sec,minu,hour,day,week,mon;
uchar n_sec,n_minu,n_hour;
uint year;
uchar set_2=1,set_1=1;
uchar hs,hg,mis,mig,ss,sg;
uchar nhs,nhg,nms,nmg,nss=0,nsg=0;
uchar ms,mg,ds,dg,w;
uchar code table[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,
0X90,0X88,0X83,0XC6,0XA1,0X8E,0X86,0xbf}; //0~F,-,共陽
//uchar code tableyi[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
//0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};//滾襪0-F,-,共陰
uchar code table_d[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef}; //0~9數組,帶小數點
uchar table1[]={31,31,29,31,30,31,30,31,31,30,31,30,31}; //閏年
uchar table2[]={31,31,28,31,30,31,30,31,31,30,31,30,31}; //非閏年
void delay(uint); //延時函數
void timer0(); //走時中斷函數
void jishi(); //計時函數
void key_change(); //切換顯示按鍵函數
void key_set(); //設置時間按鍵函數
void disp(uchar,uchar,uchar,uchar,uchar,uchar); //顯示函數
void zd_clock(); //整點報時函數
void nz_clock(); //鬧鍾函大衫激數
uchar incone(uchar); //加1函數
uchar decone(uchar); //減1函數
void set_time(); //設置時間函數
void set_clock(); //設置鬧鍾函數
void set_mdw(); //設置月日星期函數
void main() //主函數
{
EA=1;
ET0=1;
TR0=1;
TMOD=0x01;
TH0=0x4c; //50ms初值 晶振11.0592
TL0=0x00;
hour=23;minu=59;sec=49; //賦初值:11點59分0秒塌沖
n_hour=12;n_minu=56;n_sec=0; //鬧鍾賦初值12點1分0秒
year=2008;mon=5;day=14;week=3;//年月日星期賦初值2008年5月11日星期天;祝天下所有母親節日快樂
while(1)
{
hs=hour/10; //時分秒HH.MM.SS
hg=hour%10;
mis=minu/10;
mig=minu%10;
ss=sec/10;
sg=sec%10;
ms=mon/10; //月日-星期MM.DD.-W
mg=mon%10;
ds=day/10;
dg=day%10;
w=week;
nhs=n_hour/10; //鬧鍾定時HH.MM.SS
nhg=n_hour%10;
nms=n_minu/10;
nmg=n_minu%10;
nss=n_sec/10;
nsg=n_sec%10;
key_change(); //s4按鍵掃描
key_set(); //s2按鍵掃描
set_time(); //設置時間
set_mdw(); //設置月日星期
set_clock(); //設置鬧鍾
if(set_1==1) //正常走時顯示
{
disp(hs,hg,mis,mig,ss,sg);
}
if(set_1==2) //設置時間,LED1閃亮
{
disp(hs,hg,mis,mig,ss,sg);
if(sec%2==0)
{led2=1;led3=1;led1=~led1;}
// else
// {led1=1;}
}
if(set_1==3) //正常顯示月日-星期
{
disp(ms,mg,ds,dg,16,w);
}
if(set_1==4) //設置月日-星期,LED2閃亮
{
disp(ms,mg,ds,dg,16,w);
if(sec%2==0)
{led1=1;led3=1;led2=~led2;}
// else
// {led2=1;}
}
if(set_1==5) //正常顯示定時
{
disp(nhs,nhg,nms,nmg,nss,nsg);
}
if(set_1==6) //設置鬧鍾定時,LED3閃亮
{
disp(nhs,nhg,nms,nmg,nss,nsg);
if(sec%2==0)
{led1=1;led2=1;led3=~led3;}
// else
// {led3=1;}
}
zd_clock(); //整點報時
nz_clock(); //鬧鍾
}
}
void timer0() interrupt 1 //50ms中斷函數
{
TMOD=0x01;
TH0=0x4c; //50ms初值 晶振11.0592
TL0=0x00;
count++;
if(count==20)
{
count=0;
sec++;
jishi(); //調計時函數
}
}
void jishi() //計時函數
{
if(sec==60)
{
sec=0;
minu++;
if(minu==60)
{
minu=0;
hour++;
if(hour==24)
{ hour=0;
day++;
week++;
if(week==8)
{week=0;}
if(year%4==0&&year%100!=0||year%400==0) //閏年
{
if(day==table1[mon]+1)
{
day=0;
mon++;
if(mon==13)
{mon=0;year++;}
}
}
else //非閏年
{
if(day==table2[mon]+1)
{
day=0;
mon++;
if(mon==13)
{mon=0;year++;}
}
}
}
}
}
}
void key_change() //s1按鍵掃描
{
if(s1==0)
{
delay(200);
if(s1==0)
{
set_1++;
while(!s1);
if(set_1==7)
{set_1=1;}
}
}
}
void key_set() //s2按鍵掃描
{
if(s2==0)
{
delay(10);
if(s2==0)
{
set_2++;
while(!s2);
if(set_2==4)
{set_2=1;}
}
}
}
void disp(uchar a1,uchar a2,uchar a3,uchar a4,uchar a5,uchar a6) //顯示函數
{
QB1=1;
QB2=0;
QB3=0;
QB4=0;
QB5=0;
QB6=0;
P3=table[a1]; //段碼送P0口
delay(10); //延時一小會
QB1=0;
QB2=1;
QB3=0;
QB4=0;
QB5=0;
QB6=0;
P3=table[a2]; //第2個數碼管顯示,帶小數點
delay(10);
QB1=0;
QB2=0;
QB3=1;
QB4=0;
QB5=0;
QB6=0;
P3=table[a3]; //第3個數碼管顯示
delay(10);
QB1=0;
QB2=0;
QB3=0;
QB4=1;
QB5=0;
QB6=0;
P3=table[a4]; //第4個數碼管顯示,帶小數點
delay(10);
QB1=0;
QB2=0;
QB3=0;
QB4=0;
QB5=1;
QB6=0;
//第5個數碼管顯示
P3=table[a5];
delay(10);
QB1=0;
QB2=0;
QB3=0;
QB4=0;
QB5=0;
QB6=1;
P3=table[a6]; //第6個數碼管顯示
delay(10);
QB1=0;
QB2=0;
QB3=0;
QB4=0;
QB5=0;
QB6=0;
}
void zd_clock() //整點報時函數
{
if(minu==59&&(sec==53||sec==55||sec==57))
{
fm=0;
delay(5);
fm=1;
delay(5);
}
fm=0;
if(minu==59&&sec==59)
{
fm=0;
delay(5);
fm=1;
delay(5);
fm=0;
}
}
void nz_clock() //鬧鍾函數
{
if(hour==n_hour&&minu==n_minu&&sec==n_sec)
//if((sec%2==0)&&sec<30)
{
fm=0;
delay(1);
fm=1;
delay(1);
}
}
void set_time() //設置時間函數
{
if(set_1==2)
{
if(set_2==1)
{
hour=incone(hour);
if(hour==24)
{hour=0;}
// if(hour<0)
// {hour=23;}
hour=decone(hour);
}
if(set_2==2)
{
minu=incone(minu);
if(minu==60)
{minu=0;}
// if(minu<0)
// {minu=59;}
minu=decone(minu);
}
}
}
void set_mdw() //設置月日星期函數
{
if(set_1==4)
{
if(set_2==1)
{
mon=incone(mon);
if(mon==13)
{mon=1;}
mon=decone(mon);
// if(mon==0)
// {mon=12;}
}
if(set_2==2)
{
day=incone(day);
if(day==32)
{day=0;}
day=decone(day);
// if(day==0)
// {day=0;}
}
if(set_2==3)
{
week=incone(week);
if(week==8)
{week=0;}
week=decone(week);
// if(week==0)
// {week=7;}
}
}
}
void set_clock() //設置鬧鍾函數
{
if(set_1==6)
{
if(set_2==1)
{
n_hour=incone(n_hour);
if(n_hour==24)
{n_hour=0;}
n_hour=decone(n_hour);
if(n_hour==0)
{n_hour=0;}
}
if(set_2==2)
{
n_minu=incone(n_minu);
if(n_minu==60)
{n_minu=0;}
n_minu=decone(n_minu);
if(n_minu==0)
{n_minu=0;}
}
}
}
uchar incone(uchar n) //加1函數
{
if(s3==0)
{ delay(200);
if(s3==0)
{
n++;
while(!s3);
}
}
return(n);
}
uchar decone(uchar m) //減1函數
{
if(s4==0)
{
delay(200);
if(s4==0)
{
m--;
while(!s4);
if(m<0)
{m=0;}
}
}
return(m);
}
void delay(uint k) //延時函數
{
uint i,j;
for(i=k;i>0;i--)
for(j=80;j>0;j--);
}
F. c語言編寫數字時鍾
#include<stdio.h>
#include<windows.h>
int main()
{
for(int i=0;i<24;i++)
for(int j=0;j<60;j++)
for(int k=0;k<5;k++)
{
system("cls");
printf("%0.2d:%0.2d:%0.2d",i,j,k);
Sleep(1000);
}
}
G. 跪求C語言編寫的時鍾(能修改時鍾時間,且時鍾是指針式的 只編寫修改時間的部分也行)
////////////////////////////////////////////
//程序名稱:鍾表模擬程序(表針形式)
//編譯環境:VisualC++6.0,EasyX2011驚蟄版
//程序編寫:BestAns<[email protected]>
//最後更新:2010-10-30
//
#include
<graphics.h>
#include
<conio.h>
#include
<math.h>
#define
PI
3.1415926536
void
DrawHand
(
int
hour
,
int
minute
,
int
second
)
{
double
a_hour
,
a_min
,
a_sec
;
//時、分、秒針的弧度值
int
x_hour
,
y_hour
,
x_min
,
y_min
,
x_sec
,
y_sec
;
//時、分、秒針的末端位置
//計算時、分、秒針的弧度值
a_sec
=
second
*
2
*
PI
/
60
;
a_min
=
minute
*
2
*
PI
/
60
+
a_sec
/
60
;
a_hour
=
hour
*
2
*
PI
/
12
+
a_min
/
12
;
//計算時、分、秒針的末端位置
x_sec
=
int
(
120
*
sin
(
a_sec
));
y_sec
=
int
(
120
*
cos
(
a_sec
));
x_min
=
int
(
100
*
sin
(
a_min
));
y_min
=
int
(
100
*
cos
(
a_min
));
x_hour
=
int
(
70
*
sin
(
a_hour
));
y_hour
=
int
(
70
*
cos
(
a_hour
));
//畫時針
setlinestyle
(
PS_SOLID
,
NULL
,
10
);
setcolor
(
WHITE
);
line
(
320
+
x_hour
,
240
-
y_hour
,
320
-
x_hour
/
7
,
240
+
y_hour
/
7
);
//畫分針
setlinestyle
(
PS_SOLID
,
NULL
,
6
);
setcolor
(
LIGHTGRAY
);
line
(
320
+
x_min
,
240
-
y_min
,
320
-
x_min
/
5
,
240
+
y_min
/
5
);
//畫秒針
setlinestyle
(
PS_SOLID
,
NULL
,
2
);
setcolor
(
RED
);
line
(
320
+
x_sec
,
240
-
y_sec
,
320
-
x_sec
/
3
,
240
+
y_sec
/
3
);
}
void
DrawDial
()
{
//繪制一個簡單的表盤
circle
(
320
,
240
,
2
);
circle
(
320
,
240
,
60
);
circle
(
320
,
240
,
160
);
outtextxy
(
296
,
310
,
"BestAns"
);
//繪制刻度
int
x
,
y
;
for
(
int
i
=
0
;
i
<
60
;
i
++)
{
x
=
320
+
int
(
145
*
sin
(
PI
*
2
*
i
/
60
));
y
=
240
+
int
(
145
*
cos
(
PI
*
2
*
i
/
60
));
if
(
i
%
15
==
0
)
bar
(
x
-
5
,
y
-
5
,
x
+
5
,
y
+
5
);
else
if
(
i
%
5
==
0
)
circle
(
x
,
y
,
3
);
else
putpixel
(
x
,
y
,
WHITE
);
}
}
void
main
()
{
initgraph
(
640
,
480
);
//初始化640x480的繪圖窗口
DrawDial
();
//繪製表盤
setwritemode
(
R2_XORPEN
);
//設置XOR繪圖模式
//繪製表針
SYSTEMTIME
ti
;
//定義變數保存當前時間
while
(!
kbhit
())
//按任意鍵退出鍾表程序
{
GetLocalTime
(&
ti
);
//獲取當前時間
DrawHand
(
ti
.
wHour
,
ti
.
wMinute
,
ti
.
wSecond
);
//畫表針
Sleep
(
1000
);
//延時1秒
DrawHand
(
ti
.
wHour
,
ti
.
wMinute
,
ti
.
wSecond
);
//擦表針(擦表針和畫表針的過程是一樣的)
}
closegraph
();
//關閉繪圖窗口
}
其了就是用了easyx