本文目錄一覽:
- 1、用C語言做數字時鐘每走一秒響一次,求大神告訴源代碼
- 2、C語言 電子時鐘源代碼程序 帶圖 謝謝 最好有注釋
- 3、求一個關於用C語言編寫的電子時鐘的程序,內容要有鬧鐘,可以調整時間
- 4、C語言電子時鐘設計
- 5、c語言時鐘代碼
用C語言做數字時鐘每走一秒響一次,求大神告訴源代碼
「響一次」需要牽涉到圖形編程中的音樂播放問題,需要自己下載圖形編程相關庫文件,具體實現請自己在TODO里添加播放音樂的代碼
數字時鐘的實現很簡單,運用time.h相關函數即可
#includestdio.h
#includestdlib.h
#includetime.h
time_t oldt=-1;
struct tm *p;
bool Printdate()
{
time_t t=time(NULL);
if(t!=oldt)
{
oldt=t;
p=localtime(t);
system(“cls”);
printf(“%d/%d/%d 周”,1900+p-tm_year,1+p-tm_mon,p-tm_mday,p-tm_hour,p-tm_min,p-tm_sec);
switch(p-tm_wday)
{
case 1:printf(“一”);break;
case 2:printf(“二”);break;
case 3:printf(“三”);break;
case 4:printf(“四”);break;
case 5:printf(“五”);break;
case 6:printf(“六”);break;
case 7:printf(“日”);break;
}
printf(” %d:%02d:%02d “, p-tm_hour, p-tm_min, p-tm_sec);
return 1;
}
return 0;
}
main()
{
while(1)
if(Printdate())
{
/*************TODO*************/
/**For example: printf(“\a”);**/
}
return 0;
}
C語言 電子時鐘源代碼程序 帶圖 謝謝 最好有注釋
#include stdio.h
#include time.h
#include”stdlib.h”
#include “windows.h”
void main ()
{
while(1)
{
time_t rawtime;
struct tm * timeinfo;
time ( rawtime );
timeinfo = localtime ( rawtime );
printf ( “\007The current date/time is:\n %s”, asctime (timeinfo) );
Sleep(1000);
system(“cls”);
}
}
求一個關於用C語言編寫的電子時鐘的程序,內容要有鬧鐘,可以調整時間
#includereg52.h
#includeabsacc.h
#includeintrins.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;i8;i++)
{
if((senddata0x01)==0)
MN_RXD=0;
else
MN_RXD=1;
_nop_();
MN_TXD=0;
_nop_();
MN_TXD=1;
senddata=senddata1;
}
}
//**********************************
//顯示程序函數
//**********************************
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;n5000;n++);
for(n=0;n6;n++)
{
SendData(TABLE[10]);
}
*/
}
//**********************************
//按鍵控制函數
//**********************************
void keycan()
{
unsigned int n;
EA=0;
if(key0==0) // 分鐘調整
{
for(n=0;n10000;n++); //延時去抖動
while(key0==0);
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60) //到一時
{
CLOCK[2]=0;
}
display();
}
if(key1==0) // 小時調整
{
for(n=0;n10000;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();
}
}
C語言電子時鐘設計
#includegraphics.h #includemath.h #includedos.h #define PI 3.1415926 //屏幕中心的坐標(640X480模式下) #define mid_x 320 #define mid_y 240 int main() { int graphdriver=DETECT,graphmode; int end_x,end_y; struct time curtime; float th_hour,th_min,th_sec; initgraph(graphdriver,graphmode,”C:\\TC2″); //初始化VGA屏幕模式 setbkcolor(BLACK); //使用黑色的背景色 while(!kbhit(0)) //若有鍵盤輸入,則跳出,即是結束程序 { setcolor(GREEN); //把畫筆設為綠色 circle(mid_x,mid_y,180); //鐘的外圓 circle(mid_x,mid_y,150); //鐘的內圓 circle(mid_x,mid_y,1); //畫出鐘的圓心 gettime(curtime); //取得系統當前時間 th_sec=(float)curtime.ti_sec*0.1047197551; //把秒針的角度化為弧度,為以後繪製時方便,下同 th_min=(float)curtime.ti_min*0.1047197551+th_sec/60.0; //分針的弧度 th_hour=(float)curtime.ti_hour*0.5235987755+th_min/12.0; //時度的弧度,注意整時是12等分的,所時乘的是3.14/180*5 //計算出時針的尾的坐標(時針長70) end_x=mid_x+70*sin(th_hour); end_y=mid_y-70*cos(th_hour); setcolor(RED); line(mid_x,mid_y,end_x,end_y); //用紅色線畫出時針 //計算出分針坐標(分針長110) end_x=mid_x+110*sin(th_min); end_y=mid_y-110*cos(th_min); setcolor(RED); line(mid_x,mid_y,end_x,end_y); //用紅色畫出分針 end_x=mid_x+140*sin(th_sec); end_y=mid_y-140*cos(th_sec); setcolor(RED); line(mid_x,mid_y,end_x,end_y); //同上,畫出秒針,長為140 //畫出鐘盤上的刻度,刻度長20 line(140,240,160,240); //9點對應的大刻度 line(320,60,320,80); //12點對應的大刻度 line(500,240,480,240); //3點的刻度 line(320,420,320,400); //6點的刻度 line(410,395.7,400,378.4); //5點 line(475.7,330,458.4,320); //4點 line(475.7,150,458.4,160); //2點 line(410,84.3,400,101.6); //1點 line(230,84.3,240,101.6); //11點 line(164.3,150,181.6,160); //10點 line(164.3,330,181.6,320); //8點 line(230,395.7,240,378.4); //7點 sleep(BLUE); //這裡應該是打錯,停止一秒,應為sleep(1000) cleardevice(); //清除屏幕上的顯示 } closegraph(); //關閉VGA屏幕,即返迴文本方式 return 0; }
c語言時鐘代碼
#includegraphics.h /* 引入graphic.h */
#includemath.h /* 引入math.h */
#includedos.h /* 引入dos.h */
#define pi 3.1415926 /*定義pi=3.14159*/
#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300;
#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240;
#define d(a,b,c) X(a,b,c);Y(a,b,c);line(300,240,x,y) /*定義……*/
void init() /*初始化程序*/
{int i,l,x1,x2,y1,y2; /*定義……*/
setbkcolor(1); /*設置顏色*/
circle(300,240,200); /*作園*/
circle(300,240,205);
circle(300,240,5);
for(i=0;i60;i++) /*循環(算時間)*/
{if(i%5==0) l=15;
else l=5;
x1=200*cos(i*6*pi/180)+300;
y1=200*sin(i*6*pi/180)+240;
x2=(200-l)*cos(i*6*pi/180)+300;
y2=(200-l)*sin(i*6*pi/180)+240;
line(x1,y1,x2,y2);
}
}
main()
{
int x,y;
int gd=VGA,gm=2;
unsigned char h,m,s; /*定義*/
struct time t[1];
initgraph(gd,gm,”d:\\tc”);
init();
setwritemode(1);
gettime(t);
h=t[0].ti_hour;
m=t[0].ti_min;
s=t[0].ti_sec; /*定義時分秒*/
setcolor(7); /*設置顏色*/
d(150,h,30);
setcolor(14);
d(170,m,6);
setcolor(4);
d(190,s,6);
while(!kbhit()) /*獲取鍵盤相應*/
{while(t[0].ti_sec==s)
gettime(t); /*C語言中得到時間的函數*/
sound(400); /*計算時間……*/
delay(70);
sound(200);
delay(30);
nosound();
setcolor(4);
d(190,s,6);
s=t[0].ti_sec;
d(190,s,6);
if (t[0].ti_min!=m)
{
setcolor(14);
d(170,m,6);
m=t[0].ti_min;
d(170,m,6);
}
if (t[0].ti_hour!=h)
{ setcolor(7);
d(150,h,30);
h=t[0].ti_hour;
d(150,h,30);
sound(1000);
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
}
getch(); /*設置空格後退出*/
closegraph();
}
具體的。。就是套用用幾個函數算時間。。
不要對這種很長的東西害怕,其實大部分都是在畫這個鐘~
加油哦~
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/304772.html