Percobaan 02 7 - Segmen Ver 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

PERCOBAAN 02: Seven Segment LED

2.1

Pemrograman Seven Segment LED Tanpa Sub-rutin

A. Langkah kerja :
1. Buat rangkaian sebagai berikut

220 Ohm

10K Ohm

2. Buat program baru sebagai berikut:

void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(9, 0); // start with the "dot" off

MIKROPOSESOR DAN ANTARMUKA| Modul 01: I/O Digital

}
void loop() {
// write '9'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '8'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '7'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
delay(1000);
// write '6'
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '5'
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '4'
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
delay(1000);
// write '3'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);

MIKROPOSESOR DAN ANTARMUKA| Modul 01: I/O Digital

digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
delay(1000);
// write '2'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 1);
delay(1000);
// write '1'
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
delay(1000);
// write '0'
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
delay(4000);
}

3. Jalankan program dan amati tampilan pada 7-segmen.

B. Tugas
1. Buat program untuk menampilkan angka 9,8,7,,2,1,0 secara berulang
2. Buat program untuk menampilkan angka 0,1,2,,7,8,9,8,7,,2,1,0 secara berulang

2.2 Pemrograman Seven Segment LED dengan Sub-rutin


A. Langkah kerja :
1. Gunakan rangkaian pada percobaan 2.1
2. Buat program sebagai berikut
byte seven_seg_digits[10][7] = { { 1,1,1,1,1,1,0 }, // = 0
{ 0,1,1,0,0,0,0 }, // = 1
{ 1,1,0,1,1,0,1 }, // = 2
{ 1,1,1,1,0,0,1 }, // = 3
{ 0,1,1,0,0,1,1 }, // = 4
{ 1,0,1,1,0,1,1 }, // = 5
{ 1,0,1,1,1,1,1 }, // = 6
{ 1,1,1,0,0,0,0 }, // = 7
{ 1,1,1,1,1,1,1 }, // = 8

MIKROPOSESOR DAN ANTARMUKA| Modul 01: I/O Digital

{ 1,1,1,0,0,1,1 } // = 9
};
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
writeDot(0); // start with the "dot" off
}
void writeDot(byte dot) {
digitalWrite(9, dot);
}
void sevenSegWrite(byte digit) {
byte pin = 2;
for (byte segCount = 0; segCount < 7; ++segCount) {
digitalWrite(pin, seven_seg_digits[digit][segCount]);
++pin;
}
}
void loop() {
for (byte count = 10; count > 0; --count) {
delay(1000);
sevenSegWrite(count - 1);
}
delay(4000);
}

3. Compile dan upload program, lihat hasil yang terjadi.

B. Tugas
1. Ulangi tugas pada Percobaan 2.1

2.3 Tampilan 7-segmen dengan inputan tombol


A. Langkah kerja :
1. Gunakan rangkaian pada percobaan 2.1
2. Buat program sebagai berikut
const int switchPin = 11;
const int ledPin = 13;
int state;
int lastState;
int buttonCounter = 0;
byte seven_seg_digits[10][7] = { { 1,1,1,1,1,1,0 }, // = 0
{ 0,1,1,0,0,0,0 }, // = 1
{ 1,1,0,1,1,0,1 }, // = 2
{ 1,1,1,1,0,0,1 }, // = 3
{ 0,1,1,0,0,1,1 }, // = 4

MIKROPOSESOR DAN ANTARMUKA| Modul 01: I/O Digital

{ 1,0,1,1,0,1,1 },
{ 1,0,1,1,1,1,1 },
{ 1,1,1,0,0,0,0 },
{ 1,1,1,1,1,1,1 },
{ 1,1,1,0,0,1,1 }
};

// = 5
// = 6
// = 7
// = 8
// = 9

void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
writeDot(0); // start with the "dot" off
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void writeDot(byte dot) {
digitalWrite(9, dot);
}
void sevenSegWrite(byte digit) {
byte pin = 2;
for (byte segCount = 0; segCount < 7; ++segCount) {
digitalWrite(pin, seven_seg_digits[digit][segCount]);
++pin;
}
}
void loop() {
state = digitalRead(switchPin);
if (state != lastState) {
if (state == HIGH) {
if (buttonCounter <= 9){
buttonCounter++;
} else buttonCounter=1;
Serial.println(buttonCounter);
}
lastState = state;
}
sevenSegWrite(buttonCounter - 1);
delay(2);
}

3. Compile dan upload program, lihat hasil yang terjadi.

B. Tugas
1. Buat program down-counter

MIKROPOSESOR DAN ANTARMUKA| Modul 01: I/O Digital

You might also like