This is my second experiment with Energia LaunchPad boards. This time I wanted to see how difficult would be to adapt for the LaunchPad a program which I originally developed for Arduino.
If are interested in how I did setup my Linux box for working with the LaunchPad and the Energia libraries, I’ve described the whole process in this post.
It turned out to be quite easy and the program needed only a few pin configuration changes.
Here is a link to the breadboard schema:
http://www.softwareliberopinerolo.org/images/arduino/D14-N_3_Display_7seg_.jpg
The code:
/**
Display 7 segmenti per corso arduino.
segmenti:
a 3 P2_4
b 4 P1_1
c 5 P1_2
d 6 P1_3
e 7 P1_4
dp 8 P1_5
f 9 P2_0
g 10 P2_1
Q1 2 transistor unità P2_2
Q2 11 transistor decine P1_7
SW1 12 P2_3
SW2 13 P2_5
*/
#define SW1 P2_3
#define SW2 P2_5
#define DECINE P1_7
#define UNITA P2_2
#define DEBOUNCE 200
byte valore = 0;
byte decine_attivo = 0;
byte seven_seg_digits[11][7] = {
{ 0,0,0,0,0,0,0 }, // = blank
{ 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
{ 1,1,1,0,0,1,1 } // = 9
};
byte segment_to_pin[8] = {P2_4, P1_1, P1_2, P1_3, P1_4, P2_0, P2_1, P1_5};
unsigned long last_read;
byte led = 0;
void write_digit(byte cifra, bool dp){
for(int i=0; i<7; i++){
digitalWrite(segment_to_pin[i], seven_seg_digits[cifra+1][i]);
}
digitalWrite(segment_to_pin[7], dp);
}
void setup(){
for(int p=0; p<8; p++){
pinMode(segment_to_pin[p], OUTPUT);
digitalWrite(segment_to_pin[p], HIGH);
}
pinMode(DECINE, OUTPUT);
pinMode(UNITA, OUTPUT);
pinMode(P1_0, OUTPUT);
pinMode(P1_6, OUTPUT);
digitalWrite(P1_0, LOW);
digitalWrite(P1_6, LOW);
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
digitalWrite(SW1, HIGH);
digitalWrite(SW2, HIGH);
last_read = millis();
}
void blank_digit(){
for(int i=0; i<7; i++){
digitalWrite(segment_to_pin[i], 0);
}
digitalWrite(segment_to_pin[7], 0);
}
void loop(){
if(digitalRead(SW1) == LOW){
if(millis() - last_read > DEBOUNCE){
valore--;
digitalWrite(P1_0, led);
led = ! led;
last_read = millis();
}
}
if(digitalRead(SW2) == LOW){
if(millis() - last_read > DEBOUNCE){
valore++;
digitalWrite(P1_6, led);
led = ! led;
last_read = millis();
}
}
valore = constrain(valore, 0, 99);
decine_attivo = !decine_attivo;
if(decine_attivo){
digitalWrite(UNITA, LOW);
if(valore/10){
write_digit(valore/10, 0);
} else {
blank_digit();
}
digitalWrite(DECINE, HIGH);
} else {
digitalWrite(DECINE, LOW);
write_digit(valore%10, 0);
digitalWrite(UNITA, HIGH);
}
delay(1);
}
Juan
Hello, nice and interesting way of using the displays, do you think i can emulate this reasoning to control three 7 segment displays?? which transistors did you used?
Regards,
Juan
Alessandro Pasotti
@juan,
yes, you can also drive three digits, they will be a little dimmer since each digit will be on for one third of the time.
I used stock NPN BC547 transistors, look here for details:
http://www.softwareliberopinerolo.org/images/arduino/D14-N_3_Display_7seg_.jpg