Problem with interrupt pic16f877a

Status
Not open for further replies.

32bits

Newbie level 3
Joined
Jan 11, 2013
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,308
Hi, I am new to the forum, nice to meet you everyone!

I am working on a project that needs to do an interrupt from external source (RB0/INT) every time that there is a falling edge (TTL), at the moment I am just testing it with a push button, but the final idea is to use a quadrature signal as a input, similar to an encoder signal (I do not know if that can be done yet), but I want to go step by step. Below is my code, basically every time that I push the button it should go to the interrupt but it won't go when I am debugging, I do not know if I am doing something wrong, i would appreciate your help.

By the way I am using boostc compiler and pic16f877a. Many thanks.

Code:
#include <system.h>
 
#ifdef _PIC16F877A
#pragma DATA _CONFIG, _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC & _LVP_OFF
#endif
 
#pragma CLOCK_FREQ 16000000
void inter_set();
 
void main()
 
{      
            
        inter_set();
       
        while(1);
        {
                                porte.0 = 0;
        }
       
}
 
void inter_set()
{
 
                 
 
        trisb = 0b11111111;            //PortB as input
        trise = 0b00000000;            //PortE as output
        option_reg = 0b10000000;    // Interrupt on falling edge of RB0/INT pin
        intcon = 0b00000000;          // Clear interrupt
        intcon = 0b10010000;          // Enables the RB0/INT external interrupt                                                                          
 
       
}
 
void interrupt()
{
 
porte.0 = 1; 
clear_bit( intcon, RBIF );  // Clear flag bit
        
}

My configuration:
 

Try enabling the RBIE first, then enabling GIE.

I suspect your code is actually working though. When the interrupt is called it sets PORTE bit 1 but as soon as you return to the main() it gets cleared again. You may only be setting the bit for a few uS before resetting it again, is this what you intended?

Brian.
 

betwixt thanks for you help,
I tried what you said but still does not work.
I tried the code below, but still the same, anyone has any example of external interrupt using boostc compiler. thanks in adavance.

Code:
#include <system.h>

#ifdef _PIC16F877A
#pragma DATA _CONFIG, _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC & _LVP_OFF
#endif

#pragma CLOCK_FREQ 20000000 
void Test(void);
void inter_set();


void main()

{    
            
    inter_set();
	
	while(1) { } 
         
     
} 


void inter_set()
{
	portb = 0x00;
	trisb = 0x01;
	set_bit( option_reg, INTEDG );  	// Set for rising edge
	set_bit( intcon, INTE );		    // Enable external interrupt
	set_bit( intcon, RBIE );			// RB change interrupt
    set_bit( intcon, GIE );				// Set global interrupt			       
}


void interrupt(void)
{
   if(INTF)  // External interrupt, button RB0 has been pressed 
   {                  
   Test(); 
   clear_bit( intcon, INTF );      // clear the interrupt 
   } 

}

    
void Test(void){

porte.0 = 1;
delay_10us;

}
 

Solved. The interrupt was happening really fast and I could not see it. Using breakpoints I managed to see it. many thanks again.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…