0% found this document useful (0 votes)
31 views

Kalkulator Coding

This document contains code for a calculator program written for an ATmega16 microcontroller. It defines functions for reading input from a keypad, performing basic math operations, and displaying output on an LCD screen. The main program loop continuously calls the kalkulator() function, which gets keypad input, identifies the operator, operates on two operands, and displays the result. This allows the microcontroller to function as a basic calculator by reading button presses from the keypad and performing calculations.

Uploaded by

Kinesan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Kalkulator Coding

This document contains code for a calculator program written for an ATmega16 microcontroller. It defines functions for reading input from a keypad, performing basic math operations, and displaying output on an LCD screen. The main program loop continuously calls the kalkulator() function, which gets keypad input, identifies the operator, operates on two operands, and displays the result. This allows the microcontroller to function as a basic calculator by reading button presses from the keypad and performing calculations.

Uploaded by

Kinesan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*****************************************************

CodeWizardAVR V1.24.8d Professional


Chip type : ATmega16
*****************************************************/
#include <mega16.h>
#include <delay.h>
#include <lcd.h>
#include <stdlib.h>
#include <math.h>
#define scanKeypad 1
#define stopScan 0

// Alphanumeric LCD Module functions


#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
//==================
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 ;
}
}
//==========================

int bacaKeyPad(void);
float kalkulator(void);

int operand1 = 0, operand2 = 0, operator ;


float hasil = 0 ;
char kode=0 , bufferLCD[50] ;

float kalkulator(void)
{
int StatusBaca = scanKeypad ;
kode = bacaKeyPad();
if( kode < 10 ) // kalau angka 0-9 ditekan
{
operand1 = (operand1*10)+ kode ;

itoa(kode , bufferLCD); // rubah kode ke ASCII


lcd_puts(bufferLCD); //tampilkan kode operand1 yg ditekan
delay_ms(50);
}
//==========================
if( kode > 9 && kode < 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('+') ;
kode = operator; // simpan operator yg dipilih

{
kode = bacaKeyPad();

if( kode < 10 )


{
operand2 = (operand2*10)+kode ;

itoa(kode , bufferLCD); // rubah kode ke ASCII


lcd_puts(bufferLCD); //tampilkan kode operand2 yg ditekan
delay_ms(50);

}
else if(kode == 14) //jika ditekan &amp;quot;=&amp;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 LCD
delay_ms(100);
kode = 0 ;
StatusBaca = stopScan ;
}
}
}
//==============================

return 0;
}

void main(void)
{

PORTA=0x00;
DDRA=0x0F;

PORTC=0x00;
DDRC=0x00;

lcd_init(20);
while (1)
{
kalkulator();
};
}

You might also like