Jump to content
Sign in to follow this  
Elvis75

Newbie Interrupts

Recommended Posts

Hi

 

Im currently struggling with my first interrupt example. Please could you offer some adive on setting up my basic program which fires and interrupt and turns an led on when a button has been pressed. I have included the source code below.

 

Also i cannot seem to find any of the key words relating to the compiler e.g. (INT2IF - this does not work!).

I usually end up guessing most. Could you give advice where i can find these?

 

Cheers

 

 

#include <system.h>

 

 

 

#pragma CLOCK_FREQ 4000000

 

// *********************************** Setup Port Pins ***********************************

 

void setup_pins(void)

{

trisa=0x00; // RA5 as output to led

trisb=0x03; // RB2 as input to enable switch, RB0 as input to enable live programming

trisc=0x00;

porta=0x00;

portb=0x00;

portc=0x00;

}

 

// *********************************** Setup Interrupt ***********************************

 

void setup_interrupt(void)

{

 

 

// enable interrupts

set_bit( intcon3, INT2IE); // INT2 external interrupt enable bit

set_bit( intcon, GIE ); // Enable global interrupt

}

 

//************************************* Interrupt ****************************************

 

void interrupt( void )

{

if(INT2IF) // External interrupt, button RB2 has been pressed

{

porta = porta | 0x20;

clear_bit( intcon, INT2IF ); //clear TMR2 overflow flag

}

}

 

// ****************************************** Main ***************************************

 

void main(void)

{

setup_pins();

setup_interrupt();

 

while(1);

}

Share this post


Link to post
Share on other sites
if(INT2IF) // External interrupt, button RB2 has been pressed

This line needs to have "intcon.INT2IF" instead. INT2IF itself just evaluates to the index (0-7) of INT2IF in the intcon register.

 

As far as I can see, everything else looks good.

Also i cannot seem to find any of the key words relating to the compiler e.g. (INT2IF - this does not work!).

I usually end up guessing most. Could you give advice where i can find these?

Under the SourceBoost directory (C:\Program Files\SourceBoost on my computer), there is a directory called "include" which has the definitions for each device. Find your device in there (eg. for a P16F73 find p16f73.h) and it will list all bit and register definitions.

 

HTH

Share this post


Link to post
Share on other sites
Under the SourceBoost directory (C:\Program Files\SourceBoost on my computer), there is a directory called "include" which has the definitions for each device.  Find your device in there (eg. for a P16F73 find p16f73.h) and it will list all bit and register definitions.

Better to look at PIC16F73.h, the header files with just a 'P' prefix are for the old C2C-plus compiler!

 

Regards

Dave

Share this post


Link to post
Share on other sites
Better to look at PIC16F73.h, the header files with just a 'P' prefix are for the old C2C-plus compiler!

The thought crossed my mind, but I glanced through them quickly and thought it was the other way around! Thanks for the clarification.

Share this post


Link to post
Share on other sites
Hi

 

Im currently struggling with my first interrupt example.  Please could you offer some adive on setting up my basic program which fires and interrupt and turns an led on when a button has been pressed.  I have included the source code below.

 

Also i cannot seem to find any of the key words relating to the compiler e.g. (INT2IF - this does not work!).

I usually end up guessing most. Could you give advice where i can find these?

 

Cheers

 

 

#include <system.h>

 

 

 

#pragma CLOCK_FREQ 4000000

 

// *********************************** Setup Port Pins ***********************************

 

void setup_pins(void)

{

trisa=0x00;  // RA5 as output to led

trisb=0x03;  // RB2 as input to enable switch, RB0 as input to enable live programming

trisc=0x00;

porta=0x00;

portb=0x00;

portc=0x00;

}

 

// *********************************** Setup Interrupt ***********************************

 

void setup_interrupt(void)

{

   

 

// enable interrupts

set_bit( intcon3, INT2IE); // INT2 external interrupt enable  bit

set_bit( intcon, GIE ); // Enable global interrupt

}

 

//************************************* Interrupt ****************************************

 

void interrupt( void )

{

if(INT2IF) // External interrupt, button RB2 has been pressed

{

porta = porta | 0x20;

clear_bit( intcon, INT2IF );  //clear TMR2 overflow flag

}

}

 

// ****************************************** Main ***************************************

 

void main(void)

{

setup_pins();

setup_interrupt();

 

while(1);

}

 

Instead of using macros why don't you try direct bit access. Its much more easy and universal. You can use it on any compiler.

 

say you want to set RB port change interrupt enable bit (RBIE), simply write..

intcon.3 = 1;

 

say you want to clear the RB port change interrupt flag bit in your ISR then..

intcon.0 = 0; will do the job.

 

you want to enable the global interrupt enable..

intcon.7 = 1;

 

