External Interrupt Samples needed

General discussion on mikroPascal PRO for PIC32.
Post Reply
Author
Message
bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

External Interrupt Samples needed

#1 Post by bobspencerr » 08 Jan 2012 14:57

Hi people,

Thanks to JPC for his wonderful help in another thread regarding On Change events.
I however need to get a sample of how to setup and use the External Interrupt.
I have to be able to get a trigger on a rising edge only (or falling only) to a set pin.
I am getting desperate and confused now and have read all of the manuals I can find.

I am using the LV32MX board with the PIC32MX360F128L.

Here s what I have tried so far.

Code: Select all

program RFIDTest;

// LCD module connections
      var LCD_RS : sbit  at LATB2_bit;
      var LCD_EN : sbit  at LATB3_bit;
      var LCD_D4 : sbit  at LATB4_bit;
      var LCD_D5 : sbit  at LATB5_bit;
      var LCD_D6 : sbit  at LATB6_bit;
      var LCD_D7 : sbit  at LATB7_bit;

      var LCD_RS_Direction : sbit at TRISB2_bit;
      var LCD_EN_Direction : sbit at TRISB3_bit;
      var LCD_D4_Direction : sbit at TRISB4_bit;
      var LCD_D5_Direction : sbit at TRISB5_bit;
      var LCD_D6_Direction : sbit at TRISB6_bit;
      var LCD_D7_Direction : sbit at TRISB7_bit;
// End LCD module connections

var z:word;
    tempDummy : word;
    
procedure External3Interrupt(); iv IVT_EXTERNAL_3; ilevel 2; ics ICS_AUTO;
var dummy: word;
begin
     dummy := portA;
     latc1_bit := 1;
     IFS0.INT1IF :=0;
end;

procedure init_all;
begin
   //Initialize_RFIDSection;
   tempDummy := 0;
   DisableInterrupts();
   Lcd_Init();                          // Initialize LCD
   Lcd_Cmd(_LCD_CLEAR);                 // Clear display
   Lcd_Cmd(_LCD_CURSOR_OFF);            // Cursor off
   LCD_Out(1, 1, 'Robert Spencer  ');   // Write text in first row
   LCD_Out(2, 1, 'RFID Reader     ');   // Write text in second row
   delay_ms(100);
   TRISC.1 := 0;
   TRISC.2 := 0;
   TRISC.3 := 0;
   AD1PCFG := 0xFFFF;                   //Configure for digital
   
   TRISC.1  := 0;
   TRISA.14 := 1; //External Interupt 3  
   IPC0Set := 0x00000008; // set priority level to 2
   IEC0.INT3IE :=1;
   EnableInterrupts();
end;

begin
  init_all;
  while true do
  begin

       LATC.1 := 0;
       LATC.2 := 0;
       latc.3 := not portc.3;
       delay_ms(1000);
  end;
end.


piort
Posts: 1379
Joined: 28 Dec 2005 16:42
Location: Laval,Québec,Canada,Earth... :-)
Contact:

Re: External Interrupt Samples needed

#2 Post by piort » 08 Jan 2012 15:43

hi,
sometime it can be simple thing like... you put C.1 at 1 in the ISR but the same pin at zero in the main loop. so that can be working but too fast than you cant see it .... try something like...

Code: Select all

program RFIDTest;
     
procedure External3Interrupt(); iv IVT_EXTERNAL_3; ilevel 2; ics ICS_AUTO;
var dummy: word;
begin
     dummy := portA;
     If latc.1 = 1 then latc.1 := 0 else latc.1 := 1 ; // if ISR happens, change state of pin
     IFS0.INT1IF :=0;
end;

procedure init_all;
begin
   //Initialize_RFIDSection;
   DisableInterrupts();
   AD1PCFG := 0xFFFF;                   //Configure for digital
   TRISC.1 := 0;
   TRISC.2 := 0;
   TRISA.14 := 1; //External Interupt 3 
   IPC0Set := 0x00000008; // set priority level to 2
   IEC0.INT3IE :=1;
   EnableInterrupts();
end;

begin
  init_all;
  while true do
  begin
       If latc.2 = 1 then latc.2 := 0 else latc.2 := 1 ; // change state of the pin every second to proof main loop working (config speed is ok)
       delay_ms(1000);
  end;
end.
HTH a bit ;-)

bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

Re: External Interrupt Samples needed

#3 Post by bobspencerr » 09 Jan 2012 06:19

Thanks Piort,

I must be close then.
Ill try the sample you did tonight.

Cheers

bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

Re: External Interrupt Samples needed

#4 Post by bobspencerr » 09 Jan 2012 09:45

Woops,

Still not working.....
I have made sure that the portA.14 is actually changing and the LEDs on the board are changing.
Surely it cant be this hard to migrate from the Pic18s to Pic32......

Any other suggestions appreciated.

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

Re: External Interrupt Samples needed

#5 Post by jpc » 09 Jan 2012 09:49

hereby a combined example demonstrating CN interrupt and external interrupt, it is clear that external interrupts are the fastest.
It really is worth the effort reading all relevant sections in the datasheet, i admit this is not a simple task as they may be spreaded over several documents/sections but you will find most things in fact to be very simple and with the experience you have on earlier PIC family's accessing peripherals in general is rather straightforward.

Code: Select all

program RFIDTest;

// LCD module connections
      var LCD_RS : sbit  at LATB2_bit;
      var LCD_EN : sbit  at LATB3_bit;
      var LCD_D4 : sbit  at LATB4_bit;
      var LCD_D5 : sbit  at LATB5_bit;
      var LCD_D6 : sbit  at LATB6_bit;
      var LCD_D7 : sbit  at LATB7_bit;

      var LCD_RS_Direction : sbit at TRISB2_bit;
      var LCD_EN_Direction : sbit at TRISB3_bit;
      var LCD_D4_Direction : sbit at TRISB4_bit;
      var LCD_D5_Direction : sbit at TRISB5_bit;
      var LCD_D6_Direction : sbit at TRISB6_bit;
      var LCD_D7_Direction : sbit at TRISB7_bit;
// End LCD module connections

var z:word;

procedure CN_ISR; iv IVT_CHANGE_NOTICE ; ilevel 5; ics ICS_AUTO;
var dummy : word;
begin
  dummy := portf;   // read port to clear mismatch and buffer the state to allow for responding to individual CN-events
  if dummy.5 = 1 then  latc1_bit := 1;
  // now test for other CN pins that obviously should be configured as such
  // and deal with them one by one
  cnif_bit := 0;
end;

procedure ExtInt3_ISR(); iv IVT_EXTERNAL_3; ilevel 2; ics ICS_AUTO;
begin
  latc2_bit := 1 ;
  INT3IF_bit := 0;
end;

procedure init_all;
begin
   DisableInterrupts();
   Lcd_Init();                          // Initialize LCD
   Lcd_Cmd(_LCD_CLEAR);                 // Clear display
   Lcd_Cmd(_LCD_CURSOR_OFF);            // Cursor off
   LCD_Out(1, 1, 'Robert Spencer  ');   // Write text in first row
   LCD_Out(2, 1, 'RFID Reader     ');   // Write text in second row
   delay_ms(100);
   TRISC.1 := 0;
   TRISC.2 := 0;
   TRISC.3 := 0;
   AD1PCFG := 0xFFFF;                   //Configure for digital
   cnip2_bit := 1;                      // set interrupt priority
   cnip1_bit := 0;
   cnip0_bit := 1;
   CNPUE18_bit := 0;                    // disable weak pullup
   cnen18_bit :=1;                      // enable CN interrupt for this pin
   ON__CNCON_bit:= 1;                   // enable CN interrupts
   cnie_bit := 1;
   // configure Ext INT3
   int3ip0_bit := 0;                    // IP-level 2
   int3ip1_bit := 1;
   int3ip2_bit := 0;
   INT3EP_bit := 0;                     // INT3 falling edge
   INT3IE_bit := 1;
   EnableInterrupts();
end;

begin
  init_all;
  while true do
  begin
       LATC.1 := 0;
       LATC.2 := 0;
       latc.3 := not portc.3;
       delay_ms(1000);
  end;
end.
Au royaume des aveugles, les borgnes sont rois.

bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

Re: External Interrupt Samples needed

#6 Post by bobspencerr » 09 Jan 2012 11:14

Fantastic JPC,

I relly appreciate the helpy yet again.
I am going to devote a lot of time to really reading the documents.

Cheers

Post Reply

Return to “mikroPascal PRO for PIC32 General”