Thursday, April 29, 2010

--> Simple digital stopwatch using at89s51 Microcontroller

Here's a simple digital stopwatch with AT89S51 microcontroller. Two common cathode type 7segment displays are used to display the time.
Operation:
  • The display will show 00 at the startup
  • Press the switch SP1 to get the watch to run
  • Press the switch again to stop the watch
  • To start the watch press the same switch again.
 Circuit Diagram:

Assembly program:
[code:asm]TCNTA EQU 30H
TCNTB EQU 31H
SEC EQU 32H
KEYCNT EQU 33H
SP1 BIT P3.5
ORG 00H
LJMP START
ORG 0BH
LJMP INT_T0
START: MOV KEYCNT,#00H
MOV SEC,#00H
MOV A,SEC
MOV B,#10
DIV AB
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
MOV A,B
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P2,A
MOV TMOD,#02H
SETB ET0
SETB EA
WT: JB SP1,WT
LCALL DELY10MS
JB SP1,WT
INC KEYCNT
MOV A,KEYCNT
CJNE A,#01H,KN1
SETB TR0
MOV TH0,#06H
MOV TL0,#06H
MOV TCNTA,#00H
MOV TCNTB,#00H
LJMP DKN
KN1: CJNE A,#02H,KN2
CLR TR0
LJMP DKN
KN2: CJNE A,#03H,DKN
MOV SEC,#00H
MOV A,SEC
MOV B,#10
DIV AB
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
MOV A,B
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P2,A
MOV KEYCNT,#00H
DKN: JNB SP1,$
LJMP WT
DELY10MS:
MOV R6,#20
D1: MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
RET
INT_T0:
INC TCNTA
MOV A,TCNTA
CJNE A,#100,NEXT
MOV TCNTA,#00H
INC TCNTB
MOV A,TCNTB
CJNE A,#4,NEXT
MOV TCNTB,#00H
INC SEC
MOV A,SEC
CJNE A,#100,DONE
MOV SEC,#00H
DONE: MOV A,SEC
MOV B,#10
DIV AB
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
MOV A,B
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P2,A
NEXT: RETI
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
END [/code]
 C code:
[code:c]#include
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00};
unsigned char second;
unsigned char keycnt;
unsigned int tcnt;
void main(void)
{
unsigned char i,j;
TMOD=0x02;
ET0=1;
EA=1;
second=0;
P0=dispcode[second/10];
P2=dispcode[second ];
while(1)
{
if(P3^5==0)
{
for(i=20;i>0;i--)
for(j=248;j>0;j--);
if(P3^5==0)
{
keycnt++;
switch(keycnt)
{
case 1:
TH0=0x06;
TL0=0x06;
TR0=1;
break;
case 2:
TR0=0;
break;
case 3:
keycnt=0;
second=0;
P0=dispcode[second/10];
P2=dispcode[second];
break;
}
while(P3^5==0);
}
}
}
}
void t0(void) interrupt 1 using 0
{
tcnt++;
if(tcnt==400)
{
tcnt=0;
second++;
if(second==100)
{
second=0;
}
P0=dispcode[second/10];
P2=dispcode[second];
}
} [/code]


-------------------------------------------------------

No comments:

Post a Comment