You dont need to use the tmr0 interrupt flag in your code if you are generating

an interrupt only by a switch ( or change in portb state).

Share this post


Link to post
Share on other sites

Hi

 

Bad news, it does not work. I found one problem, i had 0x03 set for RB2 when i should have had 0x05 set. Anyway below is my interrupt funtion that does not work.

 

void interrupt(void)

{

if(intcon3.INT2IF) // External interrupt, button RB2 has been pressed

{

toggle_led();

clear_bit(intcon3, INT2IF); //clear the interrupt

}

}

 

 

 

 

 

I have tried changing my initialisation processes, but this also does not work:

 

void setup_interrupt(void)

{

set_bit(intcon3, INT2IE); // RB2, INT2 external interrupt enable bit

set_bit(intcon2, INTEDG2); // Interrupt on rising edge

set_bit(intcon, GIE); // Enable global interrupt (intcon.7=1;)

}

 

 

 

 

Any ideas?

 

 

Cheers Elvis75

Share this post


Link to post
Share on other sites
Hi

 

Bad news, it does not work.  I found one problem, i had 0x03 set for RB2 when i should have had 0x05 set. Anyway below is my interrupt funtion that does not work.

 

void interrupt(void)

{

if(intcon3.INT2IF) // External interrupt, button RB2 has been pressed

{

    toggle_led();

    clear_bit(intcon3, INT2IF); //clear the interrupt

}

}

 

 

 

 

 

I have tried changing my initialisation processes, but this also does not work:

 

void setup_interrupt(void)

set_bit(intcon3, INT2IE);  // RB2, INT2 external interrupt enable  bit

        set_bit(intcon2, INTEDG2); // Interrupt on rising edge

set_bit(intcon, GIE);      // Enable global interrupt (intcon.7=1;)

}

 

 

 

 

Any ideas?

 

 

Cheers Elvis75

 

Elvis75,

As far as i know we have got external interrupt on only one pin ie. RB0/INT pin & its edge triggered( either rising edge or falling edge decided by option_reg.6.

This interrupt can be enabled by setting the INTE bit(intcon.4).The associated interrupt flag for this is INTF or intcon.1, which should be cleared in the interrupt service routine before re-enabling the interrupt.

 

Next we have got portb input change interrupt.

This can happen only on portb pins 7,6,5, & 4. So a change in state of RB2 will not cause an interrupt to occur.

To use this interrupt you need to enable it by setting bit RBIE(intcon.3)

Lets use this for your example..

code..

void interrupt (void)
{
portb.1=~portb.1;  //toggle led 

// wait for some time to see led before we get another interrupt
delay_ms(250); 

intcon.0=0;  //clear the flag to receive the next interrupt
}  

void main ()
{
trisb = 0b11111101; // only RB1 is made output to light the led.

//connect a switch to any portb pins <7:4>

intcon.7=1;  //enable global interrupt
intcon.3=1; //enable RB port change interrupt

while(1); //wait until a key is pressed for an interrupt 

}

 

If you wish, you can make just one of the 4 portb pins(<7:4>) as input( in which you connect your switch) & the rest can be made as output pin by changing the trisb value.

 

Raghunathan.

Edited by ra68gi

Share this post


Link to post
Share on other sites

Hi

 

Im currently using the EZ-Turbo controller (pic 18F2520), and RB0 is used to program the controller via the programming lead. Due to this, i am using INT2. Is this correct or

can i not use INT2?

 

Cheers Elvis75

Share this post


Link to post
Share on other sites
Hi

 

Im currently using the EZ-Turbo controller (pic 18F2520), and RB0 is used to program the controller via the programming lead.  Due to this, i am using INT2.  Is this correct or

can i not use INT2?

 

Cheers Elvis75

 

Sorry Elvis75,

My code is for PIC16f series of micro & not 18f.

I have not used the 18 series, so can't help.

Please look into the microchip data sheet & set the bits associated with INT2.

 

Looks like int2 works on RB2 which is also an anolog input. So make sure to configure it to digital mode before you use it.

 

On seeing the data sheet this is what i found.

 

The registers and bits associated with int2 are..

 

1. rcon- & the bit rcon.7 has to be cleared

2. intcon- & the bit(gie) intcon.7 has to be set.

3. intcon2- & the bit(intedg2) intcon2.4 ( make your choice for edge)

4. intcon3- & the bit(int2ie) intcon3.4 has to be set to enable interrupt.

5. intcon3-& the bit(int2if) intcon3.1 is a flag that you need to clear in the ISR.

 

Hope this helps.

 

Raghunathan.

Share this post


Link to post
Share on other sites

Hi all

 

Thanks for your help. I followed the steps in the last post, and also set porta in digital I/O mode and the project worked fine.

 

thanks again

 

Elvis75

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...