Special Offer - EasyPIC v7 with FREE LCD and GLCD with TouchPanel
It is currently 23 Aug 2014 16:35

All times are UTC + 1 hour




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: 26 Jul 2012 12:47 
Offline

Joined: 10 Jul 2012 09:23
Posts: 36
I need to know how to write a code for ultrasonic sensor using Mikroc for measure the distance plz help friend I'm new to electronic field please help


Top
 Profile  
 
PostPosted: 26 Jul 2012 15:41 
Offline

Joined: 27 Jun 2010 12:07
Posts: 554
Location: England
Libstock does not appear to have a complete project for MikroC, only for Pascal.

... but there is a library that might get you started.

http://www.libstock.com/projects/view/240/ultrasonic-sensor-range-finder-library

Have not tried it for myself

_________________
Experienced hobbyist


Top
 Profile  
 
PostPosted: 27 Jul 2012 03:26 
Offline

Joined: 10 Jul 2012 09:23
Posts: 36
can u please explain this code

void interrupt();
void main() {
OPTION_REG.INTEDG=0;
INTCON.GIE=1;
INTCON.INTE=1;
TRISA=0x00;
PORTA=0x00;
while(1){
}
}
void interrupt(){
 if(INTCON.INTF==1){
 PORTA=0xFF;
 Delay_ms(1000);
 PORTA=0x00;
 INTCON.INTF=0;
 }   }


Top
 Profile  
 
PostPosted: 27 Jul 2012 06:30 
Offline

Joined: 27 Jun 2010 12:07
Posts: 554
Location: England
Nipuna56 wrote:
can u please explain this code

I can maybe explain what I think the code is trying to do, but the code is hard to excuse for the following reasons:

1 It is uncommented

2 The braces are badly formatted, making it hard to follow

3 Putting a long delay within an interrupt routine is usually considered bad practice

It looks to me to be a simple test program just to prove that an interrupt triggers in response to external event.
It simply lights one or more LEDs connected to PORTA (presumably), holds them on for 1 second (to give enough time for a user to see the LEDs light), then turns the LEDs off again.

This is only guesswork, since the code has no comments to help, and you offer no other information, such as a circuit diagram, to give more clues.

Quote:
can u
Please avoid text-speak on this forum. This should be "Can you" thanks

_________________
Experienced hobbyist


Top
 Profile  
 
PostPosted: 08 Aug 2012 14:45 
Offline

Joined: 10 Jul 2012 09:23
Posts: 36
ok friend

//Crystal 4MHz
//speed of sound 340m/s
//1 TMR0 cycle = (340/1000000)*256*0.5*100 = 4.352 in cm
#define TRIG PORTD.F0
#define ECHO PORTD.F1

double distance;
void main() {

TRISD.F0 = 1;
TRISD.F1 = 0;
TRISC=0;

OPTION_REG.T0CS = 0;
OPTION_REG.PSA = 0;
OPTION_REG.PS0 = 1;
OPTION_REG.PS1 = 1;
OPTION_REG.PS2 = 1;

  while(1){

  TRIG = 0;
  delay_us(10);
  TRIG = 1;
  delay_us(10);
  TRIG = 0;

  while(ECHO==0);
  TMR0=0;
  while(ECHO==1);
  distance = (double)TMR0 * 4.352;
  PORTC = (char)distance;//to check
  Delay_ms(100);   
  }

}


i found this code also can u please tell me what are
OPTION_REG.T0CS = 0;
OPTION_REG.PSA = 0;
OPTION_REG.PS0 = 1;
OPTION_REG.PS1 = 1;
OPTION_REG.PS2 = 1;



and why they use this thing
distance = (double)TMR0 * 4.352;



Top
 Profile  
 
PostPosted: 08 Aug 2012 16:16 
Offline

Joined: 27 Jun 2010 12:07
Posts: 554
Location: England
Nipuna56 wrote:
i found this code also can u please tell me what are
OPTION_REG.T0CS = 0;
OPTION_REG.PSA = 0;
OPTION_REG.PS0 = 1;
OPTION_REG.PS1 = 1;
OPTION_REG.PS2 = 1;


The datasheet for whichever PIC the code was written for will explain the use of these register bits. There is no mention of which PIC is used, so I cannot comment further.

Nipuna56 wrote:
and why they use this thing
distance = (double)TMR0 * 4.352;

