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
- // 8segment anode led test
- #include <stdio.h>
- #include <stdlib.h>
- #include <wiringPi.h>
- #include <string.h>
- #include <stdint.h>
- #define Pins 8
- #define P0 0
- #define Digs 3
- #define D0 10
- static const uint8_t segmentDigits [] =
- {
- // a b c d e f g p Segments
- // 0 1 2 3 4 5 6 7 wiringPi pin No.
- 1, 1, 1, 1, 1, 1, 0, 0, // 0
- 0, 1, 1, 0, 0, 0, 0, 0, // 1
- 1, 1, 0, 1, 1, 0, 1, 0, // 2
- 1, 1, 1, 1, 0, 0, 1, 0, // 3
- 0, 1, 1, 0, 0, 1, 1, 0, // 4
- 1, 0, 1, 1, 0, 1, 1, 0, // 5
- 1, 0, 1, 1, 1, 1, 1, 0, // 6
- 1, 1, 1, 0, 0, 0, 0, 0, // 7
- 1, 1, 1, 1, 1, 1, 1, 0, // 8
- 1, 1, 1, 1, 0, 1, 1, 0, // 9
- 1, 1, 1, 0, 1, 1, 1, 0, // A
- 0, 0, 1, 1, 1, 1, 1, 0, // b
- 1, 0, 0, 1, 1, 1, 0, 0, // C
- 0, 1, 1, 1, 1, 0, 1, 0, // d
- 1, 0, 0, 1, 1, 1, 1, 0, // E
- 1, 0, 0, 0, 1, 1, 1, 0, // F
- 0, 0, 0, 0, 0, 0, 0, 0, // blank
- } ;
-
- DisplayDigits (char* digits)
- {
- int i;
- uint8_t digit, segment ;
- uint8_t index, d, segVal ;
- for (digit = 0 ; digit < Digs; ++digit) {
- digitalWrite (D0 + digit, 1) ;
- d = toupper (digits [digit]) ;
- if ((d >= '0') && (d <= '9')) // Digit
- index = d - '0' ;
- else if ((d >= 'A') && (d <= 'F')) // Hex
- index = d - 'A' + 10 ;
- else
- index = 16 ; // Blank
- for (segment = 0 ; segment < 8 ; ++segment) {
- segVal = segmentDigits [index * 8 + segment] ;
- digitalWrite (segment+P0, !segVal) ;
- //when add delay,you can understand how to work
- // delay(1000);
- delayMicroseconds (100) ;
- digitalWrite (segment+P0, 1) ;
- }
- digitalWrite (D0 + digit, 0) ;
- }
- }
- int main (void)
- {
- int i;
- char digits [8] ;
- if(wiringPiSetup() == -1) return 1;
- for(i=0;i<Pins;i++) {
- pinMode(P0+i, OUTPUT);
- digitalWrite(P0+i, 0);
- }
- for(i=0;i<Digs;i++) {
- pinMode(D0+i, OUTPUT);
- digitalWrite(D0+i, 0);
- }
- strcpy(digits,"123");
- for(i=0;i<3000;i++) DisplayDigits(digits);
- strcpy(digits,"ABC");
- for(i=0;i<3000;i++) DisplayDigits(digits);
- for(i=0;i<Pins;i++) {
- digitalWrite(P0+i, 0);
- }
- for(i=0;i<Digs;i++) {
- digitalWrite(D0+i, 0);
- }
- exit;
- }