CAN bus interrupts on PIC32

General discussion on mikroC PRO for PIC32.
Author
Message
mrpoons
Posts: 12
Joined: 18 Sep 2012 10:23

CAN bus interrupts on PIC32

#1 Post by mrpoons » 01 Jun 2013 13:23

I am trying to get CAN bus interrupts working, at the moment without any success.

I have two easyPIC Fusion v7 boards with PIC32MX795F512L mcu cards which are properly connected and successfully pass CAN messages back and forth. I am now trying to add a CAN receive interrupt.

I have enabled interrupts, set the CAN2 interrupt priority and enabled both the CAN2 interrupt and CAN2 Receive Buffer interrupts.

EnableInterrupts();

CAN2IP0_bit = 1; // Set CAN2 interrupt priority
CAN2IP1_bit = 1; // to level 6
CAN2IP2_bit = 1;

CAN2IE_bit = 1;
RBIE_C2INT_bit = 1;

I have also defined an interrupt service routine

void C2Interrupt() iv IVT_CAN_2 ilevel 7 ics ICS_AUTO
{
STATUS_LED = ~STATUS_LED;
}

While CAN messages are received the interrupt service routine is never called.

It is always possible I am doing something dumb...

Can anyone help.

Thanks

bartt
Posts: 156
Joined: 20 Jul 2012 18:52
Location: Left coast

Re: CAN bus interrupts on PIC32

#2 Post by bartt » 03 Jun 2013 17:39

I only see one thing with your code.
I would not call EnableInterrupts() until after all the other setup is complete. It probably doesn't matter, but I don't see anything else glaring.
Please keep us posted on this subject, there is some CAN interest here..

mrpoons
Posts: 12
Joined: 18 Sep 2012 10:23

Re: CAN bus interrupts on PIC32

#3 Post by mrpoons » 04 Jun 2013 09:47

Thanks for taking the time to reply.

I have tried EnableInterrupts() at the start before any other initialization and after all of the other initialization is complete. Either way no CAN interrupt. My other interrupts including Timers work as intended.

I am still investigating and will update the forum with what I find. Any suggestions would be gratefully received.

Thanks again

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: CAN bus interrupts on PIC32

#4 Post by dejan.odabasic » 04 Jun 2013 14:26

Hello,

You can look at CAN interrupt as Interrupt Controller inside of "main" Interrupt Controller.
Beside enabling CAN interrupt, you need to specify which of CAN's many interrupts will be enabled.
Please take a look at explanation of CiINT: CAN INTERRUPT REGISTER.

Every enabled CAN interrupt will rise the IFS1bits.CAN2IF.

Please take a look at following document:
http://ww1.microchip.com/downloads/en/D ... 61154C.pdf

On page 90 you can find block diagram of interrupt controller.

Best regards.

paulcat
Posts: 29
Joined: 03 Jun 2014 20:51

Re: CAN bus interrupts on PIC32

#5 Post by paulcat » 05 Aug 2015 14:47

Hello I realise this is an old thread but where can I find a list of the available SFRs
as per code included from the Interrupt assistant ? They are not in the help file.

I have looked at the Datasheet re: IFS1bits.CAN2IF so if I wish to configure manually what is the syntax ?

e.g.

CAN2IP0_bit = 1; // Set CAN2 interrupt priority
CAN2IP1_bit = 1; // to level 6
CAN2IP2_bit = 1;

CAN2IE_bit = 1;
RBIE_C2INT_bit = 1;

I am using a PIC32MX570F512 with a single CAN controller on board (CAN1).
The TX is fine but I cannot get the RX to work with the CAN1Read function or interrupts.
I would like to look at the errors coming from the CAN registers if possible.

I have had it working on the PIC32MX795 device.
Thanks

Paul

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: CAN bus interrupts on PIC32

#6 Post by aCkO » 06 Aug 2015 05:19

Use CTRL+ALT+D to open MCU definition file.

Regards

paulcat
Posts: 29
Joined: 03 Jun 2014 20:51

Re: CAN bus interrupts on PIC32

#7 Post by paulcat » 06 Aug 2015 10:02

Thank you aCkO, I found them in the end, but that shortcut is much easier!

petrus88
Posts: 30
Joined: 24 Oct 2019 12:10

Re: CAN bus interrupts on PIC32