This converts the time taken between outgoing ultrasound "ping" and the received echo, into the distance that this represents. The longer the time between send and receive, the longer the distance that the ultrasound must have travelled.

_________________
Experienced hobbyist


Top
 Profile  
 
PostPosted: 08 Aug 2012 18:35 
Offline

Joined: 10 Jul 2012 09:23
Posts: 36
i hope to use 16f877A or 16f887


Top
 Profile  
 
PostPosted: 08 Aug 2012 18:59 
Offline

Joined: 27 Jun 2010 12:07
Posts: 554
Location: England
Nipuna56 wrote:
i hope to use 16f877A or 16f887
And what PIC was the code written for?

Every PIC is different. Do not expect code for one PIC to work on another, without some changes and some effort on your part.

_________________
Experienced hobbyist


Top
 Profile  
 
PostPosted: 08 Aug 2012 21:23 
Offline

Joined: 10 Jul 2012 09:23
Posts: 36
no friend i want to know what are mentioned in these lines and how can it change to according to my code

OPTION_REG.T0CS = 0;
OPTION_REG.PSA = 0;
OPTION_REG.PS0 = 1;
OPTION_REG.PS1 = 1;
OPTION_REG.PS2 = 1;


Top
 Profile  
 
PostPosted: 08 Aug 2012 22:05 
Offline

Joined: 27 Jun 2010 12:07
Posts: 554
Location: England
This register and these bits seem to be the same for 877A and 887, so with luck, no change is necessary, as long as your clock frequency does not change from the original.

These bits are all about how fast timer 0 counts.

The comments at the top of your code show the calculation for the selected pre-scaler of 1:256 with 4MHz crystal.

_________________
Experienced hobbyist


Top
 Profile  
 
PostPosted: 09 Aug 2012 03:42 
Offline

Joined: 10 Jul 2012 09:23
Posts: 36
according to my knowledge from this code port c shows what is the distance that measures from ultra so. sensor
is that correct?


Top
 Profile  
 
PostPosted: 09 Aug 2012 06:34 
Offline

Joined: 27 Jun 2010 12:07
Posts: 554
Location: England
I would say that the author intended to output the result to PORTC, yes.

_________________
Experienced hobbyist


Top
 Profile  
 
PostPosted: 09 Aug 2012 11:04 
Offline

Joined: 10 Jul 2012 09:23
Posts: 36
is this code correct
i want to get my output through lcd display

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 RA1_bit;
sbit LCD_EN at RA3_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 TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_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;
void main() {
  INTCON = 0x00;                        //turn off interrupts
  ADRESH = 0x00;
  ADRESL = 0x00;
  ADCON1 = 0x06;                        //all inputs are digital
  ADCON0 = 0x00;

  TRISD  = 0x00;                        //PORTD is input


  PORTD = 0x00;

  TRISA = 0x00;
  PORTA = 0x00;
 
 

  Lcd_Init();                           //Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                  // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);             // Cursor off



     PORTA.F2=0;






TRISB.F6 = 0;
TRISB.F7 = 1;


OPTION_REG.T0CS = 0;
OPTION_REG.PSA = 0;
OPTION_REG.PS0 = 1;
OPTION_REG.PS1 = 1;
OPTION_REG.PS2 = 1;

  while(1){

  TRIG = 0;
  delay_us(10);
  TRIG = 1;
  delay_us(10);
  TRIG = 0;

 while(ECHO==0);
 TMR0=0;

  while(ECHO==1);
  distance = (double)TMR0 * 4.352;
    wordtostr(distance,txt);
  lcd_out(1,6,txt) ;
  Delay_ms(500);
    Lcd_Cmd(_LCD_CLEAR);                  // Clear display
 

  }






}


Top
 Profile  
 
PostPosted: 09 Aug 2012 16:37 
Offline

Joined: 27 Jun 2010 12:07
Posts: 554
Location: England
Quote:
is this code correct
Try it out for yourself and see.

If it works - great.

If it does not work, then you need to learn how to debug code for yourself. Now is a good time to learn how.

I would test out the code for you but:

1 I do not own an ultrasonic distance measuring device
2 I do not know what measuring device you are using
3 It is your project, so it is better that you debug your own code

Let me know how it goes

_________________
Experienced hobbyist


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group