ultrasonic hc sr04

Digital Fiber, Photoelectric, Laser Range, Optical, Temperature, Rotary Encoder, Ultrasonic, Gas, Gyro, Accelerometer, FlexiBend, Flexiforce, Compass......

Re: ultrasonic hc sr04

Postby captenusop » Fri Jan 06, 2012 11:47 am

Tq,, everyone.. now i have modified the signal to trig... yes i have done the TRISB.
But I don’t rely know how to use timer0/1,, I just assign the signal as variable then display it on lcd,,
I think that might be the problem.
Somebody can help me with reading signal, how to setup timer0/1,, and how to use it...
Tq..
CODE: SELECT_ALL_CODE
#include <htc.h>
#include <stdio.h>
#include "lcd.c"
#define _XTAL_FREQ 20000000
__CONFIG(0X3f32); //configuration bits@ microcontroller

void pulse ( void ) ;
void __delay_sec(int x);  // function delcaration

//*********************************************
void main(void)
{  //start main
   int h; //variable
   TRISB=0b00000001;
   PORTB=0b00000000;
   lcd_init();   
    h=RB0;
   lcd_clear();
   while(1){   //start endless loop
         
         pulse();
         h=RB0 ;
         printf(" ULTRASONIC %d"   ,h);
         __delay_sec(10);
             lcd_clear();
         h=0;      
    }
} //end of while
//*********************************************

void pulse ( void )
{   RB1=0;
   __delay_us(2);
   RB1=1;
   __delay_us(10);
   RB1=0;
}

void __delay_sec(int x)  // function delcaration
{
 int j;
 int k;
 for(j=0;j<x;j++)
     for(k=0;k<5;k++)
    __delay_ms(20);
}
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: ultrasonic hc sr04

Postby ABSF » Fri Jan 06, 2012 10:28 pm

captenusop WROTE:how to setup timer0/1,, and how to use it..


They are all in the datasheet under the chapter "Timer 0" and "Timer 1"

Or you can google with "timer with 16F877a"

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: ultrasonic hc sr04

Postby ABSF » Sun Jan 08, 2012 7:51 pm

I have scribber some pseudo codes to emulate the function PulseIn() as follows:

CODE: SELECT_ALL_CODE
pseudo codes to emulate PulseIn()

// clock = 4MHz,  Timer 1 is used

TMR1H=TMR1L=0X00       //SETUP TIMER 1 = 0000
TMR1CS=0                  //TIMER MODE.  a count of 1 = 1uS
TICKPS=00                  //PRESCALER 1:1

MAIN()
{
 PULSE()         //SEND TRIG PULSE
 TMR1ON=1      //START TIMER 1
 WAIT TILL ECHO=0   // echo will go from H->L
 TMR1ON=0      //STOP TIMER 1
 DURATION = TMR1 VALUE  // Duration is 16 bit but TMR1L & TMR1H  are 8 bit
 reset tmr1 to 0x0000        // optional
 CONVERT DURATION TO DISTANCE         //see ober's post
 DISPLAY ON LCD              // must init LCD at the beginning
}


I haven't tried out the above so it would act just as a guide to your program.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: ultrasonic hc sr04

Postby captenusop » Tue Jan 10, 2012 9:22 am

:lol:
I WILL TRY IT,, THANKS
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: ultrasonic hc sr04

Postby rezza_mrkool » Mon Apr 30, 2012 1:14 am

So? what is the coding for timer ?? can someone post the complete coding for this ultrasonic hc sr04...plzzzzz.....
rezza_mrkool
Newbie
 
Posts: 8
Joined: Mon Apr 30, 2012 1:10 am

Re: ultrasonic hc sr04

Postby rezza_mrkool » Tue May 01, 2012 2:57 am

In L-H-L trigger mode, why u need 2us then 10us then no delay.....in datasheet it stated that the repeat interval of trig signal should be greater than 60ms to avoid interference between connective measurements. Another thing, how to set up timer and write program to calculate time (us) wave between trigger to obstacle....sry im newbie here....thx
rezza_mrkool
Newbie
 
