Maxbotic Ultrasonic sensor how to control?

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

Re: Maxbotic Ultrasonic sensor how to control?

Postby robosang » Sat Nov 07, 2009 2:22 pm

Hahah! Finally is working? I din go through the code, but hopefully other that use Ultrasonic through PWM can benefit from your code :shock:
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby Light » Sun Feb 21, 2010 9:25 pm

thanks a lot for sharing your experience with others.
Im using MikroC PRO for compiling source codes...well..i have a problem when i tried to compile your code, the errors below occurs:
can't open include file #include <pic.h>
can't open include file #include <stdlib.h>
can't open include file #include <string.h>
can't open include file #include <stdio.h>
can't open include file #include "delay.h"
can't open include file #include "delay.c"

do i have to add those .H files into 'include' directory? if yes, can you please upload it here because i tried searching for them and i keep finding different results where i dont know which one is the correct one.

i have more questions about the source code (since im weak in programing) but i want to compile the code successfully first.

thank you
Light
Novice
 
Posts: 19
Joined: Wed Dec 02, 2009 2:57 am

Re: Maxbotic Ultrasonic sensor how to control?

Postby luqman hakim » Mon Feb 22, 2010 9:29 am

hi shao.. tq for you sharing about the code... i`m using MicroChip MPLAB IDE v8.43 to compile your last post source code that you said had been success.. but fail.. i don`t where is the error in your source code

