View: 3949|Reply: 2

Using a 7 segment 4 digit display with OP

[Copy link]

2

threads

3

posts

40

credits

Novice

Rank: 1

credits
40
Published in 2017-3-11 05:33:35 | Show all floors |Read mode
Edited by AFT at 2017-3-11 05:36

Hey Guys,

I've got a 7 segment 4 digit display (Common anode configuration), and I want to use the Orange Pi to drive it. I've got the wiringPi installed and I'm using Python.

Are there any tutorials for a beginner like me?

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2017-3-12 07:18:32 | Show all floors
Edited by nopnop2002 at 2017-3-12 07:24

It'll be as follows by a C language.
You must be change segmentDigits according to the device you use.

example for 8 segments 3 digits.

common pin : GPIO10-12
segment led pin : GPIO0-7

  1. // 8segment anode led test
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <wiringPi.h>
  5. #include <string.h>
  6. #include <stdint.h>

  7. #define Pins    8
  8. #define P0      0
  9. #define Digs    3
  10. #define D0      10

  11. static const uint8_t segmentDigits [] =
  12. {
  13. // a  b  c  d  e  f  g  p    Segments
  14. // 0  1  2  3  4  5  6  7    wiringPi pin No.
  15.    1, 1, 1, 1, 1, 1, 0, 0,    // 0
  16.    0, 1, 1, 0, 0, 0, 0, 0,    // 1
  17.    1, 1, 0, 1, 1, 0, 1, 0,    // 2
  18.    1, 1, 1, 1, 0, 0, 1, 0,    // 3
  19.    0, 1, 1, 0, 0, 1, 1, 0,    // 4
  20.    1, 0, 1, 1, 0, 1, 1, 0,    // 5
  21.    1, 0, 1, 1, 1, 1, 1, 0,    // 6
  22.    1, 1, 1, 0, 0, 0, 0, 0,    // 7
  23.    1, 1, 1, 1, 1, 1, 1, 0,    // 8
  24.    1, 1, 1, 1, 0, 1, 1, 0,    // 9
  25.    1, 1, 1, 0, 1, 1, 1, 0,    // A
  26.    0, 0, 1, 1, 1, 1, 1, 0,    // b
  27.    1, 0, 0, 1, 1, 1, 0, 0,    // C
  28.    0, 1, 1, 1, 1, 0, 1, 0,    // d
  29.    1, 0, 0, 1, 1, 1, 1, 0,    // E
  30.    1, 0, 0, 0, 1, 1, 1, 0,    // F
  31.    0, 0, 0, 0, 0, 0, 0, 0,    // blank
  32. } ;


  33. DisplayDigits (char* digits)
  34. {
  35.     int i;
  36.     uint8_t digit, segment ;
  37.     uint8_t index, d, segVal ;

  38.     for (digit = 0 ; digit < Digs; ++digit) {
  39.       digitalWrite (D0 + digit, 1) ;
  40.       d = toupper (digits [digit]) ;
  41.       if ((d >= '0') && (d <= '9'))           // Digit
  42.       index = d - '0' ;
  43.       else if ((d >= 'A') && (d <= 'F'))    // Hex
  44.       index = d - 'A' + 10 ;
  45.       else
  46.       index = 16 ;                             // Blank
  47.       for (segment = 0 ; segment < 8 ; ++segment) {
  48.         segVal = segmentDigits [index * 8 + segment] ;
  49.         digitalWrite (segment+P0, !segVal) ;
  50. //when add delay,you can understand how to work
  51. //        delay(1000);
  52.         delayMicroseconds (100) ;
  53.         digitalWrite (segment+P0, 1) ;
  54.       }
  55.       digitalWrite (D0 + digit, 0) ;
  56.     }
  57. }

  58. int main (void)
  59. {
  60.     int i;
  61.     char digits [8] ;

  62.     if(wiringPiSetup() == -1) return 1;
  63.     for(i=0;i<Pins;i++) {
  64.         pinMode(P0+i, OUTPUT);
  65.         digitalWrite(P0+i, 0);
  66.     }
  67.     for(i=0;i<Digs;i++) {
  68.         pinMode(D0+i, OUTPUT);
  69.         digitalWrite(D0+i, 0);
  70.     }

  71.     strcpy(digits,"123");
  72.     for(i=0;i<3000;i++) DisplayDigits(digits);

  73.     strcpy(digits,"ABC");
  74.     for(i=0;i<3000;i++) DisplayDigits(digits);

  75.     for(i=0;i<Pins;i++) {
  76.         digitalWrite(P0+i, 0);
  77.     }
  78.     for(i=0;i<Digs;i++) {
  79.         digitalWrite(D0+i, 0);
  80.     }
  81.     exit;
  82. }
Copy code



2

threads

3

posts

40

credits

Novice

Rank: 1

credits
40
 Author| Published in 2017-3-15 00:48:14 | Show all floors
nopnop2002 replied at 2017-3-12 07:18
It'll be as follows by a C language.
You must be change segmentDigits according to the device you us ...

Thanks, Your code worked. I found another one in python

  1. from pyA20.gpio import gpio
  2. from pyA20.gpio import port
  3. from time import sleep
  4. import time

  5. gpio.init()

  6. segments =  (port.PA12,port.PA6,port.PA8,port.PA11,port.PC3,port.PA21,port.PC1,port.PA7)

  7. for segment in segments:
  8.     gpio.setcfg(segment, gpio.OUTPUT)
  9.     gpio.output(segment, 1)

  10. digits = (port.PG8,port.PC0,port.PC2,port.PA9)

  11. for digit in digits:
  12.     gpio.setcfg(digit, gpio.OUTPUT)
  13.     gpio.output(digit, 0)

  14. num = {' ':(1,1,1,1,1,1,1,1),
  15.     '0':(0,0,1,0,1,0,0,0),
  16.     '1':(1,1,1,0,1,0,1,1),
  17.     '2':(0,0,1,1,0,0,1,0),
  18.     '3':(1,0,1,0,0,0,1,0),
  19.     '4':(1,1,1,0,0,0,0,1),
  20.     '5':(1,0,1,0,0,1,0,0),
  21.     '6':(0,0,1,0,0,1,0,0),
  22.     '7':(1,1,1,0,1,0,0,0),
  23.     '8':(0,0,1,0,0,0,0,0),
  24.     '9':(1,0,1,0,0,0,0,0)}

  25. try:
  26.     while True:
  27.         n = time.ctime()[11:13]+time.ctime()[14:16]
  28.         s = str(n).rjust(4)
  29.         for digit in range(4):
  30.             for loop in range(0,8):
  31.                 gpio.output(segments[loop], num[s[digit]][loop])
  32.                 if (int(time.ctime()[18:19])%2 == 0) and (digit == 1):
  33.                     gpio.output(port.PA8, 0)
  34.                 else:
  35.                     gpio.output(port.PA8, 1)
  36.             gpio.output(digits[digit], 1)
  37.             time.sleep(0.005)
  38.             gpio.output(digits[digit], 0)
  39.                        
  40. finally:
  41.     print("done")
Copy code
This one shows time on the screen.

You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list