Is simple code for 7 segment correct

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

I got two files for a 7 segment display, connected in a trivial way (i.e PORTA 0 to segment a ... PORTA 6 to segment g):
a file to be included only once, simple.h

 python litseg.py -s simple -w simple.txt -f b >t && cat simple.h
#ifndef __SIMPLE_H
#define __SIMPLE_H
#define MASK_SIMPLE  0b01111111
#define NMASK_SIMPLE  0xFF ^ 0b01111111

 void init_simple();

 void ecrit_simple(uint8_t digit);
 uint8_t codes_PORTAsimple [10 ] = {
                0b00111111,
                0b00000110,
                0b01011011,
                0b01001111,
                0b01100110,
                0b01101101,
                0b01111101,
                0b00000111,
                0b01111111,
                0b01101111
 };
#endif

and a file to be compiled and linked, simple.c

#include "simple.h"

 void init_simple()
  {
DDRA = 0b01111111;
  }
 

 void ecrit_simple(uint8_t digit)
  {
  PORTA &= ~0b01111111;
  PORTA |= codes[digit];
  }

Connections are such a segment is lit if the correponding pin is high...
When one writes a digit to a (or more, later) port(s), one takes care not to corrupt the other unused pins -maybe used elsewhere-

Are these two simple files correct ?

This topic has a solution.
Last Edited: Thu. Jan 4, 2018 - 09:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I only see a couple possible problems. In .h codes_PORTAsimple is defined. Typically variables are defined in .c. In the .c, codes, is not defined, I assume it should be codes_PORTAsimple.

It all starts with a mental vision.

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

Thanks Kit Carlson:
I hope I could fix part of it; I retried with pins active low (i.e a segment is lit when the corresponding port pin is low)
I got the following include file :

 python litseg.py -s simple -w simple.txt -f x -i  >t && cat simple.h
#ifndef __SIMPLE_H
#define __SIMPLE_H
#define MASK_SIMPLE  0x7f
#define NMASK_SIMPLE  0xFF ^ 0x7f

 void init_simple();

 void ecrit_simple(uint8_t digit);
#endif

and the file to be compiled, simple.c

#include "simple.h"
uint8_t codes_PORTAsimple [10 ] = {
                0xc0,
                0xf9,
                0xa4,
                0xb0,
                0x99,
                0x92,
                0x82,
                0xf8,
                0x80,
                0x90
 };

 void init_simple()
  {
   DDRA = 0x7f;
  }
 

 void ecrit_simple(uint8_t digit)
  {
  PORTA |= 0x7f;
  PORTA &= codes_PORTAsimple[digit];
 }

Edited :
changed penultimate line of simple.c
code generated is now
PORTA &= ~codes_PORTAsimple[digit];
(for active low, seems better)

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#define _a	0x40
#define _b	0x20
#define _c	0x10
#define _d	0x08
#define _e	0x04
#define _f	0x02
#define _g	0x01
#define _dp	0x80
static unsigned char segtbl[] = {
	_a|_b|_c|_d|_e|_f	, // 0
	_b|_c			, // 1
	_a|_b|_d|_e|_g		, // 2
	_a|_b|_c|_d|_g		, // 3
	_b|_c|_f|_g		, // 4
	_a|_c|_d|_f|_g		, // 5
	_a|_c|_d|_e|_f|_g	, // 6
	_a|_b|_c		, // 7
	_a|_b|_c|_d|_e|_f|_g	, // 8
	_a|_b|_c|_f|_g		, // 9
};
char seg7buffer[5];		// global segment buffer

void write7seg(unsigned char value, unsigned char digit)
{
    seg7buffer[digit] = segtbl[value];
}

void multiplex(void)		// call every 10ms
{
	static unsigned char count;
        if (++count >= 5) count = 0;
	SEGMENTPORT = seg7buffer[count];
        COLUMNPORT = (1 << count);
}        

Obviously you can invert the tables if you like. Or simply invert at runtime. e.g. for a single display:

    PORT = ~segtbl[value];

Apart from mapping the segments to the relevant port pins, the actual 7 segment operations are trivial. Note that you should test for valid range of input or you my exceed the array bounds.

David.

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

Thanks you David for pointing out that range should be checked:
generated function becomes

 uint8_t ecrit_simple(uint8_t digit)
  {
   if (digit > 10) return(0); 
  PORTA &= ~0b01111111;
  PORTA |= codes_PORTAsimple[digit];
 return(1);
}

(I am aware I forgot to turn off the digit)

Updating a full port (and sometimes dp is not used) may corrupt other used pins... And, if a port has special functions (an UART takes two pins, an I2C too, SPI takes 3 or 4 pins) one would like to share ports for the same 7 segments... I know this is trivial, but nothing is morefrustrating than debugging typos for trivial functions...
Therefore I thought having things made "automagically" by python -I am trying to learn- would lead to less errors...

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

Life is a lot simpler with a single port for multiplexing. A single display really does not matter if you spread it across unrelated pins.

Incidentally, some peripherals steal pins for their own use. So you can write to the whole port and it will not affect the stolen pins.
Others (like SPI) do not override GPIO completely.

David.

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

    Well, this old topic (trying to validate automatic generation of 7 segment display code) has little, if any logical relation with

 

 Fig.  5 Topology of a 40 kV 750 W dc-dc converter

You should open a new topic, ("general electronics" does not sem unfit),

and perhaps, before, look at wikipedia (wikipedia contributors have more time ro explain than avr-freaks ) 

https://en.wikipedia.org/wiki/Ch... seems a good strting point.

 

Oh, btw: voltages seem unusually high and very dangerous (no one in his sense would dare to advice you in potentially lethal experiments: misunderstanding linked, say, to language difficulties might have unpleasant consequences..., your life expectation will be longer if you begin by

a) reading wikipedia

b) simple electronic experiment, with low voltages -pump charges ware used for +-10 v generation).

 

Last Edited: Thu. Jan 4, 2018 - 09:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I marked it as being given a solution (D.Prentice one : generatin automatically with multiplexing many segmnets would be more complicated and to days, some graphical displays are ... cheaper than LCD ... wich are cheaper than 7 segments) ;

As I do not hope this thread should be hijacked for high tension generation (too dangerous), is there a BIG BUTTON to lock it...

 

* Denis I have the BIG BUTTON and just pressed it. Ross *

Last Edited: Thu. Jan 4, 2018 - 09:45 AM
Topic locked