|
|
View previous topic :: View next topic |
Author |
Message |
samu350
Joined: 27 Jan 2019 Posts: 9
|
Confused about how to set up a level change int [Solved] |
Posted: Sun Jan 27, 2019 3:00 pm |
|
|
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
|
|
Posted: Sun Jan 27, 2019 3:03 pm |
|
|
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
|
|
Posted: Sun Jan 27, 2019 3:15 pm |
|
|
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
|
|
Posted: Sun Jan 27, 2019 5:31 pm |
|
|
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: 1354
|
|
Posted: Sun Jan 27, 2019 8:25 pm |
|
|
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
|
|
Posted: Mon Jan 28, 2019 1:05 pm |
|
|
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. |
|
|
|
|
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
|