Tải bản đầy đủ

Code chương trình VA LUU DO THUAT TOAN máy tính đơn giản



Code chương trình:
#include
#include
#include
#include
#define RS P3_0
#define RW P3_1 //RW=0 => ghi
#define EN P3_2 //RW=1 => doc
//RS=0 => code
//RS=1 => data
#define LCD_PORT P0
sbit cot0=P2^0;
sbit cot1=P2^1;
sbit cot2=P2^2;
sbit cot3=P2^3;
sbit hang0=P2^4;
sbit hang1=P2^5;
sbit hang2=P2^6;
sbit hang3=P2^7;

int sttphim;
unsigned char dau;


void wait(void)
{
long n = 0;
EN=1;// Dua chan cho fep len cao
RS=0;// Chon thanh ghi lenh
RW=1;// Doc tu LCD
LCD_PORT=0xFF;// Gia tri 0xFF
while(1){n++; if(n>100) break;}// Kiem tra co ban
// Neu ban dem n den 100 roi thoat khoi while
EN=0;// Dua xung cao xuong thap de chot
RW=0;// Doc tu LCD
}
void delay(long time) /* ham delay */
{
long n;
for(n=0;n{;}
}
void delay_5ms(){
int i,j;
for(i=0;i<250;i++)
for(j=0;j<4;j++){}
}
void delay_15ms(){
int i,j;
for(i=0;i<250;i++)
for(j=0;j<100;j++){}
}
void LCDWriteCmd(unsigned char c)
{
RS=0;
RW=0;
LCD_PORT=c;
EN=1;
EN=0;
delay_5ms();

}
void LCDWriteData(unsigned char c)
{
RS=1;
RW=0;
LCD_PORT=c;
EN=1;
EN=0;
delay(1000);
}
void LCD_init()
{
delay_15ms();


LCDWriteCmd(0x30); //che do 8 bit
LCDWriteCmd(0x30);
LCDWriteCmd(0x30);
LCDWriteCmd(0x38); //hien thi 2 dong
LCDWriteCmd(0x0C); //bat con tro
// LCDWriteCmd(0x06); //dich con tro sang ben phai
LCDWriteCmd(0x01); // Xoa man hinh LCD
}
void LCD_clear()
{
LCDWriteCmd(0x01);
}
void LCD_home()
{
LCDWriteCmd(0x80); //ep con tro ve dau dong
}
void LCD_putstr(unsigned char *s)
{
while (*s)
{
LCDWriteData(*s);
s++;
}
}
void LCDwritei(long d)
{ unsigned int i,j;
unsigned char a[7];
a[6]=d%10;
d=d/10;
a[5]=d%10;
d=d/10;
a[4]=d%10;
d=d/10;
a[3]=d%10;// Chia lay phan du, duoc chu so hang don vi
d=d/10;// Chia lay phan nguyen, duoc nhung chu so da bo hang don vi
a[2]=d%10;// Duoc chu so hang chuc
d=d/10;// Nhung chu so da bo hang don vi va hang chuc
a[1]=d%10;// Duoc hang tram
d=d/10;// Duoc chu so da bo hang don vi, chuc, tram
a[0]=d%10;// Duoc hang ngan
for(i=0;i<7;i++)
{
if(a!=0)
break;
}
for(j=i;j<7;j++)
{
LCDWriteData(48+a[j]);
}
}


/***************************Ham quet phim************************/
void quetphim()
{ int y;
P2=0x0f;
if((!cot0)|(!cot1)|(!cot2)|(!cot3))
{
for(y=0;y<5000;y++){
if(cot0==0) //neu la hang 0
{
hang0=1;
hang1=1;
hang2=1;
hang3=1;
cot0=0;
if(!hang0) sttphim=0;
if(!hang1) sttphim=1;
if(!hang2) sttphim=2;
if(!hang3) sttphim=3;
}
else if(cot1==0) //neu la hang 1
{
hang0=1;
hang1=1;
hang2=1;
hang3=1;
cot1=0;
if(!hang0) sttphim=4;
if(!hang1) sttphim=5;
if(!hang2) sttphim=6;
if(!hang3) sttphim=7;
}
if(cot2==0)
{
hang0=1;
hang1=1;
hang2=1;
hang3=1;
cot2=0;
if(!hang0) sttphim=8;
if(!hang1) sttphim=9;
if(!hang2) dau='+';
if(!hang3) dau='-';
}
if(cot3==0)
{
hang0=1;
hang1=1;
hang2=1;
hang3=1;
cot3=0;
if(!hang0) dau='*';
if(!hang1) dau='/';


if(!hang2) dau='C';
if(!hang3) dau='=';
}
}
}
}
/**************************Chương tr?nh chính***********************/
void main(void)
{ long fnumber;
long lnumber;
long result,sodu,dev;
unsigned char sign;
unsigned char hienthi;
int i,kt,j;
kt=0;
i=0;
LCD_init();
LCDWriteCmd(0x01);
LCDWriteCmd(0x80);
LCD_putstr("CALCULATOR VER 1");
while(1)
{
dau='w';
sttphim=10;
quetphim();
if(sttphim!=10)
{
if(kt==0){
if(i==0)
{LCD_clear();}
fnumber=fnumber*10+sttphim;
i=1;
}
else{
lnumber=lnumber*10+sttphim;
}
hienthi=sttphim+48;
LCDWriteData(hienthi);
}
else
{
switch(dau)
{
case 'C':
{
LCDWriteCmd(0x01);
break;


}
case '+':
{
LCDWriteData(dau);
kt=1;
sign='+';
break;
}
case '-':
{
LCDWriteData(dau);
kt=1;
sign='-';
break;
}
case '*':
{
LCDWriteData(dau);
kt=1;
sign='*';
break;
}
case '/' :
{
LCDWriteData(dau);
kt=1;
sign='/';
break;
}
case '=' :
{
if(sign=='+')
{
LCDWriteData(dau);
result = fnumber+lnumber;
LCDwritei(result);
kt=0;
fnumber=0;
lnumber=0;
i=0;
}
else if(sign=='-')
{
LCDWriteData(dau);
if(fnumber>lnumber)
result = fnumber-lnumber;
else{
LCDWriteData('-');


result = lnumber-fnumber;
}
LCDwritei(result);
kt=0;
fnumber=0;
lnumber=0;
i=0;
}
else if(sign=='*')
{
LCDWriteData(dau);
result = fnumber*lnumber;
LCDwritei(result);
kt=0;
fnumber=0;
lnumber=0;
i=0;
}
else if(sign=='/')
{
LCDWriteData(dau);
if(lnumber==0)
{
LCDWriteCmd(0x01);
LCD_putstr("ERROR");
}
else
{
result = fnumber/lnumber;
LCDwritei(result);
sodu = fnumber%lnumber;
}
if(sodu!=0)
{
LCDWriteData(',');
for(j=0;j<5;j++)
{
dev=(sodu*10)/lnumber;
LCDwritei(dev);
dev=(sodu*10)%lnumber;
sodu=dev;
}
kt=0;
fnumber=0;
lnumber=0;
i=0;
}
else
{
kt=0;
fnumber=0;
lnumber=0;
i=0;
}
}
else


break;
}
default :
break;
}
}
}
}



x

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×