DSPICPRO3 CAN Bus Interrupts

General discussion on mikroPascal PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
pppyurok
Posts: 9
Joined: 12 Jan 2013 19:52

DSPICPRO3 CAN Bus Interrupts

#1 Post by pppyurok » 21 Jan 2013 18:58

Hello,
I'm trying to connect CAN1 CAN2 module on DSPICPRO3 (dsPIC30F6014A)
Here is a code

Code: Select all

procedure UsartReceiverInit;
var I, Dummy: byte;
begin
   UART1_Init(9600);
   delay_ms(100);

  for I := 0 to 3 do Dummy := U1RxReg;   // clear Uart1 Fifo
  U1RXIF_bit := 0;                       // clear interrupt received flag
  U1RXIE_bit := 0;                       // disable uart1 received interrupt
  U1TXIF_bit:=0;                         // disable uart1 transmitted interrupt
end;

procedure Test_interrupt(); iv IVT_ADDR_C2INTERRUPT; ics ICS_AUTO;
begin

  UART1_Write_Text('This is interrupt');
 if RXB0IF_C2INTF_bit or RXB1IF_C2INTF_bit then
  begin
    RXB0IF_C2INTF_bit:=0;
    RXB1IF_C2INTF_bit:=0;
         Can_Rcv_Flags := 0;                                 // clear message flags
            Msg_Rcvd:=0;
            msg_rcvd := CAN2Read(msg_id, data_, data_len, Can_Rcv_Flags);
        if (msg_rcvd) then
        begin
          UART1_Write_Text('Receive');
        end;
  end;
end;

begin

   UsartReceiverInit;
   UART1_Write_Text('Ready');
   
    ADPCFG := 0xFFFF;
    PORTB  := 0;
    TRISB  := 0;
   PORTF:=0;
    PORTG:=0;
    TRISF.B0 := 1;
    TRISF.B1 := 0;
    TRISG.B0 := 1;
    TRISG.B1 := 0;

  Can_Init_Flags := 0;
  Can_Send_Flags := 0;
  Can_Rcv_Flags  := 0;

  Can_Init_Flags :=   _CAN_CONFIG_SAMPLE_THRICE and    // form value to be used
                      _CAN_CONFIG_PHSEG2_PRG_ON and    // with CANInitialize
                      _CAN_CONFIG_XTD_MSG and
                      _CAN_CONFIG_DBL_BUFFER_ON and
                      _CAN_CONFIG_MATCH_MSG_TYPE and
                      _CAN_CONFIG_LINE_FILTER_OFF;

  CAN1Initialize(1,3,3,3,1,Can_Init_Flags);               // initialize CAN
  CAN2Initialize(1,3,3,3,1,Can_Init_Flags);               // initialize CAN

  IRXIE_C1INTE_bit:=0;IRXIE_C2INTE_bit:=0;
  WAKIE_C1INTE_bit:=0;WAKIE_C2INTE_bit:=0; 
  ERRIE_C1INTE_bit:=0;  ERRIE_C2INTE_bit:=0; 
  TXB2IE_C1INTE_bit:=0; TXB2IE_C2INTE_bit:=0 ;
  TXB1IE_C1INTE_bit:=0; TXB1IE_C2INTE_bit:=0 ;
  TXB0IE_C1INTE_bit:=0; TXB0IE_C2INTE_bit:=0 ;
  RXB1IE_C1INTE_bit:=0; RXB1IE_C2INTE_bit:=1 ;
  RXB0IE_C1INTE_bit :=0;RXB0IE_C2INTE_bit :=1;
  
   tx_flags := _CAN_TX_PRIORITY_0 and _CAN_TX_XTD_FRAME   and _CAN_TX_NO_RTR_FRAME; 
                    // set message flags
  data_[0]:=1;
 while true do
 begin
        msg_id:=3;
        CAN1Write(msg_id, data_, 1, tx_flags);
        delay_ms(100);
 end;
end.
It doesn't hit into test_interrupt procedure. What i missed?

(When i use CAN2Read(msg_id, data_, data_len, Can_Rcv_Flags); in while(true) circle it works )
Thanks

pppyurok
Posts: 9
Joined: 12 Jan 2013 19:52

Re: DSPICPRO3 CAN Bus Interrupts

#2 Post by pppyurok » 21 Jan 2013 19:53

when I added

Code: Select all

IEC2.B6:=1; // CAN2 (combined) interrupt enable bit

it was once hit to test_interrupt
but then seem go to reset
i have terminal string
Ready This is interrupt Ready
Any suggestions?

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: DSPICPRO3 CAN Bus Interrupts

#3 Post by janko.kaljevic » 22 Jan 2013 19:26

Hello,

I have tested CAN interrupt on dsPIC30f6014A without problems.

Have you tried to set C2IP bits?

