Seo 0 Posted June 17, 2013 Share Posted June 17, 2013 Hi I write a code. But it didnt work. Led is burning and burning out not with range. Where is the problem #include "msp430g2452.h" #define tr BIT0 #define ec BIT1 #define led BIT6 int range=0; void main( void ) { WDTCTL = WDTPW + WDTHOLD; //watchdog stop DCOCTL=CALDCO_1MHZ; //frequency setup BCSCTL1=CALBC1_1MHZ; P1DIR = tr+led; P1OUT=0x00; //all are zero for(; { P1OUT^=tr; //Trigger is on for 10 us __delay_cycles(10); P1OUT^=tr; if(P1IN&ec) { range++; //when echo coming range is increasing } if(range>50) { P1OUT=led; range=0; } __delay_cycles(1000000); //waiting some P1OUT^=led; } } Quote Link to post Share on other sites
cubeberg 540 Posted June 17, 2013 Share Posted June 17, 2013 You might try out this code. It's written for an Ultrasonic backpack that I'm working on. It is designed for the QFN version of the 2553, so it has P3 available - but that part of the code can easily be changed. The Echo pin from the device is connected to P2.0 through a voltage divider (since the device works on 5v which would damage the pins on the MSP430). The trigger pin is connected to 2.3. I *think* the code is figuring out the distance in CM - I'd have to double check - I think it was easier to calculate than inches. UBackpack.zip Quote Link to post Share on other sites
simpleavr 399 Posted June 17, 2013 Share Posted June 17, 2013 if(P1IN&ec) { range++; //when echo coming range is increasing } I think you meant to increment "range" until "echo" pin becomes high. In such case it should be a while condition. Otherwise range can only be 0 or 1 and it would loop thru and do another trigger. while (P1IN&ec) { range++; //when echo coming range is increasing } Quote Link to post Share on other sites
Seo 0 Posted June 23, 2013 Author Share Posted June 23, 2013 I think you meant to increment "range" until "echo" pin becomes high. In such case it should be a while condition. Otherwise range can only be 0 or 1 and it would loop thru and do another trigger. while (P1IN&ec) { range++; //when echo coming range is increasing } thanks but it didnt solve Quote Link to post Share on other sites
nemetila 12 Posted June 26, 2013 Share Posted June 26, 2013 I think you don't clear range in every for cycles. replace this code: if(range>50) { P1OUT = led; range=0; } with this code: if(range>50) { P1OUT = led; } range=0; Quote Link to post Share on other sites
username 198 Posted July 21, 2013 Share Posted July 21, 2013 Unless i'm missing something, your code will create about a 10us pulse every 1 second. It will also sample the ec pin once every 1 second. Thats quite a ways away from what you need to do in order to get the module to work. Also, if your LED is burning, its probably not your software. Perhaps a resistor might help? Seems you need abit better understanding about how the module works and how code works in general. Code examples http://forum.43oh.com/topic/4027-hc-sr04-ultrasound-module-driver-code/ http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/ 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.