Berikut ini contoh Program Kalkulator untuk Operasi aritmetik sederhana antara operand1 dan operand 2 hasil = operand1  operator  operand2 Operator yg digunakan :  X(kali), / (bagi), + (tambah), dan  – (kurang) Contoh  operasi aritmatik :   hasil = 34 X 2 keterangan: 34 adalah operand1 X adalah operator 2 adalah operand2 Rangkaian Simulasi dengan Proteus 
Flowchart Program 
Ringkasan Program
Flowchart Program 
Ringkasan Program
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
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 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
 | /*****************************************************CodeWizardAVR V1.24.8d ProfessionalChip type : ATmega16*****************************************************/#include <mega16.h>#include &amp;lt;delay.h&amp;gt;#include &amp;lt;lcd.h&amp;gt;#include &amp;lt;stdlib.h&amp;gt;#include &amp;lt;math.h&amp;gt;#define scanKeypad 1#define stopScan 0// Alphanumeric LCD Module functions#asm.equ __lcd_port=0x15 ;PORTC#endasm#include &amp;lt;lcd.h&amp;gt;int bacaKeyPad(void);float kalkulator(void);int operand1 = 0 , operand2 = 0 ;float hasil = 0 ;char kode=0 , bufferLCD[50] ;void main(void){PORTA=0x00;DDRA=0x0F;PORTC=0x00;DDRC=0x00;lcd_init(20);while (1){kalkulator();};}//==================int bacaKeyPad(void){char StatusBaca = scanKeypad ;while(StatusBaca==scanKeypad){PORTA.0 = 1 ;PORTA.1 = 0 ;PORTA.2 = 0 ;PORTA.3 = 0 ;if(PINA.4 == 1){return 7 ; StatusBaca = stopScan;}if(PINA.5 == 1){return 8 ; StatusBaca = stopScan;}if(PINA.6 == 1){return 9 ; StatusBaca = stopScan;}if(PINA.7 == 1){return 10; StatusBaca = stopScan;}//==========================================PORTA.0 = 0 ;PORTA.1 = 1 ;PORTA.2 = 0 ;PORTA.3 = 0 ;if(PINA.4 == 1){return 4 ; StatusBaca = stopScan;}if(PINA.5 == 1){return 5 ; StatusBaca = stopScan;}if(PINA.6 == 1){return 6 ; StatusBaca = stopScan;}if(PINA.7 == 1){return 11; StatusBaca = stopScan;}//==========================================PORTA.0 = 0 ;PORTA.1 = 0 ;PORTA.2 = 1 ;PORTA.3 = 0 ;if(PINA.4 == 1){return 1 ; StatusBaca = stopScan;}if(PINA.5 == 1){return 2 ; StatusBaca = stopScan;}if(PINA.6 == 1){return 3 ; StatusBaca = stopScan;}if(PINA.7 == 1){return 12; StatusBaca = stopScan;}//==========================================PORTA.0 = 0 ;PORTA.1 = 0 ;PORTA.2 = 0 ;PORTA.3 = 1 ;if(PINA.4 == 1){return 15; StatusBaca = stopScan;}if(PINA.5 == 1){return 0 ; StatusBaca = stopScan;}if(PINA.6 == 1){return 14; StatusBaca = stopScan;}if(PINA.7 == 1){return 13; StatusBaca = stopScan;}StatusBaca = scanKeypad ;}}//==========================float kalkulator(void){int StatusBaca = scanKeypad ;kode = bacaKeyPad();if( kode &amp;lt; 10 ) // kalau angka 0-9 ditekan{operand1 = (operand1*10)+ kode ;itoa(kode , bufferLCD);                    // rubah kode ke ASCIIlcd_puts(bufferLCD);                      //tampilkan kode  operand1 yg ditekandelay_ms(50);}//==========================if( kode &amp;gt; 9 &amp;amp;&amp;amp; kode &amp;lt; 16 ) // jika ditekan 10,11,12,13,14,15 (X, -, +, atau /){if(kode == 10)lcd_putchar('/') ;if(kode == 11)lcd_putchar('X') ;if(kode == 12)lcd_putchar('-') ;if(kode == 13)lcd_putchar('+') ;operator = kode;                      // simpan operator yg dipilih{kode = bacaKeyPad();if( kode &amp;lt; 10 ){operand2 = (operand2*10)+kode ;itoa(kode , bufferLCD);                    // rubah kode ke  ASCIIlcd_puts(bufferLCD);                        //tampilkan kode  operand2 yg ditekandelay_ms(50);}else if(kode == 14) //jika ditekan &quot;=&quot; laksanakan operasi{lcd_putchar('=');if(operator == 10){ // jika ditekan '/'hasil = operand1 / operand2 ;}if(operator == 11){ // jika ditekan 'X'hasil = operand1 * operand2 ;}if(operator == 12){ // jika ditekan '-'hasil = operand1 - operand2 ;}if(operator == 13){ // jika ditekan '+'hasil = operand1 + operand2 ;}ftoa(hasil , 1 , bufferLCD);                // rubah hasil ke string ( string adalah array berisi ascii diakhiri NULL )lcd_puts(bufferLCD);                         // print hasil ke LCDdelay_ms(100);kode = 0 ;StatusBaca = stopScan ;}}}//==============================return 0;} | 