Code: Select all

  C2IP_0_bit = 1;
  C2IP_1_bit := 1;
  C2IP_2_bit := 1; // Priority level 6
  C2IE_bit := 1; //Enable CAN2 Interrupt
  C2IF_bit := 0; //,Clear Interrupt Flag
  RXB0IE_C2INTE_bit := 1;
  RXB1IE_C2INTE_bit := 1;
Also as I can see your ISR is OK and it should trigger when message is received in either of two buffers.
I assume that all other things are set properly like node IDs.

Best regards.

pppyurok
Posts: 9
Joined: 12 Jan 2013 19:52

Re: DSPICPRO3 CAN Bus Interrupts

#4 Post by pppyurok » 23 Jan 2013 18:20

Thanks for the reply,
i tried to set C2IP bit as you wrote. It still not working.
I slightly modified the code

Code: Select all

procedure Test(); iv IVT_ADDR_C2INTERRUPT; ics ICS_AUTO;
begin
  if          RXB0OVFL_C2INTF_bit   then UART1_Write_Text('1')
  else if     RXB1OVFL_C2INTF_bit   then UART1_Write_Text('2')
  else if     TXBO_C2INTF_bit       then UART1_Write_Text('3')
  else if     TXBP_C2INTF_bit       then UART1_Write_Text('4')
  else if     RXBP_C2INTF_bit       then UART1_Write_Text('5')
  else if     TXWARN_C2INTF_bit     then UART1_Write_Text('6')
  else if     RXWARN_C2INTF_bit     then UART1_Write_Text('7')
  else if     EWARN_C2INTF_bit      then UART1_Write_Text('8')
  else if     IRXIF_C2INTF_bit      then UART1_Write_Text('9')
  else if     WAKIF_C2INTF_bit      then UART1_Write_Text('10')
  else if     ERRIF_C2INTF_bit      then UART1_Write_Text('11')
  else if     TXB2IF_C2INTF_bit     then UART1_Write_Text('12')
  else if     TXB1IF_C2INTF_bit     then UART1_Write_Text('13')
  else if     TXB0IF_C2INTF_bit     then UART1_Write_Text('14')
  else if     RXB1IF_C2INTF_bit     then UART1_Write_Text('15')
  else if     RXB0IF_C2INTF_bit     then UART1_Write_Text('16');

     delay_ms(100);
   Can_Rcv_Flags := 0;                                 // clear message flags
       Msg_Rcvd:=0;
            msg_rcvd := CAN2Read(msg_id, data_, data_len, Can_Rcv_Flags);
        if (msg_rcvd) then
        begin
          UART1_Write_Text('Receive');
        end;
        
   C2INTF:=0;
   C2CTRL:=0;
   IFS2.b6:=0;
end;
It once gets into interrupt and IRXIF_C2INTF_bit is set. (Invalid Message)
I'm not sure exactly, but i investigate _CAN_TX_XTD_FRAME : word = 0xF7; // XXXXX0XX
In dsPIC30F reference manual section 23 CAN Module
register CiTXnSID
bit 0 TXIDE:Extended Identifier bit
1= Message will transmit extended identifier
0= Message will transmit standard identifier
This is doesn't corresponds to _CAN_TX_XTD_FRAME (bit 3)

Have you used CAN Library functions (CANxInitialize, CANxWrite e.t.c.)?
Also is this possible - connect CAN1 and CAN2 on dsPICPRO3?

Thanks in advance
Last edited by pppyurok on 23 Jan 2013 18:42, edited 1 time in total.

pppyurok
Posts: 9
Joined: 12 Jan 2013 19:52

Re: DSPICPRO3 CAN Bus Interrupts

#5 Post by pppyurok » 23 Jan 2013 18:33

P.S. you wrote
I assume that all other things are set properly like node IDs
I don't use any filter and mask or something I did not understand?
P.S.S. I try to connect two can module on one dsPICPRO3 board

pppyurok
Posts: 9
Joined: 12 Jan 2013 19:52

Re: DSPICPRO3 CAN Bus Interrupts

#6 Post by pppyurok » 23 Jan 2013 19:44

I modified Microchip CE_034 for work in Normal mode

Code: Select all

procedure UsartReceiverInit;
var I, Dummy: byte;
begin
   UART1_Init(9600);
   delay_ms(100);

  Setup_Completed_1 := false;
  Setup_Completed_2 := false;
  Setup_Start_byte := false;
  UsartBuffWritePtr := 0;

  for I := 0 to 3 do Dummy := U1RxReg;   // clear Uart1 Fifo
 
  IEC0.b10:=0;
  IEC0.b9:=0;
  U1RXIF_bit := 0;                       // clear interrupt received flag
  U1RXIE_bit := 0;                       // enable uart1 received interrupt

  U1TXIF_bit:=0;                         // disable uart1 transmitted interrupt
end;

