- #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:
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.
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.