+ Post New Thread
Page 2 of 2 FirstFirst 1 2
Results 21 to 31 of 31
  1. #21
    Full Member level 5
    Points: 2,598, Level: 11
    Magvitron's Avatar
    Join Date
    Aug 2012
    Location
    Kollam (Quilon), India, India
    Posts
    241
    Helped
    71 / 71
    Points
    2,598
    Level
    11
    Blog Entries
    2

    Re: how to write a mikroc code for ultrasonic sensor

    im afraid, its a no. you have to start counting as soon as the trigger signal is given and then when you get the echo pulse you got to stop that. code like that.
    The occasional visit of success provides just the excitement an engineer needs to face work the following day.
    Koichi Tanaka



  2. #22
    Full Member level 1
    Points: 672, Level: 5

    Join Date
    Jul 2012
    Posts
    119
    Helped
    0 / 0
    Points
    672
    Level
    5

    Re: how to write a mikroc code for ultrasonic sensor

    How can i do that ?



    •   Alt15th August 2012, 18:07

      advertising

        
       

  3. #23
    Full Member level 5
    Points: 2,598, Level: 11
    Magvitron's Avatar
    Join Date
    Aug 2012
    Location
    Kollam (Quilon), India, India
    Posts
    241
    Helped
    71 / 71
    Points
    2,598
    Level
    11
    Blog Entries
    2

    Re: how to write a mikroc code for ultrasonic sensor

    you need to think like this.
    send signal : start the timer
    receive signal : end timer
    delay : the value of timer.
    The occasional visit of success provides just the excitement an engineer needs to face work the following day.
    Koichi Tanaka



  4. #24
    Full Member level 1
    Points: 672, Level: 5

    Join Date
    Jul 2012
    Posts
    119
    Helped
    0 / 0
    Points
    672
    Level
    5

    Re: how to write a mikroc code for ultrasonic sensor

    Friend how can i Start timer and end timer?
    please help me



    •   Alt15th August 2012, 19:01

      advertising

        
       

  5. #25
    Full Member level 5
    Points: 2,598, Level: 11
    Magvitron's Avatar
    Join Date
    Aug 2012
    Location
    Kollam (Quilon), India, India
    Posts
    241
    Helped
    71 / 71
    Points
    2,598
    Level
    11
    Blog Entries
    2

    Re: how to write a mikroc code for ultrasonic sensor

    better to refer datasheet.. refer to page # 75 ... will give a hint..

    btw feeling a bit sleepy, will give you some code tomorrow :) sleep well
    Last edited by Magvitron; 15th August 2012 at 20:01.
    The occasional visit of success provides just the excitement an engineer needs to face work the following day.
    Koichi Tanaka



    •   Alt15th August 2012, 19:38

      advertising

        
       

  6. #26
    Full Member level 1
    Points: 672, Level: 5

    Join Date
    Jul 2012
    Posts
    119
    Helped
    0 / 0
    Points
    672
    Level
    5

    Re: how to write a mikroc code for ultrasonic sensor

    ok friend thank you
    if you tell it tomorrow it is a priceless help for me



  7. #27
    Full Member level 5
    Points: 2,598, Level: 11
    Magvitron's Avatar
    Join Date
    Aug 2012
    Location
    Kollam (Quilon), India, India
    Posts
    241
    Helped
    71 / 71
    Points
    2,598
    Level
    11
    Blog Entries
    2

    Re: how to write a mikroc code for ultrasonic sensor

    hey i wrote this.. might be crude but here goes..
    T1CON = 0b00110100; // config timer refer page 79 of datasheet
    im using timer 1 here.
    here we use prescalor of 8, and the internal clock ,precisly fosc/4
    so one increment will be (let the crstal you use is 8mhz)
    8m/(4*8) ie 250000hz or 4us
    distance traveled by sound is about 343 m/s (in air at 20degrees) so for every 4us that will be
    1.372 mm
    PS. the sound is reflected back, double distance ;) so we have to divide the value from timer
    with 2.
    heres the proteus design
    ultrasound.rar

    Code:
    char txt[6];
    #define TRIG PORTB.F6
    #define ECHO PORTB.F7
    // Keypad module connections
    
    // End Keypad module connections
    
    // 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
            double distance;
            float final_dist;
    void main() {
      Lcd_Init();                           //Initialize LCD
      Lcd_Cmd(_LCD_CLEAR);                  // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);             // Cursor off
      Lcd_Out(1,1,"UltraSonicxx");
      TRISB.F6=0;//port b.6 as out port
      TRISB.F7=1;//portb.7 as input from the module
      T1CON = 0b00110100; // config timer refer page 79 of datasheet
      /*
        here we use prescalor of 8, and the internal clock ,precisly fosc/4
        so one increment will be (let the crstal you use is 8mhz)
        8m/(4*8) ie 250000hz or 4us
        distance traveled by sound is about 343 m/s (in air at 20degrees) so for every 4us  that will be
        1.372 mm
        the sound is reflected back, so we have to divide the value from timer
        with 2
      */
    
      while(1)
      {
      //trigger signal to module
      TRIG = 0;
      delay_us(10);
      TRIG = 1;
      delay_us(10);
      T1CON.TMR1ON =1;   //start the timer
      TRIG = 0;
      while(ECHO==0);  //wait for echo
      T1CON.TMR1ON =0; //stop the timer (timer1)
      Lcd_Out(1,1,"UltraSonicxx ");
      distance = TMR1H + TMR1L;
      distance = distance/2;
      final_dist = distance * 1.372;
      Floattostr(distance,txt);
      lcd_out(2,1,txt) ;
      lcd_out(2,15,"mm") ;
      Delay_ms(500);
      Lcd_Cmd(_LCD_CLEAR);                  // Clear display
      }
    }
    The occasional visit of success provides just the excitement an engineer needs to face work the following day.
    Koichi Tanaka



  8. #28
    Full Member level 1
    Points: 672, Level: 5

    Join Date
    Jul 2012
    Posts
    119
    Helped
    0 / 0
    Points
    672
    Level
    5

    Re: how to write a mikroc code for ultrasonic sensor

    this code doesn't work correctly friend
    it change distance but not relevant to distance
    i think it shows the time



  9. #29
    Full Member level 5
    Points: 2,598, Level: 11
    Magvitron's Avatar
    Join Date
    Aug 2012
    Location
    Kollam (Quilon), India, India
    Posts
    241
    Helped
    71 / 71
    Points
    2,598
    Level
    11
    Blog Entries
    2

    Re: how to write a mikroc code for ultrasonic sensor

    Code:
    /*****************************************************************************
     * Distance calc
     *  pic 16f887a .
     *  Portrait orientation
     *****************************************************************************
     * FileName:        magvitron.mcppi
     * Dependencies:    timer 1
     * Processor:       PIC16F87A
     * Compiler:        MIKRO C PRO FOR PIC  V 1.65 (2009)
     * Company:         Edaboard
     *
     * Software License Agreement
     *
     * Copyright © 2011 xxxxxx  All rights reserved.
     *
     * Author               Date        Comment
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Manukrishnan        18/08/12        ...
     *****************************************************************************/
    char txt[14];
    #define TRIG PORTB.F6
    #define ECHO PORTB.F7
    // Keypad module connections
    // End Keypad module connections
    // 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 distance;
    int test;
    //main function
    void main() {
      Lcd_Init();                                                                    //Initialize LCD
      Lcd_Cmd(_LCD_CLEAR);                                                           // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);                                                      // Cursor off
      Lcd_Out(1,2,"((--Distance--))");
      TRISB.F6=0;                                                                   //port b.6 as out port
      TRISB.F7=1;                                                                   //portb.7 as input from the module
      T1CON = 0b00000000;                                                           // config timer refer page 79 of datasheet
      INTCON =0b11000000;
      PIE1 =0x01;
      /*
        >>here we use prescalor of 8, and the internal clock ,precisly fosc/4
        so one increment will be (let the crstal you use is 8mhz)
        8m/(4*8) ie 250000hz or 4us
        >>distance traveled by sound is about 343 m/s (in air at 20degrees)
        for 30 uS the sound will travel about 1 c.m.
        (we have a precision of 1c.m if you need to change by calculating the number 
        of increment for the patricular length and then changing the calculation 
        accordingly
        >>the sound is reflected back, so we have to divide the value from timer
        with 2
      */
    
      while(1)
      {
      if(test ==1)
      {
       lcd_out(2,15,"*Error : Ovflw*") ;
       delay_ms(1000);
       test =0 ;
      }
      /**********************************
      trigger pulse to the module
      **********************************/
      TRIG = 0;
      delay_us(10);
      TRIG = 1;
      /**********************************
      end trigger, Start Timer 1
      **********************************/
      T1CON.TMR1ON =1;
      /**********************************
      End timer,
      **********************************/
      TRIG = 0;
      /**********************************
      Listen for echo
      **********************************/
      while(ECHO==0);
      /**********************************
      Gottcha!, turn timer off
      **********************************/
      T1CON.TMR1ON =0;
      /**********************************
      whats the distance?
      **********************************/
      distance = (4*TMR1H)+(TMR1L/60);
      /* distance calculation, the timer 1 is
      used here, as the lower nibble of it overflows,
      it will increment the higher nibble.
      the incrementing will be @246 or at 150us.
      that corresponds to 4 cm, so we have to multiply the
      higher nibble with 4 and add it will the current
      value if TMR1L/60 for distance calculation.
      */
      TMR1H=TMR1L=0;
      /**********************************
      distance to LCD.
      **********************************/
      inttostr(distance,txt);
      lcd_out(2,3,txt) ;
      lcd_out(2,11,"cm") ;
      Delay_ms(500);
      /**********************************
      End routine
      **********************************/
      }
      }
      /**********************************
      check for overflow of timer1
      **********************************/
    void interrupt()
    {
      if (PIR1.TMR1IF)
       {
       test =1;
       distance = distance +1;
       PIR1.TMR1IF = 0;
       }
      }
    hey i updated it ..
    this might work with an accuracy of 1 c.m
    The occasional visit of success provides just the excitement an engineer needs to face work the following day.
    Koichi Tanaka



    •   Alt18th August 2012, 10:57

      advertising

        
       

  10. #30
    Newbie level 4
    Points: 105, Level: 1

    Join Date
    Feb 2013
    Posts
    5
    Helped
    0 / 0
    Points
    105
    Level
    1

    Re: how to write a mikroc code for ultrasonic sensor

    i need to write code for HC SR-04 in C18 compiler.....plz help me to solve this...



  11. #31
    Advanced Member level 4
    Points: 7,584, Level: 20
    Achievements:
    7 years registered
    paulfjujo's Avatar
    Join Date
    Jun 2008
    Location
    France 01120
    Posts
    1,264
    Helped
    257 / 257
    Points
    7,584
    Level
    20

    Re: how to write a mikroc code for ultrasonic sensor

    Quote Originally Posted by Nipuna56 View Post
    i think according to this code it measure the time between send signal(trig) and received signal(echo)
    is that correct?
    Yes...
    but where do you arm TMR0 TMR0_ON ? for counting.
    What is your PIC reference 16F or 18F ..

    Code:
     
         distance = (double)TMR0 * 4.352;
        wordtostr(distance,txt);
      lcd_out(1,6,txt) ;
    you can't convert a double float as an unsigned int..
    use FloatToStr function..



--[[ ]]--