mình cần thay phím 'on' bằng ham 'sin' mình viết bằng ceil C
bạn nào giúp mình minh xin cảm ơn rât nhiều!!
phần code
#include <regx51.h>
#define COL P1
int xdphim(void); //quet de xac dinh phim nhan
void nhanphim(void); //nhan mot key
void gtrphim(void); //tinh gia tri so nhap tu ban phim
void gan(void); //xac dinh cac phep +,-,x,/ tu ban phim va tinh ra ket qua
void tinhtoan(void); //xac dinh phep +,-,x,/ tu ban phim va ket qua tu phep tinh truoc
void hienthilcd(long int); //hien thi ket qua
void lcdinit(void); //khoi tao lcd
void lcdcmd(unsigned char); //ghi lenh ra lcd
void lcddata(unsigned char); //ghi du lieu ra lcd
void reset(void); //reset he thong ve gia tri gan ban dau
void tre(unsigned int); //tre
int matranp[4][4]={7,8,9,10,
4,5,6,11,
1,2,3,12,
14,0,15,13};
sfr ldata=0xA0;
sbit rs=P3^0;
sbit rw=P3^1;
sbit en=P3^2;
sbit h0=P3^4;
sbit h1=P3^5;
sbit h2=P3^6;
sbit h3=P3^7;
long int b,c,gt=0,tp;
int digflag=1; //neu bang '1', thi thuc hien phep tinh neu bang '0' thi khong thu hien phep tinh
int noflag=1; //neu bang '1', '2' lan luot la gia tri truoc va sau khi nhan dau
int opflag=0; //neu bang 1 gia tri truoc dau '+','-','x','/' duoc nhan, 0 la chua duoc nhan
int loaidau=0; //loai phep tinh duoc nhan neu=0 mac dinh,bang 1 la '+', 2 la '-', 3 la 'x', 4 la '/'
int daubang=0; //neu bang '1' la dau bang duoc nhan bang '0' la dau '=' chua duoc nhan
int key=0;
int temp;
int songuyen=0; // neu =1 thi la ket qua la so nguyen am neu =0 thi la ket qua nguyen duong
unsigned int hang,cot;
float remflt,fa,fc;
unsigned int i,j;
unsigned char ch;
unsigned int dem=0;
void write_string(char *s)
{
while(*s)
{
lcddata( *s) ;
s++;
}
}
void main(void)
{
lcdinit(); //khoi tao lcd
lcdcmd(0x01); //xoa man hinh
lcdcmd(0x80); // dua con tro ve dau dong 1
write_string(" MAY TINH ");
lcdcmd(0xc0); //dua contro ve dau dong2
write_string(" Lop D09DTA1 ");
tre(200);
lcdinit();
lcdcmd(0x01);
lcdcmd(0x80);
write_string(" NHOM 3 ");
lcdcmd(0x80);
write_string(" TAI-VietAnh ");
while(1)
{
COL=0xFF;
h0=0; h1=0; h2=0; h3=0;
nhanphim();
key=xdphim();
if(key>=0&&key<=9)
{
temp=key;
if((noflag==1||noflag==2)&&digflag==1)
gtrphim();
}
if(key>=10&&key<=13)
{
temp=key;
if(opflag==1)
gan();
}
if(key==15)
{
temp=key;
if(daubang==1)
tinhtoan();
}
if(key==14)
{
temp=key;
reset();
}
}
}
int xdphim(void) //quet de xac dinh phim nhan
{
do
{
tre(20); //kiem tra nhieu phim nhan
hang=COL;
hang=hang&(0x0F);
}while(hang==0x0F);
while(1)
{
h0=0;
h1=1;
h2=1;
h3=1;
hang=COL;
hang=hang&(0x0F);
if(hang!=0x0F){cot=0;break;}
h0=1;
h1=0;
h2=1;
h3=1;
hang=COL;
hang=hang&(0x0F);
if(hang!=0x0F){cot=1;break;}
h0=1;
h1=1;
h2=0;
h3=1;
hang=COL;
hang=hang&(0x0F);
if(hang!=0x0F){cot=2;break;}
h0=1;
h1=1;
h2=1;
h3=0;
hang=COL;
hang=hang&(0x0F);
if(hang!=0x0F){cot=3;break;}
}
if(hang==0x0E)key=matranp[cot][0];
if(hang==0x0D)key=matranp[cot][1];
if(hang==0x0B)key=matranp[cot][2];
if(hang==0x7)key=matranp[cot][3];
return key;
}
void nhanphim(void)
{
do
{
tre(5);
hang=COL;
hang=hang&(0x0F);
}while(hang!=0x0F);
}
void gtrphim(void) // tinh ket qua tu cac phim duoc nhan
{
if(noflag==1) opflag=1;
lcddata(48+key);
gt=(gt*10)+key;
}
void gan(void)
{
if(noflag==1)fa=gt; // gia tri phim
if(daubang==0&&noflag==2)fa=fc; //gia tri lay ket qua truoc
if(key==13){loaidau=1; lcddata('+'); }
if(key==12){loaidau=2; lcddata('-'); }
if(key==11){loaidau=3; lcddata('x'); }
if(key==10){loaidau=4; lcddata(253); }
digflag=1; //dat phep tinh moi
opflag=0; //khong the nhan duoc cho den khi nut on duoc nhan hoac nhan '+','-','x','/'
noflag=2; //gia tri sau khi nhan '+','-','x','/'
gt=0;
daubang=1;
}
void tinhtoan(void)
{
if(noflag==2)b=gt;
daubang=0; //dau '=' da duoc nhan
digflag=0; //khong thuc hien phep tinh nao cho den khi nhan'+','-','x','/' hoac nhan 'on'
opflag=1; // gia tri moi
songuyen=0;
if(loaidau==1)fc=fa+b;
if(loaidau==2)fc=fa-b;
if(loaidau==3)fc=fa*b;
if(loaidau==4)fc=fa/b;
if(fc<0)
{
fc=fc*(-1); songuyen=1;
} //neu ket qua am thi songuyen=1
c=fc;
remflt=fc-c;
tp=remflt*10000;
gt=0;
lcdinit();
if(songuyen==1) //vi songuyen=1 nhu tren moi hien thi dau '-'
{
lcddata('-');
fc=fc*(-1);
}
hienthilcd(c); //hien thi so truoc dau '.'
lcddata('.');
hienthilcd(tp);
}
void hienthilcd(long int ketqua)
{
long int digcnt;
unsigned char ctemp;
int l;
for(digcnt=1;digcnt<10000000000;digcnt=digcnt*10)
if((ketqua/digcnt)>=0&&(ketqua/digcnt)<=9) break;
for(;digcnt>=1;digcnt=digcnt/10)
{
l=ketqua/digcnt;
ctemp=48+l;
lcddata(ctemp); //hien thi ket qua
ketqua=ketqua%digcnt;
}
}
void lcdinit(void) //khoi tao lcd
{
dem=0;
lcdcmd(0x38); // 2 dong ma tran 5x7
tre(1);
lcdcmd(0x0E); // hien thi dau con tro khong nhap nhay
tre(1);
lcdcmd(0x01); // xoa man hinh
tre(1);
lcdcmd(0x06); //dich chuyen con tro sang ben phai
tre(1);
lcdcmd(0x80); // quay tro lai dong thu nhat
tre(1);
}
void lcdcmd(unsigned char value) //lcdcmd() Gui lenh ra LCD
{
ldata=value;
rs=0; // Ghi lenh
rw=0;
en=1;
tre(5);
en=0;
}
void lcddata(unsigned char value) // gui du lieu ra lcd
{
if(dem==16){lcdcmd(0xC0); dem=0;} //neu het 16 o thi xuong dong 2
ldata=value;
rs=1; // ghi du lieu
rw=0;
en=1;
tre(1);
en=0;
dem++;
}
void reset(void) //reset lai cac dieu kien ban dau de thuc hien phep tinh moi
{
noflag=1;
digflag=1;
opflag=0;
loaidau=0;
daubang=0;
gt=0;
lcdinit();
}
void tre(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1000;j++);
}
bạn nào giúp mình minh xin cảm ơn rât nhiều!!
phần code
#include <regx51.h>
#define COL P1
int xdphim(void); //quet de xac dinh phim nhan
void nhanphim(void); //nhan mot key
void gtrphim(void); //tinh gia tri so nhap tu ban phim
void gan(void); //xac dinh cac phep +,-,x,/ tu ban phim va tinh ra ket qua
void tinhtoan(void); //xac dinh phep +,-,x,/ tu ban phim va ket qua tu phep tinh truoc
void hienthilcd(long int); //hien thi ket qua
void lcdinit(void); //khoi tao lcd
void lcdcmd(unsigned char); //ghi lenh ra lcd
void lcddata(unsigned char); //ghi du lieu ra lcd
void reset(void); //reset he thong ve gia tri gan ban dau
void tre(unsigned int); //tre
int matranp[4][4]={7,8,9,10,
4,5,6,11,
1,2,3,12,
14,0,15,13};
sfr ldata=0xA0;
sbit rs=P3^0;
sbit rw=P3^1;
sbit en=P3^2;
sbit h0=P3^4;
sbit h1=P3^5;
sbit h2=P3^6;
sbit h3=P3^7;
long int b,c,gt=0,tp;
int digflag=1; //neu bang '1', thi thuc hien phep tinh neu bang '0' thi khong thu hien phep tinh
int noflag=1; //neu bang '1', '2' lan luot la gia tri truoc va sau khi nhan dau
int opflag=0; //neu bang 1 gia tri truoc dau '+','-','x','/' duoc nhan, 0 la chua duoc nhan
int loaidau=0; //loai phep tinh duoc nhan neu=0 mac dinh,bang 1 la '+', 2 la '-', 3 la 'x', 4 la '/'
int daubang=0; //neu bang '1' la dau bang duoc nhan bang '0' la dau '=' chua duoc nhan
int key=0;
int temp;
int songuyen=0; // neu =1 thi la ket qua la so nguyen am neu =0 thi la ket qua nguyen duong
unsigned int hang,cot;
float remflt,fa,fc;
unsigned int i,j;
unsigned char ch;
unsigned int dem=0;
void write_string(char *s)
{
while(*s)
{
lcddata( *s) ;
s++;
}
}
void main(void)
{
lcdinit(); //khoi tao lcd
lcdcmd(0x01); //xoa man hinh
lcdcmd(0x80); // dua con tro ve dau dong 1
write_string(" MAY TINH ");
lcdcmd(0xc0); //dua contro ve dau dong2
write_string(" Lop D09DTA1 ");
tre(200);
lcdinit();
lcdcmd(0x01);
lcdcmd(0x80);
write_string(" NHOM 3 ");
lcdcmd(0x80);
write_string(" TAI-VietAnh ");
while(1)
{
COL=0xFF;
h0=0; h1=0; h2=0; h3=0;
nhanphim();
key=xdphim();
if(key>=0&&key<=9)
{
temp=key;
if((noflag==1||noflag==2)&&digflag==1)
gtrphim();
}
if(key>=10&&key<=13)
{
temp=key;
if(opflag==1)
gan();
}
if(key==15)
{
temp=key;
if(daubang==1)
tinhtoan();
}
if(key==14)
{
temp=key;
reset();
}
}
}
int xdphim(void) //quet de xac dinh phim nhan
{
do
{
tre(20); //kiem tra nhieu phim nhan
hang=COL;
hang=hang&(0x0F);
}while(hang==0x0F);
while(1)
{
h0=0;
h1=1;
h2=1;
h3=1;
hang=COL;
hang=hang&(0x0F);
if(hang!=0x0F){cot=0;break;}
h0=1;
h1=0;
h2=1;
h3=1;
hang=COL;
hang=hang&(0x0F);
if(hang!=0x0F){cot=1;break;}
h0=1;
h1=1;
h2=0;
h3=1;
hang=COL;
hang=hang&(0x0F);
if(hang!=0x0F){cot=2;break;}
h0=1;
h1=1;
h2=1;
h3=0;
hang=COL;
hang=hang&(0x0F);
if(hang!=0x0F){cot=3;break;}
}
if(hang==0x0E)key=matranp[cot][0];
if(hang==0x0D)key=matranp[cot][1];
if(hang==0x0B)key=matranp[cot][2];
if(hang==0x7)key=matranp[cot][3];
return key;
}
void nhanphim(void)
{
do
{
tre(5);
hang=COL;
hang=hang&(0x0F);
}while(hang!=0x0F);
}
void gtrphim(void) // tinh ket qua tu cac phim duoc nhan
{
if(noflag==1) opflag=1;
lcddata(48+key);
gt=(gt*10)+key;
}
void gan(void)
{
if(noflag==1)fa=gt; // gia tri phim
if(daubang==0&&noflag==2)fa=fc; //gia tri lay ket qua truoc
if(key==13){loaidau=1; lcddata('+'); }
if(key==12){loaidau=2; lcddata('-'); }
if(key==11){loaidau=3; lcddata('x'); }
if(key==10){loaidau=4; lcddata(253); }
digflag=1; //dat phep tinh moi
opflag=0; //khong the nhan duoc cho den khi nut on duoc nhan hoac nhan '+','-','x','/'
noflag=2; //gia tri sau khi nhan '+','-','x','/'
gt=0;
daubang=1;
}
void tinhtoan(void)
{
if(noflag==2)b=gt;
daubang=0; //dau '=' da duoc nhan
digflag=0; //khong thuc hien phep tinh nao cho den khi nhan'+','-','x','/' hoac nhan 'on'
opflag=1; // gia tri moi
songuyen=0;
if(loaidau==1)fc=fa+b;
if(loaidau==2)fc=fa-b;
if(loaidau==3)fc=fa*b;
if(loaidau==4)fc=fa/b;
if(fc<0)
{
fc=fc*(-1); songuyen=1;
} //neu ket qua am thi songuyen=1
c=fc;
remflt=fc-c;
tp=remflt*10000;
gt=0;
lcdinit();
if(songuyen==1) //vi songuyen=1 nhu tren moi hien thi dau '-'
{
lcddata('-');
fc=fc*(-1);
}
hienthilcd(c); //hien thi so truoc dau '.'
lcddata('.');
hienthilcd(tp);
}
void hienthilcd(long int ketqua)
{
long int digcnt;
unsigned char ctemp;
int l;
for(digcnt=1;digcnt<10000000000;digcnt=digcnt*10)
if((ketqua/digcnt)>=0&&(ketqua/digcnt)<=9) break;
for(;digcnt>=1;digcnt=digcnt/10)
{
l=ketqua/digcnt;
ctemp=48+l;
lcddata(ctemp); //hien thi ket qua
ketqua=ketqua%digcnt;
}
}
void lcdinit(void) //khoi tao lcd
{
dem=0;
lcdcmd(0x38); // 2 dong ma tran 5x7
tre(1);
lcdcmd(0x0E); // hien thi dau con tro khong nhap nhay
tre(1);
lcdcmd(0x01); // xoa man hinh
tre(1);
lcdcmd(0x06); //dich chuyen con tro sang ben phai
tre(1);
lcdcmd(0x80); // quay tro lai dong thu nhat
tre(1);
}
void lcdcmd(unsigned char value) //lcdcmd() Gui lenh ra LCD
{
ldata=value;
rs=0; // Ghi lenh
rw=0;
en=1;
tre(5);
en=0;
}
void lcddata(unsigned char value) // gui du lieu ra lcd
{
if(dem==16){lcdcmd(0xC0); dem=0;} //neu het 16 o thi xuong dong 2
ldata=value;
rs=1; // ghi du lieu
rw=0;
en=1;
tre(1);
en=0;
dem++;
}
void reset(void) //reset lai cac dieu kien ban dau de thuc hien phep tinh moi
{
noflag=1;
digflag=1;
opflag=0;
loaidau=0;
daubang=0;
gt=0;
lcdinit();
}
void tre(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1000;j++);
}
Comment