Tugas Kelompok Nomor 3

Description
code arduino

Please download to get full document.

View again

of 5
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information
Category:

Documents

Publish on:

Views: 3 | Pages: 5

Extension: DOCX | Download: 0

Share
Transcript
  /* Tugas Kelompok Nomor 3: Lengkapi kode 1.7 Kalkulator; */ // Pemetaan Port Arduino untuk 7 segmen #define BUTTON1 4 #define BUTTON2 2 #define BCD1 8 #define T1 12 #define T2 13 // Pemetaan Port Arduino untuk keypad #define DATA_AV 2 #define KEY0 4 // konstanta periode #define DISPLAY_PERIODE 50 // konstanta OPERATOR #define ADD 0xA #define SUB 0xB #define MUL 0xC #define DIV 0xD #define CLR 0xE #define CALC 0xF // fungsi memeriksa apakah keypad ditekan boolean keyIsPressed() { static boolean key_old = 0; boolean b1 = digitalRead(DATA_AV); boolean s1 = (!b1 && key_old);; key_old = b1; return s1; } // fungsi membaca keypad int readKey() { int key = digitalRead(KEY0); key |= digitalRead(KEY0+1) << 1; key |= digitalRead(KEY0+2) << 2; key |= digitalRead(KEY0+3) << 3; return key; } // fungsi mentranslasi kode keypad (0-15) menjadi biner (0 - 0xF) char biner_table[] = {1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF,0}; char keyToBiner(int key) { return biner_table[key]; } // fungsi untuk menampilkan BCD ke 7 segment melalui encoder  void writeDigit(int bcd){ for(int i=0; i<4; i++) { digitalWrite(BCD1 + i, bcd & 0x01); bcd = bcd >> 1; } } // fungsi mengisi 3 digit array BCD dari angka int digits[3]; void writeDigit3(int angka){ digits[0] = data % 10; data = data / 10; digits[1] = data % 10; data = data / 10; digits[2] = data % 10; } // fungsi untuk menampilkan 3 digit BCD ke 7-segment memakai shift register int index = 0; void shiftDigit() { writeDigit(digits[index // memasukan nilai pada salah satu digits digitalWrite(T1, index & 0x02); digitalWrite(T2, index & 0x01); index = (index + 1 ) % 3; } // fungsi mengisi 3 digit array BCD dengan operator void writeOperator(int op) { digit[0] = op; // op akan sesuai hexa a,b,c,d digit[1] = 15; // tidak tampil apa-apa digit[2] = 15; // tidak tampil apa-apa } // variabel global #define STATE_1 0 #define STATE_2 1 int state=0; int next_state=0; boolean da; int key, biner; int value1, value2; int opr; long next_display; void setup() { Serial.begin(115200); // inisialisasi mode pin for (int pin=DATA_AV; pin<=KEY0+3; pin++) { pinMode(pin, INPUT); } // inisialisasi digits & shift 7-segment writeDigit3(0);  ¬†digitalWrite(T1, LOW); digitalWrite(T2, LOW); // inisialisasi waktu next_display = millis(); // inisialisasi kalkulator biner1 = biner2 = 0; } // fungsi transisi catatValue1 // menggubah harga value1 sesuai masukan biner, // tapi lakukan hanya kalau tidak menyebabkan overflow void entryValue1(int biner) { if (value1 < 100) { value1 = (value1 * 10) + biner; writeDigit3(value1); } Serial.print( value1= ); Serial.println(value1); } // fungsi transisi catatValue2 // menggubah harga value2 sesuai masukan biner void entryValue2(int biner) { if (value2 < 100) { value2 = (value2 * 10) + biner; writeDigit3(value2); } Serial.print( value2= ); Serial.println(value2); } // fungsi transisi push // mengingat operator yang ditekan void push(int op) { Serial.print( push ); Serial.println(value1); opr = op; writeOperator(opr); } // fungsi transisi pop // mengoperasikan value1 thd value2 sesuai operator void pop() { Serial.print( pop ); Serial.print(value1); Serial.print( ); Serial.print(opr, HEX); Serial.print( ); Serial.print(value2); switch(opr) { case ADD: value1 = value1 + value2; break; case SUB: value1 = value1 - value2; break;  ¬†case MUL: value1 = value1 * value2; break; case DIV: value1 = value1 / value2; break; } value2 = 0; writeDigit3(value1); Serial.print( => ); Serial.println(value1); } // fungsi transisi clear void clear() { value2 = value1 = 0; writeDigit3(value1); Serial.println( clear ); } // fungsi keadaaan state1 // sesuai diagram transisi keadaan 1.10 void state1() { Serial.print( State 1 ); switch(biner) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: entryValue1(biner); break; case ADD: case SUB: case MUL: case DIV: push(biner); next_state = STATE_2; break; case CLR: clear(); break; } } // fungsi keadaaan state2 // sesuai diagram transisi keadaan 1.10 void state2() { Serial.print( State 2 ); switch(biner) { case 0: case 1: case 2: case 3:
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks