C code and seven segment display

  • Thread starter Steve Collins
  • Start date
  • Tags
    C code Code
In summary, the code displays a counter value on four 7 segment displays. Each time a button is pressed, the counter is incremented.
  • #1
Steve Collins
46
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
  • #2
so are you trying to convert 'count' to a digit to display?
 
  • #3
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.
 
  • #4
okay, do you have a multi-digit display or is it only one digit?
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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.
 
  • #9
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:

FAQ: C code and seven segment display

What is C code?

C code is a programming language used to write computer programs. It is a high-level language that allows programmers to write efficient and portable code for a wide range of applications.

What is a seven segment display?

A seven segment display is a type of electronic display that consists of seven LED segments arranged in a specific pattern. It is commonly used to display numbers and some letters, and is often found in digital clocks, calculators, and other electronic devices.

How can I display numbers on a seven segment display using C code?

To display numbers on a seven segment display using C code, you can use the digitalWrite() function to turn on and off the individual LED segments. You will also need to define the pattern for each number in your code.

Can I control multiple seven segment displays using C code?

Yes, you can control multiple seven segment displays using C code. You will need to use a multiplexing technique, where each display is turned on and off in quick succession, giving the illusion that all displays are on simultaneously. This technique can be achieved using loops and delays in your code.

Are there any libraries or resources available for working with C code and seven segment displays?

Yes, there are several libraries and resources available for working with C code and seven segment displays. Some popular options include the SevenSeg library for Arduino, the LED7Seg library for Raspberry Pi, and various online tutorials and guides for working with C code and seven segment displays.

Similar threads

Replies
7
Views
4K
Replies
9
Views
2K
Replies
7
Views
2K
Replies
3
Views
1K
Replies
4
Views
1K
Replies
7
Views
2K
Back
Top