1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C code and seven segment display

  1. Jan 23, 2013 #1
    I need to write some c code that displays a counter value on four 7 segment displays. The number should increment by one every time a button is pushed. I cannot test this code as my home computer will not run Quartus 2 so I am working blind at the moment, but I would still like to ask some advice on problems that I am having.

    Here is my code:


    Code (Text):
    // The counter is initialised to 0
    // Each time Button 1 on the DE0 is pressed, the counter is incremented
    // Note that the buttons on the DE0 are already debounced

    #include "sys/alt_stdio.h"   //for the alt_putstr function below.  Outputs to Eclipse console
    #include "altera_avalon_pio_regs.h"  //for the I/O functions in the while loop below
    #include "system.h"

    int  main(void)
    {
        alt_putstr("This is the ELEE1062 version of the NIOS processor");
                  int buttons = 0; //the buttons on the DE0
        int count = 0; //general purpose counter

        while(1)
        {
            buttons=IORD_ALTERA_AVALON_PIO_DATA(PUSHBUTTONS1_2_BASE); //read the value of the pushbuttons

            while((buttons & 0x01) == 1) // i.e. while pushbutton 1 is not pressed
            {
                buttons=IORD_ALTERA_AVALON_PIO_DATA(PUSHBUTTONS1_2_BASE); //read the value of the pushbuttons
            }

            count=count+1;

        Switch(digit) // turn on/off segments to match decimal values
                 {
            case 0:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0x000000ff);
            break;

            case 1:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0xf00fffff);
            break;

            case 2:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0x00f00f0f);
            break;

            case 3:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0x0000ff0f);
            break;

            case 4:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0xf00ff00f);
            break;

            case 5:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0x0f00f00f);
            break;

            case 6:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0x0f00000f);
            break;

            case 7:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0x000fffff);
            break;

            case 8:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0x0000000f);
            break;
       
            case 9:
            IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,0x0000f00f);
            break;
                    }

     //display the value of count in binary, using the 7 segment display

            while((buttons & 0x01) == 0) //i.e. while pushbutton 1 is pressed
            {
                buttons=IORD_ALTERA_AVALON_PIO_DATA(PUSHBUTTONS1_2_BASE); //read the value of the pushbuttons
            }

        }
    }
    The above code is probably wrong, but I’m sure that I will get it to work when I have access to Quartus. At the moment I am more concerned with how it can be done rather than how to do it… If that makes sense!

    I am not sure how to display a number above 9. I know that I have to shift to the left by 8 <<, but surely there must be a better way of doing this than doing a switch to case 9999.
     
  2. jcsd
  3. Jan 23, 2013 #2

    jedishrfu

    Staff: Mentor

    so are you trying to convert 'count' to a digit to display?
     
  4. Jan 23, 2013 #3
    Yes.

    The display should increment by one everytime a pushbutton is pressed.
     
  5. Jan 23, 2013 #4

    jedishrfu

    Staff: Mentor

    okay, do you have a multi-digit display or is it only one digit?
     
  6. Jan 23, 2013 #5

    rcgldr

    User Avatar
    Homework Helper

    You can convert your count from binary to decimal on every loop, or you can use a modified increment function, that when the low order digit reaches 10, sets the low order digit back to 0 and increments the next lowest order digit.
     
  7. Jan 23, 2013 #6
    It is 4 digits, so someone with too much time on their hands could sit and press the pushbutton 9999 times and the correct number will be displayed.
     
  8. Jan 23, 2013 #7
    This sounds along the lines of where I should be heading.
     
  9. Jan 23, 2013 #8

    jedishrfu

    Staff: Mentor

    Yes you'd make a 4 element counter array like digit[4] and you increment digit[0] value until it becomes 10 then you reset it to zero and increment digit[1] ...

    then you push the digit array to segment display digits.
     
  10. Jan 23, 2013 #9

    rcgldr

    User Avatar
    Homework Helper

    Another hint, instead of switch case, you could index an array of display values:

    Code (Text):

    int segments[10] = {
      0x000000ff,0xf00fffff,0x00f00f0f,0x0000ff0f,
      0xf00ff00f,0x0f00f00f,0x0f00000f,0x000fffff,
      0x0000000f,0x0000f00f};

    int  main(void)
    {
    /* ... */
        IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,segments[digit]);
    /* ... */
     
     
  11. Jan 28, 2013 #10
    Cheers for your help guys.

    The path I ended up going down was:

    Code (Text):
    switch (1)
     
    {
       case 0:
         count1 = 0xC0; //etc
    }

    switch (2)
     
    {
       case 0:
         count2 = 0xC000; //etc
    }

    switch (3)
     
    {
       case 0:
         count3 = 0xC00000; //etc
    }

    switch (4)
     
    {
       case 0:
         count4 = 0xC0000000; //etc
    }

    count = count1 + count2 + count3 + count4

    Display count
    This is probably a very convoluted way of going about it, but it works.

    I am still interested in how the array system would work so any further explanation would be great. I'm understanding C more and more as I use it, but I'm still waiting for the penny to drop and all becomes clear so I can move onto more complicated stuff.
     
    Last edited: Jan 28, 2013
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C code and seven segment display
Loading...