Go Down

Topic: A simple single digit seven segment display (Read 2653 times) previous topic - next topic

arduinohabib

I wrote a simple library for a common cathode seven segment display, you can add it in a new tab.
Please feel free to use it or post comments!
Quote

#include<Arduino.h>
#define a 13
#define b 12
#define c 11
#define d 10
#define e 9
#define f 8
#define g 7
#define dp 6 
void zero(){
  digitalWrite(a, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
}
void zerooff(){
  digitalWrite(a, LOW);
  digitalWrite(d, LOW);
  digitalWrite(f, LOW);
  digitalWrite(e, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
}
void one(){
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
}
void oneoff(){
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
}
void two(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(d, HIGH);
}
void twooff(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(g, LOW);
  digitalWrite(e, LOW);
  digitalWrite(d, LOW);
}
void three(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
}
void threeoff(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(g, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
}
void four(){
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
}
void fouroff(){
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
}
void five(){
  digitalWrite(a, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
}
void fiveoff(){
  digitalWrite(a, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
}
void six(){
  digitalWrite(a, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
}
void sixoff(){
  digitalWrite(a, LOW);
  digitalWrite(f, LOW);
  digitalWrite(e, LOW);
  digitalWrite(g, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
}
void seven(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
}
void sevenoff(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
}
void eight(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void eightoff(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void nine(){
  digitalWrite(a, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
}
void nineoff(){
  digitalWrite(a, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
}
void clearall(){
  oneoff();
  twooff();
  threeoff();
  fouroff();
  fiveoff();
  sixoff();
  sevenoff();
  eightoff();
  nineoff();
  zerooff();
}

What is man's best friend? The breadboard!

dxw00d

Take a look at this one - http://www.arduino.cc/playground/Code/SevenSegment.

Rather than having a separate pair of functions for each number, it has an array that stores the pattern for each number in a single byte and iterates through the bits to light the segments.

fungus


I wrote a simple library for a common cathode seven segment display, you can add it in a new tab.
Please feel free to use it or post comments!



What happens if I do this?

two();
seven();

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

arduinohabib

That will make it display a two on top of a seven.

You first have to define which pins you connect abcdefg segments to in your sketch. Then if you type zero(); then the seven segment display will display zero, one(); will display one, etc. zerooff(); will turn zero off. clearall(); will turn everything off. This is an example:
Quote

#include"sevenseg.h"
int test;
#define a 13
#define b 12
#define c 11
#define d 10
#define e 9
#define f 8
#define g 7
#define dp 6
void setup(){
 pinMode(a, OUTPUT);
 pinMode(b, OUTPUT);
 pinMode(c, OUTPUT);
 pinMode(d, OUTPUT);
 pinMode(e, OUTPUT);
 pinMode(f, OUTPUT);
 pinMode(g, OUTPUT);
 pinMode(dp, OUTPUT);
}
void loop(){
 zero();
 delay(1000);
 zerooff();
 one();
 delay(1000);
 oneoff();
 two();
 delay(1000);
 twooff();
 three();
 delay(1000);
 threeoff();
 four();
 delay(1000);
 fouroff();
 five();
 delay(1000);
 fiveoff();
 six();
 delay(1000);
 sixoff();
 seven();
 delay(1000);
 sevenoff();
 eight();
 delay(1000);
 eightoff();
 nine();
 nineoff();
}


[/quote]
What is man's best friend? The breadboard!

dxw00d

Please use code tags instead of quote. It's the '#' button next to the quote tag button.

Why not just turn off the segments that aren't needed? Then you wouldn't need the xxxoff functions.

arduinohabib

What is man's best friend? The breadboard!

fungus


That will make it display a two on top of a seven.

You first have to define which pins you connect abcdefg segments to in your sketch. Then if you type zero(); then the seven segment display will display zero, one(); will display one, etc. zerooff(); will turn zero off. clearall(); will turn everything off. This is an example:


But then you either have to know what the current display is or call clearAll() which is slow because it writes every segment multiple times.

Wouldn't it be better to have "two()" and "seven()" turn off the segments they don't need as well as turn on the ones they do?

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

dhenry

Quote
Please feel free to use i


Something like this is basically unusable in a real application. Its timing, critical to such an implementation is loop dependent and its scalability (to add more digits) is poor.

What you should do is to implement it inside of a timer. So all the user application does is to update the display buffer and the display routine will update the display in the background.

dxw00d

What timing? It just digitalWrite()s segment pins high or low.

arduinohabib

Thanks so much for the feedback dear friends! I shall modify the library.
What is man's best friend? The breadboard!

dhenry

I will give you a skeleton:

0) define a bunch of macros to turn on / off segments / digits. Those macros are Common Cathod or Common Anode aware.
1) use a routine to initialize all pins.
2) write a code that upon each call, display one digit from a buffer, and rolls over to the next.
3) set up a timer, pointer a function pointer to the routine in 2) above.
4) in the timer isr, execute the function pointer (now pointing to the routine in 2)).

Once that is up, the entire display function is transparent to the user. All the user code needs to do is to fill the buffer as needed and the display routine will update the buffer automagically.

dxw00d

@dhenry - Complete overkill for the requirement, and there are already libraries that do that, including the one I linked to earlier.

arduinohabib


I will give you a skeleton:

0) define a bunch of macros to turn on / off segments / digits. Those macros are Common Cathod or Common Anode aware.
1) use a routine to initialize all pins.
2) write a code that upon each call, display one digit from a buffer, and rolls over to the next.
3) set up a timer, pointer a function pointer to the routine in 2) above.
4) in the timer isr, execute the function pointer (now pointing to the routine in 2)).

Once that is up, the entire display function is transparent to the user. All the user code needs to do is to fill the buffer as needed and the display routine will update the buffer automagically.


Thank you so much @dhenry! Thank you for the support! I will go by what you said!
What is man's best friend? The breadboard!

fungus



I will give you a skeleton:

0) define a bunch of macros to turn on / off segments / digits. Those macros are Common Cathod or Common Anode aware.
1) use a routine to initialize all pins.
2) write a code that upon each call, display one digit from a buffer, and rolls over to the next.
3) set up a timer, pointer a function pointer to the routine in 2) above.
4) in the timer isr, execute the function pointer (now pointing to the routine in 2)).

Once that is up, the entire display function is transparent to the user. All the user code needs to do is to fill the buffer as needed and the display routine will update the buffer automagically.


Thank you so much @dhenry! Thank you for the support! I will go by what you said!


No...that's way too complex for what's needed.

The library can be improved but there's no need for interrupts or anything like that.
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

CrossRoads

How about doing it with direct port manipulation, speed it up quite a bit.
Or define it to drive the digits from an array:
Code: [Select]

display[] = {
B00111111, // 0
B00000110, // 1
B01011011, // 2
B01001111, // 3
B01100110, // 4
B01101101, // 5
B01111011, // 6
B00000111, // 7
B01111111, // 8
B01101111, // 9
}
// write the digits, assume PORTD with bit0 = asegment, 1=b,2=c,3=d,4=e,5-f,6=g, 7=decimal point it used
//    a
// f    b
//    g
// e   c
//    d
// clear the bits
PORTD = PORTD & 0x00
// now set the font
PORTD = display[x]; // x = 0 to 9, or bigger if define some other characters: A,b,C,d,E,F,H,L,P,U, -,_,


and do the library  stuff.
That would make it real quick!
Or share the 8 bits across 2 ports if you didn't want to commit D0,D1 to LED driver (serial pins).
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up