Sei sulla pagina 1di 4

PIC: Calculadora Simples

Esta simples calculadora utilizando o PIC16F877A, possui as quatro


operaes bsicas (adio, subtrao, diviso e multiplicao).
Tambm possvel realizando clculos com nmeros fracionados.
DOWNLOAD
Projeto Calculadora;
CDIGO-FONTE:
1.
2.
3.
4.
5.
6.
7.

/*
*Projeto: Calculadora
*MCU: PIC16F877A
*Clock: HS 8MHz
*Descrio: Simples calculadora
*Compilador: MikroC PRO PIC
*Autor: Tiago H. Melo

8.
*/
9.
char keypadPort at PORTD;
10.
sbit LCD_RS at RB0_bit; sbit LCD_RS_Direction at
TRISB0_bit;
11.
sbit LCD_EN at RB1_bit; sbit LCD_EN_Direction at
TRISB1_bit;
12.
sbit LCD_D4 at RB2_bit; sbit LCD_D4_Direction at
TRISB2_bit;
13.
sbit LCD_D5 at RB3_bit; sbit LCD_D5_Direction at
TRISB3_bit;
14.
sbit LCD_D6 at RB4_bit; sbit LCD_D6_Direction at
TRISB4_bit;
15.
sbit LCD_D7 at RB5_bit; sbit LCD_D7_Direction at
TRISB5_bit;
16.
char teclas[] =
{'7','8','9','/','4','5','6','*','1','2','3','-','.','0','=','+'};
17.
char numero[9];
18.
float lvalue, rvalue;
19.
char op, flags, resultado[16];
20.
#define CALCULO_OK flags.F0
21.
#define NEGATIVO flags.F1
22.
#define lcd_clear() lcd_cmd(_LCD_CLEAR);
23.
#define LimparNumero for(i=0;i<8;i++) *(numero + i) = 0;
24.
25.
//esta funcao faz a varredura do teclado e retorna o valor
da tecla pressionada
26.
char keyRead(char keys[])
27.
{
28.
char key;
29.
key = keypad_key_press();
30.
if(key != 0) return keys[key-1];
31.
return 0;
32.
}
33.
void main()
34.
{
35.
char k, pos, op, i;
36.
keypad_init();
37.
Lcd_Init();
38.
Lcd_Cmd(_LCD_CLEAR);
39.
Lcd_Cmd(_LCD_CURSOR_OFF);
40.
while(1)
41.
{
42.
k = keyRead(teclas);//leitura do teclado
43.
if(CALCULO_OK)
44.
{
45.
pos = 0; //reseta posicao
46.
CALCULO_OK = 0; //reseta calculo
47.
lcd_clear(); //limpa display
48.
lcd_cmd(_LCD_RETURN_HOME);

49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.

for(i=0;i<8;i++){ //limpa as variaveis


*(numero + i) = 0;
*(resultado + i) = 0;
*(resultado +i + 8) = 0;
}
}
//se a teclas pressiona for um numero
if((k>='0' && k<='9') || k=='.')
{
*(numero + pos++) = k;
if(pos >= 8) pos = 0;
lcd_Out_CP((numero+(pos-1)));
}
else if(k == '+' || k=='-' || k=='/' || k=='*')
{
op = k;
pos = 0;
lcd_chr_CP(op);
lvalue = atof(numero);
LimparNumero;
}
else if(k == '=')
{
rvalue = atof(numero);
NEGATIVO = 0;
lcd_chr_CP('=');
switch(op)
{
case '+':
lvalue += rvalue;
break;
case '-':
if(lvalue >= rvalue)
{
lvalue -= rvalue;
}
else
{
lvalue = rvalue - lvalue;
NEGATIVO = 1;
}
break;
case '/':
if(rvalue!=0)lvalue /= rvalue;
else lvalue = 0;
break;
case '*':
lvalue *= rvalue;

97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.

break;
}
//converte numero para string
floattostr(lvalue, (resultado + NEGATIVO));
if(NEGATIVO) *(resultado) = '-';
lcd_out(2, 1, resultado);
CALCULO_OK = 1;
}
delay_ms(200);
}
}

http://microcontrolandos.blogspot.com.br/2013/05/pic-calculadora-simples.html

Potrebbero piacerti anche