Pic16f877a interrupt doesn't work !!!

Status
Not open for further replies.

anjalimidhuna

Member level 3
Joined
Jan 4, 2014
Messages
62
Helped
2
Reputation
4
Reaction score
2
Trophy points
8
Location
india
Activity points
358
Hi all,
i have stuck with interrupt problem. My program doesn't enter into the interrupt routine. I am using PIC16F877A with 4 MHz crystal freq.I used MP LAB with Hi-tech C.following is my code
Code:
void interrupt isr(void)
{
	unsigned int Count=0,Count1=0;	
	if(TMR1IF)
	{
		Count++; 
		if(Count==15)
		{
			Count1++;
		}
		TMR1IF=0;
		TMR1IE=1;
	}
}
void main()
{
	TRISC4=0;
	RC4=0;
	T1CON=0b00000001;
	TMR1H=0XFF;
	TMR1L=0XFF;
	INTCON=0b11000000;
	PIE1=0b00000001;
	while(1)
	{
		if(Count1>=1)
		RC4=~RC4;
		for(int A=0;A<=5000;A++);
	}
}
any config setting required to work isr ? i can't realize my problem. The code build successfully but doesn't give any output .
 

Try this code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
unsigned int Count=0, Count1=0;
 
void interrupt isr(void)
{
        
    if(TMR1IF)
    {
        if(++Count==15)
        {
            ++Count1;
        }
        TMR1H=0X00;
        TMR1L=0X00;
        TMR1IF=0;       
    }
}
 
void main()
{
    TRISC4=0;
    RC4=0;
    T1CON=0b00000001;
    TMR1H=0XFF;
    TMR1L=0XFF;
    INTCON=0b11000000;
    PIE1=0b00000001;
    while(1)
    {
        if(Count1>=1)
        RC4=~RC4;
        for(int A=0;A<=5000;A++);
    }
}

 
thaq milan it works
mind to explain why my code is not working
i have one more doubt . it only once enter into the isr.if i clear Count1 in the if loop the port doesn't change any more. why these al happens. :roll:
 
Last edited:

Genraly u require configuration bit for selecting internal or external crystal try this for external crystal
__CONFIG (0x0005);
also don't initializes any variable in ISR
don't make TMR1IE again in ISR it require only once instead u reload count of timer in ISR
try for
TMR1H=0XF0;
TMR1L=0XFF;

what is ur application
 

Code for 8Mhz u can change as per ur Crystal & init variable require in ur code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include  <htc.h>
 __CONFIG (0x0005); 
//--------------------------------------------------------------------------
void interrupt Timer_ISR(void)
{                     
         if(T0IF) 
         {
           T0IF = 0;
           TMR0       = 100;
           sec_count++;           // Increment second counter after every 20ms
           if(sec_count>=50)      // Is 1 Second complete ? (20 ms *50)
           {
              sec_count=0;        // Reset 1 sec counter
              flag.sec=1;         // Set 1 sec completed indicating flag
              RB4 = !RB4;
           }
         }      
}                                 // End of timer0 interrupt service routine
//------------------------MAIN PROGRAM STARTS-------------------------------
void main(void)
{
         di();
     init_hardware();             // Initialise port as input & output
         init_timer();
     PEIE=1;
     GIE=1;
     while(1)
     {
 
     }
}                                      // main ends
//----------------------------------------------------------------------------------
void init_hardware(void)
{
         OPTION_REG = 0x07;               // Prescale 256
         OSCCON     = 0x71;
         ADCON0     = 0;
         ADCON1     = 0;
         ANSEL      = 0; 
         ANSELH     = 0; 
         TRISA      = 0x0F;
         TRISB      = 0; 
         TRISC      = 0;
         TRISD      = 0; 
         PORTB      = 0x00;     
}
//-----------------------------------------------------------------------------
void init_timer(void)
{
         TMR0IF     = 0;
         INTCON     = 0xE0;               // Enable Global & Peripheral Interrupt 
         TMR0       = 100;         
}   
//-----------------------------------------------------------------------------


Is ur academic project or hoby or anything else
 
Last edited by a moderator:

The most common reason an interrupt only works 1 time is the simple fact that it is not reenabled. There are two causes for this, the first is that the interrupt itself is not reenabled, the second is that the return from the ISR does not enable the master interrupt flag.

Just to clarify. It is the interrupt that needs to be reeabled, not the timer. I've only programmed the PIC processores in Assembly, but there are 2 different return instructions, RETURN from a normal subroutine, and RETFIE from ISR routines. The last one will reenable the master interrupt flag.

When dealing with ISRs you should also be aware of the corruption of the status register, if you don't save it when entering an ISR.
 
Last edited:

Interrupt enable activity is done once before the main but in ISR u require to clear corresponding interrupt flag , require to reload count of timer. No need to re enable as it not disable.

If u don't know basic then don't try programming instead start reading datasheet, basic function of timer , crystal , compiler etc then start coding otherwise u require lot of time to study
 
Try this code which is better. Mention Fosc used.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
unsigned char Count = 0, ledFlag = 0;
 
void interrupt isr(void)
{
    if(TMR1IF)
    {
        if(++Count == 4){   // Fosc = 4 MHz, Interrupt every 250 ms, 4 intrrupt = 1 sec
               Count = 0;
               ledFlag = 1;
        }
 
        TMR1H = 0x0B;
        TMR1L = 0xDB;
        TMR1IF = 0;
    }
}
 
void main()
{
    TRISC4 = 0;
    RC4 = 0;
    T1CON = 0x21;
    TMR1H = 0x0B;
    TMR1L = 0xDB;
    INTCON = 0xC0;
    PIE1 = 0x01;
    
    while(1)
    {
        if(ledFlag){
              RC4 = ~RC4;
              ledFlag = 0;
        }
    }
}

 
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…