Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

help needed for interrupts for 18f4520 using megapic

Status
Not open for further replies.

friendshipz

New Member
hi,

i am currently doing my FYP which requires me to create interrupts for the button on the megapic, the use of the button would be for executing command for (Start, Stop and reset). I have learnt interrupts from my module using chip 18f4520 with the green board but not the megapic. I have been trying to get it working using the megapic but so far i only managed to light up the led light and the interrupt is not working.

my project consist of both hardware and software, i am almost done with the hardware part and doing troubleshooting for the software.

The following code are the one i am currently working with, i simply want to create interrupt for the button so that i can switch on and off for the led light, i hope that you guys can offer me some help and advice so that i can create interrupt for the (start, stop and reset).

for megapic the led light is PORTD and set it to 'low'



#include <p18f4520.h>


void main (void)
{


PORTB = 0; // clear PORTB
TRISBbits.TRISB7 = 1; // assign Interrupt pin
LATD = 0; // Set PORTD output pins state first
TRISDbits.TRISD0 = 0; // PORTD.0 (LED1) is an output
TRISDbits.TRISD1 = 0; // PORTD.1 (LED2) is an output
TRISDbits.TRISD2 = 0; // PORTD.2 (LED3) is an output
TRISDbits.TRISD3 = 0; // PORTD.3 (LED4) is an output
TRISDbits.TRISD4 = 0; // PORTD.4 (LED5) is an output
TRISDbits.TRISD5 = 0; // PORTD.5 (LED6) is an output
TRISDbits.TRISD6 = 0; // PORTD.6 (LED7) is an output
TRISDbits.TRISD7 = 0; // PORTD.7 (LED8) is an output




INTCONbits.GIE = 1; // Global Interrupt Enable Bit
INTCONbits.RBIE = 1; // External Interrupt Enable Bit
INTCONbits.RBIF = 0; // External Interrupt Flag Bit, make the Flag down
INTCON2bits.RBIP = 1; // External Interrupt 0 Edge Select Bit, making it on rising edge
while(1);
}

#pragma interrupt ISR
void ISR (void)
{
if (PORTBbits.RB7 == 0)
{
PORTDbits.RD0 = 0; /* light up*/
}
else
{
PORTDbits.RD0 = 1; // switch off */
}
INTCONbits.INT0IF = 0; // External Interrupt Flag Bit, make the Flag down
}
#pragma code interrupt_vector_section=0x08
void my_interrupt_vector (void)
{
_asm //Inline ASM codes
goto ISR
_endasm
}
#pragma code




Thanks in advance,
suki
 
I think,

INTCONbits.INT0IF = 0;

in your ISR needs to be,

INTCONbits.RBIF = 0;

Mike.
 
thanks for the reply mike,

i tried your suggestion and it still doesnt seem to work, i felt that there is a few mistake i made and i cannot figure it out. today i just found out that i need to do some conversion of analog to digital by adding this code

ADCON1= 0x0f

it gave me syntax error when i added this line into my code.

hope to get more advice, thx =]
 
Last edited:
Looking at your code again I see that you don't enable interrupt priorities and so all interrupts go to the low priority interrupt vector at 0x18 - you are using the high vector. Setting RBIP has no effect if IPEN isn't set.

You also don't have any config data in your code. You need a line such as,
#pragma config WDT = OFF, OSC = INTIO67


Mike.
 
I tried to add

RCONbits.IPEN =1 //enable priority interrupts

but there seems to be some syntax error and i cannot build successfully
 
I assume you know you need a semicolon after the statement.

I.E.
RCONbits.IPEN =1; //enable priority interrupts

Mike.
 
yeh, my mistake, i missed out the semicolon, it build successfully but the interrupts is still not working, i went to do some internet research on port B interrupt to change, so i did some modification but it is not working =[



#include <p18f4520.h>
#pragma config WDT = OFF, OSC = INTIO67

void main (void)
{
ADCON1 = 0x0f; // All channels Digital
INTCON = 0b10001000; //Enables all interrupts & the RB port change interrupt
PORTB = 1; // clear PORTB
LATD = 0; // Set PORTD output pins state first
TRISBbits.TRISB7 = 1; // assign Interrupt pin

TRISDbits.TRISD0 = 0; // PORTD.0 (LED1) is an output


RCONbits.IPEN =1; //enable priority interrupts
INTCONbits.RBIF = 0; //Reset the RB Port Change Interrupt Flag bit
INTCONbits.RBIE = 1; // External Interrupt Enable Bit
INTCON2bits.RBIP = 1; // External Interrupt 0 Edge Select Bit, making it on rising edge
INTCONbits.GIE = 1; // Global Interrupt Enable Bit



while(1);
}

#pragma interrupt ISR
void ISR (void)

{
if (PORTBbits.RB7 == 1 )
{
PORTDbits.RD0 = 0; /* light up*/
}
else
{
PORTDbits.RD0 = 1; // switch off */
}

INTCONbits.RBIF = 0; // External Interrupt Flag Bit, make the Flag down
}
#pragma code interrupt_vector_section=0x0008
void my_interrupt_vector (void)
{
_asm //Inline ASM codes
goto ISR
_endasm
}
#pragma code
 
Your code may be crashing as you enable interrupts before you enable priority interrupts and so a low priority interrupt could occur. Why not just use low priority interrupts any way.

Mike.
 
so do i change

RCONbits.IPEN =0;

and

change 0x0008 to

#pragma code interrupt_vector_section=0x00018 ?


i am in class right now so i do not have the megapic and PICKIT 2 with me, but i am eager to solve this problem as soon as possible, your help is greatly appreicated =].
 
You should be able to leave out the IPEN bit as it defaults to zero and, yes, change the vector address to 0x18.

Mike.
 
hi mike,

i have changed to low priority and it is not working as well...

i just wanted the button to turn on and off of the led light and i think maybe my code is too complicated or maybe the way i sequence them is wrong.:(

The switch (SW1) is at portB (RB7)

the led light for megapic is at portD (RD0) and set to 'low'
 
hi ppl,

regarding for my code i felt that maybe i have some missing code or that i messed up with my '1' and '0'

if i messed big time with my code and if possible, please modify my code in anyway. thank you
 
Status
Not open for further replies.

Latest threads

Back
Top