Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] hc-sr04 with pic18f1330 error

Status
Not open for further replies.

ruben91

Junior Member level 3
Joined
Nov 17, 2014
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
209
i write a code to test my HC-SR04 sensor to measure distance but seem there is no responce from the sensor, after i program and when i turn on the pic, the LED from port A0 turn on and not turn off even i move more than 100 cm..pls help

i'm using pickit 2, mplab ide wit c18 compiler, internal OSC 4Mhz.. tq

Code:
#include<p18f1330.h>
#include<delays.h>
//#include <stdio.h>
//#include <stdlib.h>
#include <math.h>

#pragma config OSC=INTIO2
#pragma config WDT=OFF
#pragma config PWMPIN = OFF

#define Trig PORTBbits.RB3
#define Echo PORTBbits.RB2

void Call_Init_PIC (void);
void sendPulse(void);
void T0Delay10us(void);
void delay2(void);

unsigned int distance ;
unsigned int time;

void main(void)
{
OSCCONbits.IRCF2 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 0;

TRISAbits.TRISA0=0;
TRISAbits.TRISA1=0;

Call_Init_PIC ();
while(1)
{
//T1CON=0x0;
TMR1H= 0;
TMR1L= 0;
sendPulse();

while(Echo == 0);

T1CONbits.TMR1ON = 1;
while(Echo == 1);
T1CONbits.TMR1ON = 0;
//Delay10KTCYx(255);   
time= TMR1H;
time= time<<8;
time= time|TMR1L;
distance = (.0017* time);
Delay10KTCYx(100); 
PIR1bits.TMR1IF=0;
/*
time = (TMR1L | (TMR1H<<8));   //Reads Timer Value
    time = time/58.82;                //Converts Time to Distance
    time = time + 1;                  //Distance Calibration\
    
if(time>=2 && time<=6)
{

PORTBbits.RB0=1;
Delay10KTCYx(255);
PORTBbits.RB0=0;
Delay10KTCYx(255);


}
*/
//if(time>=7)
//{


//PORTBbits.RB1=1;


//}

if(distance<=100)

{

LATAbits.LATA0=1;
Delay10KTCYx(100);
LATAbits.LATA0=0;
Delay10KTCYx(100);

}

if(distance>=101)

{

LATAbits.LATA0=0;
Delay10KTCYx(100);
LATAbits.LATA1=1;


}
}
}

void Call_Init_PIC (void)
{
TRISBbits.TRISB3=0;
TRISBbits.TRISB2=1;
}


void sendPulse(void)
	{
	Trig=1;
Delay10TCYx(1);
	Trig=0;
Delay10KTCYx(255);
Delay10KTCYx(255);
	}
/*
void T0Delay10us()
	{
	T0CON=0x00;
	TMR0H=0XFF;
	TMR0L=0XCD;
	T0CONbits.TMR0ON=1;
	while(INTCONbits.TMR0IF==0);
	T0CONbits.TMR0ON=0;
	INTCONbits.TMR0IF=0;
	}

void delay2()
{
	Delay1TCY();
	Delay1TCY();
	Delay1TCY();
	Delay1TCY();
	Delay1TCY();
}
*/
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top