#8 Post by petrus88 » 20 Nov 2019 12:38

Hello,
I apologize but I did not understand how to solve the problem, I am in the same situation,
I have enabled it as an example above:

Code: Select all

CAN2IP0_bit = 1; // Set CAN2 interrupt priority
CAN2IP1_bit = 1; // to level 6
CAN2IP2_bit = 1;

CAN2IE_bit = 1;
RBIE_C2INT_bit = 1;

and declared my funcion interrup in main file, but my interrupt still does not go

can you help me

please

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: CAN bus interrupts on PIC32

#9 Post by stefan.filipovic » 21 Nov 2019 16:30

Hi,

Could you please provide us with more details?
Could you post a minimal project (zipped) for inspection?

Kind regards,
Stefan Filipović

petrus88
Posts: 30
Joined: 24 Oct 2019 12:10

Re: CAN bus interrupts on PIC32

#10 Post by petrus88 » 22 Nov 2019 12:09

hi Stefan,
tnks for your reply.

I'm using mikroc pro for Pic32 whit 2 evaluation EV board Easy pic fusion, i'm tring can bus communication using the standard can bus example, in the board n°2 i have changed the fw to use the canbus with in interrupt-mode and not in polling mode but not work and unfortunately i' m not sure of my configuration.

i'm also sniffing the can bus and i see the communicaton who start of the first board but the second board not reply, because the interrupt not is active.

in attach my software interrupt

Regard
P
Attachments
CAN 2nd - Interrupt.zip
(214.86 KiB) Downloaded 118 times

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: CAN bus interrupts on PIC32

#11 Post by stefan.filipovic » 26 Nov 2019 10:28

Hi,

I believe you need to enable this bit as well:

Code: Select all

MODIE_C2INT_bit = 1;
Kind regards,
Stefan Filipović

petrus88
Posts: 30
Joined: 24 Oct 2019 12:10

Re: CAN bus interrupts on PIC32

#12 Post by petrus88 » 26 Nov 2019 14:25

Tnks,
The interupt are now enabled, but i'm a problem to read the data.

it seem who the library can not read the data buffer.

this is now my configuration:

register initialization:

Code: Select all



  CAN2IP0_bit = 1; 
  CAN2IP1_bit = 1; 
  CAN2IP2_bit = 1;


  RBIE_C2INT_bit  =1;
  MODIE_C2INT_bit = 1;
  CAN2IE_bit = 1;
  RXNEMPTYIF_C2FIFOINT1_bit = 1;
  EnableInterrupts();

and ISR:

Code: Select all


void Can2_interrupt() iv IVT_CAN_2 ilevel 7 ics ICS_SRS 
{
   
    Msg_Rcvd = CAN2Read(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags);        // receive message
    LATD = Can_Rcv_Flags;
    if ((Rx_ID == ID_1st) && Msg_Rcvd  ) {                                           // if message received check id
      LATB = RxTx_Data[0];                                                         // id correct, output data at PORTB
      RxTx_Data[0]++;
      RxTx_Data[1]='c';  //c = 99d
      CAN2Write(ID_2nd, RxTx_Data, 2, Can_Send_Flags);                             // send incremented data back
    }

  LATA = ~LATA;
  
}

When the interrupt is generated the Msg_Rcvd is == 0 as it can not rad the data..

TNKS

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: CAN bus interrupts on PIC32

#13 Post by stefan.filipovic » 26 Nov 2019 15:06

Hi,

It worked well on my hardware, please find the test projects I've used for the test in the attachment.

Make sure to add a 120 Ohm resistor between CAN H and L lines as specified by CAN bus protocol. (if you do not have this resistor, you can try with some other)

Kind regards,
Attachments
CAN 2nd - Interrupt.zip
(234.31 KiB) Downloaded 147 times
CAN 1st.zip
(206.78 KiB) Downloaded 134 times
Stefan Filipović

petrus88
Posts: 30
Joined: 24 Oct 2019 12:10

Re: CAN bus interrupts on PIC32

#14 Post by petrus88 » 27 Nov 2019 10:36

TNKS Stefan,

all work fine.

P

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: CAN bus interrupts on PIC32

#15 Post by stefan.filipovic » 27 Nov 2019 14:04

Hi,

You're welcome.

Kind regards,
Stefan Filipović

Post Reply

Return to “mikroC PRO for PIC32 General”