Maxbotic Ultrasonic sensor how to control?

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

Maxbotic Ultrasonic sensor how to control?

Postby Shao » Mon Oct 26, 2009 10:39 am

Hi,

I planning to use ultrasonic sensor which had PWM output which can direct use/read in Pic16f877A. But in programming wise, I facing problem in using timer. Anyone can help?

I referred sample code in Cytron DIY PR23 but its ain't help me much.

Sample code I modified:

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

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

//============================================================================
//   Define
//============================================================================
//============================================================================
//   Function Prototype
//============================================================================
void rangecalc(void);        // Find range based on input
void init(void);
//============================================================================
//   global variable
//============================================================================

// Sensor declarations
unsigned int To=0,TH=0;
unsigned int value;      // Range to be used for calculation


//============================================================================
//   Main Function
//============================================================================
void main(void)
{
   DelayMs(800);
   init();   
   rangecalc();
}

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

void init()
{
   // Tris configuration (input or output)
   TRISA = 0b00000011;            //set RA0 and RA1 pin as input,other as output

   // TMR 0 configuation
   T0CS = 0;                  
   PSA = 0;                  
   PS2 = 1;                  // prescale 1:32
   PS1 = 1;                  //
   PS0 = 1;                  //
   TMR0IE = 1;                  // TMR0 Interrupt
   TMR0 = 0;
                        
}

//================================================================================
// FUNCTIONS
//================================================================================   
//**************************************************
// rangecalc() - Calculate the range from the sensor depending on type of range
void rangecalc(void)
{   
   if(RA1 == 0)
   {
   TMR0 = 0;   // clear all counter involved, start new count for period of RA1 high
   To = 0;      
   }

   else
   {
   TH = TMR0 + To;   // RA1 is 0 mean is falling form 1, save TH, RA1 high period
   value = TH;   
   }      
   
}



Any comment?
Shao
Newbie
 
Posts: 8
Joined: Mon Oct 26, 2009 10:31 am

Re: Maxbotic Ultrasonic sensor how to control?

Postby robosang » Mon Oct 26, 2009 2:49 pm

Alamak.... paste the source code again.....

Try it on your board. How do you measure the pulse width? You want to use timer0 but do you know how timer0 work? You created function to check pulse at RA1, but did you configure it as digital input? you put if and else to check for the pulse to ON and OFF, but you didn't loop it, how can it measure? Sorry to say the code will not work.

Even in rangecalc function, the if(RA1==0), you are actually checking for it to be low, but all your comments, and codes under if and else lead to checking for RA1 to be high. This is not going to work. Please and I say it again, please go through your code carefully, don come to forum and put this careless code hoping for solution.

1. Configure PORTA as digital input or use other port.
2. Understand how timer0 works if you want to use timer0.
3. If you use polling method, please wait for the pulse, starting from ON to OFF.
4. If you wanted to use interrupt, you need to understand timer0 interrupt.
5. Finally, when you obtain the value, you need to use it, either display it on LCD, or convert it to some output such as LEDs, and etc.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby sich » Mon Oct 26, 2009 3:03 pm

Fuiyo...someone is very angry. Hey robosang, really appreciate your contribution in this forum.

Shao,
First of all, do as robosang advised. He has answered your question and that're what any forumer can give at the moment. Then please paste your source code using the Code Display option labelled "Code" at the top of writing column. Your code will look like this:

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

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

//============================================================================
//   Define
//============================================================================
//============================================================================
//   Function Prototype
//============================================================================
void rangecalc(void); // Find range based on input
void init(void);
//============================================================================
//   global variable
//============================================================================

// Sensor declarations
unsigned int To=0,TH=0;
unsigned int value;    // Range to be used for calculation


//============================================================================
//   Main Function
//============================================================================
void main(void)
{
DelayMs(800);
init();   
rangecalc();
}

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

void init()
{
// Tris configuration (input or output)
TRISA = 0b00000011;    //set RA0 and RA1 pin as input,other as output

// TMR 0 configuation
T0CS = 0;   
PSA = 0;   
PS2 = 1;    // prescale 1:32
PS1 = 1;    //
PS0 = 1;    //
TMR0IE = 1;    // TMR0 Interrupt
TMR0 = 0;

}

