CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

HCSR04 Ultrasonic sensor with pic 16f877a

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sooryakrishnan.u



Joined: 20 Jan 2016
Posts: 1

View user's profile Send private message

HCSR04 Ultrasonic sensor with pic 16f877a
PostPosted: Wed Jan 20, 2016 12:50 am     Reply with quote

Hi i am using HCSR04 Ultrasonic Sensor with PIC16F877A for obstacle detection. I just need to count the no of incoming and outgoing people in my project. But the issue is that i am not receiving anything in my teraterm.
This is the code i wrote. Here i have just used one hcsr04 for clarity. Can any one point me out where i have gone wrong?

Code:

#include <16F877A.h>
#device ADC=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(crystal=20000000)
#use STANDARD_IO( A )
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1)

#define TRIG PIN_A0
#define ECHO PIN_B4


/****************************************/

 unsigned int count = 0;
#INT_RB
void  RB_isr(void)
{
count++;
}

void main()
{

   delay_ms(3000);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);      //104 ms overflow

   enable_interrupts(INT_RB);
   enable_interrupts(GLOBAL);
 
   while(TRUE)
   {
 
      output_low(ECHO);
      output_low(TRIG);
      delay_us(5);
      output_high(TRIG);                         
      delay_us(10);                            // sending 10us pulse
      output_low(TRIG);
     
 
 while(input(ECHO))                       // Wait for high state of echo pin
 {}
 printf("Count = %u",count);


   }

}


+++++++++++++++++++
Code block added.
- Forum Moderator
+++++++++++++++++++
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Jan 20, 2016 2:11 am     Reply with quote

First learn to use the code buttons...

Then without looking at anything else in the code, there is a critical problem. You _must_ read portB, inside the INT_RB handler.

INT_RB, is an 'interrupt on change'. It goes 'TRUE', whenever portB, differs from the portB 'latch'. The latch is set to the current portB status, when the port is read.

Now this means that if the portB interrupt triggers, and you don't read the port (to reset the latch), it'll immediately trigger again. So effectively the INT_RB handler can never exit....

There are a number of interrupts like this. INT_TBE for instance, will keep triggering, until you load something into the transmit buffer. INT_RDA, will keep triggering until you read the receive buffer.

In the cases of interrupts like this, you must clear/handle what the interrupt is set to trigger on, before you exit the interrupt handler. The compiler will clear the interrupt flag, but it immediately gets set again....

So in your code at present, INT_RB triggers, and the handler can then never exit, so the rest of the code stops working. Crying or Very sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group