Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

7-SEGMENT led display function optimisation

Status
Not open for further replies.

BiDoU

Newbie level 6
Joined
Jan 23, 2005
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
121
Hello,

I made this function maybe rapidly, but I don't know if it's possible to optimise it. Because it takes a lot of program memory data... (like 200 bytes).

It's for a PIC16Fxxx...

I think it's the basic form for this function. The using of this function is like vAffichage('7');



Code:
#include <pic.h>

#define SEGMENT_A RB0
#define SEGMENT_B RB1
#define SEGMENT_C RB2
#define SEGMENT_D RB3
#define SEGMENT_E RB4
#define SEGMENT_F RB5
#define SEGMENT_G RB6

#define ON 1
#define OFF 0

void vAffichage(unsigned char ucCharacter)
{
  switch(ucCharacter)
  {
     case '0':
     case 'O':
          SEGMENT_A = ON;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = ON;
          SEGMENT_E = ON;
          SEGMENT_F = ON;
          SEGMENT_G = OFF;
          break;
     case '1':
     case 'l':
          SEGMENT_A = OFF;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = OFF;
          SEGMENT_E = OFF;
          SEGMENT_F = OFF;
          SEGMENT_G = OFF;
          break;
     case '2':
          SEGMENT_A = ON;
          SEGMENT_B = ON;
          SEGMENT_C = OFF;
          SEGMENT_D = ON;
          SEGMENT_E = ON;
          SEGMENT_F = OFF;
          SEGMENT_G = ON;
          break;
     case '3':
          SEGMENT_A = ON;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = ON;
          SEGMENT_E = OFF;
          SEGMENT_F = OFF;
          SEGMENT_G = ON;
          break;
     case '4':
          SEGMENT_A = OFF;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = OFF;
          SEGMENT_E = OFF;
          SEGMENT_F = ON;
          SEGMENT_G = ON;
          break;
     case '5':
     case 'S':
          SEGMENT_A = ON;
          SEGMENT_B = OFF;
          SEGMENT_C = ON;
          SEGMENT_D = ON;
          SEGMENT_E = OFF;
          SEGMENT_F = ON;
          SEGMENT_G = ON;
          break;
     case '6':
          SEGMENT_A = ON;
          SEGMENT_B = OFF;
          SEGMENT_C = ON;
          SEGMENT_D = ON;
          SEGMENT_E = ON;
          SEGMENT_F = ON;
          SEGMENT_G = ON;
          break;
     case '7':
          SEGMENT_A = ON;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = OFF;
          SEGMENT_E = OFF;
          SEGMENT_F = OFF;
          SEGMENT_G = OFF;
          break;
     case '8':
          SEGMENT_A = ON;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = ON;
          SEGMENT_E = ON;
          SEGMENT_F = ON;
          SEGMENT_G = ON;
          break;
     case '9':
          SEGMENT_A = ON;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = OFF;
          SEGMENT_E = OFF;
          SEGMENT_F = ON;
          SEGMENT_G = ON;
          break;
     case 'd':
          SEGMENT_A = OFF;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = ON;
          SEGMENT_E = ON;
          SEGMENT_F = OFF;
          SEGMENT_G = ON;
          break;
     case 'o':
          SEGMENT_A = OFF;
          SEGMENT_B = OFF;
          SEGMENT_C = ON;
          SEGMENT_D = ON;
          SEGMENT_E = ON;
          SEGMENT_F = OFF;
          SEGMENT_G = ON;
          break;
     case 'A':
          SEGMENT_A = ON;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = OFF;
          SEGMENT_E = ON;
          SEGMENT_F = ON;
          SEGMENT_G = ON;
          break;
     case 'C':
          SEGMENT_A = ON;
          SEGMENT_B = OFF;
          SEGMENT_C = OFF;
          SEGMENT_D = ON;
          SEGMENT_E = ON;
          SEGMENT_F = ON;
          SEGMENT_G = OFF;
          break;
     case 'F':
          SEGMENT_A = ON;
          SEGMENT_B = OFF;
          SEGMENT_C = OFF;
          SEGMENT_D = OFF;
          SEGMENT_E = ON;
          SEGMENT_F = ON;
          SEGMENT_G = ON;
          break;
     case 'H':
          SEGMENT_A = OFF;
          SEGMENT_B = ON;
          SEGMENT_C = ON;
          SEGMENT_D = OFF;
          SEGMENT_E = ON;
          SEGMENT_F = ON;
          SEGMENT_G = ON;
          break;
     case 'L':
          SEGMENT_A = OFF;
          SEGMENT_B = OFF;
          SEGMENT_C = OFF;
          SEGMENT_D = ON;
          SEGMENT_E = ON;
          SEGMENT_F = ON;
          SEGMENT_G = OFF;
          break;
     case 'P':
          SEGMENT_A = ON;
          SEGMENT_B = ON;
          SEGMENT_C = OFF;
          SEGMENT_D = OFF;
          SEGMENT_E = ON;
          SEGMENT_F = ON;
          SEGMENT_G = ON;
          break;
     case '_':
          SEGMENT_A = OFF;
          SEGMENT_B = OFF;
          SEGMENT_C = OFF;
          SEGMENT_D = ON;
          SEGMENT_E = OFF;
          SEGMENT_F = OFF;
          SEGMENT_G = OFF;
          break;                                                      
  }//Fin switch(ucCharacter).

}
 

