Jump to content
43oh

Using ultrasonic sensor


Recommended Posts

Hi I am trying to connect an ultrasonic sensor to the launchpad but am having trouble writing the program in Keil uvision

This is the code is Energia and this is working perfectly.

 

 pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW);

  delayMicroseconds(2);       //setting low o/p for sometime to ensure a clean burst

 

  digitalWrite(pingPin, HIGH); //sending a pulse of 5u Sec

  delayMicroseconds(5);

  digitalWrite(pingPin, LOW);

 

  pinMode(pingPin, INPUT);

  duration = pulseIn(pingPin, HIGH);

 

Now this is equvalant code I wrote in c for Keil.

 


    GPIO_PORTB_DIR_R |= 0x01;

    GPIO_PORTB_DATA_R &= ~0x01;

    Delay(2);

   

    GPIO_PORTB_DATA_R |= 0x01;

    Delay(5);

    GPIO_PORTB_DATA_R &= ~0x01;

    

    GPIO_PORTB_DIR_R &= ~0x01;

    

    while(GPIO_PORTB_DATA_R&0x01 == 0x00);

    Start=NVIC_ST_CURRENT_R;

    while(GPIO_PORTB_DATA_R&0x01 == 0x01);

    End=NVIC_ST_CURRENT_R;

 

    

    if(Start>End)

       Start=Start-End;

    else

      Start=(Start+(16777215-End));

 

    if(i<1000)

      Array[i++]=Start;                          //Holds each value of start

    

    Delay(100);                               //This delay so that next sampling is not immediately


 

As far as I can see, both are equivalent and yet the controll is going into an infinite loop on

while(GPIO_PORTB_DATA_R&0x01 == 0x01); Any ideas? Thanks a lot.

 

 

Link to post
Share on other sites

I don't think the while statement is necessarily wrong, if the intent is to wait until the condition fails.

 

You're configuring PB0; is that the pin you intend to configure (pingPin isn't defined in what you showed)? Do you enable it for its digital function (GPIO_PORTB_DEN_R) somewhere else? See this post for the rather complex sequence necessary to configure GPIOs.

Link to post
Share on other sites

I don't think the while statement is necessarily wrong, if the intent is to wait until the condition fails.

 

You're configuring PB0; is that the pin you intend to configure (pingPin isn't defined in what you showed)? Do you enable it for its digital function (GPIO_PORTB_DEN_R) somewhere else? See this post for the rather complex sequence necessary to configure GPIOs.

I enabled DEN and all but I didn't DR2R registers. I'll try it. Thanks for replaying.

Link to post
Share on other sites

You shouldn't have a semi colon after the while statement.

 

 

while(GPIO_PORTB_DATA_R&0x01 == 0x00);    should just be     while(GPIO_PORTB_DATA_R&0x01 == 0x00)

while(GPIO_PORTB_DATA_R&0x01 == 0x01);    should just be     while(GPIO_PORTB_DATA_R&0x01 == 0x01)

Actually I think that should be there. The statement says to wait till the data register turns either 1 or 0 as set by the sensor.

Link to post
Share on other sites

I enabled DEN and all but I didn't DR2R registers. I'll try it.

The power-up drive configuration is usable, unlike the power-up DEN configuration, so that probably won't help. I'd try taking the working Energia program and replace Energia operations one-by-one with what you believe is the register-level equivalents, to see where it stops working. Then figure out what the difference is.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...