|
~: C program for RPM counter using 89C2051 :~
#include<reg51.h> // include 8051 register file
unsigned char data tmp; // define any temperory register
sbit led1 = P1^7; // define led1 as 7th bit of P1
sbit led2 = P1^6; // define led2 as 6th bit of P1
void delay(void); // function initialization
void dely(void);
void hextodesi(unsigned char x);
void intr1(void) interrupt 2 // external interrupt 1 function
{
EX1=0; // disable all interrupts for 10 sec
led1=0; // LED indication
TH0=0x00; // reset counter
TL0=0x00;
TR0=1; // start counter
delay(); // give 10 sec delay
TR0=0; // stop counter
tmp=TL0; // store count
tmp=tmp*6; // multiply it by 6
hextodesi(tmp); // convert it into ASCII
led1=1; // indication off
EX1=1; // again enable interrupts
}
void hextodesi(unsigned char x) // convert hex to desi and then to ASCII
{
unsigned int tmp1,tmp2,t,t1,i,k;
unsigned char asci[3];
tmp1 = (x & 0x0F); //get lower nibble of hex value
tmp2 = x >> 4; // get upper nibble of hex value
tmp2 = tmp2*16; // multiply upper nibble by 16 and
t = tmp1+tmp2; // add upper and lower nibble to get decimal value
if(t>=100) // if number is greater then 100
{
i=2; // take three digits
while(t>10) // get each digit one by one till it is
{ // greater then 10
t1=t%10;
asci[i]=t1+0x30; // convert them it to ASCII
t=t/10;
i--;
}
asci[0]=t+0x30;
}
else // if its less then 100 then
{
t1=t%10;
asci[2]=t1+0x30;
t=t/10;
asci[1]=t+0x30;
asci[0]=0x30; // take first digit as zero
}
for(k=0;k<=2;k++) // one by one send all three digits
{
led2=0;
SBUF = asci[k];
while(TI==0);
TI=0;
led2=1;
dely();
}
}
void delay() // generate delay of aprox 10 sec
{
int i,j;
for(i=0;i<200;i++)
for(j=0;j<10000;j++);
}
void dely() // generate delay of aprox 100 ms
{
int l;
for(l=0;l<10000;l++);
}
void main()
{
TMOD=0x25; // initialize T0 as 16 bit counter and T1 as 16 bit timer
SCON=0x40; // serial communication mode 1
TH1=0xF3; // load the count to set buad rate of 9.6 KBPS
TL1=0xF3;
TR1=1; // start timer
IE=0x84; // enable external interrupt 1
while(1); // continuous loop
} |