//================================================================================
// FUNCTIONS
//================================================================================   
//**************************************************
// rangecalc() - Calculate the range from the sensor depending on type of range
void rangecalc(void)
{   
if(RA1 == 0)
{
TMR0 = 0;   // clear all counter involved, start new count for period of RA1 high
To = 0;   
}

else
{
TH = TMR0 + To;   // RA1 is 0 mean is falling form 1, save TH, RA1 high period
value = TH;   
}   

}


Very nice isn't it? 8-)
~> 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 ober » Mon Oct 26, 2009 3:20 pm

Oh... sorry to see robosang pissed off. Anyway, everyone is still welcome here to post question, just try to follow the rules and regulations.

Yup, sich has given a good example for pasting source code.

robosang also given a good guidance though he is a bit over re-act. Thanks for the guidance, man! We hope to get more people giving consultation.

Shao,
please do go through your code carefully. It seem that there are a lot of bugs. Timer0 can be used to measure the pulse width of ultrasonic sensor, however, like robosang pointed out. There are many methods. Good luck!
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby Shao » Mon Oct 26, 2009 11:06 pm

Robosang,

Thank for the advices, sorry to make you pissed off. Learning in progress.

I learned how to use timer0 today by consulting lecturer, hopefully I can do something with it and come out with a better code. Btw, how to set port A as digital I/O? I thought just needed to TRISA 0b00000011 (1-input,0-output)?

Sich,

Sorry for not reading rules&regulations. Kinda rush this morning and this account also created today. I will remember to use the "code" function in posting in forum.

Ober,

Yea...I need some luck here...Thank you
Shao
Newbie
 
Posts: 8
Joined: Mon Oct 26, 2009 10:31 am

Re: Maxbotic Ultrasonic sensor how to control?

Postby Shao » Tue Oct 27, 2009 2:31 am

Need some guide here...

Firstly, I used timer0 to calculate the time needed for the sensor to generate a PWM feedback signal after supply 5V to the sensor. After that I just divide the timer0 value with 2 due to the sound travel go and back. But how should I convert the value of timer0 to inch unit and measure my distance? example: Let say how many below x inch it will light up a LED.

Hm I not sure my code will be working or not but basically that my limit for today.
CODE: SELECT_ALL_CODE
//============================================================================
//   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);
//============================================================================
//   global variable
//============================================================================
//============================================================================
//   Main Function
//============================================================================
void main(void)
{
   DelayMs(800);
   init();
   RA1=1;
   DelayMs(250);  //sensor module power up time
   ultrasonic();
}

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

void init()
{
   // Tris configuration (input or output)
   TRISA = 0b00000001;      //set RA0 pin as sensor input,other as output (RA1 power on sensor)
   TRISB = 0b00000000;      //set PORTB as output

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

//================================================================================
// FUNCTIONS
//================================================================================   
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
void ultrasonic(void)      
{
   
while(1)                     // loop forever
   {                                          
   unsigned int value;
   unsigned int distance;

      if(RA0 == 1)
      {
      TMR0 = 0;   // clear timer counter involved, start new count for period when RA0 detected high
      value = 0;
      }
      
      else if (RA0 == 0)
      {
      value = TMR0;   // save value when RA0 low period, TMR0 continue counting
      distance = ((value/2)/147);   // how to calculate inch value? per inch = 147us with 20Mhz internal clock.
   
         if (distance>40)         
         {
         RB1=1;
         }
         else if (distance>30)                        
         {
         RB2=1;
         }
         else if (distance>20)                                       
         {
         RB3=1;
         }
         else                                                    
         {
         RB4=1;
         }
   
      }
      
   }
}


Don't hesitate to comment me...I'm willing learn from mistake...
Shao
Newbie
 
Posts: 8
Joined: Mon Oct 26, 2009 10:31 am

Re: Maxbotic Ultrasonic sensor how to control?

Postby ober » Tue Oct 27, 2009 3:00 pm

You do not need to control the power to ultrasonic sensor in order to obtain the PWM from it. The PWM is output from ultrasonic sensor is being transmitted out from the sensor consistently. We need to measure the width of PWM in order to obtain current distance of obstacle in front of ultrasonic. Bare in mind, we are not controlling the sensor, we are reading it.

I did not go through your code in depth, as robosang say, since you are using PORTA, you need to configured it to digital. if you go through data sheet under PORTA, you will notice there is a register named "ADCON1", you need to take care of that.

Have you tested your code in hardware?
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby Shao » Mon Nov 02, 2009 8:31 pm

Anyone can give me some hint in programming ultrasonic sensor? I tried for whole week with my hardware still cant able to use it.

My project:
I give an output to RB2 to powerup my sensor and grounded it too with cytron SK40B. I received the signal from the PWM output from sensor. In my programming, I calculate the time used when signal is high on pin(RB4) using timer0 in Pic16F877A(20Mhz). I tried to give an output on PortA with the value.


Problem:
I get the value from timer0 but I dot know how to convert it into distance as 147microsecond equal to 1 inch(resolution of sensor). Any comment?

Code:
CODE: SELECT_ALL_CODE
//============================================================================
//   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);
//============================================================================
//   global variable
unsigned int result;
unsigned int To=0,TH=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)
   }
   
   if (RB2==1)
   {                                                        
      if (RB4)               // RB4 is 1 mean is rising from 0       
      {
      TMR0 = 0;            // clear all counter involved, start new count for period of RB4 high
      To = 0;
      }
   
      else TH = TMR0 + To;         // RB4 is 0 mean is falling from 1 save TH, RB1 high period
   }
}
   