Posts: 8
Joined: Mon Apr 30, 2012 1:10 am

Re: ultrasonic hc sr04

Postby ABSF » Thu May 03, 2012 12:10 pm

rezza_mrkool WROTE:In L-H-L trigger mode, why u need 2us then 10us then no delay.....in datasheet it stated that the repeat interval of trig signal should be greater than 60ms to avoid interference between connective measurements.


If you refer to the project here, then everything would make sense...

Arduino Ultrasonic Ranging With HC-SR04 | ITead Studio
http://iteadstudio.com/application-note ... h-hc-sr04/

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: ultrasonic hc sr04

Postby rezza_mrkool » Fri May 04, 2012 2:36 am

ok....i understand that....but i cant calculate the formula plus using the timer....can someone check my coding for any mistake....plzzz....my project due date is around the corner....

CODE: SELECT_ALL_CODE
#include <pic.h>
__CONFIG (0x3732);

#define trigger         RC2
#define echo         RC3
#define _XTAL_FREQ      20000000
#define   MHZ   *1000L
#define   KHZ   *1

#define   DelayUs(x)   { unsigned char _dcnt; \
           _dcnt = (((x)*(20MHZ))/(24MHZ))|1; \
           while(--_dcnt != 0) \
              continue; \
           _dcnt = (((x)*    (20MHZ))/(24MHZ))|1; \
           while(--_dcnt != 0) \
              continue; }

void DelayMs(unsigned char y);

void main (void)
{
   unsigned char i;
   unsigned int time;
   unsigned int distance;
   
   TRISC = 0b00001000;
   PORTC = 0b00000000;
   TRISD = 0b00000000;
   PORTD = 0b00000000;
   

   T1CON = 0b00010000;
   TMR1H = 0;
   TMR1L = 0;

   while(1)
   {
      for (i=0;i<8;i++)
      {   TMR1H=0;
         TMR1L=0;

         trigger = 0;
         DelayUs(2);

         trigger = 1;
         DelayUs(10);

         trigger = 0;
         

         while (echo==1);
         TMR1ON = 1;
         while (echo==0);
         TMR1ON = 0;

         time = TMR1H;
         time = (time<<8)|TMR1L;
         
      }      

                 distance = ((time*340)/2)*100                        //in cm
      
      if(distance>15){
      PORTD = 0b10000000;
      }
   }
}

void DelayMs(unsigned char y)
{
   unsigned char   i;
   do {
      i = 4;
      do {
         DelayUs(250);
      } while(--i);
   } while(--y);
}
rezza_mrkool
Newbie
 
Posts: 8
Joined: Mon Apr 30, 2012 1:10 am

Re: ultrasonic hc sr04

Postby ABSF » Fri May 04, 2012 8:51 am

Checking others program needs a lot of time and is a tedious job. You should run your program and tell us what is wrong and where you suspected the parts that caused the problem.

Your timer1 is running on a prescaler of 1:2.
Did you verify that the delay_us() is correct using debug?
I suspect something is not right here too:

while (echo==1); //if echo is detected
TMR1ON = 1; //turn on timer1
while (echo==0); //if echo stops
TMR1ON = 0; //stop timer1


Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: ultrasonic hc sr04

Postby rezza_mrkool » Fri May 04, 2012 2:27 pm

Right...im running on 1:2 prescalar...im verify delay us according to cytron example c code....my problem is how to write program on formula calculation and to get distance time....btw, what's wrong with the code u mention?
while (echo==1); //if echo is detected
TMR1ON = 1; //turn on timer1
while (echo==0); //if echo stops
TMR1ON = 0; //stop timer1
rezza_mrkool
Newbie
 
Posts: 8
Joined: Mon Apr 30, 2012 1:10 am

PreviousNext

Return to Sensor

Who is online

Users browsing this forum: No registered users and 13 guests