CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC24 and #INT_EXT1

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
soonc



Joined: 03 Dec 2013
Posts: 215

View user's profile Send private message

PIC24 and #INT_EXT1
PostPosted: Sun Jun 21, 2015 6:26 pm     Reply with quote

I tried setting up to use external interrupt but it does not interrupt !

I change the code to use INT0 which is not a #pin_select pin and it works just fine.

What did I do wrong with this code ?

Thanks for any help.

Compiler V5.047

Code:

#include <24FJ128GA306.h>
#build (stack=256)

#ZERO_RAM
#FUSES NOWDT         //No Watch Dog Timer
#FUSES NOJTAG        //JTAG disabled
#FUSES CKSFSM        //Clock Switching is enabled

//#device nested_interrupts=true  // did not help but use later for final code

#device ICD=TRUE
#device ICSP=2
#device adc=12
#use delay(crystal=20000000)

int8 n8Counter;
int1 n1Button;

/////////////////////////////////////
// Uart 1
#pin_select U1TX = PIN_F5
#pin_select U1RX = PIN_F4
#use rs232(baud=9600, xmit=PIN_F5, rcv=PIN_F4, STREAM=PCom1)


/////////////////////////////////////
//
#pin_select INT1 = PIN_B0

#INT_EXT1
void EX1_ISR()
{
  n1Button=1;
  n8Counter++;
}

/////////////////////////////////////
//
void main()
{

 ext_int_edge(1, L_to_H);
 
enable_interrupts(INT_EXT1);

 //enable_interrupts(INTR_NORMAL); // tried this
 enable_interrupts(INTR_GLOBAL);


 fprintf(PCom1,"Com1 alive\r\n");

 
  while(1)
  {
    if ( n1Button )
    {
      fprintf(PCom1,"Interrupts %lu\r\n",n8Counter);
      n1Button=0;
    }
 
  }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Mon Jun 22, 2015 2:40 am     Reply with quote

As posted, it works.....

There are a couple of comments:
Use:
Code:

#pin_select U1TX = PIN_F5
#pin_select U1RX = PIN_F4
#use rs232(baud=9600, UART1, STREAM=PCom1)


I've had problems on various compiler versions when using relocatable peripherals if you just use the pin names, rather than the actual device name. Safer (and avoids the danger of typing the pin wrong), to use the UART name.

I was running without ICD. (so removed the ICD lines). Possible that there is a problem here, since B0 is for ICD1 (probably the default).

I did also try disabling all analog ports (but then removed it, and it still worked). I was working on the basis that the analog input has higher priority than the relocatable peripheral.

Have you tried a simple 'poll PIN_B0' program and verified the pin is being seen to go high?. Something like:
Code:

void main()
{
   int1 old_pin;
   fprintf(PCom1,"Com1 alive\r\n");
   old_pin = input(PIN_B0);
 
   while(1)
   {
       if (old_pin != input(PIN_B0))
       {
           old_pin=!old_pin;
           fprintf(PCom1,"Pin changed\r\n");
       }
   }
}

To verify that the pin actually is changing?.
benoitstjean



Joined: 30 Oct 2007
Posts: 564
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Mon Jun 22, 2015 6:50 am     Reply with quote

I use the PIC24HJ256GP206 and a bunch of different interrupts (serial, external pins, timers etc).

From your short message, *I think* what you're trying to do will not work but there's another alternative. You haven't provided much details but I will answer based on what I read in your post.

What I can see from your code is that you're trying to obtain some external hardware interrupts, but on serial pins F4/F5. That, I believe, will not work.

From what I have tried, external interrupts can only be triggered on pins B0, F6 (INT0), D8 (INT1), D9 (INT2), D10 (INT3) and D11 (INT4), that's it (at least that's what I use). For pins F6, D8-D11, you also need to specify on which edge you want to trigger.

For pin B0, you must add the following flag such as:

enable_interrupts( INTR_CN_PIN | PIN_B0 );

That will specify that PIN_B0 is an interrupt-on-change pin.

Then for the other pins, my code is too big to post here but I believe that this is what you need (this is a copy-paste from my code):

In your .h:

#define PIN_INT_ON_CHG PIN_B0
#define PIN_INPUT_0 PIN_F6
#define PIN_INPUT_1 PIN_D8
#define PIN_INPUT_2 PIN_D9
#define PIN_INPUT_3 PIN_D10
#define PIN_INPUT_4 PIN_D11


In your .c:

ext_int_edge( 0, H_TO_L );
ext_int_edge( 1, H_TO_L );
ext_int_edge( 2, H_TO_L );
ext_int_edge( 3, H_TO_L );
ext_int_edge( 4, H_TO_L );