procedure Test(); iv IVT_ADDR_C2INTERRUPT; ics ICS_AUTO;
begin

  if          RXB0OVFL_C2INTF_bit   then UART1_Write_Text('1')
  else if     RXB1OVFL_C2INTF_bit   then UART1_Write_Text('2')
  else if     TXBO_C2INTF_bit       then UART1_Write_Text('3')
  else if     TXBP_C2INTF_bit       then UART1_Write_Text('4')
  else if     RXBP_C2INTF_bit       then UART1_Write_Text('5')
  else if     TXWARN_C2INTF_bit     then UART1_Write_Text('6')
  else if     RXWARN_C2INTF_bit     then UART1_Write_Text('7')
  else if     EWARN_C2INTF_bit      then UART1_Write_Text('8')
  else if     IRXIF_C2INTF_bit      then UART1_Write_Text('9')
  else if     WAKIF_C2INTF_bit      then UART1_Write_Text('10')
  else if     ERRIF_C2INTF_bit      then UART1_Write_Text('11')
  else if     TXB2IF_C2INTF_bit     then UART1_Write_Text('12')
  else if     TXB1IF_C2INTF_bit     then UART1_Write_Text('13')
  else if     TXB0IF_C2INTF_bit     then UART1_Write_Text('14')
  else if     RXB1IF_C2INTF_bit     then UART1_Write_Text('15')
  else if     RXB0IF_C2INTF_bit     then UART1_Write_Text('16');


     delay_ms(100);
   Can_Rcv_Flags := 0;                                 // clear message flags
       Msg_Rcvd:=0;
            msg_rcvd := CAN2Read(msg_id, data_, data_len, Can_Rcv_Flags);
        if (msg_rcvd) then
        begin
          UART1_Write_Text('Receive');
        end;
        
   C2INTF:=0;
   C2CTRL:=0;
   IFS2.b6:=0;
end;



begin

  UsartReceiverInit;
  UART1_Write_Text('Ready');
  
   C1CTRL.B11 := 1; //CANCKS			// Select the CAN Master Clock . It is equal to Fcy here.
   C2CTRL.B11 := 1;			// Select the CAN Master Clock . It is equal to Fcy here.
  								// equal to Fcy.(Fcy=30Mhz)								// equal to Fcy.(Fcy=30Mhz)

    C1CFG1:=0x0013 ;
    C2CFG1:=0x0013;

    C1CFG2:= 0x07BF; // 10MHz 10kbit
    C2CFG2:= 0x07BF;

    C2IP_0_bit := 1;
    C2IP_1_bit := 1;
    C2IP_2_bit := 1; // Priority level 6
    C2INTF := 0;
    IFS2.B6 := 0;
    IEC2.B6:=1;
    C2INTE := 0x00FF;               //Enable all interrupt sources


      // We are initializing the Receive Buffer 0 and Receive Buffer 1 for CAN1 and CAN2


 C1RX0CON := 0x0000;
 C1RX1CON := 0x0000;
 C2RX0CON := 0x0000;
 C2RX1CON := 0x0000; 	// Receive Buffer1 and 0 Status
                       //and Control Register for CAN1 and CAN2


 // Acceptance Mask Register0SID and Register1SID associated with Recieve Buffer0
 // and Receive Buffer1 for CAN1 and CAN2
 C1RXM0SID  := 0x1FFD;
 C1RXM1SID  := 0x1FFD;
 C2RXM0SID  := 0x1FFD;
 C2RXM1SID  := 0x1FFD;

 // Acceptance Mask Register0EIDH and Register1EIDH associated with Recieve Buffer0
 // and Receive Buffer1 for CAN1 and CAN2
 C1RXM0EIDH := 0x0FFF;
 C1RXM1EIDH := 0x0FFF;
 C2RXM0EIDH := 0x0FFF;
 C2RXM1EIDH := 0x0FFF;

 // Acceptance Mask Register0EIDL and Register1EIDL associated with Recieve Buffer0
 //and Receive Buffer1 for CAN1 and CAN2
 C1RXM0EIDL := 0xFC00;
 C1RXM1EIDL := 0xFC00;
 C2RXM0EIDL := 0xFC00;
 C2RXM1EIDL := 0xFC00;


//Initializing of Acceptance Filter n Standard Identifier for CAN1

 C1RXF0SID 	:= 0x0AA8;	//CAN1 Receive Acceptance Filter2 SID
 C1RXF2SID 	:= 0x1555;   //CAN1 Receive Acceptance Filter2 SID
 C1RXF2EIDH := 0x0004;   //CAN1 Receive Acceptace  Filter2 Extended Identifier high byte
 C1RXF2EIDL := 0x8C00;	//CAN1 Receive Acceptance Filter2 Extended identifier low byte

