Trying to program a altera DE0 board

In summary, the conversation is about someone seeking help with their code for programming a Altera DE0 board to measure distance using a SRF05 ultrasonic range finder. The person has written a code but is now stuck and is getting errors during debugging. They are seeking assistance with the errors and potential mistakes in their code. The conversation also includes a discussion on formatting and indentation in the code.
  • #1
pranjal091994
5
0
Hi everyone can anyone help me in my code. I am trying to program a altera DE0 board to measure distance using a SRF05(ultrasonic range finder). I have written a code but now I am stuck.



Code:
#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 "sys/alt_timestamp.h"
#include "system.h"
#define setHeaderOuts HEADEROUTPUTS_BASE+0x10  	//HEADEROUTPUTS_BASE is defined in system.h of the _bsp file.  It refers to the base address in the Qsys design
												//the hex offset (in this case 0x10, which is 16 in decimal) gives the number of bytes of offset
												//each register is 32 bits, or 4 bytes
												//so to shift to register 4, which is the outset register, we need 4 * (4 bytes) = 16 bytes
#define clearHeaderOuts HEADEROUTPUTS_BASE+0x14 //to shift to register 5 (the 'outclear' register) we need to shift by 5 * (4 bytes) = 20 bytes, (=0x14 bytes)
												// offset of 5 corresponds to the 'outclear' register of the PIO.
#define HEADERINPUTS_BASE+0x07

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 5;

int  main(void)
{
	alt_putstr("This is the NIOS processor");
	unsigned char buttons; //the buttons on the DE0
	//unsigned char trigger;
	unsigned int count;
	const int clock = 50000000;
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimetres:
  //long duration, inches, cm;
  int seg, ts, delay;
  
  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
		}
		alt_timestamp_start();  //clock starts
		while((buttons & 0x01) == 1)
		{
			IOWR_ALTERA_AVALON_PIO_DATA(clearHeaderOuts,0x01); //turn off the first pin of the output port
			IOWR_ALTERA_AVALON_PIO_DATA(setHeaderOuts,0x01);	//turn on the first pin of the output port
			delay = 12000000 //alt_timestamp_start(); //clock starts
			count = alt_timestamp(); //records clock value
			if (count > 10000000)
			{
				ts = IORD_ALTERA_AVALON_PIO_DATA(HEADERINPUTS_0x01);	//reads from the first input pin of DE0
			
			ts=count/clock  //not sure about this function
			IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,seg);  //DE0 7 segment displays cm I think?
			}
		}
	
	}
   switch (ts)
		{
			case 0:
	      	seg= 0xC0;
	       	break;
			case 1:
			seg= 0xF9;
			break;
			case 2:
			seg= 0xA4;
			break;
			case 3:
            seg= 0xB0;
			break;
			case 4:
			seg= 0x99;
			break;
			case 5:
			seg= 0x92;
			break;
			case 6:
			seg= 0x82;
			break;
			case 7:
			seg= 0xF8;
			break;
			case 8:
			seg= 0x80;
			break;
			case 9:
			seg= 0x98;
			break;
			default:
			seg= 0xC0;
			break;
		}

	
}
 
Technology news on Phys.org
  • #2
What do you mean by stuck?

What are you seeing in your results?

or is it that you don't know how to debug your code?
 
  • #3
When I try debug the program I get errors at the following codes:

#define HEADERINPUTS_BASE+0x07
delay = 12000000 //alt_timestamp_start(); //clock starts
count = alt_timestamp(); //records clock value
ts = IORD_ALTERA_AVALON_PIO_DATA(HEADERINPUTS_0x01); //reads from the first input pin of DE0
ts=count/clock //not sure about this function
IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,seg); //DE0 7 segment displays cm I think?

for the headerinputs the error message I get is that it's undeclared.
for the delay function and ts=count/clock function it says syntax error.
 
  • #4
pranjal091994 said:
When I try debug the program I get errors at the following codes:

#define HEADERINPUTS_BASE+0x07
I believe you are missing a space in the above. I would write it like this:
Code:
#define HEADERINPUTS_BASE 0x07
I am assuming that HEADERINPUTS_BASE is at location 0x07 in memory, but you know your system better than I do.

pranjal091994 said:
delay = 12000000 //alt_timestamp_start(); //clock starts
The syntax error is that your assignment statement doesn't end with a semicolon.

Also, what is the size of an int on your system? If an int is 16 bits, then 12000000 is too big to fit in an int. If an int is 32 bits, then you're OK.
pranjal091994 said:
count = alt_timestamp(); //records clock value
ts = IORD_ALTERA_AVALON_PIO_DATA(HEADERINPUTS_0x01); //reads from the first input pin of DE0
ts=count/clock //not sure about this function
IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,seg); //DE0 7 segment displays cm I think?

for the headerinputs the error message I get is that it's undeclared.
for the delay function and ts=count/clock function it says syntax error.

You can't debug a program until you can compile and link it without errors. Your compiler is telling you that you still have errors.

Also, your code is poorly formatted, which makes it difficult for humans to read. For example, you are indenting too much in your while(1) loop.
Instead of this --
Code:
while(1)
       {
                  buttons = ...
                  // more code
       }
do this --
Code:
while(1)
{
     buttons = ...
     // more code
}
When you indent so much, especially using tabs, it makes the lines of code so long that you have to scroll to see the ends of the longer lines.