enable_interrupts( INTR_CN_PIN | PIN_INT_ON_CHG );
enable_interrupts( INT_EXT0 );
enable_interrupts( INT_EXT1 );
enable_interrupts( INT_EXT2 );
enable_interrupts( INT_EXT3 );
enable_interrupts( INT_EXT4 );


Then in some other file:

#INT_CNI
void PIN_INT_ON_CHG_ISR( void )
{
// Put your code here
}

#INT_EXT0
void INT_EXT_INPUT0( void )
{
// Put your code here
}

#INT_EXT1
void INT_EXT_INPUT1( void )
{
// Put your code here
}

#INT_EXT2
void INT_EXT_INPUT2( void )
{
// Put your code here
}

#INT_EXT3
void INT_EXT_INPUT3( void )
{
// Put your code here
}

#INT_EXT4
void INT_EXT_INPUT4( void )
{
// Put your code here
}


If you want to get fancy and trigger off every edge like I do, everytime you get an interrupt on a pin, then in the #INT_EXTx function, you toggle the ext_int_edge( 0, H_TO_L ) (high-to-low) to ext_int_edge( 0, L_TO_H ) (low-to-high). You set yourself a flag that gets toggled such as:

#INT_EXTx
void INT_EXT_INPUTx( void )
{
if( IsInputxLtoH == TRUE )
{
IsInputxLtoH = FALSE;
ext_int_edge( x, H_TO_L );
}
else
{
IsInput0LtoH = TRUE;
ext_int_edge( x, L_TO_H );
}

However, since you're trying to interrupt on the serial pins, then why not just use the serial interrupts like this:

#INT_RDA
void INT_SerialRX( void )
{
char = fgetc( PCom1 );
}

I've been using the above code for 2 years and it works fine. The #INT_RDA will get triggered upon every received character. You need some way to handle the characters and queue-them up in some message queue.

I've never bothered using the #INT_TBA interrupt for character transmission because I don't care to know if a character has been transmitted or not (plus I use DMA transfers for serial transmission so that's very fast).

As for the external interrupt pins, I get an interrupt every time the pin changes state.

Hope this helps, you haven't provided much details.

I think if you search for my posts, you will find one that talks about external interrupt pins. Hope this helps.
soonc



Joined: 03 Dec 2013
Posts: 215

View user's profile Send private message

Thanks
PostPosted: Mon Jun 22, 2015 7:20 am     Reply with quote

Ttelmah wrote:
As posted, it works.....

There are a couple of comments:
Use:
Code:

#pin_select U1TX = PIN_F5
#pin_select U1RX = PIN_F4
#use rs232(baud=9600, UART1, STREAM=PCom1)


I've had problems on various compiler versions when using relocatable peripherals if you just use the pin names, rather than the actual device name. Safer (and avoids the danger of typing the pin wrong), to use the UART name.

I was running without ICD. (so removed the ICD lines). Possible that there is a problem here, since B0 is for ICD1 (probably the default).

I did also try disabling all analog ports (but then removed it, and it still worked). I was working on the basis that the analog input has higher priority than the relocatable peripheral.

Have you tried a simple 'poll PIN_B0' program and verified the pin is being seen to go high?. Something like:
Code:

void main()
{
   int1 old_pin;
   fprintf(PCom1,"Com1 alive\r\n");
   old_pin = input(PIN_B0);
 
   while(1)
   {
       if (old_pin != input(PIN_B0))
       {
           old_pin=!old_pin;
           fprintf(PCom1,"Pin changed\r\n");
       }
   }
}

To verify that the pin actually is changing?.


Thanks to bringing me down to earth... I ran a little wild with the new found feature of #pin_select! PIN_B0 was set to GND !

I'm testing several different features that I need to port code from a working (11 years) PIC18 platform to the PIC24, for this I used one of the CCS PIC24 boards and replaced the existing chip with the PIC24FJ128GA306.
All this because the new version of this design requires 4 H/W serial ports, and some more RAM for the new features.

Thanks for all the help. So far it's going fairly smoothly thanks to your help and from others on the forum.
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Mon Jun 22, 2015 7:59 am     Reply with quote

I must admit with BGA, and high density SM packages, I've learnt to 'always' be suspicious that the pin actually is doing what you expect. GND!... Eeekl.

At least it was an obvious answer. Smile
soonc



Joined: 03 Dec 2013
Posts: 215

View user's profile Send private message

Thanks
PostPosted: Mon Jun 22, 2015 3:49 pm     Reply with quote

Thanks. The code works, it was my silly mistake.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group