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.

[SOLVED] Pic18 controller interrupt

Status
Not open for further replies.

Ranbeer Singh

Full Member level 5
Joined
Jul 30, 2015
Messages
259
Helped
22
Reputation
44
Reaction score
22
Trophy points
1,298
Location
Faridabad India
Activity points
3,266
I have tried some sites to learn for pic controller interrupt vectors. I want to use INT0 bit pin external interrupt. I tried with these codes but no result.
Code:
// High priority interrupt vector

#pragma code isr = 0x08 // store the below code at address 0x08
#pragma interrupt isr
void isr()
{
// function statements
	if(INTCONbits.INT0IF)
	{
		RPM++;
		PORTBbits.RB2=1;
		INTCONbits.INT0IF = 0;
	}
}
#pragma code


void main()
{
	TRISA=0xFF;
	TRISC=0x00;
	PORTC=0x00;
	TRISB=0x01;
	T0CON=0x06;

	PR2=220;			// Puse width OnTime in Micro Second = PR2+1/prescaler(16)/Postscaler(16)
	CCPR2L=0;     		// OnTime from AN0 pin.

	ADCON0=0x01;       	// Use AN0 for analogic input
	ADCON1=0x0D;
	ADCON2=0xAE;
	// interrupts / USART interrupts configuration
    RCONbits.IPEN   = 0; // disable interrupt priority
    INTCONbits.GIE  = 1; // enable interrupts
    INTCONbits.PEIE = 1; // enable peripheral interrupts.
	INTCONbits.INT0IE=1;
	INTCONbits.INT0IF=0;
	INTCON2bits.INTEDG0=1;
}
 

have you set the reg. op for the timer ???
 

What type of timer? I am talking for interrupts.
Interupt is Hardware flags, when an Hardware signal has make an event or overfloat, that rice a Hardware flag bit. that the ISR read, and when it read this flag it will run the ISR.
 

Basically i don't know more for interrupts. Please explain me. I am not using any timer for interrupts.
 

Thanks for your valued help. But no codes for external interrupt. Request to you for send a example codes for external interrupt handling.
 

Thanks for your valued help. But no codes for external interrupt. Request to you for send a example codes for external interrupt handling.
https://ww1.microchip.com/downloads/en/DeviceDoc/31008a.pdf

I use the cx8 compilor so it may be different.

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
T0CON =0x87; //TMR0ON=1; T08BIT=0; T0CS=0; T0SE=0; PSA=0; T0PS2:T0PS0=111;
  INTCON=0xe0;//GIE/GIEH=1 PEIE/GIEL=1 TMR0IE=1 00 TMR0IF=0 00
//-------------------------------------------------------------------------------------------------
void interrupt tc_int(void)
{
    if (TMR0IE && TMR0IF) {
        TMR0IF=0;
        rf++;
       LATC1 =!LATC1;//blaa_off = !blaa_off
        
    }
    // process other interrupt sources here, if required
}

 

The code in the initial posting does not contain an infinite loop in the 'main' function. Therefore, the C18 'run-time' code will be calling the 'main' function correctly and then, then the 'main ' function returns, it will then takes it 'corrective action' for what it sees as a fault of resetting the device and calling 'main' again.
Therefore you have the processor working flat out doing absolutely nothing. Look at Seciotn 3.3 of the C18 C Comiler User Guide.
While the ISR code you have written is not wrong as such, the more traditional pattern is to put a 'goto' introduction at 0x08 that will take you to the actual ISR code which is in the "#pragma code" space. The reason is that, if and when you try to add interrupt priorities, you need to also put a 'goto' at 0x18 for the low priority ISR. If you try to do what you are doing now, then the ISR will overflow into that memory location and will cause trouble. Therefore it is a good habit to get in to now to. Check out Section 2.9.2.3 of the C18 C Compiler User Guide for code examples.
Susan
 

Finally i found some codes for INT0 in MAZIDI book. But These codes are not working too.

Code:
#include<p18f2520.h>

// High priority interrupt vector
void INT0_ISR(void);
void chk_isr(void);
#pragma interrupt chk_isr
void chk_isr(void)
{
	if(INTCONbits.INT0IF==1) INT0_ISR();
}

#pragma code isr = 0x08 // store the below code at address 0x08
void isr(void)
{
	_asm
		GOTO chk_isr
	_endasm
}
#pragma code

void main()
{
	TRISB=1;		// B0 as Input
	INTCONbits.INT0IE=1;
	INTCONbits.INT0IF=0;
	INTCONbits.GIE=1;
	
	while(1){}
}

void INT0_ISR()
{
	PORTBbits.RB2=1;
	INTCONbits.INT0IF=0;
}


I have changed #pragma code isr = 0x08 to #pragma code isr = 0x18
 

What do you mean "...not working..."?
Does it compile? Does it load into the device? have you tried using a debugger to single step through the code to make sure that it is running at all? Is the ISR being called?
I see that the INT0 pin on the PIC18F2520 also has AN12 on it - have you made sure that pin is working in 'digital' mode (look up ADCON1 and the PBADEN configuration bit)?
In general, it is not a good idea to call functions from within an ISR (especially when you later move to mode capable families of devices). In this case, there is no reason why you should not put the code that is the 'INT0_ISR' function directly into the "chk_isr" function. However what you have written in that part is not wrong.
Susan
 

Yes I have done all of things and PBADEN= OFF. I am simulating it in proteus ISIS not with hardware. My other codes are working properly.
 

I'm sorry but I still do not understand what is "not working".
Can you confirm that the simulator is working correctly with respect to interrupt handling? Is there an "errata" or similar for the simulator. (Personally I never trust simulators as they need not correctly exhibit actual hardware performance.)
Can you set a breakpoint in the ISR and see that the code is called?
By the way, the general rule is to read from the PORT and write to the LAT. The hardware is probably helping you here (in hardware, writes to the PORT also write to the LAT) but the simulator might not be reflecting this behaviour.
Susan
 

I am satisfied by your comment. May be simulator do not simulate ISR or do with other type. I will try to these codes with hardware.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top