You are on page 1of 2

/* The software program for 89C51 micro controller is written in C language.

It
is compiled, simulated, debugged in KEIL (IDE). Here is complete C program code
with comments. */
#include<reg51.h>
sbit op = P2^0;
unsigned int i=0,c=0,d=8;
unsigned char byte[9]={0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00};
unsigned char byt;
void keydly() // key debounce delay
{
int p,q;
for(p=0;p<30;p++)
for(q=0;q<1000;q++);
}
void delay(int d) // basic 1 ms delay subroutine
{
int r;
TL0 = 0x26;
TH0 = 0xFC;
TR0 = 1;
for(r=0;r<d;r++)
{
while(TF0==0);
TF0 = 0;
TL0 = 0x26;
TH0 = 0xFC;
}
TR0 = 0;
}
void int0(void) interrupt 0
// external interrupt 1 subroutine
{
byt=P1;
// get digital input on P1
}
void int1(void) interrupt 2 // external interrupt 2 subroutine
{
int t;
if(d!=0) delay(d); // apply output pulse after delay
else for(t=0;t<50;t++);
op=0;
for(t=0;t<100;t++);
op=1;
}
void main()
{
TMOD = 0x01;
// initialize timer0 as 16 bit timer
P1=0xFF;
// P1 as input port

op=0;
// P2.0 as output
op=1;
P0=0x00; // P0 as output port
P0=byte[i]; // motor off LED off
IE=0x81; // enable external interrupt 0 only
IT0=1;
IT1=1;
back: byt=0xFF;
while(byt==0xFF); //loop until no key is pressed
keydly(); // key debounce delay
switch(byt)
{
case 0xF7: // for button 3
if(d>0) d--; // decrease delay
if(i<9) i++; // increase bar graph
P0=byte[i];
break;
case 0xFB: // for button 2
if(d<8) d++; // increase delay
if(i>0) i--;
P0=byte[i]; // decrease bar graph
break;
case 0xFD: // for button 1
c++; // enable or disable ext int 1
if((c%2)==1) {EX1=1; P0=byte[i];}
else {EX1=0; P0=byte[0];}
break;
}
goto back;
// continuous loop
}