#include <REGX51.H>
unsigned long ref_speed,read_counter, error, kp, t,dc,Ton;
sbit outbit=P1^0;
char i;
main()
{
TMOD=0x15;
IE=0x88; //Enable timer 1 interrupt
kp=1;
t=100;
ref_speed=800;
TH1=255;
TL1=240;
TR1=1;
while (1)
{
error=0;
TH0=0;
TL0=0;
TR0=1;
for ( i=0;i<1000;i++);
TR0=0;
read_counter=TL0;
TR0=1;
error=ref_speed -( read_counter );
read_counter=read_counter*1000/15;
error=kp*error;
dc=50+error;
if (dc>=90)
dc=90;
elseif(dc<=10)
dc=10;
else
;
Ton=(dc*100);
}
}
void t1isr(void) interrupt 3
{
TR1=0;
if (outbit==0)
{outbit=!outbit;
TH1=255;
TL1=255-Ton;
TR1=1;
}
else
{outbit=!outbit;
TH1=255;
TL1=255-(100-Ton);
TR1=1;
}
}