Having Trouble With 7 Segment Display

Thread Starter

vsvinit0

Joined Dec 7, 2016
14
Thank you every one [ #dl324, #dannyf, #shteii01, #hp1729, #ScottWang ]. I finally managed to run my CA mode 7 segment display. Upon reading your replies i figured it out on how to drive a CA mode 7 segment display.

Here's my code although its not good and optimise but would appreciate others if they help me to optimise this code.

Code:
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(3, GPIO.OUT)
GPIO.setup(31, GPIO.OUT)    # PIN-a
GPIO.setup(32, GPIO.OUT)    # PIN-b
GPIO.setup(33, GPIO.OUT)    # PIN-c
GPIO.setup(35, GPIO.OUT)    # PIN-d
GPIO.setup(36, GPIO.OUT)    # PIN-e
GPIO.setup(37, GPIO.OUT)    # PIN-f
GPIO.setup(38, GPIO.OUT)    # PIN-g
GPIO.setup(40, GPIO.OUT)    # PIN-h

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.HIGH)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.HIGH)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.HIGH)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.HIGH)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.HIGH)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(32, GPIO.LOW)
GPIO.output(31, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(32, GPIO.HIGH)
GPIO.output(31, GPIO.HIGH)
GPIO.output(33, GPIO.HIGH)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.LOW)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(32, GPIO.LOW)
GPIO.output(31, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(32, GPIO.LOW)
GPIO.output(31, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.HIGH)
GPIO.output(33, GPIO.HIGH)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(32, GPIO.LOW)
GPIO.output(31, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.HIGH)
GPIO.output(33, GPIO.HIGH)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.HIGH)
GPIO.output(33, GPIO.HIGH)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.LOW)
GPIO.output(32, GPIO.HIGH)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.HIGH)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.HIGH)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.HIGH)
GPIO.output(32, GPIO.LOW)
GPIO.output(33, GPIO.LOW)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.HIGH)

time.sleep(1.5)

GPIO.output(3, GPIO.HIGH)
GPIO.output(31, GPIO.HIGH)
GPIO.output(32, GPIO.HIGH)
GPIO.output(33, GPIO.HIGH)
GPIO.output(35, GPIO.HIGH)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.LOW)

time.sleep(1.5)

GPIO.cleanup()
Links to demo video :

 

dannyf

Joined Sep 13, 2015
2,197
when I write code, I write it to a logic device: rather than set pin 31 to turn on a segment a, I turn on segment a and however segment a may be defined. in the future, if I were to move the pins around, or go from CC to CA, all I need is to redefine the segments and may code will work -> this allows the human to focus on more value-add work and the computer / compiler on the dumber stuff.

For example, for your code, I would do the following, in C:

Code:
#define SEG_A 31 //seg_a on pin 31
#define SEG_B 32 //seg_b on pin 32
...
all of your code from this point onward reference to SEG_A, SEG_B, ... instead of the specific pins

I would also define a set of macros to turn on / off a segment, depending on if the display is CC or CA
Code:
#define DISPLAY_CC  //define if display is CC. comment out if CA

#if defined(DISPLAY_CC)
#define SEG_ON(seg)  GPIO.output(seg, HIGH) //turn on a CC segment by setting it
#define SEG_OFF(seg) GPIO.output(seg, LOW) //turn off a CC segment by clearing it
#else
#define SEG_ON(seg) GPIO.output(seg, LOW) //turn on a CA segment by clearing it
#define SEG_OFF(seg) GPIO.output(seg, HIGH) //turn off a CA segment by setting it
#endif
So now you can use SEG_ON() or SEG_OFF() to turn on / off a segment,without needing to know if it is CC or CA.

For your display, I would write a routine that does it, with a parameter indicating the number to be displayed:

