Ananthan 0 Posted May 3, 2014 Share Posted May 3, 2014 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. Quote Link to post Share on other sites
SixSixSevenSeven 23 Posted May 3, 2014 Share Posted May 3, 2014 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) Quote Link to post Share on other sites
pabigot 355 Posted May 3, 2014 Share Posted May 3, 2014 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. Quote Link to post Share on other sites
Ananthan 0 Posted May 4, 2014 Author Share Posted May 4, 2014 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. Quote Link to post Share on other sites
Ananthan 0 Posted May 4, 2014 Author Share Posted May 4, 2014 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. Quote Link to post Share on other sites
pabigot 355 Posted May 4, 2014 Share Posted May 4, 2014 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. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.