Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to write a mikroc code for ultrasonic sensor

Status
Not open for further replies.
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.
 
Friend how can i Start timer and end timer?
please help me
 

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:
ok friend thank you
if you tell it tomorrow it is a priceless help for me
 

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
View attachment 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
  }
}
 
this code doesn't work correctly friend
it change distance but not relevant to distance
i think it shows the time
 

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
 

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

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..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top