Re: Simulating a seven-segment display

Discussion in 'C Programming' started by lovecreatesbeauty@gmail.c0m, Jul 2, 2009.

  1. 0m

    0m Guest

    On Jul 2, 5:31 pm, "0m"
    <> wrote:
    > $ cat a.c
    > /* a.c */
    >
    > static const char segs[16][7 + 1] =
    > {
    > "ABCDEF ", /* 0 */
    > " BC ", /* 1 */
    > "AB DE G", /* 2 */
    > "ABCD G", /* 3 */
    > " BC FG", /* 4 */
    > "A CD FG", /* 5 */
    > "A CDEFG", /* 6 */
    > "ABC ", /* 7 */
    > "ABCDEFG", /* 8 */
    > "ABC FG", /* 9 */
    > "ABC EFG", /* 0xA */
    > " CDEFG", /* 0xB */
    > "A DEF ", /* 0xC */
    > " BCDE G", /* 0xD */
    > "A DEFG", /* 0xE */
    > "A EFG", /* 0xF */
    >
    > };
    >
    > void lightup(char seg[5][3], const char c)
    > {
    > char i, *p;
    >
    > if (c >= '0' && c <= '9') i = c - '0';
    > else if (c >= 'A' && c <= 'Z') i = c - 'A' + 10;
    > else if (c >= 'a' && c <= 'z') i = c - 'a' + 10;


    else return;

    > p = segs;
    > while (*p){
    > switch(*p++){
    > case 'A':
    > seg[0][1] = '-';
    > break;
    > case 'B':
    > seg[1][2] = '|';
    > break;
    > case 'C':
    > seg[3][2] = '|';
    > break;
    > case 'D':
    > seg[4][1] = '-';
    > break;
    > case 'E':
    > seg[3][0] = '|';
    > break;
    > case 'F':
    > seg[1][0] = '|';
    > break;
    > case 'G':
    > seg[2][1] = '-';
    > break;
    > }
    > }
    >
    > }
    >
    > void showup(char seg[5][3], int lncnt, int clcnt)
    > {
    > int i, j;
    > for (i = 0; i != lncnt; i++){
    > for (j = 0; j != clcnt; j++){
    > printf("%c", seg[j]);
    > }
    > printf("\n");
    > }
    >
    > }
    >
    > void cleanup(char seg[5][3], int lncnt, int clcnt)
    > {
    > int i, j;
    > for (i = 0; i != lncnt; i++){
    > for (j = 0; j != clcnt; j++){
    > seg[j] = ' ';
    > }
    > }
    >
    > }
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > char seg[5][3] =
    > {
    > " ",
    > " ",
    > " ",
    > " ",
    > " "
    > };
    >
    > lightup(seg, '8');
    > showup(seg, 5, 3);
    > cleanup(seg, 5, 3);
    > printf("\n");
    > lightup(seg, 'b');
    > showup(seg, 5, 3);
    >
    > return 0;}
    >
    > $


    It must be the worst piece in this thread though I spend a whole
    afternoon on this piece. Currently, it can't display multiple 7-seg
    digits on one line. Why I can't come up with code like Peter Nilsson's
    and luserXtrog's above. (how old are you two guys :))

    Sigh.
     
    0m, Jul 2, 2009
    #1
    1. Advertising

  2. 0m

    Lew Pitcher Guest

    On July 2, 2009 06:02, in comp.lang.c, 0m
    () wrote:

    > On Jul 2, 5:31 pm, "0m"
    > <> wrote:
    >> $ cat a.c
    >> /* a.c */
    >>
    >> static const char segs[16][7 + 1] =
    >> {
    >> "ABCDEF ", /* 0 */
    >> " BC ", /* 1 */
    >> "AB DE G", /* 2 */
    >> "ABCD G", /* 3 */
    >> " BC FG", /* 4 */
    >> "A CD FG", /* 5 */
    >> "A CDEFG", /* 6 */
    >> "ABC ", /* 7 */
    >> "ABCDEFG", /* 8 */
    >> "ABC FG", /* 9 */
    >> "ABC EFG", /* 0xA */
    >> " CDEFG", /* 0xB */
    >> "A DEF ", /* 0xC */
    >> " BCDE G", /* 0xD */
    >> "A DEFG", /* 0xE */
    >> "A EFG", /* 0xF */
    >>
    >> };
    >>
    >> void lightup(char seg[5][3], const char c)
    >> {
    >> char i, *p;
    >>
    >> if (c >= '0' && c <= '9') i = c - '0';
    >> else if (c >= 'A' && c <= 'Z') i = c - 'A' + 10;


    You may want to re-examine this statement. See below

    >> else if (c >= 'a' && c <= 'z') i = c - 'a' + 10;


    You may want to re-examine this statement.

    The C standard does not guarantee that alphabetic characters are allocated
    in a continuous sequence. There are platforms where the alphabetic
    characters are allocated "out of sequence", or with varying increments
    (i.e., where 'J' - 'I' > 1 ).

    While your above two statements will compile (likely without any
    diagnostic), and will execute (again, likely without any diagnostic), on
    some platforms (maybe yours), they will produce unintended and unexpected
    results. (Likely that i will be set such that segs will be
    out-of-bounds.


    > else return;
    >
    >> p = segs;
    >> while (*p){
    >> switch(*p++){
    >> case 'A':
    >> seg[0][1] = '-';
    >> break;
    >> case 'B':
    >> seg[1][2] = '|';
    >> break;
    >> case 'C':
    >> seg[3][2] = '|';
    >> break;
    >> case 'D':
    >> seg[4][1] = '-';
    >> break;
    >> case 'E':
    >> seg[3][0] = '|';
    >> break;
    >> case 'F':
    >> seg[1][0] = '|';
    >> break;
    >> case 'G':
    >> seg[2][1] = '-';
    >> break;
    >> }
    >> }
    >>
    >> }
    >>
    >> void showup(char seg[5][3], int lncnt, int clcnt)
    >> {
    >> int i, j;
    >> for (i = 0; i != lncnt; i++){
    >> for (j = 0; j != clcnt; j++){
    >> printf("%c", seg[j]);
    >> }
    >> printf("\n");
    >> }
    >>
    >> }
    >>
    >> void cleanup(char seg[5][3], int lncnt, int clcnt)
    >> {
    >> int i, j;
    >> for (i = 0; i != lncnt; i++){
    >> for (j = 0; j != clcnt; j++){
    >> seg[j] = ' ';
    >> }
    >> }
    >>
    >> }
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> char seg[5][3] =
    >> {
    >> " ",
    >> " ",
    >> " ",
    >> " ",
    >> " "
    >> };
    >>
    >> lightup(seg, '8');
    >> showup(seg, 5, 3);
    >> cleanup(seg, 5, 3);
    >> printf("\n");
    >> lightup(seg, 'b');
    >> showup(seg, 5, 3);
    >>
    >> return 0;}
    >>
    >> $

    >
    > It must be the worst piece in this thread though I spend a whole
    > afternoon on this piece. Currently, it can't display multiple 7-seg
    > digits on one line. Why I can't come up with code like Peter Nilsson's
    > and luserXtrog's above. (how old are you two guys :))
    >
    > Sigh.


    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Jul 3, 2009
    #2
    1. Advertising

  3. 0m

    Richard Bos Guest

    luserXtrog <> wrote:

    > int seg[] =3D {
    > /*0*/ A+B+C+D+E+F,
    > /*1*/ B+C,
    > /*2*/ A+B+ D+E+ G,
    > /*3*/ A+B+C+D+ G,
    > /*4*/ B+C+ F+G,
    > /*5*/ A+ C+D+ F+G,
    > /*6*/ A+ C+D+E+F+G,
    > /*7*/ A+B+C,
    > /*8*/ A+B+C+D+E+F+G,
    > /*9*/ A+B+C+ F+G,
    > /*A*/ A+B+C+ E+F+G,
    > /*b*/ C+D+E+F+G,
    > /*C*/ A+ D+E+F,
    > /*d*/ B+C+D+E+ G,
    > /*E*/ A+ D+E+F+G,
    > /*F*/ A+ E+F+G,
    > };


    Or even

    /*a*/ A+B+C+D+E+ G,
    /*b*/ C+D+E+F+G,
    /*c*/ A+ D+E+F,
    /*d*/ B+C+D+E+ G,
    /*e*/ A+B+ D+E+F+G,
    /*f*/ A+ E+F+G,

    Or possibly

    /*c*/ D+E+ G,

    Richard
     
    Richard Bos, Jul 5, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,800
    darshana
    Oct 26, 2008
  2. Elnikety
    Replies:
    2
    Views:
    1,414
    Elnikety
    May 7, 2007
  3. electronsteve

    7 segment display

    electronsteve, Nov 7, 2008, in forum: VHDL
    Replies:
    0
    Views:
    1,109
    electronsteve
    Nov 7, 2008
  4. Felipe Ribeiro

    Simulating a seven-segment display

    Felipe Ribeiro, Jul 1, 2009, in forum: C Programming
    Replies:
    16
    Views:
    1,042
    Squeamizh
    Jul 22, 2009
  5. luserXtrog

    Re: Simulating a seven-segment display

    luserXtrog, Jul 3, 2009, in forum: C Programming
    Replies:
    1
    Views:
    348
    luserXtrog
    Jul 4, 2009
Loading...

Share This Page