How to programe ultrasonic sensor using Mikroc
Posted: 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
Development Tools, Compilers, Books
https://forum.mikroe.com/
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;
} }
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
Please avoid text-speak on this forum. This should be "Can you" thankscan u
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;
Code: Select all
distance = (double)TMR0 * 4.352;
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;
And what PIC was the code written for?Nipuna56 wrote:i hope to use 16f877A or 16f887
Code: Select all
OPTION_REG.T0CS = 0;
OPTION_REG.PSA = 0;
OPTION_REG.PS0 = 1;
OPTION_REG.PS1 = 1;
OPTION_REG.PS2 = 1;
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
}
}
Try it out for yourself and see.is this code correct