How to programe ultrasonic sensor using Mikroc

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Nipuna56
Posts: 36
Joined: 10 Jul 2012 09:23

How to programe ultrasonic sensor using Mikroc

#1 Post by Nipuna56 » 26 Jul 2012 12:47

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

hexreader
Posts: 1782
Joined: 27 Jun 2010 12:07
Location: England

Re: How to programe ultrasonic sensor using Mikroc

#2 Post by hexreader » 26 Jul 2012 15:41

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/2 ... er-library

Have not tried it for myself
Start every day with a smile...... (get it over with) :)

Nipuna56
Posts: 36
Joined: 10 Jul 2012 09:23

Re: How to programe ultrasonic sensor using Mikroc

#3 Post by Nipuna56 » 27 Jul 2012 03:26

can u please explain this code

Code: Select all

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;
 }   }

hexreader
Posts: 1782
Joined: 27 Jun 2010 12:07
Location: England

Re: How to programe ultrasonic sensor using Mikroc

#4 Post by hexreader » 27 Jul 2012 06:30

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.
can u
Please avoid text-speak on this forum. This should be "Can you" thanks
Start every day with a smile...... (get it over with) :)

Nipuna56
Posts: 36
Joined: 10 Jul 2012 09:23

Re: How to programe ultrasonic sensor using Mikroc

#5 Post by Nipuna56 » 08 Aug 2012 14:45

ok friend

Code: Select all

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

Code: Select all

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

Code: Select all

distance = (double)TMR0 * 4.352;


hexreader
Posts: 1782
Joined: 27 Jun 2010 12:07
Location: England

Re: How to programe ultrasonic sensor using Mikroc

#6 Post by hexreader » 08 Aug 2012 16:16

Nipuna56 wrote: i found this code also can u please tell me what are

Code: Select all

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

Code: Select all

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.
Start every day with a smile...... (get it over with) :)

Nipuna56
Posts: 36
Joined: 10 Jul 2012 09:23

Re: How to programe ultrasonic sensor using Mikroc

#7 Post by Nipuna56 » 08 Aug 2012 18:35

i hope to use 16f877A or 16f887

hexreader
Posts: 1782
Joined: 27 Jun 2010 12:07
Location: England

Re: How to programe ultrasonic sensor using Mikroc

#8 Post by hexreader » 08 Aug 2012 18:59

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.
Start every day with a smile...... (get it over with) :)

Nipuna56
Posts: 36
Joined: 10 Jul 2012 09:23

Re: How to programe ultrasonic sensor using Mikroc

#9 Post by Nipuna56 » 08 Aug 2012 21:23

no friend i want to know what are mentioned in these lines and how can it change to according to my code

Code: Select all

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

hexreader
Posts: 1782
Joined: 27 Jun 2010 12:07
Location: England

Re: How to programe ultrasonic sensor using Mikroc

#10 Post by hexreader » 08 Aug 2012 22:05

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.
Start every day with a smile...... (get it over with) :)

Nipuna56
Posts: 36
Joined: 10 Jul 2012 09:23

Re: How to programe ultrasonic sensor using Mikroc

#11 Post by Nipuna56 » 09 Aug 2012 03:42

according to my knowledge from this code port c shows what is the distance that measures from ultra so. sensor
is that correct?

hexreader
Posts: 1782
Joined: 27 Jun 2010 12:07
Location: England

Re: How to programe ultrasonic sensor using Mikroc

#12 Post by hexreader » 09 Aug 2012 06:34

I would say that the author intended to output the result to PORTC, yes.
Start every day with a smile...... (get it over with) :)

Nipuna56
Posts: 36
Joined: 10 Jul 2012 09:23

Re: How to programe ultrasonic sensor using Mikroc

#13 Post by Nipuna56 » 09 Aug 2012 11:04

is this code correct
i want to get my output through lcd display

Code: Select all

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
  

  }






}

hexreader
Posts: 1782
Joined: 27 Jun 2010 12:07
Location: England

Re: How to programe ultrasonic sensor using Mikroc

#14 Post by hexreader » 09 Aug 2012 16:37

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
Start every day with a smile...... (get it over with) :)

Post Reply

Return to “mikroC PRO for PIC General”