Your switch statement is nearly unreadable. To make it easier to read, indent the bodies of each case, and line up the braces with the switch keyword. Instead of using tabs to indent, use 2 or 3 spaces for each indent level.
Code:
  switch (ts)
{
   case 0:
      seg= 0xC0;
      break;
   case 1:
      seg= 0xF9;
      break;
   case 2:
      seg= 0xA4;
      break;
   case 3:
      // and so on
}
 
Last edited:
  • Like
Likes 1 person
  • #5
I did the necessary changes but I am still not able to get the results I wanted. I tried a different approach to set the delay. My microcontroller's int is 32bits. When I compile it now it does it well but still doesn't display the distance it found from the ultrasonic range finder onto the 7-segement display. What could I be doing wrong? and sorry the formatting of my code is going wrong on this blog. If you copy everything and paste it on Notepad++ it comes out fine.



Code:
#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 "sys/alt_timestamp.h"
#include "system.h"
#define setHeaderOuts HEADEROUTPUTS_BASE+0x10  	//HEADEROUTPUTS_BASE is defined in system.h of the _bsp file.  It refers to the base address in the Qsys design
												//the hex offset (in this case 0x10, which is 16 in decimal) gives the number of bytes of offset
												//each register is 32 bits, or 4 bytes
												//so to shift to register 4, which is the outset register, we need 4 * (4 bytes) = 16 bytes
#define clearHeaderOuts HEADEROUTPUTS_BASE+0x14 //to shift to register 5 (the 'outclear' register) we need to shift by 5 * (4 bytes) = 20 bytes, (=0x14 bytes)
												// offset of 5 corresponds to the 'outclear' register of the PIO.
#define HEADERINPUTS_BASE 0x07

int  main(void)
{
    alt_putstr("This is the ELEE1062 version of the NIOS processor");
    unsigned char buttons; //the buttons on the DE0
    unsigned int count;
    const int clock = 50000000;
    int seg, ts, delay;
  
  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
     }
     alt_timestamp_start();  //clock starts
     while((buttons & 0x01) == 1)
     {
        IOWR_ALTERA_AVALON_PIO_DATA(clearHeaderOuts,0x01); //turn off the first pin of the output port
        IOWR_ALTERA_AVALON_PIO_DATA(setHeaderOuts,0x01);	//turn on the first pin of the output port
        delay = 12000000; //alt_timestamp_start(); //clock starts
        count = alt_timestamp(); //records clock value
        /*if (count > 10000000)
	{
	ts = IORD_ALTERA_AVALON_PIO_DATA(HEADERINPUTS_0x01);	//reads from the first input pin of DE0
	 }*/
        ts=count/clock;  //not sure about this function
	
        while((ts & 0x01) == 1)	//Wait until the echo pulse is concluded
        {
	   ts=IORD_ALTERA_AVALON_PIO_DATA(HEADERINPUTS_BASE);      //Check echo pulse
        }
    
        ts = seg;
    
        IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,seg);  //DE0 7 segment displays cm I think?
    }
	
  }
  switch (ts)  //To display number on the 7 - segment display
  {
    case 0:
       	seg= 0xC0;
       	break;
    case 1:
	seg= 0xF9;
	break;
    case 2:
	seg= 0xA4;
	break;
    case 3:
        seg= 0xB0;
	break;
    case 4:
	seg= 0x99;
	break;
    case 5:
	seg= 0x92;
	break;
    case 6:
	seg= 0x82;
	break;
    case 7:
	seg= 0xF8;
	break;
    case 8:
	seg= 0x80;
	break;
    case 9:
	seg= 0x98;
	break;
    default:
	seg= 0xC0;
	break;
  }
}
 
Last edited by a moderator:
  • #6
How about doing us a favor by formatting your code to make it readable, as I described in post #4?
 
  • #7
Is that better?
 
  • #8
Yes, plus I tweaked it a bit more. Which variable represents the distance, ts? I would put in a debugging output statement to print the value of this variable before trying to light up the 7 segment display. You are using the alt_putstr() function to print a string. I'm guessing there are other output functions in the same header that would allow you to print a numeric value.
 

1. How do I connect the Altera DE0 board to my computer?

To connect the Altera DE0 board to your computer, you will need a USB cable. Connect one end of the USB cable to the mini-USB port on the DE0 board and the other end to an available USB port on your computer. Make sure your computer recognizes the board and installs any necessary drivers.

2. What software do I need to program the Altera DE0 board?

You will need the Altera Quartus Prime software, which is available for free on the Altera website. This software allows you to write and compile your code, as well as program the DE0 board.

3. How do I write and compile code for the Altera DE0 board?

To write and compile your code, you will need to open the Quartus Prime software and create a new project. Then, write your code in the designated editor and click the compile button. This will generate a programming file that can be used to program the DE0 board.

4. How do I program the Altera DE0 board?

To program the DE0 board, you will need to open the Quartus Prime Programmer tool. Connect the DE0 board to your computer and select the correct device and programming file. Then, click the program button to transfer the code onto the board.

5. How do I test my program on the Altera DE0 board?

To test your program, you will need to connect the necessary input and output devices to the DE0 board, such as LEDs, switches, or sensors. Then, run the program on the board and observe the results. You can also use the Quartus Prime SignalTap tool to analyze and debug your program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
11K
Back
Top