7 Segment Display Interface Arduino

00.28 Posted by Ali Mahfud , No comments
Antarmuka 7-Segment pada Arduino


Contoh penggunaan 7 segment pada Arduino
1. One Digit Up Counter
    - Merangkai rangkaian seperti gambar di atas
Sketch :
/*
* One-digit up counter
* Displays number 0 to 9 (up counter) on one digit 7-seg display cc
*/
// bits representing segments a through g and dp for number 0-9
const byte numeral[10] = {
//abcdefg and dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B00111110, // 6
B11100000, // 7
B11111110, // 8
B11100110, // 9
};
// pins for decimal point and each segment
// dp,g,f,e.d.c.b.a
const int segmentPins[8] = {9,8,7,6,5,4,3,2};
void setup()
{
for(int i=0; i < 8; i++)
{
pinMode(segmentPins[i], OUTPUT); // set all pins to output
}
}
void loop()
{
for(int i=0; i <= 10; i++)
{
showDigit(i);
delay(1000);
}
// the last value if i is 10 and this will turn the display off
delay(2000); // pause two seconds with the display off
}
// Displays a number from 0 through 9 on a 7-segment display
// any value not within the range of 0-9 turns the display off
void showDigit( int number)
{
boolean isBitSet;
for(int segment = 1; segment <= 8; segment++)
{
if( number < 0 || number > 9){
isBitSet = 0; // turn off all segments
}
else{
// isBitSet will be true if given bit is 1
isBitSet = bitRead(numeral[number], segment);
}
//isBitSet = ! isBitSet; // use this line if common anode display
digitalWrite( segmentPins[segment], isBitSet);
}
}

2. Display Input Potensiometer
Sketch:
/*
* Display input potensiometer
* Displays number 0 to 9 from a potensio on one digit 7-seg display cc
*/
// bits representing segments a through g and dp for number 0-9
const byte numeral[10] = {
// abcdefg and dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B00111110, // 6
B11100000, // 7
B11111110, // 8
B11100110, // 9
};
// pins for decimal point and each segment
// dp,g,f,e.d.c.b.a
const int segmentPins[8] = {9,8,7,6,5,4,3,2};
void setup()
{
Serial.begin(9600);
for(int i=0; i < 8; i++)
{
pinMode(segmentPins[i], OUTPUT); // set all pins to output
}
}
void loop()
{
int potVal = analogRead(A0); // pin A0 for potensio
potVal=map(potVal,0,1023,0,9);
Serial.println(potVal);
showDigit(potVal);
}
// Displays a number from 0 through 9 on a 7-segment display
// any value not within the range of 0-9 turns the display off
void showDigit( int number)
{
boolean isBitSet;
for(int segment = 1; segment <= 8; segment++)
{
isBitSet = bitRead(numeral[number], segment);
//isBitSet = ! isBitSet; // use this line if common anode display
digitalWrite( segmentPins[segment], isBitSet);
}
}

3. Multiplexing two digit display 7 Segment (7 Segment 2 digit)
Sketch:
/*
* Multiplexing two-digit display sketch
* Displays the value of a potensio (0 - 99) on a multiplex two-digit display
*/
// bits representing segments a through g and dp for number 0-9
const int numeral[10] = {
//abcdefg and dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B00111110, // 6
B11100000, // 7
B11111110, // 8
B11100110, // 9
};
// pins for decimal point and each segment
// dp,g,f,e.d.c.b.a
const int segmentPins[8] = {1,2,3,4,5,6,7,8};
const int nbrDigits= 2; // the number of digits in the LED display
//digit 1 2
const int digitPins[nbrDigits] = {10,11}; // 10=MSD, 11=LSD
void setup()
{
Serial.begin(9600);
for(int i=0; i < 8; i++)
pinMode(segmentPins[i], OUTPUT); // set all segs and dp pins to outp
for(int i=0; i < nbrDigits; i++)
pinMode(digitPins[i], OUTPUT);
}
void loop()
{
int potVal = analogRead(A0); // pin A0 connected to potensio
potVal = map(potVal,0,1023,0,99);
Serial.println(potVal);
showNumber(potVal);
}
void showNumber( int number)
{
if(number == 0)
showDigit(0, nbrDigits-1) ; // display 0 in the rightmost digit
else
{
// display the value corresponding to each digit
// leftmost digit is 0, rightmost is one less than the number of places
for (int digit = nbrDigits-1; digit >= 0; digit--)
{
if(number > 0)
{
showDigit(number % 10, digit) ;
number = number / 10;
}
}
}
}
// Displays given number on a 7-segment display at the given digit position
void showDigit( int number, int digit)
{
digitalWrite( digitPins[digit], HIGH );
for (int segment = 1; segment < 8; segment++)
{
boolean isBitSet = bitRead(numeral[number], segment);
// isBitSet will be true if given bit is 1
isBitSet = ! isBitSet; // use this line for common cathode display
digitalWrite(segmentPins[segment], isBitSet);
}
delay(5);
digitalWrite(digitPins[digit], LOW );
}