Trying to program a altera DE0 board

  • Thread starter Thread starter pranjal091994
  • Start date Start date
  • Tags Tags
    Board Program
AI Thread Summary
The discussion revolves around programming an Altera DE0 board to measure distance using an SRF05 ultrasonic range finder. The original poster is encountering issues with their code, particularly with syntax errors and undeclared variables. Key problems identified include an incorrect definition for `HEADERINPUTS_BASE`, missing semicolons, and potential issues with the size of integer variables. Suggestions for debugging include ensuring the code compiles without errors, improving code formatting for readability, and using debugging output statements to print variable values, particularly the distance variable `ts`, before displaying it on a 7-segment display. Despite making adjustments, the user is still unable to display the measured distance, indicating ongoing challenges in the implementation.
pranjal091994
Messages
5
Reaction score
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
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?
 
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.
 
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
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:
How about doing us a favor by formatting your code to make it readable, as I described in post #4?
 
Is that better?
 
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.
 

Similar threads

Replies
9
Views
12K
Back
Top