Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

DE0 help

  1. Jan 18, 2014 #1
    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 (Text):

    #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;
            }

       
    }
     
  2. jcsd
  3. Jan 18, 2014 #2

    jedishrfu

    Staff: Mentor

    What do you mean by stuck?

    What are you seeing in your results?

    or is it that you dont know how to debug your code?
     
  4. Jan 18, 2014 #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.
     
  5. Jan 18, 2014 #4

    Mark44

    Staff: Mentor

    I believe you are missing a space in the above. I would write it like this:
    Code (Text):
    #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.

    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.
    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 (Text):
    while(1)
           {
                      buttons = ...
                      // more code
           }
     
    do this --
    Code (Text):
    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 (Text):
      switch (ts)
    {
       case 0:
          seg= 0xC0;
          break;
       case 1:
          seg= 0xF9;
          break;
       case 2:
          seg= 0xA4;
          break;
       case 3:
          // and so on
    }
     
     
    Last edited: Jan 18, 2014
  6. Jan 18, 2014 #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 (Text):
    #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: Jan 18, 2014
  7. Jan 18, 2014 #6

    Mark44

    Staff: Mentor

    How about doing us a favor by formatting your code to make it readable, as I described in post #4?
     
  8. Jan 18, 2014 #7
    Is that better?
     
  9. Jan 18, 2014 #8

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: DE0 help
  1. Fortran help (Replies: 0)

  2. Help in fortran (Replies: 2)

  3. [Fortran] help (Replies: 2)

  4. FORTRAN Help (Replies: 1)

  5. Fortran Help (Replies: 2)

Loading...