Untuk alternatif lain coba kode untuk keypad 4×4 berikut ini
#define delaykeypad 200
int kode=0xff ,data;
int key(void)
{
int key(void)
{
PORTA.4=0;
delay_ms(3);
if (PINA.0==0) {kode=13;delay_ms(delaykeypad);}
else if (PINA.1==0){kode=15;delay_ms(delaykeypad);}
else if (PINA.2==0){kode=0;delay_ms(delaykeypad);}
else if (PINA.3==0){kode=14;delay_ms(delaykeypad);}
delay_ms(3);
if (PINA.0==0) {kode=13;delay_ms(delaykeypad);}
else if (PINA.1==0){kode=15;delay_ms(delaykeypad);}
else if (PINA.2==0){kode=0;delay_ms(delaykeypad);}
else if (PINA.3==0){kode=14;delay_ms(delaykeypad);}
PORTA.4=1;
PORTA.5=0;
delay_ms(3);
if (PINA.0==0) {kode=12;delay_ms(delaykeypad);}
else if (PINA.1==0){kode=9;delay_ms(delaykeypad);}
else if (PINA.2==0){kode=8;delay_ms(delaykeypad);}
else if (PINA.3==0){kode=7;delay_ms(delaykeypad);}
PORTA.5=0;
delay_ms(3);
if (PINA.0==0) {kode=12;delay_ms(delaykeypad);}
else if (PINA.1==0){kode=9;delay_ms(delaykeypad);}
else if (PINA.2==0){kode=8;delay_ms(delaykeypad);}
else if (PINA.3==0){kode=7;delay_ms(delaykeypad);}
PORTA.5=1; PORTA.6=0;
delay_ms(3);
if (PINA.0==0) {kode=11;delay_ms(delaykeypad);}
else if (PINA.1==0){kode=6;delay_ms(delaykeypad);}
else if (PINA.2==0){kode=5;delay_ms(delaykeypad);}
else if (PINA.3==0){kode=4;delay_ms(delaykeypad);}
delay_ms(3);
if (PINA.0==0) {kode=11;delay_ms(delaykeypad);}
else if (PINA.1==0){kode=6;delay_ms(delaykeypad);}
else if (PINA.2==0){kode=5;delay_ms(delaykeypad);}
else if (PINA.3==0){kode=4;delay_ms(delaykeypad);}
PORTA.6=1; PORTA.7=0;
delay_ms(3);
if (PINA.0==0) {kode=10;delay_ms(delaykeypad);}
else if (PINA.1==0){kode=3;delay_ms(delaykeypad);}
else if (PINA.2==0){kode=2;delay_ms(delaykeypad);}
else if (PINA.3==0){kode=1;delay_ms(delaykeypad);}
delay_ms(3);
if (PINA.0==0) {kode=10;delay_ms(delaykeypad);}
else if (PINA.1==0){kode=3;delay_ms(delaykeypad);}
else if (PINA.2==0){kode=2;delay_ms(delaykeypad);}
else if (PINA.3==0){kode=1;delay_ms(delaykeypad);}
PORTA.7=1;
delay_ms(3);
delay_ms(3);
if(kode !=0xff)
{
data = kode;
kode =0xff;
return data;
}
{
data = kode;
kode =0xff;
return data;
}
}

Tidak ada komentar:
Posting Komentar