How to programe ultrasonic sensor using Mikroc
How to programe ultrasonic sensor using Mikroc
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
Re: How to programe ultrasonic sensor using Mikroc
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
... 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) ![Smile :)](./images/smilies/icon_smile.gif)
![Smile :)](./images/smilies/icon_smile.gif)
Re: How to programe ultrasonic sensor using Mikroc
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;
} }
Re: How to programe ultrasonic sensor using Mikroc
I can maybe explain what I think the code is trying to do, but the code is hard to excuse for the following reasons:Nipuna56 wrote:can u please explain this code
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.
Please avoid text-speak on this forum. This should be "Can you" thankscan u
Start every day with a smile...... (get it over with) ![Smile :)](./images/smilies/icon_smile.gif)
![Smile :)](./images/smilies/icon_smile.gif)
Re: How to programe ultrasonic sensor using Mikroc
ok friend
i found this code also can u please tell me what are
and why they use this thing
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);
}
}
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;
Re: How to programe ultrasonic sensor using Mikroc
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: i found this code also can u please tell me what areCode: Select all
OPTION_REG.T0CS = 0; OPTION_REG.PSA = 0; OPTION_REG.PS0 = 1; OPTION_REG.PS1 = 1; OPTION_REG.PS2 = 1;
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.Nipuna56 wrote:and why they use this thingCode: Select all
distance = (double)TMR0 * 4.352;
Start every day with a smile...... (get it over with) ![Smile :)](./images/smilies/icon_smile.gif)
![Smile :)](./images/smilies/icon_smile.gif)
Re: How to programe ultrasonic sensor using Mikroc
i hope to use 16f877A or 16f887
Re: How to programe ultrasonic sensor using Mikroc
And what PIC was the code written for?Nipuna56 wrote:i hope to use 16f877A or 16f887
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) ![Smile :)](./images/smilies/icon_smile.gif)
![Smile :)](./images/smilies/icon_smile.gif)
Re: How to programe ultrasonic sensor using Mikroc
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;
Re: How to programe ultrasonic sensor using Mikroc
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.
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) ![Smile :)](./images/smilies/icon_smile.gif)
![Smile :)](./images/smilies/icon_smile.gif)
Re: How to programe ultrasonic sensor using Mikroc
according to my knowledge from this code port c shows what is the distance that measures from ultra so. sensor
is that correct?
is that correct?
Re: How to programe ultrasonic sensor using Mikroc
I would say that the author intended to output the result to PORTC, yes.
Start every day with a smile...... (get it over with) ![Smile :)](./images/smilies/icon_smile.gif)
![Smile :)](./images/smilies/icon_smile.gif)
Re: How to programe ultrasonic sensor using Mikroc
is this code correct
i want to get my output through lcd display
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
}
}
Re: How to programe ultrasonic sensor using Mikroc
Try it out for yourself and see.is this code correct
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) ![Smile :)](./images/smilies/icon_smile.gif)
![Smile :)](./images/smilies/icon_smile.gif)