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.

HCSR04 and pic16f84 with xc8

Status
Not open for further replies.

julian403

Full Member level 5
Joined
Feb 28, 2014
Messages
254
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Location
Argentina
Activity points
2,105
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 )

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
 

hello,


interupt flag is RBIF
you need to set RBIE to Enable rthe interrupt
but you must test RBIF, without changing RBIE

and you must read RB4 to reset RBIF ... or RBIF=0 inside the interrupt
check that in the PIC datasheet ..

also, set GIE_bit once in the main
and don't use it inside the interrupt..
 
Ok I do it but it still do not work. I think it's because variable which changes on the interrupt but then I need to set it to 0 on the funtion void cuenta(), as you can see. I think that when variable is 1 it's never goes to 0 again.

Code:
#include <xc.h>
#include <stdint.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 SALIDA PORTBbits.RB2

volatile char variable=0;


void interrupt isr(void) 
{ 
    if(PORTBbits.RB4==0) 
    {
    variable++;
    } 
    INTCONbits.RBIF = 0;    //clear interrupt request flag
}


char cuenta (void)
{ 
   
    char c=0;
     __delay_us(15); 
     PORTA=0xFF; 
     __delay_us(15); 
     PORTA=0x00; 
    
     while(!(variable))
     {
         c++;
         __delay_us(100);
     }
     variable=0;
     return c;
}

void main(void) {
    

        TRISB=0b11110000; 
        TRISA=0x00; 
        INTCON=0x98;
        OPTION_REG=0x98;
        char d1=0;
        char l1=0;
         
        d1=cuenta();
        l1=d1;
        while(1)
        {
            l1=cuenta();   
              
            if(l1!=d1) 
            {
                SALIDA=1; 
                __delay_ms(1000); 
      
             }
            d1=cuenta();
            variable=0;
        }
}
 
Last edited:

hello


you have to test RBIF bit for interrupt , not the Bit4 of PORTB...

void interrupt isr(void)
{
char c;

if(RBIF_bit==1)
{
cp=PORTB; // read the PORTB
variable++;
INTCONbits.RBIF = 0; //clear interrupt request flag
}
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top