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 CCS Technical Support

Confused about how to set up a level change int [Solved]

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



Joined: 27 Jan 2019
Posts: 9

View user's profile Send private message

Confused about how to set up a level change int [Solved]
PostPosted: Sun Jan 27, 2019 3:00 pm     Reply with quote

PIC: dsPIC33EP512MC80

I've been searching but most information I find is related to how to set up a external B0 interruption.

I just want to make it so D8, D9 and D11 are buttons that when pressed, toggle the output of B11.

This is the code I've been testing, when I turn on the PIC it's low, but when I click the button, it toggles to high but stays there, no matter if I press it again it won't go low.

Any help is greatly appreciated.

Code:

#include <33EP512MC806.h>
#device ICSP=1
#use delay(internal=19940617)

#FUSES   NOWDT                    //No Watch Dog Timer
#FUSES   CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES   NOBROWNOUT               //No brownout reset
#FUSES   NOJTAG                   //JTAG disabled

#use     FIXED_IO( B_outputs=PIN_B15,PIN_B14,PIN_B13,PIN_B12,PIN_B11,PIN_B0 )
#DEFINE  DELAY 1000


Code:

#include <main.h>

#Int_CNI
void interrruption(){
   //Code
   output_toggle(PIN_B11);
   delay_ms(4000);
   
   clear_interrupt(PIN_D8);
   clear_interrupt(PIN_D9);
   clear_interrupt(PIN_D11);
}

void main()
{
   enable_interrupts(global);
   enable_interrupts(PIN_D8);
   enable_interrupts(PIN_D9);
   enable_interrupts(PIN_D10);
   ext_int_edge(h_to_l);

   set_pullup(TRUE,PIN_D8);
   set_pullup(TRUE,PIN_D9);
   set_pullup(TRUE,PIN_D10);
   
   output_low(PIN_B11);
   output_low(PIN_B12);
   output_low(PIN_B13);
   output_low(PIN_B14);
   output_low(PIN_B15);
   
   
   while(true){
      output_high(PIN_B12);
   } 
}



Last edited by samu350 on Thu Jan 31, 2019 2:36 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 27, 2019 3:03 pm     Reply with quote

Quote:
clear_interrupt(PIN_D8);
clear_interrupt(PIN_D9);
clear_interrupt(PIN_D11);
}

void main()
{
enable_interrupts(global);
enable_interrupts(PIN_D8);
enable_interrupts(PIN_D9);
enable_interrupts(PIN_D10);

These are not the correct parameters for the interrupt functions.
Look at the end of the 33EP512MC806.h file. It lists the parameters
that you can use.
samu350



Joined: 27 Jan 2019
Posts: 9

View user's profile Send private message

PostPosted: Sun Jan 27, 2019 3:15 pm     Reply with quote

PCM programmer wrote:
Quote:
clear_interrupt(PIN_D8);
clear_interrupt(PIN_D9);
clear_interrupt(PIN_D11);
}

void main()
{
enable_interrupts(global);
enable_interrupts(PIN_D8);
enable_interrupts(PIN_D9);
enable_interrupts(PIN_D10);

These are not the correct parameters for the interrupt functions.
Look at the end of the 33EP512MC806.h file. It lists the parameters
that you can use.


I see, I thought I could use them, this got me a little bit confused. Thought I could use a pin as a source of interruption, because as of now it currently works, but it detects both edges when I press the button so it pretty much stays there forever. And I only want it to detect high to low interruptions.

Code:

#define INTR_CN_PIN              0x8000  // or in a PIN_xx constant (enable/disable only)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 27, 2019 5:31 pm     Reply with quote

I may be wrong. The CCS manual says this:
Quote:

intr_cn_pin | pin_xx – Enables a CN pin interrupts


Because I only have the PCM and PCH compilers, I will stop trying to
answer questions about PCD.
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Sun Jan 27, 2019 8:25 pm     Reply with quote

There are two types of edge triggered interrupts:

1. General External Interrupts - These trigger on only one type of edge (Low to High by default usually, but can be changed to High to Low). Each pin gets its own interrupt

2. Change Notification - These trigger on any change to the pin, high or low (some chips have configurations that let you set to only one edge...I don't believe your chip has that feature). All pins share the same interrupt, so you have to do some footwork to figure out which one did yourself, unless you only enable one of them of course.

You can go about this a couple of ways:

1. You can assign D8, D9, and D11 each to their own separate interrupt, set their configuration to High to Low and have their interrupt handlers all be similar. D9 is already EXT0. D8 and D11 need to be setup via #pin_select, in addition to the interrupt configurations, as INT_EXT2 and INT_EXT3. You can use the ext_int_edge() function to set them to "high to low" detection.

EXAMPLE
Code:

// D9 is hard pinned to EXT0
#pin_select INT1 = PIN_D8
#pin_select INT2 = PIN_D11

#int_ext0
void ext0_isr(){
   output_toggle(PIN_B11);
}
#int_ext1
void ext1_isr(){
   output_toggle(PIN_B11);
}
#int_ext2
void ext2_isr(){
   output_toggle(PIN_B11);
}

//in the main
ext_int_edge(0,H_TO_L);
ext_int_edge(1,H_TO_L);
ext_int_edge(2,H_TO_L);
enable_interrupts(INT_EXT0);
enable_interrupts(INT_EXT1);
enable_interrupts(INT_EXT2);




2. You can continue on with change notification, but you have to add some code to your handler to detect the high to low transition on each pin manually. You also need to use the enable_interrupts() call correctly as suggested above:

Code:

#int_cni
void cni_isr(){
   static int1 d8_state = 0;
   static int1 d9_state = 0;
   static int1 d11_state = 0;
   BOOLEAN toggle = FALSE;

   
   if(input_state(PIN_D8) != d8_state){
      d8_state = ~d8_state;
      if(!d8_state){
         toggle = TRUE;
      }
   }
   if(input_state(PIN_D9) != d9_state){
      d9_state = ~d9_state;
      if(!d9_state){
         toggle = TRUE;
      }
   }
   if(input_state(PIN_D11) != d11_state){
      d11_state = ~d11_state;
      if(!d11_state){
         toggle = TRUE;
      }
   }
   
   if(toggle){
      output_toggle(PIN_B11);
   }

}


// in the main
enable_interrupts(INTR_CN_PIN | PIN_D8);
enable_interrupts(INTR_CN_PIN | PIN_D9);
enable_interrupts(INTR_CN_PIN | PIN_D11);


notice all separate lines. I didn't check all the logic. It's just for example for ideas.
samu350



Joined: 27 Jan 2019
Posts: 9

View user's profile Send private message

PostPosted: Mon Jan 28, 2019 1:05 pm     Reply with quote

Thanks a lot! Halfway yesterday I realized about this problem and I decided to just use the DSPIC external interruptions, much more comfortable. I assigned these pins to the external interrupts 1, 2 and 3. Works flawlessly.
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