Calculatrice pic 16f877a

Signaler
Messages postés
1
Date d'inscription
mardi 17 avril 2012
Statut
Membre
Dernière intervention
9 mai 2010
-
 chouaieb -
bonjour
on me demande de réaliser une calculatrice à l’aide d’un microcontrôleur PIC 16F877A.
1-conception de la carte qui sert de simulateur
2-programmation du microcontrôleur

comment faire et si une personne peut me donner le code source
A voir également:

1 réponse

unsigned short kp; //store pressed value to be displayed on lcd
unsigned short kp_decimal; //store decimal value
unsigned short result=0; //store the result of calculation
char result_text[4]; //store converted(string) result to be displayed on lcd
unsigned short num1,num2; //store first number, store second number
unsigned short op; //store operation sign
bit clr; //flag for input again



// Keypad 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



void keypad()
{
do //enter do loop
{
kp = 0; // Reset input char variable

do // Wait for key to be pressed and released
{
kp = Keypad_Key_Click(); // Store key code in kp variable
}while (!kp); //stay in the loop if kp = 0, (if no button is pressed)

switch (kp) //transform key to char to be displayed on LCD
{


case 1: kp = '7'; kp_decimal =7; break;
case 2: kp = '8'; kp_decimal =8; break;
case 3: kp = '9'; kp_decimal =9; break;
case 4: kp = '/'; kp_decimal ='/'; break;
case 5: kp = '4'; kp_decimal =4; break;
case 6: kp = '5'; kp_decimal =5; break;
case 7: kp = '6'; kp_decimal =6; break;
case 8: kp = '*'; kp_decimal ='*'; break;
case 9: kp = '1'; kp_decimal =1; break;
case 10: kp = '2'; kp_decimal =2; break;
case 11: kp = '3'; kp_decimal =3; break;
case 12: kp = '-'; kp_decimal ='-'; break;
case 13: kp = 'C'; clr=1; break;
case 14: kp = '0'; kp_decimal =0; break;
case 15: kp = '='; break;
case 16: kp = '+'; kp_decimal ='+'; break;
}

if(kp != '#' && kp != '*') // Print pressed key on LCD except '#' and '*'
{
Lcd_Chr_cp(kp);
}

if(kp=='#' || clr==1)break; //break from the loop if '#' is pressed or clr is set

}while (1); //stay in the loop for ever

}


void main()
{

Keypad_Init(); // Initialize Keypad

Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

Lcd_out(1,6,"Calculator"); // welcome on LCD
delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear display


Loop: //main loop

//Get First Number
NO1_Loop:
Lcd_out(1,1,"No1: ");
keypad(); //go to keypad function to get the input
if(clr==1) //if clr is set
{
lcd_out(1,5, " "); //clear first number position
clr=0; //reset clear variable
goto NO1_Loop; //return to get first number input
}
num1 = kp_decimal; //store first number
delay_ms(200); //wait


//Get Second Number
NO2_Loop:
Lcd_out(2,1,"No2: ");
keypad(); //go to keypad function to get the input
if(clr==1) //if clr is set
{
lcd_out(2,5, " "); //clear second number position
clr=0; //reset clear variable
goto NO2_Loop; //return to get second number input
}
num2 = kp_decimal; //store second number

delay_ms(200); //wait


//Get Operation
OP_Loop:
Lcd_out(3,1,"OP: ");
keypad(); //go to keypad function to get the input
if(clr==1) //if clr is set
{
lcd_out(3,5, " "); //clear operation position
clr=0; //reset clear variable
goto OP_Loop; //return to get operation
}
op = kp_decimal; //store operation sign

delay_ms(200); //wait


//Do Calculations
switch(op) //check operation sign
{
case '+': //if operation sign +
result = num1 + num2; //add the two numbers and store the result
ByteToStr(result,result_text); //convert to result to string
lcd_out(4,1,"Sum = ");
Lcd_out_cp(result_text); //display the result
break; //end

case '-': //if operation sign -
result = num1 - num2; //subtract the two numbers and store the result
ByteToStr(result,result_text); //convert to result to string
lcd_out(4,1,"Difference = ");
Lcd_out_cp(result_text); //display the result
break; //end

case 'x': //if operation sign x
result = num1 * num2; //multiply the two numbers and store the result
ByteToStr(result,result_text); //convert to result to string
lcd_out(4,1,"Product = ");
Lcd_out_cp(result_text); //display the result
break; //end

case '/': //if operation sign /
result = num1 / num2; //divide the two numbers and store the result
ByteToStr(result,result_text); //convert to result to string
lcd_out(4,1,"Quotient = ");
Lcd_out_cp(result_text); //display the result
break; //end
}

//Enter keypad to clear
keypad();
if(clr==1)
{
Lcd_Cmd(_LCD_CLEAR);
clr=0;
goto Loop;
}

goto Loop; //return to main loop
}