Universal code for seven segs:

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I had a device, in which a 7seg display was connected to a single port. But all mixed up because of the ideal PCB design. I spent a little time making up constants.
Then, another project, another wiring, and I couldn't use my old constant definitions of characters. So I decided to make this.

If you have a seven seg display connected to one port than this is the library for you. in the first section you simply define which bitnumber is which segment and then you can simply take values from the constant array segs[].

segs[0] will be the image of a 0 character as is on a seven seg. display. and so on. This library contains values from 0 to F (hex).

Feel free to use it.

#define _s_A  2
#define _s_B  0
#define _s_C  6
#define _s_D  4
#define _s_E  3
#define _s_F  1
#define _s_G  7
#define _s_dot 5  //here you add which segment is which pin of the port

const unsigned char segs[] =
{
	_BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F),	//0
	_BV(_s_B) | _BV(_s_C),	//1
	_BV(_s_A) | _BV(_s_B) | _BV(_s_D) | _BV(_s_E) | _BV(_s_G),	//2
	_BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_D) | _BV(_s_G),  //3
	_BV(_s_B) | _BV(_s_C) | _BV(_s_F) | _BV(_s_G),	//4
	_BV(_s_A) | _BV(_s_C) | _BV(_s_D) | _BV(_s_F) | _BV(_s_G), //5
	_BV(_s_A) | _BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G), //6
	_BV(_s_A) | _BV(_s_B) | _BV(_s_C), //7
	_BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G),//8
	_BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_F) | _BV(_s_G),//9
	
	_BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G),	//A
	_BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G),	//B
	_BV(_s_A) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F), //C
	_BV(_s_B) | _BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_G), //D
	_BV(_s_A) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G), //E
	_BV(_s_A) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G) //F

}

(the 'font' was referenced from wikipedia)
PS: Leon, sorry if this is offtopic.

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

more effective not to use constants in memory, but define them through 'define' :

#define i7s_allon  (0) 
#define i7s_alloff (0xff)
#define i7s_0 (i7s_sa & i7s_sb & i7s_sc & i7s_sd & i7s_se & i7s_sf)
#define i7s_1 (i7s_sb & i7s_sc)                            
#define i7s_2 (i7s_sa & i7s_sb & i7s_sd & i7s_se & i7s_sg)
#define i7s_3 (i7s_sa & i7s_sb & i7s_sc & i7s_sd & i7s_sg)
#define i7s_4 (i7s_sb & i7s_sc & i7s_sf & i7s_sg)         
#define i7s_5 (i7s_sa & i7s_sc & i7s_sd & i7s_sf & i7s_sg)
#define i7s_6 (i7s_sa & i7s_sc & i7s_sd & i7s_se & i7s_sf & i7s_sg)
#define i7s_7 (i7s_sa & i7s_sb & i7s_sc)                   
#define i7s_8 (i7s_sa & i7s_sb & i7s_sc & i7s_sd & i7s_se & i7s_sf & i7s_sg)
#define i7s_9 (i7s_sa & i7s_sb & i7s_sc & i7s_sd & i7s_sf & i7s_sg)
#define i7s_A (i7s_sa & i7s_sb & i7s_sc & i7s_se & i7s_sf & i7s_sg)
#define i7s_b (i7s_sc & i7s_sd & i7s_se & i7s_sf & i7s_sg)
#define i7s_C (i7s_sa & i7s_sd & i7s_se & i7s_sf)          
#define i7s_c (i7s_sd & i7s_se & i7s_sg)                   
#define i7s_d (i7s_sb & i7s_sc & i7s_sd & i7s_se & i7s_sg)
#define i7s_E (i7s_sa & i7s_sd & i7s_se & i7s_sf & i7s_sg)
#define i7s_F (i7s_sa & i7s_se & i7s_sf & i7s_sg)          
#define i7s_H (i7s_sb & i7s_sc & i7s_se & i7s_sf & i7s_sg)
#define i7s_h (i7s_sc & i7s_se & i7s_sf & i7s_sg)          
#define i7s_L (i7s_sd & i7s_se & i7s_sf)                   
#define i7s_n (i7s_sc & i7s_se & i7s_sg)                   
#define i7s_o (i7s_sc & i7s_sd & i7s_se & i7s_sg)          
#define i7s_P (i7s_sa & i7s_sb & i7s_se & i7s_sf & i7s_sg)
#define i7s_q (i7s_sa & i7s_sb & i7s_sc & i7s_sf & i7s_sg)
#define i7s_r (i7s_se & i7s_sg)                            
#define i7s_t (i7s_sd & i7s_se & i7s_sf & i7s_sg)          
#define i7s_Y (i7s_sb & i7s_sc & i7s_sd & i7s_sf & i7s_sg)
#define i7s_U (i7s_sb & i7s_sc & i7s_sd & i7s_se & i7s_sf)
#define i7s_u (i7s_sc & i7s_sd & i7s_se)                   
#define i7s_Pcyr (i7s_sa & i7s_sb & i7s_sc & i7s_se & i7s_sf)
#define i7s_Gcyr (i7s_sa & i7s_se & i7s_sf)                
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David,

If you agree I'll actually move this to the tutorial forum where I think it might be more appropriate (and then I'll delete this post and your response to clear the "noise")

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cliff: hmm, it's not a tutorial really, it's more of a code snippet (if I understand the meaning of the word correctly).

Maybe a time for a new thread: Where to post interesting and useful code pieces?

[edit]:
Maybe make a sticky in the Tutorials section or the Academy section?

If you think it's more apropriate there, then feel free to do so.

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.

Last Edited: Thu. Dec 20, 2007 - 07:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

urriz77: Your aproach would be very inefective in the application I used it for: http://daqq.eu/index.php?show=pr...

Instead of:

PORTD = (~segs[min_10]);

I'd have to use:

switch(min_10)
{
case 0:
 PORTD = i7s_0;
 break;
case 1:
 ....
}

It would create a massive code that only compares. I prefer my approach.

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.