//Initializing of Acceptance Filter n Standard Identifier for CAN2

 C2RXF0SID 	:= 0x0AA8;	//CAN2 Receive Acceptance Filter0 SID
 C2RXF2SID 	:= 0x1555;	//CAN2 Receive Acceptance Filter2 SID
 C2RXF2EIDH := 0x0004; 	//CAN2 Receive Acceptace  Filter2 Extended Identifier high byte
 C2RXF2EIDL := 0x8C00;	//CAN2 Receive Acceptance Filter2 Extended identifier low byte

 //-----------------------------------------------------------------------------------------------------------------------
						// Configure Transmit Registers Buffer 0 and Transmit Buffer 1
//-----------------------------------------------------------------------------------------------------------------------

 C1TX0CON := 0x0003;     // High priority
 C1TX0SID := 0x50A8;     // SID
 C1TX0EID := 0x0000;     // EID
 C1TX0DLC := 0x01C0;		//Select the Data word Length for CAN1 Transmit Buffer0 which is 8 byte

 // Data Field 1,Data Field 2, Data Field 3, Data Field 4 // 8 bytes selected by DLC
 OutData0[0]:=0x5251;
 OutData0[1]:=0x5453;
 OutData0[2]:=0x5655;
 OutData0[3]:=0x5857;

 C1TX0B1 := OutData0[0];
 C1TX0B2 := OutData0[1];
 C1TX0B3 := OutData0[2];
 C1TX0B4 := OutData0[3];

 C1TX1CON := 0x0002;             // High Intermediate priority
 C1TX1SID := 0xA855;             // SID
 C1TX1EID := 0x0004;             // EID
 C1TX1DLC := 0x8DA0;				//Select the Data word Length for CAN1 Transmit Buffer1 which
								// is 4 byte

 //Data Field 1, Data Field 2 // 4 bytes selected by DLC
  OutData1[0]:=0x5A59;
  OutData1[1]:=0x5C5B;
 C1TX1B1 := OutData1[0];
 C1TX1B2 := OutData1[1];


 C2TX0CON := 0x0003;             // High priority
 C2TX0SID := 0x50A8;             // SID = 01010101010 (0x2AA)
 C2TX0EID := 0x0000;             // EID = 0000000000000000000 (0x00000)
 C2TX0DLC := 0x01C0;				//Select the Data word Length for CAN2 Transmit Buffer0 which is
								// 8 byte
  OutData2[0]:=0x6261;
  OutData2[1]:=0x6463;
  OutData2[2]:=0x6665;
  OutData2[3]:=0x6867;
 //Data Field 1,Data Field 2, Data Field 3, Data Field 4 // 8 bytes selected by DLC
 C2TX0B1 := OutData2[0];
 C2TX0B2 := OutData2[1];
 C2TX0B3 := OutData2[2];
 C2TX0B4 := OutData2[3];

  //Configure Transmit registers // Transmit Buffer 0 and Transmit Buffer 1 for CAN2

 C2TX1CON := 0x0002;             // High Intermediate priority
 C2TX1SID := 0xA855;             // SID = 10101010101 (0x555)
 C2TX1EID := 0x0004;             // EID = 1110000000100100011 (0x00123)
 C2TX1DLC := 0x8DA0;             //Select the Data word Length for CAN1 Transmit Buffer0 which is
								//8 byte

 //Data Field 1, Data Field 2 // 4 bytes selected by DLC
 OutData3[0]:=0x6A69;
 OutData3[1]:=0x6C6B;
 C2TX1B1 := OutData3[0];
 C2TX1B2 := OutData3[1];

 //Change to Normal Operation Mode from Configuration Mode
 C1CTRL.B10:= 0;
 C1CTRL.B9:=0;
 C1CTRL.B8:= 0;
 
 while (C1CTRL.B7 <> 0) and (C1CTRL.B6 <> 0) and (C1CTRL.B5<> 0)  do
 begin
 end;
 
 C2CTRL.B10:= 0;
 C2CTRL.B9:=0;
 C2CTRL.B8:= 0;

 while (C2CTRL.B7 <> 0) and (C2CTRL.B6 <> 0) and (C2CTRL.B5<> 0)  do
 begin
 end;
 
 C1TX0CON.B3 := 1;
 C1TX1CON.B3:=1;//TXREQ = 1;
 while true do
 begin
 end;
end.
This is works now. I think i didn't use CAN library functions properly :idea: (or something in them are wrong :shock: )

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: DSPICPRO3 CAN Bus Interrupts

#7 Post by janko.kaljevic » 25 Jan 2013 15:49

Hello,

I just wanted to know how did you initialized CAN module and set BAUD rate.
This is not visible from posted code, and everything else looks OK.

For baud rate generation please check our CANculator tool:
http://www.libstock.com/projects/view/360/canculator

Best regards.

Post Reply

Return to “mikroPascal PRO for dsPIC30/33 and PIC24 General”