For 8 led displays:
It is much better to use one byte with 1 at one bit and 0 at other 7 bits.
00000001
00000010
00000100
00001000
00010000
00100000
01000000
10000000
00000001
...

In every switch instruction just rotate this byte, and put it on PORT which turns on displays.
 

switch is usually optimized using array.
7 segments means 7 bits which fits into 1 byte.
you can declare the array of bytes and store it in ROM.
 

Try this code.


#define SEG_A 0
#define SEG_B 1
#define SEG_C 2
#define SEG_D 3
#define SEG_E 4
#define SEG_F 5
#define SEG_G 6

#define ON 1
#define OFF 0

#define cod_0 (ON<<SEG_A) |(ON<<SEG_B) |(ON<<SEG_C) |(ON<<SEG_D) |(ON<<SEG_E) |(ON<<SEG_F) |(OFF<<SEG_G)
#define cod_1 (OFF<<SEG_A)|(ON<<SEG_B) |(ON<<SEG_C) |(OFF<<SEG_D)|(OFF<<SEG_E)|(OFF<<SEG_F)|(OFF<<SEG_G)
#define cod_2 (ON<<SEG_A) |(ON<<SEG_B) |(OFF<<SEG_C)|(ON<<SEG_D) |(ON<<SEG_E) |(OFF<<SEG_F)|(ON<<SEG_G)
#define cod_3 (ON<<SEG_A) |(ON<<SEG_B) |(ON<<SEG_C) |(ON<<SEG_D) |(OFF<<SEG_E)|(OFF<<SEG_F)|(ON<<SEG_G)
#define cod_4 (OFF<<SEG_A)|(ON<<SEG_B) |(ON<<SEG_C) |(OFF<<SEG_D)|(OFF<<SEG_E)|(ON<<SEG_F) |(ON<<SEG_G)
#define cod_5 (ON<<SEG_A) |(OFF<<SEG_B)|(ON<<SEG_C) |(ON<<SEG_D) |(OFF<<SEG_E)|(ON<<SEG_F) |(ON<<SEG_G)
#define cod_6 (ON<<SEG_A) |(OFF<<SEG_B)|(ON<<SEG_C) |(ON<<SEG_D) |(ON<<SEG_E) |(ON<<SEG_F) |(ON<<SEG_G)
#define cod_7 (ON<<SEG_A) |(ON<<SEG_B) |(ON<<SEG_C) |(OFF<<SEG_D)|(OFF<<SEG_E)|(OFF<<SEG_F)|(OFF<<SEG_G)
#define cod_8 (ON<<SEG_A) |(ON<<SEG_B) |(ON<<SEG_C) |(ON<<SEG_D) |(ON<<SEG_E) |(ON<<SEG_F) |(ON<<SEG_G)
#define cod_9 (ON<<SEG_A) |(ON<<SEG_B) |(ON<<SEG_C) |(OFF<<SEG_D)|(OFF<<SEG_E)|(ON<<SEG_F) |(ON<<SEG_G)
#define cod_A (ON<<SEG_A) |(ON<<SEG_B) |(ON<<SEG_C) |(OFF<<SEG_D)|(ON<<SEG_E) |(ON<<SEG_F) |(ON<<SEG_G)
#define cod_C (ON<<SEG_A) |(OFF<<SEG_B)|(OFF<<SEG_C)|(ON<<SEG_D) |(ON<<SEG_E) |(ON<<SEG_F) |(OFF<<SEG_G)
#define cod_d (OFF<<SEG_A)|(ON<<SEG_B) |(ON<<SEG_C) |(ON<<SEG_D) |(ON<<SEG_E) |(OFF<<SEG_F)|(ON<<SEG_G)
#define cod_F (ON<<SEG_A) |(OFF<<SEG_B)|(OFF<<SEG_C)|(OFF<<SEG_D)|(ON<<SEG_E) |(ON<<SEG_F) |(ON<<SEG_G)
#define cod_o (OFF<<SEG_A)|(OFF<<SEG_B)|(ON<<SEG_C) |(ON<<SEG_D) |(ON<<SEG_E) |(OFF<<SEG_F)|(ON<<SEG_G)
#define cod_H (OFF<<SEG_A)|(ON<<SEG_B) |(ON<<SEG_C) |(OFF<<SEG_D)|(ON<<SEG_E) |(ON<<SEG_F) |(ON<<SEG_G)
#define cod_L (OFF<<SEG_A)|(OFF<<SEG_B)|(OFF<<SEG_C)|(ON<<SEG_D) |(ON<<SEG_E) |(ON<<SEG_F) |(OFF<<SEG_G)
#define cod_P (ON<<SEG_A) |(ON<<SEG_B) |(OFF<<SEG_C)|(OFF<<SEG_D)|(ON<<SEG_E) |(ON<<SEG_F) |(ON<<SEG_G)
#define cod__ (OFF<<SEG_A)|(OFF<<SEG_B)|(OFF<<SEG_C)|(ON<<SEG_D) |(OFF<<SEG_E)|(OFF<<SEG_F)|(OFF<<SEG_G)

void vAffichage_ver2(unsigned char ucCharacter)
{
switch(ucCharacter)
{
case '0':
case 'O':
PORTB = cod_0;
break;
case '1':
case 'l':
PORTB = cod_1;
break;
case '2':
PORTB = cod_2;
break;
case '3':
PORTB = cod_3;
break;
case '4':
PORTB = cod_4;
break;
case '5':
case 'S':
PORTB = cod_5;
break;
case '6':
PORTB = cod_6;
break;
case '7':
PORTB = cod_7;
break;
case '8':
PORTB = cod_8;
break;
case '9':
PORTB = cod_9;
break;
case 'd':
PORTB = cod_d;
break;
case 'o':
PORTB = cod_o;
break;
case 'A':
PORTB = cod_A;
break;
case 'C':
PORTB = cod_C;
break;
case 'F':
PORTB = cod_F;
break;
case 'H':
PORTB = cod_H;
break;
case 'L':
PORTB = cod_L;
break;
case 'P':
PORTB = cod_P;
break;
case '_':
PORTB = cod__;
break;
}
}


Added after 13 minutes:

banh said:
switch is usually optimized using array.
7 segments means 7 bits which fits into 1 byte.
you can declare the array of bytes and store it in ROM.

in this case needs produce a address for array.
 

    BiDoU

    Points: 2
    Helpful Answer Positive Rating
Hi,
How about this:

Code:
constant Zero  : std_logic_vector(6 downto 0) := "1000000"; 
constant One   : std_logic_vector(6 downto 0) := "1111001"; 
constant Two   : std_logic_vector(6 downto 0) := "0100100"; 
constant Tree  : std_logic_vector(6 downto 0) := "0110000"; 
constant Four  : std_logic_vector(6 downto 0) := "0011001"; 
constant Five  : std_logic_vector(6 downto 0) := "0010010";
constant Six   : std_logic_vector(6 downto 0) := "0000010";
constant Seven : std_logic_vector(6 downto 0) := "1111000";
constant Eight : std_logic_vector(6 downto 0) := "0000000";
constant Nine  : std_logic_vector(6 downto 0) := "0010000";
--constant Ten : std_logic_vector(6 downto 0) := "0001000";
--constant Eleven    : std_logic_vector(6 downto 0) := "0000011";
--constant Twelve    : std_logic_vector(6 downto 0) := "1000110";
--constant Thirteen  : std_logic_vector(6 downto 0) := "0100001";
--constant Fourteen  : std_logic_vector(6 downto 0) := "0000110";
--constant Fifteen   : std_logic_vector(6 downto 0) := "0001110";
And inside your code something like this:
Code:
		case SevenSeg_out is
				when Zero 	=> SevenSeg_out <= One;
				when One 	=> SevenSeg_out <= Two;
				when Two 	=> SevenSeg_out <= Three;
				when Three 	=> SevenSeg_out <= Four;
				when Four 	=> SevenSeg_out <= Five;
				when Five	=> SevenSeg_out <= Six;
				when Six 	=> SevenSeg_out <= Seven;
				when Seven 	=> SevenSeg_out <= Eight;
				when Eight 	=> SevenSeg_out <= Nine;
				when Nine 	=> SevenSeg_out <= Zero;
				when others 	=> SevenSeg_out <= Zero;
			end case;

You declare SevenSegOut as a standard logic vector (6 downoto 0), pretty simple.

BR,
/Farhad
[/code]
 

cmos babe said:
Why don't you use a look up table?

Because the table will be stored in RAM memory and I have only 128 bytes for RAM...

***

I tried all solution here and it's down the memory code space by half. Thank you very much everybody!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top