Contoh Dasar Pemrograman Kalkulator dengan microcontroller AVR C Codevision dgn simulasi proteus

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 Calcisis Flowchart Program avrcalc Ringkasan Program

/*****************************************************
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
#include <lcd.h>

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 < 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('+') ;
operator = kode;                      // 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 "=" 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;
}

Perbaikan kesalahan tampilan kode  pada tulisan diatas error

Berikut ini foto hasil dari kode kalkulator sederhana  diatas  yg di terapkan pada microcontroller AVR board.

kalkulator_avr

Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis untuk pemula.

Posted on 15/09/2014, in AVR, Contoh-contoh Aplikasi and tagged . Bookmark the permalink. 5 Comments.

  1. suhu itu pakai cvavr atau bukan ?
    kok saya tidak bisa jalan ya?

  2. gan kok error ya pas coba di proteus

    • coba yg ini : /*****************************************************
      Date : 9/14/2014
      Chip type : ATmega8535
      Program type : Application
      Clock frequency : 11.059200 MHz

      *****************************************************/
      #include mega8535.h // jng lupa beri tanda kurung
      #include delay.h
      #include stdlib.h
      #include math.h
      // Alphanumeric LCD Module functions
      #asm
      .equ __lcd_port=0x15 ;PORTC
      #endasm
      #include lcd.h
      // Declare your global variables here

      int key(void);
      float kalkulator(void);
      void operand(char z);
      void Aritmetik(char z);
      void origin(void);

      float operand1 = 0 , operand2 = 0 , hasil = 0 ;
      char y=0 , lcd[25] , operator ;

      void main(void)
      {

      PORTA=0xff;
      DDRA=0x0F;

      lcd_init(20);

      lcd_gotoxy(10,0);
      lcd_putsf(“calculator”);

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

      //==================
      int key(void){
      char KEY = 1 ;
      while(KEY){

      PORTA.0 = 0 ;
      PORTA.1 = 1 ;
      PORTA.2 = 1 ;
      PORTA.3 = 1 ;
      if(PINA.4 == 0){return 7 ; KEY = 0;}
      if(PINA.5 == 0){return 8 ; KEY = 0;}
      if(PINA.6 == 0){return 9 ; KEY = 0;}
      if(PINA.7 == 0){return 10; KEY = 0;}
      //==========================================
      PORTA.0 = 1 ;
      PORTA.1 = 0 ;
      PORTA.2 = 1 ;
      PORTA.3 = 1 ;
      if(PINA.4 == 0){return 4 ; KEY = 0;}
      if(PINA.5 == 0){return 5 ; KEY = 0;}
      if(PINA.6 == 0){return 6 ; KEY = 0;}
      if(PINA.7 == 0){return 11; KEY = 0;}
      //==========================================
      PORTA.0 = 1 ;
      PORTA.1 = 1 ;
      PORTA.2 = 0 ;
      PORTA.3 = 1 ;
      if(PINA.4 == 0){return 1 ; KEY = 0;}
      if(PINA.5 == 0){return 2 ; KEY = 0;}
      if(PINA.6 == 0){return 3 ; KEY = 0;}
      if(PINA.7 == 0){return 12; KEY = 0;}
      //==========================================
      PORTA.0 = 1 ;
      PORTA.1 = 1 ;
      PORTA.2 = 1 ;
      PORTA.3 = 0 ;
      if(PINA.4 == 0){return 15; KEY = 0;}
      if(PINA.5 == 0){return 0 ; KEY = 0;}
      if(PINA.6 == 0){return 14; KEY = 0;}
      if(PINA.7 == 0){return 13; KEY = 0;}
      KEY = 1 ;
      }
      }
      //#########################################################
      void operand(char operator)
      {
      if(operator == 10)lcd_putchar(‘/’) ;
      if(operator == 11)lcd_putchar(‘X’) ;
      if(operator == 12)lcd_putchar(‘-‘) ;
      if(operator == 13)lcd_putchar(‘+’) ;
      delay_ms(100);
      }

      //#########################################################
      void Aritmetik(char operator)
      {
      if(operator == 10)hasil = operand1 / operand2 ;
      if(operator == 11)hasil = operand1 * operand2 ;
      if(operator == 12)hasil = operand1 – operand2 ;
      if(operator == 13)hasil = operand1 + operand2 ;

      ftoa(hasil , 1 , lcd);
      lcd_puts(lcd);
      delay_ms(100);
      }
      //#########################################################

      void origin(void)
      {
      operand1 = 0 ;operand2 = 0 ;hasil = 0 ;
      lcd_clear();

      }
      //==========================
      float kalkulator(void)
      {
      int Loop = 1 ; // start loop
      y = key();
      if( y == 15 ) // tombol C ditekan
      {
      origin();
      return 0 ;
      }

      if( y 9 && y < 16 ) // jika ditekan 10,11,12,13,14,15 kemudian scankeypad untuk operand2
      {
      if( y == 15 ){ // jika ditekan C
      origin();
      return 0 ;
      }
      operator = y ; //simpan kode operasi di var operator
      operand(operator); // print operator aritmetik * . , +, /

      while(Loop)
      {
      y = key();
      if( y == 15 )
      {
      origin();
      return 0 ;
      }
      if( y < 10 ) // ambil data 0~9 simpan di operand2
      {
      operand2 = (operand2*10)+y ;
      itoa(y , lcd);
      lcd_puts(lcd);
      delay_ms(50);
      }
      else if(y == 14) //jika ditekan tanda "="
      {
      lcd_putchar('=');
      Aritmetik(operator); // laksanakan operasi
      y = 0 ;
      Loop = 0 ; // operasi selesai stop loop , kembali ke atas
      }
      }
      }
      //==============================

      return 0;
      }

  3. suhu kok gak jalan ya programnya?

    • yg perlu diperhatikan keypad di proteus aktif dgn scanning bit high sedangkan keypad aktual bit low.

      contoh kode pada keypad untuk simulasi di proteus
      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;}

      sedangkan kode untuk di aplikasikan dgn keypad aktual :
      PORTA.0 = 0 ;
      PORTA.1 = 1 ;
      PORTA.2 = 1 ;
      PORTA.3 = 1 ;
      if(PINA.4 == 0){return 7 ; StatusBaca = stopScan;}
      if(PINA.5 == 0){return 8 ; StatusBaca = stopScan;}
      if(PINA.6 == 0){return 9 ; StatusBaca = stopScan;}
      if(PINA.7 == 0){return 10; StatusBaca = stopScan;}

      ==========================================================================

      Sebagai alternatif lain coba kode untuk keypad 4×4 berikut ini

      #define delaykeypad 200

      int kode=0xff ,data;
      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);}

      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=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);}

      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);}

      PORTA.7=1;
      delay_ms(3);

      if(kode !=0xff)
      {
      data = kode;
      kode =0xff;
      return data;
      }

      }

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: