1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

how to write a mikroc code for ultrasonic sensor hc-sr04

Discussion in 'Robotics & Mechatronics' started by rania1996, Jan 28, 2013.

  1. rania1996

    rania1996 New Member

    Joined:
    May 2, 2012
    Messages:
    9
    Likes:
    0
    Location:
    jordan - zarqa
    hi :)
    I want to write a mikroc code for ultrasonic sensor hc-sr04 and I need help :eek:
    I am using pic16f876a

    I am using ultrasonic sensor above my robot ... I want to make my robot go forward when the echo pin got signal and if the distance between my robot and the other one 50 cm or less

    couls you plz help me ??
     
  2. panic mode

    panic mode Well-Known Member

    Joined:
    Nov 8, 2003
    Messages:
    1,637
    Likes:
    38
    Location:
    mississauga
  3. rania1996

    rania1996 New Member

    Joined:
    May 2, 2012
    Messages:
    9
    Likes:
    0
    Location:
    jordan - zarqa
    I am not using timer .. look in arduino
    it like this :
    Code (text):

    #define trigPin 12
    #define echoPin 13

    void setup() {

      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
    }

    void loop() {
      int duration, distance;
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration/2) / 29.1;

      delay(500);
    }

    I want to write in in microC ... I wrote this code :
    TRISB.f1=0;                // trigpin is low
      TRISB.f0=1;               //  echopin high

      TRISB.f1=1;                                // trigpin high
      delay_us(10);                           // wait 10 us
      TRISB.f1=0;                              // trigpin low
         char duration;
         char distance;
       duration=button(PORTB, 0, 1, 1)
       distance = (duration/2) / 29.1;      // elzaman/2 / ( 340m/s)
       if (distance <= 50);
       rotate_f;
      delay_us(100);
    }
    }

     
    ..................

    I want to know what I should replace ( int duration ... int duration )

    and this sentence ( duration = pulseIn(echoPin, HIGH); )

    ^^"
     
    Last edited by a moderator: Aug 29, 2014
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. lijoppans

    lijoppans New Member

    Joined:
    Jul 11, 2011
    Messages:
    5
    Likes:
    0

    MikroC Code :
    Code (text):

    // LCD module connections
    sbit LCD_RS at RD2_bit;
    sbit LCD_EN at RD3_bit;
    sbit LCD_D4 at RD4_bit;
    sbit LCD_D5 at RD5_bit;
    sbit LCD_D6 at RD6_bit;
    sbit LCD_D7 at RD7_bit;

    sbit LCD_RS_Direction at TRISD2_bit;
    sbit LCD_EN_Direction at TRISD3_bit;
    sbit LCD_D4_Direction at TRISD4_bit;
    sbit LCD_D5_Direction at TRISD5_bit;
    sbit LCD_D6_Direction at TRISD6_bit;
    sbit LCD_D7_Direction at TRISD7_bit;
    // End LCD module connections

    int a;

    //Interrupt function will be automatically executed on Interrupt
    void interrupt()
    {
    if(INTCON.RBIF == 1) //Makes sure that it is PORTB On-Change Interrupt
    {
    INTCON.RBIE = 0; //Disable On-Change Interrupt
    if(PORTB.F4 == 1) //If ECHO is HIGH
    T1CON.F0 = 1; //Start Timer
    if(PORTB.F4 == 0) //If ECHO is LOW
    {
    T1CON.F0 = 0; //Stop Timer
    a = (TMR1L | (TMR1H<<8))/58.82; //Calculate Distance
    }
    }
    INTCON.RBIF = 0; //Clear PORTB On-Change Interrupt flag
    INTCON.RBIE = 1; //Enable PORTB On-Change Interrupt
    }

    void main()
    {
    char txt[7];
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR); // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

    TRISB = 0b00010000;
    INTCON.GIE = 1; //Global Interrupt Enable
    INTCON.RBIF = 0; //Clear PORTB On-Change Interrupt Flag
    INTCON.RBIE = 1; //Enable PORTB On-Change Interrupt

    Lcd_Out(1,1,"Developed By");
    Lcd_Out(2,1,"electroSome");

    Delay_ms(3000);
    Lcd_Cmd(_LCD_CLEAR);

    T1CON = 0x10; //Initializing Timer Module

    while(1)
    {
    TMR1H = 0; //Setting Initial Value of Timer
    TMR1L = 0; //Setting Initial Value of Timer

    a = 0;

    PORTB.F0 = 1; //TRIGGER HIGH
    Delay_us(10); //10uS Delay
    PORTB.F0 = 0; //TRIGGER LOW

    Delay_ms(100); //Waiting for ECHO
    a = a + 1; //Error Correction Constant
    if(a>2 && a<400) //Check whether the result is valid or not
    {
    IntToStr(a,txt);
    Ltrim(txt);
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,1,"Distance = ");
    Lcd_Out(1,12,txt);
    Lcd_Out(1,15,"cm");
    }
    else
    {
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,1,"Out of Range");
    }
    Delay_ms(400);
    }
    }
     
    Try the following link :
    Interfacing HC-SR04 Ultrasonic Sensor with PIC Microcontroller
    It contains the detailed explanation of interfacing HC-SR04 ultrasonic sensor with PIC Microcontroller using MikroC Pro and MPLAB XC8 Compilers.
     
    Last edited by a moderator: Aug 29, 2014
  6. Abhik Chowdhury

    Abhik Chowdhury New Member

    Joined:
    Jun 23, 2015
    Messages:
    5
    Likes:
    0
    how to write this code for dspic30f4011 mcu. plzzz help
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Seriously!!! The code should port over quite well.. All you need to do is change hardware specifics...
     
  8. Abhik Chowdhury

    Abhik Chowdhury New Member

    Joined:
    Jun 23, 2015
    Messages:
    5
    Likes:
    0
    no no sir its not working getting stucked with the timer part i.e. TMR1h and TMR1L. PLZZ SUGGEST SOME WAY OUT about this.
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Post YOUR code and I'll see where its not working!!
     
  10. Abhik Chowdhury

    Abhik Chowdhury New Member

    Joined:
    Jun 23, 2015
    Messages:
    5
    Likes:
    0
    // LCD module connections
    sbit LCD_RS at RE0_bit;
    sbit LCD_EN at RE1_bit;
    sbit LCD_D4 at RE2_bit;
    sbit LCD_D5 at RE3_bit;
    sbit LCD_D6 at RE4_bit;
    sbit LCD_D7 at RE5_bit;

    sbit LCD_RS_Direction at TRISE0_bit;
    sbit LCD_EN_Direction at TRISE1_bit;
    sbit LCD_D4_Direction at TRISE2_bit;
    sbit LCD_D5_Direction at TRISE3_bit;
    sbit LCD_D6_Direction at TRISE4_bit;
    sbit LCD_D7_Direction at TRISE5_bit;
    // End LCD module connections

    void IntDetection() org 0x0014 //Interrupt on INT0
    {
    LATD++;
    IFS0.F0 = 0; //interrupt flag cleared



    TRISD.F0 = 0; //portd is output
    TRISE.F8 = 1; //portf is input
    IFS0 = 0; //interrupt flag cleared
    IEC0 = 1; //Interrupt is set on a rising edge at INT0 (RF6)
    while(1) asm nop;
    }
    int a;
    char txt[7];
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR); // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
    ADPCGF = 0XFFFF; //CONFIG ANALOG PINS ON PORTB TO DIGITAL
    TRISB.F4 = 1; //RB4 as Input PIN (ECHO)

    Lcd_Out(1,1,"Developed By");
    Lcd_Out(2,1,"electroSome");

    Delay_ms(3000);
    Lcd_Cmd(_LCD_CLEAR);

    T1CON = 0x8030; //Initialize Timer Module

    while(1)
    {
    TMR1H = 0; //Sets the Initial Value of Timer
    TMR1L = 0; //Sets the Initial Value of Timer

    PORTB.F0 = 1; //TRIGGER HIGH
    Delay_us(10); //10uS Delay
    PORTB.F0 = 0; //TRIGGER LOW

    while(!PORTB.F4); //Waiting for Echo
    T1CON.F0 = 1; //Timer Starts
    while(PORTB.F4); //Waiting for Echo goes LOW
    T1CON.F0 = 0; //Timer Stops

    a = (TMR1L | (TMR1H<<8)); //Reads Timer Value
    a = a/58.82; //Converts Time to Distance
    a = a + 1; //Distance Calibration\
    if(a>=2 && a<=400) //Check whether the result is valid or not
    {
    IntToStr(a,txt);
    Ltrim(txt);
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,1,"Distance = ");
    Lcd_Out(1,12,txt);
    Lcd_Out(1,15,"cm");
    }
    else
    {
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,1,"Out of Range");
    }
    Delay_ms(400);
    }
    }



    the timer part is not wrking in dspic30f4011 its timer 1 module is different from pic16.
     
    Last edited: Jun 25, 2015
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    For a start... you can't run everything in an interrupt... you need a main function... I'm surprised that mikroC hasn't complained that there is no main()...

    Secondly... Pic33 and 24 are 16 bit and there is no TMR1L and H just TMR1..
    Thirdly.. Timer 1 interrupt vector is 11 not 14..
    Fourthly.. INT0 is on RE8 not RF6..
    Lastly.... This chip supports input capture, which will do all this automatically..

    So! use the code posted... Change the interrupt to use vector 11 and change variable a to read TMR1

    ALSO!! I like mikroC's bit declarations RE8_bit = x; .. and while(!RE8_bit);

    Personally! I think you need to get to grips with something a bit simpler... This chip is way over your head..
     
  12. Abhik Chowdhury

    Abhik Chowdhury New Member

    Joined:
    Jun 23, 2015
    Messages:
    5
    Likes:
    0
    yes you r correct that dis chip is way over my head bt actualy i am a trainee in a compny so i hav beeen assigned with dis chip. so can u pleas help me by sending me the correct code plzzz. i hav tried in all the way bt failed so plzz can u help me
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    A) I don't use that chip...
    B) I haven't got MikroC for 16 bit chips

    I can however use C30 lite, but you will still have the same problem porting to mikroC..
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    If You are a trainee, then ask your company for tuition...
     
  15. Abhik Chowdhury

    Abhik Chowdhury New Member

    Joined:
    Jun 23, 2015
    Messages:
    5
    Likes:
    0
    thank u so much sir for your support. bt here things r nt like dat bt neway i wil fig it out thnx alot :)
     

Share This Page