julian403
Full Member level 5
hello peoples. I'm trying to use the sensor HCSR04 to sense motion and I use the pic 16f84. But it do not work, the code is:
the trigger goes on the RA0 pins (all PORTA is set to output), and the echo goes on RB4, because we all know that the distance is proportional to the pulse's times duration, so the falling edge triggers the PORTB changing interrup ( RB4 to RB7 )
To see it I put a led on PORTB2, but the led is allways on so it's means that the distance which measure the sensor it's always different. But there is only one wall at a distance of 2 meters from the sensor.
There are no objects that stand and change the measurements of distances.
What happening?
maybe it's because I've changed the code a thousand times that I can not see the error. And I know what the first thing you are going to think, that my frist mistake it's trying. Do. Or do not. There is no try. So, now it must work
the trigger goes on the RA0 pins (all PORTA is set to output), and the echo goes on RB4, because we all know that the distance is proportional to the pulse's times duration, so the falling edge triggers the PORTB changing interrup ( RB4 to RB7 )
Code:
#include <xc.h>
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
#pragma config CP = OFF // Code Protection bit (Code protection disabled
#define _XTAL_FREQ 3582056
#define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/3582056)))
#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/3582.056)))
#define SALIDA PORTBbits.RB2
int variable=0;
#INT_RB
void interrupt isr(void)
{
INTCONbits.GIE=0;
if(INTCONbits.RBIE)
{
variable=variable+1;
}
INTCONbits.GIE=1;
INTCONbits.RBIE=0;
return;
}
int cuenta (void)
{
int i=0;
int c=1;
__delay_us(15);
PORTA=0xFF;
__delay_us(15);
PORTA=0x00;
while(c)
{
i++;
__delay_us(500);
if(variable>0)
{
c=0;
}
}
return (i);
}
void main(void) {
TRISB=0b111110001;
TRISA=0x00;
INTCON=0x98;
OPTION_REG=0x98;
int d1=0;
int l1=0;
d1=cuenta();
l1=d1;
variable=0;
while(1)
{
l1=cuenta();
variable=0;
if(l1!=d1)
{
SALIDA=1;
__delay_ms(1000);
d1=cuenta();
}
}
}
To see it I put a led on PORTB2, but the led is allways on so it's means that the distance which measure the sensor it's always different. But there is only one wall at a distance of 2 meters from the sensor.
There are no objects that stand and change the measurements of distances.
What happening?
maybe it's because I've changed the code a thousand times that I can not see the error. And I know what the first thing you are going to think, that my frist mistake it's trying. Do. Or do not. There is no try. So, now it must work