0

Programming DE-115 Board with 7 Segment Display to count from 0 to 99,999,999

I finished coding the 7-segment display to do a random design when it's at 250 ms, go in a circle around the outside egdes of the display at 125 ms,and now when I have it set it 50 ms, I need to code a counter to go from 0 to 99,999,999 and repeat. I want to get the basic idea of how to do this. I know I'll be using for loops inside of for loops. I assume there will be a for loop and then 7 more inside of that.

/*
 * "Hello World" example.
 *
 * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
 * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
 * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
 * device in your system's hardware.
 * The memory footprint of this hosted application is ~69 kbytes by default
 * using the standard reference design.
 *
 * For a reduced footprint version of this template, and an explanation of how
 * to reduce the memory footprint for a given application, see the
 * "small_hello_world" template.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include <time.h>

void circleSegments();//Function declaration for changing 7-segment output in circle

int main() {
    printf("Hello from Nios II!\n");
    alt_u32 current_value;
    alt_u32 current_state;
    alt_u8 current_direction;
    alt_u32 keys;
    current_state = 3;
    current_value = 1;
    current_direction = 0;
    srand(time(NULL));
    int i;
    int counter = 0;
    printf("Program running (UART)...\n");
    fprintf(stderr, "Program running (LCD)...\n");
    while (1) {
        // read the current state of the keys
        keys = IORD_ALTERA_AVALON_PIO_DATA(KEYS_BASE);
        // switch speed if necessary
        if ((keys != 7) && (keys != current_state)) {
            if (keys == 3){
                printf("speed set to 250 ms\n");
                fprintf(stderr, "speed set to 250 ms (LCD)...\n");
            }
            else if (keys == 5){
                printf("speed set to 125 ms\n");
                fprintf(stderr, "speed set to 125 ms (LCD)...\n");
            }
            else if (keys == 6){
                printf("speed set to 50 ms\n");
                fprintf(stderr, "speed set to 50 ms (LCD)...\n");
            }
            current_state = keys;
        }
        // switch direction if necessary
        if ((current_direction == 0) && (current_value == (1 << 25)))
            current_direction = 1;
        else if ((current_direction == 1) && (current_value == 1))
            current_direction = 0;
        // move light
        else if (current_direction == 0)
            current_value = current_value << 1;
        else
            current_value = current_value >> 1;
        // update lights

        if (current_state == 3){//Changes 7 Segment output to random output
            IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,rand());
            IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE,rand());
            IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE,rand());
            IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE,rand());
            IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE,rand());
            IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE,rand());
            IOWR_ALTERA_AVALON_PIO_DATA(HEX6_BASE,rand());
            IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,rand());
        }

        if (current_state == 5){
            //circleSegments();
            if (count < 3){
            IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,126);
            IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,126);
            IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE,127);
            IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE,127);
            IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE,127);
            IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE,127);
            IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE,127);
            IOWR_ALTERA_AVALON_PIO_DATA(HEX6_BASE,127);
            IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,127);
        }

        IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, current_value);
        // wait
        if (current_state == 3)
            usleep(250000);

        else if (current_state == 5)
            usleep(125000);
        else
            usleep(50000);

        counter++;
    }

    return 0;

}
//changes output of 7-segment display to move in outside circle
void circleSegments(){

    IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX6_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,127);


    IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE,126);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,126);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,125);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,123);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,119);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE,119);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE,119);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE,119);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE,119);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE,119);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX6_BASE,119);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX6_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,119);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,111);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,95);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,126);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX7_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX6_BASE,126);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX6_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE,126);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX5_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE,126);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX4_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE,126);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX3_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE,126);
    usleep(125000);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX2_BASE,127);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE,126);
    IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE,127);
}

countMax (){
    int l, m, n, o, p, q, r, s
    for(l = 0; i < 10; i++){
        for(m = 0; m < 10; m++){
            for (n = 0; n < 10; n++){
                ..... for the rest of them.

I am not sure on how to finish that code though except the for loop if someone could help.

We are using the 7 segment display on the altera DE-115 board.

splattered
  • 23
  • 5
  • Why do you think you need 7 nested loops? They will show *all possible combinations* that you can make with 7 LEDs. However ... you only want the combinations that display numbers, right? – Jongware Feb 05 '18 at 23:39
  • The alternative to 8 nested `for` loops is a single `for` loop that counts from 0 to 99999999. Inside that `for` loop, you have a second `for` loop that extracts the digits. – user3386109 Feb 05 '18 at 23:40
  • Can you explain that a bit more? I though i needed 8 for each 7 segment display. – splattered Feb 06 '18 at 00:12
  • So I would do int l, m for(l = 0; i <= 99999999; i++){ for(m = 0; m < 10; m++){ – splattered Feb 06 '18 at 00:27
  • Yup. Of course `i` has to be 32-bits. The other thing you need to figure out is the bit pattern that makes each digit. – user3386109 Feb 06 '18 at 23:36

0 Answers0