C code and seven segment display

  • Thread starter Thread starter Steve Collins
  • Start date Start date
  • Tags Tags
    C code Code
Steve Collins
Messages
45
Reaction score
0
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:
// 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.
 
Physics news on Phys.org
so are you trying to convert 'count' to a digit to display?
 
jedishrfu said:
so are you trying to convert 'count' to a digit to display?

Yes.

The display should increment by one everytime a pushbutton is pressed.
 
okay, do you have a multi-digit display or is it only one digit?
 
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.
 
jedishrfu said:
okay, do you have a multi-digit display or is it only one digit?

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.
 
rcgldr said:
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.

This sounds along the lines of where I should be heading.
 
Steve Collins said:
This sounds along the lines of where I should be heading.

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.
 
Another hint, instead of switch case, you could index an array of display values:

Code:
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]);
/* ... */
 
  • #10
Cheers for your help guys.

The path I ended up going down was:

Code:
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:

Similar threads

  • · Replies 7 ·
Replies
7
Views
4K
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K