Code:
void led7_display(num0_9) {
  //for '0', turn on seg_a..f, turn off all others
  if (num0_9 == 0) {SEG_ON(SEG_A); SEG_ON(SEG_B); SEG_ON(SEG_C); SEG_ON(SEG_D); SEG_ON(SEG_E); SEG_ON(SEG_F); SEG_OFF(SEG_G);}
  //for '1', turn on seg_b, c, turn off all others
  if (num0_9 == 1) {SEG_OFF(SEG_A); SEG_ON(SEG_B); SEG_ON(SEG_C); SEG_OFF(SEG_D); SEG_OFF(SEG_E); SEG_OFF(SEG_F); SEG_OFF(SEG_G);}
...
all your application code does is to pass a number to it and you will know that it will be correctly displayed
Code:
//user code
  led7_display(0); //display '0';
  sleep();
  led7_displa(1); //display '1';
  sleep();
  ...
this is basically a layered approach: it turns a complex job into highly stylized simplified layers each dealing with a specific set of tasks and inputs. Once you have all the layers up, your code is a naturally working piece, and all you need to worry about is often the highest layer: what it does and what inputs it takes. How it does it, after the code has been written, is often less important.

with this approach, you have a piece of code that you can use and reuse ***reliably*** for future programs. So each piece of code you write is an investment, not an expenditure.
 

Thread Starter

vsvinit0

Joined Dec 7, 2016
14
when I write code, I write it to a logic device: rather than set pin 31 to turn on a segment a, I turn on segment a and however segment a may be defined. in the future, if I were to move the pins around, or go from CC to CA, all I need is to redefine the segments and may code will work -> this allows the human to focus on more value-add work and the computer / compiler on the dumber stuff.

For example, for your code, I would do the following, in C:

Code:
#define SEG_A 31 //seg_a on pin 31
#define SEG_B 32 //seg_b on pin 32
...
all of your code from this point onward reference to SEG_A, SEG_B, ... instead of the specific pins

I would also define a set of macros to turn on / off a segment, depending on if the display is CC or CA
Code:
#define DISPLAY_CC  //define if display is CC. comment out if CA

#if defined(DISPLAY_CC)
#define SEG_ON(seg)  GPIO.output(seg, HIGH) //turn on a CC segment by setting it
#define SEG_OFF(seg) GPIO.output(seg, LOW) //turn off a CC segment by clearing it
#else
#define SEG_ON(seg) GPIO.output(seg, LOW) //turn on a CA segment by clearing it
#define SEG_OFF(seg) GPIO.output(seg, HIGH) //turn off a CA segment by setting it
#endif
So now you can use SEG_ON() or SEG_OFF() to turn on / off a segment,without needing to know if it is CC or CA.

For your display, I would write a routine that does it, with a parameter indicating the number to be displayed:

Code:
void led7_display(num0_9) {
  //for '0', turn on seg_a..f, turn off all others
  if (num0_9 == 0) {SEG_ON(SEG_A); SEG_ON(SEG_B); SEG_ON(SEG_C); SEG_ON(SEG_D); SEG_ON(SEG_E); SEG_ON(SEG_F); SEG_OFF(SEG_G);}
  //for '1', turn on seg_b, c, turn off all others
  if (num0_9 == 1) {SEG_OFF(SEG_A); SEG_ON(SEG_B); SEG_ON(SEG_C); SEG_OFF(SEG_D); SEG_OFF(SEG_E); SEG_OFF(SEG_F); SEG_OFF(SEG_G);}
...
all your application code does is to pass a number to it and you will know that it will be correctly displayed
Code:
//user code
  led7_display(0); //display '0';
  sleep();
  led7_displa(1); //display '1';
  sleep();
  ...
this is basically a layered approach: it turns a complex job into highly stylized simplified layers each dealing with a specific set of tasks and inputs. Once you have all the layers up, your code is a naturally working piece, and all you need to worry about is often the highest layer: what it does and what inputs it takes. How it does it, after the code has been written, is often less important.

with this approach, you have a piece of code that you can use and reuse ***reliably*** for future programs. So each piece of code you write is an investment, not an expenditure.

Thanks for the help #dannyf .... I got the base idea. Will try to improve my code and as a beginner i don't have much knowledge in programming but will try to cover each and every point of your explanation. Most of the time i was writing some procedural approach programs but now at least i got some idea on where to focus and what to learn. Once again thanks for the help.
 

dannyf

Joined Sep 13, 2015
2,197
I got the base idea.
with that, you will do well.

the biggest mistake I have seen people making is that they invest heavily in application-specific code. as their code is so tightly integrated with their hardware, their investments in working with an adc chip, or a lcd display, buttons, or timers, uarts, etc., cannot be reused in future projects. As such, if they start to use the same adc chip, or the same display, etc., they have to rewrite the same code again, and to debug them again.

I have worked on many chips in my years and i have a large collection of code that I wrote over the years. Many times, I can drop those code pieces in a new project, redefine some pins or behaviors, and upon recompile, I know with high confidence that my code will work, because I have debugged those code pieces in prior projects.

once you start to work in a corporate environment, you will find the same approach and it will be natural to you.
 

Thread Starter

vsvinit0

Joined Dec 7, 2016
14
with that, you will do well.

the biggest mistake I have seen people making is that they invest heavily in application-specific code. as their code is so tightly integrated with their hardware, their investments in working with an adc chip, or a lcd display, buttons, or timers, uarts, etc., cannot be reused in future projects. As such, if they start to use the same adc chip, or the same display, etc., they have to rewrite the same code again, and to debug them again.

I have worked on many chips in my years and i have a large collection of code that I wrote over the years. Many times, I can drop those code pieces in a new project, redefine some pins or behaviors, and upon recompile, I know with high confidence that my code will work, because I have debugged those code pieces in prior projects.

once you start to work in a corporate environment, you will find the same approach and it will be natural to you.
Ya you are right although i am quite beginner in this field. Yet to explore a lot and i love the way you guys have replied to my problem. Although i am getting your code but still have to learn a lot. Will soon reply with my new code most of the time i read your replies twice or trice coz there's a lot of details in your replies. If you don't mind can you add me in your friend list i wanted to explore your profile so that i can learn more and because of your privacy setting i am not able to view your profile.
 
Top