i also have more questions about the source code (since i`m weak in programing) but prefer to compile the code successfully first...
luqman hakim
Newbie
 
Posts: 11
Joined: Wed Jul 01, 2009 3:49 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby luqman hakim » Mon Feb 22, 2010 9:58 am

can't open include file "delay.h": No such file or directory..
this is error that i found..
luqman hakim
Newbie
 
Posts: 11
Joined: Wed Jul 01, 2009 3:49 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby sich » Mon Feb 22, 2010 3:01 pm

Light WROTE:thanks a lot for sharing your experience with others.
do i have to add those .H files into 'include' directory? if yes, can you please upload it here because i tried searching for them and i keep finding different results where i dont know which one is the correct one.

I think Shao modified PR23's source code so you can find the .h files in PR23 sample code provided in Cytron website.

Light WROTE:Im using MikroC PRO for compiling source codes...well..i have a problem when i tried to compile your code, the errors below occurs:

Since you're using MikroC, you may find syntax errors when you compile this source code directly without modify it. It's written for Hitech C compiler i guess.

luqman hakim WROTE:can't open include file "delay.h": No such file or directory..
this is error that i found..

Same thing goes to luqman hakin too. You need to include the .h files.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 603
Joined: Tue Apr 21, 2009 2:15 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby luqman hakim » Mon Feb 22, 2010 9:42 pm

did u mean that i have to wrote like this

#include <delay.h>
but still error like this..
can't open include file "delay.h": No such file or directory
luqman hakim
Newbie
 
Posts: 11
Joined: Wed Jul 01, 2009 3:49 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby sich » Tue Feb 23, 2010 4:27 pm

Adding #include <delay.h> in your program means you want the program to find delay.h where some of your functions called are declared. If that's the case, where's your delay.h file? So your problem may be solved by copy the file and place it in the same folder as your main program. Now where to get the file? I think it's created by Cytron for PR23 and other products. You can have a try to download it from PR23's product page. Another way is ask Shao to confirm the source.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 603
Joined: Tue Apr 21, 2009 2:15 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby Light » Mon Mar 08, 2010 10:24 pm

hi again

i tried compiling the code in HI-TECH C Compiler....it wont work unless i delete the following lines:

#include "delay.h"
#include "delay.c"
.
.
.
DelayMs(250); //sensor module power up time

means if i delete these files the code will be compiled successfully...anyway these lines r not important in this code...but what if i need to use Delay later on ? thats why im asking what's the problem.

the other question is....in the following equation:
distance=value*1.75616; // calculate inch value per inch = 147us with 20Mhz internal clock.
how the number 1.75616 have being calculated ? the datasheet mentions that 147us is the pulse width..thats all.

tq
Light
Novice
 
Posts: 19
Joined: Wed Dec 02, 2009 2:57 am

Re: Maxbotic Ultrasonic sensor how to control?

Postby luqman hakim » Mon Mar 08, 2010 10:49 pm

hi light...
try cpoy, paste.. compile in MPLAB software

CODE: SELECT_ALL_CODE
#define   PIC_CLK 20000000 //change this to 3.6864, 4, 16 or 20MHz

//============================================================================
//   Include
//============================================================================
#include <pic.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "delay.h"
#include "delay.c"

//============================================================================
//   Configuration
//============================================================================
__CONFIG ( 0x3F32 );

//============================================================================
//   Function Prototype
//============================================================================
void ultrasonic(void);   // Find range based on input
void init(void);
void rangecal(void);
//============================================================================
//   global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;

//============================================================================
//   interrupt prototype
//============================================================================================================
static void interrupt isr(void)
{
   if(TMR0IF)         // TMR0 is overflow
   {
      TMR0IF=0;    // clear flag bit
      To+=0x100;      // count number of TMR0 overflow ( make it to 16bit TMR)
   }
   
}
//============================================================================
//   Main Function
//============================================================================
void main(void)
{
   init();
   ultrasonic();
}

// Initailization
// Description : Initialize the microcontroller
//============================================================================================================

void init()
{
   // Tris configuration (input or output)
   TRISA = 0b00000000;      //set PORTA as output
   TRISB = 0b00000100;      //set RB2 pin as input, other as output
   TRISC = 0b00000000;      //set PORTA as output
   
   RB4=1;   //5V to sensor
   DelayMs(250);  //sensor module power up time

   // TMR 0 configuation
   T0CS=0;                 
   PSA=0;                 
   PS2=1;                  // prescale 1:256
   PS1=1;                  //
   PS0=1;                  //
   TMR0IE=1;                  // TMR0 Interrupt
   TMR0=0;                 
     
   GIE = 1;   //global interrupt
}

//================================================================================
// FUNCTIONS
//================================================================================   
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
void ultrasonic(void)     
{     
   while(1)
   {   
   
      if(RB2==0)               //if RB2 is high
      {
      TMR0=0;            // clear all counter involved, start new count for period of RB2 high
      To=0;
      }     
      else
      {
      TH=TMR0+To;         // RB2 is 0 mean is falling from 1 save TH, RB2 high period
      value=TH;      //value of tmr0+to
      distance=value*1.75616;   // calculate inch value per inch = 147us with 20Mhz internal clock.   
      rangecal();
      }
   }
                                                         
}

void rangecal(void)
{
   
      if(distance>100)
      {
      PORTC=0B00000001;
      }
      else if(distance>80)
      {
      PORTC=0B00000010;
      }
      else if(distance>60)
      {
      PORTC=0B00000100;
      }
      else if(distance>40)
      {
      PORTC=0B00001000;
      }
      else if(distance>20)
      {
      PORTC=0B00010000;
      }
}


at first, the code it will looks like this to success... build successfully..


then..
create one text file...

CODE: SELECT_ALL_CODE
/*

KeyGhost

Designed by Shane Tolmie Apr 99.

Microprocessor: see mainkg.c

Compiled with:   see mainkg.c

Overall goal:    Delay Routines

*/

#ifndef __DELAY_C
#define __DELAY_C

#include   "always.h"
#include   "delay.h"

void DelayBigUs(unsigned int cnt)
{
   unsigned char   i;

   i = (unsigned char)(cnt>>8);
   while(i>=1)
   {
      i--;      
      DelayUs(253);
      CLRWDT();
   }
   DelayUs((unsigned char)(cnt & 0xFF));
}


void DelayMs(unsigned char cnt)
{
   unsigned char   i;
   do {
      i = 4;
      do {
         DelayUs(250);
         CLRWDT();
      } while(--i);
   } while(--cnt);
}
/*
//this copy is for the interrupt function
void DelayMs_interrupt(unsigned char cnt)
{
   unsigned char   i;
   do {
      i = 4;
      do {
         DelayUs(250);
      } while(--i);
   } while(--cnt);
}
*/

void DelayBigMs(unsigned int cnt)
{
   unsigned char   i;
   do {
      i = 4;
      do {
         DelayUs(250);
         CLRWDT();
      } while(--i);
   } while(--cnt);
}

/*
void DelayS(unsigned char cnt)
{
   unsigned char i;
   do {
      i = 4;
      do {
         DelayMs(250);
         CLRWDT();
      } while(--i);
   } while(--cnt);
}
*/

#endif




then save as delay.txt
after that copy this file in the folder in your project.. try it..!
luqman hakim
Newbie
 
Posts: 11
Joined: Wed Jul 01, 2009 3:49 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby ober » Mon Mar 08, 2010 10:56 pm

Light WROTE:hi again

i tried compiling the code in HI-TECH C Compiler....it wont work unless i delete the following lines:

#include "delay.h"
#include "delay.c"

Normally if you include library or function from other files, we include the header file only, not the c file. So in your case, it should be:
#include "delay.h" only.

However, using "xxxx.h" mean the file is in the compiler library where it will automatically search the file. If you use <xxxx.h> mean the file is in the same folder as your program C file, or project folder.

the other question is....in the following equation:
distance=value*1.75616; // calculate inch value per inch = 147us with 20Mhz internal clock.
how the number 1.75616 have being calculated ? the datasheet mentions that 147us is the pulse width..thats all.
tq

You need to understand the relationship between value and timing. What is the value represent? How do it obtain the value? from Timer0, what is the prescaler? What does 1 or 2 or 3 and so on represent? I think PR23 did explain the ultrasonic (using PWM) in detail, you can start from there. Anyway I do not sure where you get the formula from, you need to really check with the source that wrote this code.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

PreviousNext

Return to Sensor

Who is online

Users browsing this forum: No registered users and 14 guests

cron