11 Replies Latest reply on Apr 3, 2014 7:55 AM by albert

    Distance measurement by ARM1176JZF-S(DS- 5 TOOLS) using ultrasonic sensor

    albert Bit

      I have to measure distance between an object by ARM1176JZF-S(DS- 5 TOOLS) using ultrasonic sensor. I am not being able to write the codes. I have connected the ultrasonic sensor in the GPIO pins PL061 peripheral bock (j39). Please help me write the codes.

      Header file and sample program of ultrasonic distance measuring sensor for mbed is given in HCSR04 - a mercurial repository | mbed       and another of ATMega 16 http://www.robokits.co.in/documentation/Ultrasonic%20distance%20sensor.pdf

      (
      #include "hcsr04.h"
      
      
      HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
          trigger(TrigPin), echo(EchoPin)
      {
          pulsetime.stop();
          pulsetime.reset();
          echo.rise(this,&HCSR04::isr_rise);
          echo.fall(this,&HCSR04::isr_fall);
          trigger=0;
      }
      
      HCSR04::~HCSR04()
      {
      }
      
      void HCSR04::isr_rise(void)
      {
          pulsetime.start();
      }
      void HCSR04::start(void)
      {
          trigger=1;
          wait_us(10);
          trigger=0;
      }
      
      void HCSR04::isr_fall(void)
      {
          pulsetime.stop();
          pulsedur = pulsetime.read_us();
          distance= (pulsedur*343)/20000;
          pulsetime.reset();
      }
      
      void HCSR04::rise (void (*fptr)(void))
      {
          echo.rise(fptr);
      }
      void HCSR04::fall (void (*fptr)(void))
      {
          echo.fall(fptr);
      }
      
      unsigned int HCSR04::get_dist_cm()
      {
          return distance;
      }
      unsigned int HCSR04::get_pulse_us()
      {
          return pulsedur;
      }
      Here is a sample code usage
      *********************************************************
      #include "hcsr04.h"
      HCSR04  usensor(p25,p6);
      int main()
      {
          unsigned char count=0;
          while(1) {
              usensor.start();
              wait_ms(500);
              dist=usensor.get_dist_cm();
              lcd.cls();
              lcd.locate(0,0);
              lcd.printf("cm:%ld",dist );
      
              count++;
              lcd.locate(0,1);
              lcd.printf("Distance =%d",count);
           
          }
      }
      )
      
      
      

      http://www.robokits.co.in/documentation/Ultrasonic%20distance%20sensor.pdf

       

      (

       

      Sample Program for ATMega16
      Connections:
      VCC - +5VDC
      Trig – PA.6 on ATmega16
      Echo – PA.7 on ATMega16
      GND – GND
      ATMega16 running at 16Mhz


      char timer0counter;
      void main()
      {
      char buffer[10];
      float range;
      sei(); //Enable global interrupt
      sbi(DDRA,6); //Set pin as output
      cbi(DDRA,7); //Set pin as input
      while(1)
      {
      sbi(PORTA,6); //Send Trigger
      DELAYUS(10);
      cbi(PORTA,6); //Send trigger
      http://www.robokits.co.in
      http://www.robokitsworld.com Page 4
      Ultrasonic Distance Measurement Sensor 4M [RKI-1540]
      timer0counter=0;
      TCNT0=0; //Clear timer
      while(bit_is_clear(PINA,7)); //Wait for rising edge
      sbi(TCCR0,CS02); //Select prescalar 256
      sbi(TIMSK,TOIE0); //Enable timer0 overflow interrupt
      LCD_CLRSCR();
      while(bit_is_set(PINA,7) && timer0counter<9) //wait for falling edge of echo
      {
      DELAYUS(5);
      }
      cbi(TCCR0,CS02); //Stop timer
      cbi(TIMSK,TOIE0);
      if(bit_is_set(PINA,7))
      {
      LCD_PRINT("No OBSTACLE");
      }
      else
      {
      range=(256*timer0counter+TCNT0)*16*0.017; //range conversion
      itoa(range,buffer,10);
      LCD_PRINT(buffer);
      UART_PRINT(buffer);
      UART_PRINT("\n\r");
      }
      DELAYMS(100);
      }
      }
      SIGNAL(SIG_OVERFLOW0)
      {
      cbi(TIMSK,TOIE0);
      TCNT0=0;
      timer0counter++;
      UART_PUTCHAR(timer0counter);
      sbi(TIMSK,TOIE0);
      if(timer0counter>8)
      {
      cbi(TCCR0,CS02);
      cbi(TIMSK,TOIE0}}
      )
      
      
      



        • Re: Distance measurement by ARM1176JZF-S(DS- 5 TOOLS) using ultrasonic sensor
          joealderson Employee

          Hi albert,

           

          I see that you asked a similar question before on a different part of the community. I'd suggest that if you're definitely going to use ARM11, it might be best to simplify what you are trying to do initially. Try to blink a LED first (so that you know you can control the GPIO from a program running on your board), as ultrasound can be pretty temperamental at the best of times.

           

          The alternative, which might be the fastest way to get this working - would be to get a Raspberry Pi, which is an ARM11 development board providing an easy way to get Linux running, control the GPIO etc. They have a huge community with plenty of people carrying out projects just like yours.

           

          Hope this helps,

          Joe

          1 of 1 people found this helpful
            • Re: Distance measurement by ARM1176JZF-S(DS- 5 TOOLS) using ultrasonic sensor
              albert Bit

              Thank you sir for your suggestion. Because of certain constraints I have to do the project in the versatile realview ARM1176JZF-S. I have worked in mbed, 8051, 8085 processors. I am finding it difficult to start. Please help me start. Rest probably I can manage.

              Till now I have been able to debug c programs ( like finding factorilals, fibbonacci, example programs ) in the board through the Dstream debugger. Now please tell me how to begin a program to blink a LED that you suggested. In the eclipse for DS-5 I create a new C project-i name it LED blink- executable--empty project compiler ds 5 GCC toolchain - then add a new source file.  In that source file do I need to give header file of this board? If yes , please tell how to get the library of the board. Then how to proceed?

                • Re: Distance measurement by ARM1176JZF-S(DS- 5 TOOLS) using ultrasonic sensor
                  Peter Harris Employee

                  > If yes , please tell how to get the library of the board.


                  There isn't one.


                  As mentioned a few times this board is not designed to be a micro-controller development platform, it's designed to run a big OS like Linux or Android (so the "library" is the Linux OS). There isn't an off-the-shelf bare-metal support library like you get with mbed - it just doesn't exist for this platform. All you really get is the manual I linked in my first post - you're going to have to write much of the library functionality you would find elsewhere yourself.


                  The manual will give you the address map of all of the peripherals the chip supports, and all of the peripheral manuals will be available separately at http://infocenter.arm.com.


                  HTH,

                  Pete

                  1 of 1 people found this helpful
                    • Re: Distance measurement by ARM1176JZF-S(DS- 5 TOOLS) using ultrasonic sensor
                      albert Bit

                      Sir,
                      Please teach me the very basics so that I have an idea to do go ahead. Tell me how to begin a program to blink a LED that was suggested. In the eclipse for DS-5 I create a new C project-i name it LED blink- executable--empty project compiler ds 5 GCC toolchain - then add a new source file.Then how to proceed?

                       

                       

                       

                      How to boot Linux in this board?

                        • Re: Distance measurement by ARM1176JZF-S(DS- 5 TOOLS) using ultrasonic sensor
                          Peter Harris Employee

                          From memory, the CD which came with the board should include some example projects, as should the DS-5 install itself. Try and get one of the examples to compile to an AXF file, and load that via the debugger. It will be easier to get started if you are starting with a "known good" demo which works.


                          One note on this - the board CD is older than DS-5, so the projects may not work in the IDE without some porting. The DS-5 examples may be built for a board with a different address map, so that may require porting to the board too.

                           

                          > Tell me how to begin a program to blink a LED that was suggested.


                          Once you have the demo working program the "LED" part is trivial. Look up the address of the LED control block in the manual. Write a "1" to the relevant bit at that address to turn the LED on, write a "0" bit to turn it off.


                          If you want to connect a physical LED to the GPIO block on the PCB that may work, but check that voltage the GPIO puts out.


                          > How to boot Linux in this board?


                          There _might_ be a Linux build and some instructions on the CD which came with the board, but I'm really not sure. I currently can't find the instructions which I used to use, as the ARM website has moved around quite a lot ...


                          Pete