thiết bị đo tốc độ động cơ <99000rpm
Ảnh đã được thu nhỏ. Bấm vào đây để xem hình cỡ đầy đủ. H
ì
nh
gốc cỡ 634x603 và dung lượng 12KB.
Code:
#include<AT89S53.h>
void delay_ms( unsigned int ms )
{ unsigned int i,j;
for (i=0;i<ms;i++)
for (j=0;j<120;j++){};
}
//========= LCD =====================
sbit RS = 0x93; // RS = P1_3
sbit RW = 0xB7; // RW = P3_7
sbit E = 0x94; // E = P1_4
unsigned char digit;
void LCD_PutChar(unsigned char k )
{ P2=k ; RS=1 ; RW=0 ; E=1 ; E=0 ; delay_ms(1);}
void LCD_PutCmd(unsigned char k )
{ P2=k ; RS=0 ; RW=0 ; E=1 ; E=0 ; delay_ms(1);}
void int_LCD()
{ delay_ms(50);
LCD_PutCmd(0x38);
LCD_PutCmd(0x06); // dich con tro sang phai
LCD_PutCmd(0x0E); // nhap nhay con tro
LCD_PutCmd(0x01); // xoa man hinh
LCD_PutCmd(0x02); // ve dau dong
LCD_PutCmd(0x80);
LCD_PutChar('d');LCD_PutChar('i');LCD_PutChar('a');LCD_PutC
har('=');LCD_PutChar(' ');
LCD_PutChar(' ');LCD_PutChar(' ');LCD_PutChar(';');
LCD_PutChar('t');LCD_PutChar('r');LCD_PutChar('u');LCD_PutC
har('c');
LCD_PutChar('=');
}
//================= ham mu a^b=========
unsigned int ham_mu(unsigned char a,unsigned char b)
{ unsigned char i;
unsigned int j=1;
for(i=1;i<=b;i++){j*=a;}
return(j);
}
void update_tocdo(float k)
{float y=0;
unsigned int x=0;
x=k/1;
LCD_PutCmd(0x86);
digit=(x/10000)+0x30;LCD_PutChar(digit);
digit=((x/1000)%10)+0x30;LCD_PutChar(digit);
digit=((x/100)%10)+0x30;LCD_PutChar(digit);
digit=((x/10)%10)+0x30;LCD_PutChar(digit);
digit=(x%10)+0x30;LCD_PutChar(digit);
LCD_PutChar(',');
y=(k-x)*ham_mu(10,4);
x=y/1;
digit=((x/1000)%10)+0x30;LCD_PutChar(digit);
digit=((x/100)%10)+0x30;LCD_PutChar(digit);
digit=((x/10)%10)+0x30;LCD_PutChar(digit);
digit=(x%10)+0x30;LCD_PutChar(digit);
}//+++++++++++++++++++++++++++++++++++++++++++++++
//========= key =====================
sbit Y1 = 0x80;
sbit Y2 = 0x81;
sbit OK = 0x82;
sbit D_A = 0x90;// P1_0
sbit D_B = 0x91;// P1_1
sbit D_C = 0x92;// P1_2
unsigned char count=0;
unsigned char key[10]=0;
void scan_key()
{
Y1=Y2=1;D_A=0;D_B=0;D_C=0;
if(!Y1){delay_ms(10);while(!Y1);delay_ms(10);//7
LCD_PutChar('7');key[count++]=7;
};
if(!Y2){delay_ms(10);while(!Y2);delay_ms(10);//8
LCD_PutChar('8');key[count++]=8;
};
Y1=Y2=1;D_A=0;D_B=0;D_C=1;
if(!Y1){delay_ms(10);while(!Y1);delay_ms(10);//3
LCD_PutChar('3');key[count++]=3;
};
Y1=Y2=1;D_A=0;D_B=1;D_C=0;
if(!Y1){delay_ms(10);while(!Y1);delay_ms(10);//4
LCD_PutChar('4');key[count++]=4;
};
if(!Y2){delay_ms(10);while(!Y2);delay_ms(10);
LCD_PutChar('.');
};
Y1=Y2=1;D_A=0;D_B=1;D_C=1;
if(!Y1){delay_ms(10);while(!Y1);delay_ms(10);//1
LCD_PutChar('1');key[count++]=1;
};
Y1=Y2=1;D_A=1;D_B=0;D_C=0;
if(!Y1){delay_ms(10);while(!Y1);delay_ms(10);//6
LCD_PutChar('6');key[count++]=6;
};
if(!Y2){delay_ms(10);while(!Y2);delay_ms(10);//9
LCD_PutChar('9');key[count++]=9;
};
Y1=Y2=1;D_A=1;D_B=0;D_C=1;
if(!Y1){delay_ms(10);while(!Y1);delay_ms(10);//2
LCD_PutChar('2');key[count++]=2;
};
Y1=Y2=1;D_A=1;D_B=1;D_C=0;
if(!Y1){delay_ms(10);while(!Y1);delay_ms(10);//5
LCD_PutChar('5');key[count++]=5;
};
Y1=Y2=1;D_A=1;D_B=1;D_C=1;
if(!Y1){delay_ms(10);while(!Y1);delay_ms(10);//0
LCD_PutChar('0');key[count++]=0;
};
}
//=============== encoder =============
float gtri[]=0;
float gtri_TB=0;
unsigned char so_lan=0;
unsigned char gtri_dia=0;
unsigned int gtri_truc=0;
unsigned char he_so=1;
setup_interrupts()
{
EA = 1; //cho phep cac ngat xay ra
ET0 = 1; //cho phep ngat counter0
TR0 = 1; //chay counter0
TMOD = 0x26; //counter 0 in mode 2 (8bit reload),
TL0=TH0=0; //timer 1 in mode 2 (auto reload from
TH1)
TH1 = 6; //start counter from 6-256
ET1 = 1; //cho phep ngat tran timer1
TR1 = 1; //chay timer1
PT0 = 1; //uu tien ngat counter0
PT1 = 0;
}
chuan_mau() interrupt 3 //timer 1 interrupt
{
so_lan++;
gtri[4]=gtri[3];
gtri[3]=gtri[2];
gtri[2]=gtri[1];
gtri[1]=gtri[0];
gtri[0]=TL0+count*256;
TL0=0;
count=0;
if(so_lan==5)
{gtri_TB
=((gtri[0]+gtri[1]+gtri[2]+gtri[3]+gtri[4])/(5*100*250*so_l
an))*he_so;
update_tocdo(gtri_TB);
so_lan=0;
};
// nhan OK de dung lai xem gia tri
if(!OK){TR0=TR1=ET0=ET1=0;gtri[0]=gtri[1]=gtri[2]=gtri
[3]=gtri[4]=0;
count=so_lan=0;TL0=TH0=0;
delay_ms(50);while(!OK);delay_ms(10);
while(OK);
delay_ms(50);while(!OK);delay_ms(10);ET0=TR0=ET1=TR1=1
;
};
}
dem_xung() interrupt 1 //counter 0 interrupt
{count++;
TL0=0;
}
void serial_init()
{ TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TR1 = 1;
RI = 1;
TI = 1;
}
void main()
{ Y1=Y2=OK=1;
P3_3 = 1;
P3_4 = 1;
int_LCD();
LCD_PutCmd(0x84);
while(OK)scan_key(); // OK lan 1 : nhap gtri dgkinh
banh dia
delay_ms(50);while(!OK);delay_ms(50);
so_lan=count;
for(count=0;count<so_lan;count++)
{gtri_dia+=key[count]*ham_mu(10,so_lan-count-1);};
count=0;
LCD_PutCmd(0x8D);
while(OK)scan_key(); // OK lan 2 : nhap gtri dgkinh
truc can do toc do
delay_ms(10);while(!OK);delay_ms(10);
so_lan=count;
for(count=0;count<so_lan;count++)
{gtri_truc+=key[count]*ham_mu(10,so_lan-count-1);};
LCD_PutCmd(0x01);LCD_PutCmd(0x0C);LCD_PutCmd(0x80);
LCD_PutChar('(');LCD_PutChar('v');LCD_PutChar('/');LCD_PutC
har('s');LCD_PutChar(')');
LCD_PutChar('=');
he_so=gtri_dia/gtri_truc;
count=0;
setup_interrupts();
while(1);
}