//============================================================================
//   Main Function
//============================================================================
void main(void)
{
   DelayMs(800);
   init();
   RB2=1;
   DelayMs(250);  //sensor module power up time
   ultrasonic();
}

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

void init()
{
   // Tris configuration (input or output)
   TRISA = 0b00000000;      //set PORTA as output
   TRISB = 0b00010000;      //set RB1 pin as input, other as output

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

//================================================================================
// FUNCTIONS
//================================================================================   
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
void ultrasonic(void)      
{
while(1)                     // loop forever
   {                                          
   unsigned int value;
   unsigned int distance;
   unsigned int data;

   data = TH;   //value of tmr0+to
   //value = data??
   distance=value;      
      while (distance>3)
      {
      RA0=1;
      }
      while(distance>2)
      {
      RA1=1;
      }
      while(distance>1)
      {
      RA2=1;
      }   
   }
}


Somebody save me plz...haih....cracking my head for 2 weeks alrdy...
Shao
Newbie
 
Posts: 8
Joined: Mon Oct 26, 2009 10:31 am

Re: Maxbotic Ultrasonic sensor how to control?

Postby ober » Wed Nov 04, 2009 2:01 pm

hi, cracking your head will not solve anything. Again, you need to understand the concept of measuring a pulse width.

In the program I notice you created a ISR for TMR0 interrupt. In there you also check RB2 and following checking the RB4 which connected to ultrasonic output. There is also a To to make TMR0 become 16 bit counter.

Well, 1st thing that I need to highlight to you is the prescaler for TMR0, that is too big, if you are writing the code accordingly to your comment. Timer0's clock source is from Fosc/4. if you assign 256 to it, the clock source will become ~19Khz if you are using 20MHz crystal, so you will get a pulse every 51.2us. Since you use TMR0 to interrupt at overflow, mean after 256 pulses, it will interrupt every 13.1ms. This is a fix interval of interrupt because your interrupt depend on TMR0 overflow and it is always fix. It has nothing to do with the ultrasonic output at all.

You need to monitor the PWM pin from ultrasonic, and obtain timer for calculation. In the sample code that you provide, you are monitoring timer0 and then check the PWM pin.

____|--|_______|-----|___


Let's say you have signal like shown above, your interrupt must be at the rising edge. The first interrupt, start timer/clear timer, 2nd interrupt at falling edge, copy the value in the timer and do calculation. Please study capture mode under CCP and you can also use Interrupt on Change for Port B.

Good luck!
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: Maxbotic Ultrasonic sensor how to control?

Postby Shao » Sat Nov 07, 2009 2:01 am

Hi...thank for the comment.

Sharing my working code here =)

Finally...

CODE: SELECT_ALL_CODE
//============================================================================
//   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;
      }
}
Shao
Newbie
 
Posts: 8
Joined: Mon Oct 26, 2009 10:31 am

Next

Return to Sensor

Who is online

Users browsing this forum: No registered users and 14 guests

cron