C program code to display system time as clock
The following is a source code to display the system time. If you are interested in this program, then it is easy to produce its executable file. You simply copy this code and save it on your computer with extension '.c'.For example the file name as 'clock.c'. After this you open this file in 'turboc' and run it. surely you will get the result.
The code:
/*********** vipin programs **********************************
* Program to display a virtual clock and a digital display *
* by vipin.p.nair *
* s1s2 ec cek *
* 31-3-07 *
*************************************************************/
// including header files
#include"graphics.h"
#include "dos.h"
#include"math.h"
// declaration of functions
void graphics_initialize();
void clockpic();
void needletime();
void clock_refresh();
void about();
void digital_display();
int timesec(char);
// define half of maximam x and y
#define maxx getmaxx()/2
#define maxy getmaxy()/2
// main function
void main()
{
graphics_initialize();
setbkcolor(WHITE);
clockpic();
about();
digital_display();
needletime();
closegraph();
}
//*******************************************************
// definition of functions
// initializing graphics
void graphics_initialize()
{
// auto detection mode
int driver=DETECT,mode=0,errorcode;
// initializing graphics driver
initgraph(&driver,&mode,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
printf("graphics driver not initialized");
}
// drawing picture of clock
void clockpic()
{
setcolor(BROWN);
setfillstyle(7,YELLOW);
fillellipse(maxx,maxy,200,200);
circle(maxx,maxy,200);
circle(maxx,maxy,201);
setcolor(GREEN);
setcolor(GREEN);
settextstyle(1,0,6);
outtextxy(maxx-20,38,"12");
outtextxy(maxx-11,385,"6");
outtextxy(maxx+160,maxy-28,"3");
outtextxy(135,maxy-28,"9");
outtextxy(460,120,"2");
outtextxy(145,120,"10");
outtextxy(155,300,"8");
outtextxy(400,60,"1");
outtextxy(205,60,"11");
outtextxy(220,360,"7");
outtextxy(395,362,"5");
outtextxy(460,300,"4");
setcolor(LIGHTGRAY);
settextstyle(4,0,4);
outtextxy(290,120,"Clock");
outtextxy(290,120,"Clock");
}
// function for defining the motion of needle
void needletime()
{
{
double x_sec,y_sec,
x_hr,y_hr,
x_min,y_min, prv_sec,
sec=timesec('s'),
min=timesec('m')+sec/60,
//sec*6 degree/360
hr =timesec('h')+min/12;
//min*30 degree/360
while(!kbhit())
{
digital_display();
setcolor(BROWN);
x_sec=maxx+145*cos(1.57-sec*0.017444);
setcolor(BROWN);
x_sec=maxx+145*cos(1.57-sec*0.017444);
//0.01744 = 3.14/180 (degree to radian)
y_sec=maxy-145*sin(1.57-sec*.017444); // 1.57 Radian = 90 degree
x_min=maxx+110*cos(1.57-min*0.017444);
x_min=maxx+110*cos(1.57-min*0.017444);
y_min=maxy-110*sin(1.57-min*.017444);
x_hr=maxx+70*cos(1.57-hr*0.017444);
x_hr=maxx+70*cos(1.57-hr*0.017444);
y_hr=maxy-70*sin(1.57-hr*.017444);
line(319,239,x_hr,y_hr);
line(319,239,x_hr,y_hr);
line(319,239,x_min,y_min);
line(319,239,x_sec,y_sec);
circle(maxx,maxy,1);
circle(maxx,maxy,1);
circle(maxx,maxy,2);
circle(maxx,maxy,3);
circle(maxx,maxy,4);
prv_sec=sec;
sec=timesec('s');
if(prv_sec!=sec)
if(prv_sec!=sec)
clock_refresh();
min=timesec('m')+sec/60;
min=timesec('m')+sec/60;
hr=timesec('h')+min/12;
}
}
}
// refreshing a part of clock after each second
// refreshing a part of clock after each second
void clock_refresh()
{
setcolor(WHITE);
setfillstyle(7,YELLOW);
fillellipse(maxx,maxy,150,150);
settextstyle(1,0,3);
setcolor(BLUE);
outtextxy(maxx-25,100,"Clock");
}
// proving the degree of each needle
int timesec(char tm)
{
int hr;
struct time t;
gettime(&t);
if(tm=='s')
return (t.ti_sec*6);
if(tm=='m') return (t.ti_min*6);
if(tm=='h')
{
hr=t.ti_hour;
if(hr>12)
return (30*(hr-12));
return (30*hr);
} return 0; }
// about programmer
void about()
{
char cnme[]={ 118,105,112,105,110,32 ,112,
114,111,103,114,97 ,109,115,0 },
nme[]= { 66 ,121,32 ,118,105,112,105,110,
46 ,112,46 ,110,97 ,105,114,0 },
sdy[]= { 115,49 ,115,50 ,32 ,67 ,69 ,75 ,44 ,
50 ,48 ,48 ,55 ,0 },
mil[]= { 118,105,112,105,110,112,110,64,103,
109,97 ,105,108,46 ,99 ,111,109,0 };
setcolor(DARKGRAY);
settextstyle(0,1,1);
outtextxy(10,300,cnme);
outtextxy(20,300,nme);
outtextxy(30,300,sdy);
outtextxy(40,300,mil);
}
// providing digital display of clock
void digital_display()
{
int sec=timesec('s')/6,
min=timesec('m')/6,
hr =timesec('h')/30;
char s[3],m[3],h[3];
s[1]=sec%10+48;
// adding 48 to make it the ASCII value, since char
s[0]=sec/10+48;
m[2]=h[2]=s[2]=0;
m[1]=min%10+48;
m[0]=min/10+48;
h[1]=hr%10+48;
h[0]=hr/10+48;
setcolor(RED);
setcolor(RED);
settextstyle(0,0,1);
outtextxy(maxx-40,maxy+50,h);
outtextxy(maxx-25,maxy+50,":");
outtextxy(maxx-15,maxy+50,m);
outtextxy(maxx,maxy+50,":");
outtextxy(maxx+10,maxy+50,s);
}
org 0000h
ReplyDeleteljmp beg
org 0023h
ret1
org 0030h
beg:lcall lcdcmd
start: clr p0.0
mov p1,#0ffh
mov p3,#00h
fwdleft: mov r0,p1
cjne r0,#0f1h,fwd
mov p3,#01h
mov r3,#00h
acall lcddata
fwd: mov r0,p1
cjne r0,#0f2h,fwdright
mov p3,#03h
mov r3,#09
acall lcddata
fwdright: mov r0,p1
cjne r0,#0f3h,rotateleft
mov p3,#02h
mov r3,#17
acall lcddata
rotateleft:
mov r0,p1
cjne r0,#0f4h,stop
mov p3,#05h
mov r3,#27
acall lcddata
stop:
mov r0,p1
cjne r0,#0f5h,rotateright
mov p3,#00h
mov r3,#39
acall lcddata
rotateright: mov r0,p1
cjne r0,#0f6,revleft
mov p3,#0ah
mov r3,#44
acall lcddata
revleft:
mov r0,p1
cjne r0,#0f7,reverse
mov p3,#08h
mov r3,#57
acall lcddata
reverse:
mov r0,p1
cjne r0,#0f8,revright
mov p3,#0ch
mov r3,#66
acall lcddata
revright: mov r0,p1
cjne r0,#0f9h,demo
mov p3,#04h
mov r3,#74
acall lcddata
demo:
mov r0,p1
cjne r0,#0f0h,fwdleft
mov r3,#83
acall lcddata
mov p3,#03h
call delay1
mov r0,p1
cjne r0,#0f0h,demo
mov p3,#02h
call delay1
mov r0,p1
cjne r0,#0f0h,demo
mov p3,#01h
call delay1
mov r0,p1
cjne r0,#0f0h,demo
mov p3,#0ch
call delay1
mov r0,p1
cjne r0,#0f0h,demo
mov p3,#04h
call delay1
mov r0,p1
cjne r0,#0f0h,demo
mov p3,#08h
call delay1
mov r0,p1
cjne r0,#0f0h,demo
mov p3,#05h
call delay1
mov r0,p1
cjne r0,#0f0h,demo
mov p3,#0ah
call delay1
mov r0,p1
cjne r0,#0f0h,demo
ljmp start
lcdcmd:
org 200h
mov dptr,#mycom
c1: clr a
movc a,@a+dptr
acall comnwrt
acall delay
jz cmdret
inc dptr
sjmp c1
cmdret:ret
lcddata:
send_dat: mov dptr,#mydata
d1: clr a
mov a,r3
mov a,@a+dptr
acall datawrt
acall delay
inc dptr
jz dataret
sjmp d1
dataret:ret
comnwrt:
mov p2,a
clr p0.1
clr p0.2
setb p0.3
acall delay
clr p0.3
ret
datawrt:
mov p2,a
setb p0.1
clr p0.2
setb p0.3
acall delay
clr p0.3
ret
delay: mov r4,#250
here2: mov r5,#255
here: djnz r5,here
djnz r4,here2
ret
org 400h
mycom: db 38h,0eh,01,06,84h,0
my data: db "Fwd left",0,"Forward",0,"Fwd right",0,"Rotate left",0,"Stop",0,"Rotate right",0,"Rev left",0,"Reverse",0,"Rev Right",0,"Demo",0
delay1: mov tmod,#11h
mov r2,#80
loop1: mov th1,#3ch
mov tl1,#0afh
setb tr1
here1: jnb tf1,here1
clr tr1
clr tf1
djnz r2,loop1
ret
end
hai its fine....
ReplyDeleteexcellent da
ReplyDelete