:: مهندس متواجد ::
تاريخ التسجيل: Jul 2012
المشاركات: 93
|
|
نشاط [ MAAASD ]
قوة السمعة:0
|
|
22-08-2012, 05:33 AM
المشاركة 2
|
|
وده الكود علشان لو حد كسل يعمل دونلود
كود:
//kaypad module connections
char keypadport at portd;
//end keypad module connections
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
const unsigned char kp_manager[]={7,8,9,'/',4,5,6,
'*',1,2,3,'-','c',0,'=','+'}; //keypad mapping
char kp,temp,res_txt[12],res_txt2[12],i,j;
unsigned long op1,op2,res;
unsigned long GetSeconedOp(){
unsigned long x=0;
lcd_out(2,1,"No2: ");
while(1){
kp=0;
while(!kp){
kp=keypad_key_click();
delay_ms(50);
}
if(kp_manager[kp-1]=='=') break;
lcd_chr_cp(kp_manager[kp-1]+0x30);
x=x*10+kp_manager[kp-1];
}
return x;
}
void main() {
keypad_init(); //initialize keypad
lcd_init(); //initialize lcd
lcd_cmd(_lcd_clear);
lcd_out(1,3,"Calculator");
lcd_cmd(_lcd_cursor_off);
//delay_ms(2000);
lcd_cmd(_lcd_clear);
while(1){
op1=0; op2=0; res=0;
lcd_out(1,1,"No1: ");
//getting the 1st number
while(1){
kp=0;
while(!kp){
kp=keypad_key_click();
delay_ms(50);
}
temp=kp_manager[kp-1];
if(temp=='+') goto plus;
if(temp=='-') goto minus;
if(temp=='/') goto divide;
if(temp=='*') goto multiply;
lcd_chr_cp(temp+0x30); //display ascii equivalent to decimal digit (0 to 9)
op1=op1*10+temp; //calculate 1st operand from entered digits
}
plus:
op2=GetSeconedOp();
res=op1+op2;
goto done; //to print result on LCD
minus:
op2=GetSeconedOp();
res=op1-op2;
goto done;
divide:
op2=GetSeconedOp();
res=op1/op2;
goto done;
multiply:
op2=GetSeconedOp();
res=op1*op2;
done:
longtostr(res,res_txt);
lcd_cmd(_lcd_clear);
lcd_out(1,1,"Result:");
lcd_cmd(_lcd_second_row); //move cursor to 2nd row\\
//The following method is to have output without leading blanks(spaces)
//remove blanks of result_txt áÇÒã ÊÊÚãá ÈÇáØÑíÞÉ Ïí ÚáÔÇä ãíÍÕáÔ ÇÎØÇÁ
j=0;
for(i=0;i<12;i++){
if(res_txt[i]!=' ')
{
res_txt2[j]=res_txt[i];
j++;
}
}
lcd_out_cp(res_txt2);
delay_ms(2000);
lcd_cmd(_lcd_clear);
}
}
|