CAN bus programming

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
Jan8421
Posts: 14
Joined: 30 Sep 2015 13:18

CAN bus programming

#1 Post by Jan8421 » 30 Sep 2015 14:01

Hi
I am building a device that will control some devices in my house. Now evolving tool that will simulate the operation of multiple nodes on the bus.
The device transmits to the bus twenty messages under different ID's = 20 different nodes, but from 1 device.
My problem: I modified the program CAN_1st from the library of PIC32 examples. However, the CAN bus gets only 1 periodic message ID 337.
But the bus should get 20 messages. The hardware simulation works, messages go to CAN2Write, but CAN module of PIC32 has only ID 337.
I'm not experienced in C and do not know where my mistakes. You advise me? I have EasyPIC Fusion v7, PIC32MX795F512L, mikroC PRO for PIC32
v. 3.6.0, low speed USB2CAN interface with PP2CAN software. I am working with my own CAM modules with fault tolerant TJA1054AT (100 kBd).

Code: Select all


unsigned int Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags;  // CAN priznaky
unsigned int Rx_Data_Len;                                    // - delka prijatych
                                                             // dat v bajtech
unsigned int Tx_Data_LenKec;
char RxTx_Data[8];
char Msg_Rcvd;                                               // priznak prijmu
unsigned long ID_kec, ID_2nd = 3;                              // oznaceni uzlu
unsigned long Rx_ID;
unsigned int i;                                          // index ukazatelů
//const unsigned long ID;

// Zpravy pro komunikaci s topenim (v soucasne dobe 20 zprav)
unsigned long ID_1st[] = {337, 963, 1078, 705, 849, 857, 859, 993, 995, 707,
                          1067, 1393, 1397, 1569, 1589, 1136, 1319, 1550,
                          1583, 1617};                       // oznacuje uzel
                          
const unsigned int Tx_Data_Len[] = {4, 8, 6, 5, 8, 8, 8, 8, 8, 1, 6, 6, 4, 5,
                                     3, 5, 8, 2, 4, 6};      // pocet komponentu
                                                             // ve zprave
                                                             // (max hodnota 8)
                                                             
const unsigned char D0[] = {0, 116, 7, 0, 0, 24, 0, 3, 0, 7, 11, 180, 71, 32, 0,
                            0, 0, 8, 1, 192};                // komponenty D0
                            
const unsigned char D1[] = {32, 27, 1, 0, 0, 1, 224, 106, 0, 0, 4, 0, 34, 195, 1,
                            0, 1, 0, 144, 3};                // komponenty D1
                            
const unsigned char D2[] = {40, 0, 0, 0, 0, 0, 12, 12, 0, 0, 0, 0, 0, 25, 2, 0,
                            0, 0, 96, 89};                   // komponenty D2
                            
const unsigned char D3[] = {8, 0, 0, 0, 0, 0, 136, 0, 0, 0, 0, 0, 0, 35, 0, 0,
                            32, 0, 144, 175};                // komponenty D3
                            
const unsigned char D4[] = {0, 128, 8, 4, 0, 0, 8, 0, 0, 0, 0, 0, 0, 17, 0, 16,
                            121, 0, 0, 38};                  // komponenty D4
                            
const unsigned char D5[] = {0, 64, 128, 0, 0, 43, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            101, 0, 0, 71};                  // komponenty D5
                            
const unsigned char D6[] = {0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103,
                            0, 0, 0};                        // komponenty D6
                            
const unsigned char D7[] = {0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0, 0, 0};                        // komponenty D7
                            

// rezervuje prostor pro 2 buffery s 8 zpravami (kazda zprava po 16 bajtech)
// zacatek bufferu musi byt zarovnan na 32 bitu
char FIFObuffers[2*8*16] absolute 0xA0000000;

// CAN Inicializacni konstanty
// Baudova rychlost je nastavena podle nasledujici rovnice
// Fbaud = Fosc/(2*N*BRP),  N = SYNC + PHSEG1 + PHSEG2 + PROPSEG = 16
// V tomto pripade Fbaud = 100000
/*Toto do deklaracni sekce*/
const unsigned int SJW = 1;           //puvod. hodnoty: 1 pro 125000
const unsigned int BRP = 2;           //puvod. hodnoty: 2
const unsigned int PHSEG1 = 8;        //puvod. hodnoty: 5
const unsigned int PHSEG2 = 3;        //puvod. hodnoty: 2
const unsigned int PROPSEG = 8;       //puvod. hodnoty: 8
const unsigned int CAN_CONFIG_FLAGS =
                                       _CAN_CONFIG_SAMPLE_ONCE &
                                       _CAN_CONFIG_PHSEG2_PRG_ON &
                                       _CAN_CONFIG_STD_MSG       &
                                       _CAN_CONFIG_MATCH_MSG_TYPE &
                                       _CAN_CONFIG_LINE_FILTER_OFF;

void main() {
  AD1PCFG = 0;                                    // konfiguruje ADC jako digitalni I/O
  LATB = 0;
  TRISB = 0;
  i = 0;                                          //ukazatel na prvky poli

  Can_Init_Flags = 0;                             //
  Can_Send_Flags = 0;                             // smaze priznaky
  Can_Rcv_Flags  = 0;                             //

  Can_Send_Flags =                                // formát použité hodnoty
                   _CAN_TX_STD_FRAME &            // s CAN2Write
                   _CAN_TX_NO_RTR_FRAME;

  // Inicializační sekce pro CAN
  CAN2Initialize(SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CAN_CONFIG_FLAGS);

  CAN2SetOperationMode(_CAN_MODE_CONFIG,0xFF);    // nastavení konfiguračního módu

  CAN2AssignBuffer(FIFObuffers);                  // priradime buffery
  // Konfigurujeme fifo pro RX
  CAN2ConfigureFIFO(0, 8,_CAN_FIFO_RX & _CAN_FULL_MESSAGE); // Buffer RX 8 hloubka
                                                            // 8 zprav
  // Konfigurujeme fifo pro TX
  CAN2ConfigureFIFO(1, 8,_CAN_FIFO_TX & _CAN_TX_PRIORITY_3 & _CAN_TX_NO_RTR_FRAME);
                                                  // Buffer TX hloubka 8 zprav
  // Nastaveni masky 0
  CAN2SetMask(_CAN_MASK_0, -1, _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_STD_MSG);
                                                  // nastavi vsechny bity mask1 do jednicek
  // Nastaveni filtru 1
  CAN2SetFilter(_CAN_FILTER_31, ID_2nd, _CAN_MASK_0, _CAN_BUFFER_0, _CAN_CONFIG_STD_MSG);
                                                  // nastavi id filter1 na ID
                                                  // uzlu prijimace

  CAN2SetOperationMode(_CAN_MODE_NORMAL,0xFF);    // Nastavi mod NORMAL
  
 while(1){
 
    for (i = 0; i < 20; i++) {
     ID_kec = ID_1st[i];
     Tx_Data_LenKec = Tx_Data_Len[i];
     RxTx_Data[0] = D0[i];                        // Nacteni komponentu zprav
     RxTx_Data[1] = D1[i];                        // pro odeslani z poli
     RxTx_Data[2] = D2[i];
     RxTx_Data[3] = D3[i];
     RxTx_Data[4] = D4[i];
     RxTx_Data[5] = D5[i];
     RxTx_Data[6] = D6[i];
     RxTx_Data[7] = D7[i];

     CAN2Write(ID_kec, RxTx_Data, Tx_Data_LenKec, Can_Send_Flags); // odesilame zpravu
     
    }

   /* puvodní PGM
      Msg_Rcvd = CAN2Read(&Rx_ID, RxTx_Data, &Rx_Data_Len, &Can_Rcv_Flags);        // receive message
      if ((Rx_ID == ID_2nd) && Msg_Rcvd) {                                         // if message received check id
      LATB = RxTx_Data[0];                                                       // id correct, output data at PORTB
      RxTx_Data[0]++;                                                            // increment received data
      delay_ms(10);
      CAN2Write(ID_kec, RxTx_Data, 1, Can_Send_Flags);                           // send incremented data back
    }   */
  }
}
Thank you.

Jan
Attachments
CAN_Tool.jpg
CAN_Tool.jpg (283.72 KiB) Viewed 3579 times

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: CAN bus programming

#2 Post by Aleksandar.Mitrovic » 01 Oct 2015 14:12

Hi Jan,

Can you please tell me did you try your code in debug mode?

Also, did you try to observe "ID_kec" and see if its change in "for" loop?

Best regards,
Aleksandar

Jan8421
Posts: 14
Joined: 30 Sep 2015 13:18

Re: CAN bus programming

#3 Post by Jan8421 » 01 Oct 2015 15:57

Hi Aleksandar

Yes, I tested the code in debug mode. Today I did it again. I send pictures of tests. i = 0, i = 1,...
This would be the result - Table1.
Unfortunately, it shows only the message ID 337 permanently.

THX.

Jan
Attachments
I_equal_1.jpg
I_equal_1.jpg (153.44 KiB) Viewed 3543 times
I_equal_0.jpg
I_equal_0.jpg (153.14 KiB) Viewed 3543 times
Table1.pdf
(73.52 KiB) Downloaded 139 times

Jan8421
Posts: 14
Joined: 30 Sep 2015 13:18

Re: CAN bus programming

#4 Post by Jan8421 » 02 Oct 2015 08:20

Hi Alexandar
I see now that you have not my yesterday answer.

Yes, I tried to debug the code in the debugger. I send screenshots of debugging (3 samples).

Best regards

Jan
Attachments
PP2CAN2.jpg
PP2CAN2.jpg (125.4 KiB) Viewed 3542 times
Screenshots3.jpg
Screenshots3.jpg (168.69 KiB) Viewed 3542 times

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: CAN bus programming

#5 Post by p.erasmus » 02 Oct 2015 19:25

This might help you understand how to work with PIC32 CAN
http://www.libstock.com/projects/view/2 ... an-example
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

Masterok_78
Posts: 4
Joined: 07 Apr 2020 14:17

Re: CAN bus programming

#6 Post by Masterok_78 » 06 Nov 2022 13:00

p.erasmus wrote:
02 Oct 2015 19:25
This might help you understand how to work with PIC32 CAN
http://www.libstock.com/projects/view/2 ... an-example
It's an empty ask.
Man need a help, but you talk about one example. Do you see that example? It's repeats a CAN library , and it's fully unless.
I also try to decide this situation, because exmple from CAN library makes a rerepiterred asks from device #1 only first ID mesage but timeless.

Producers of MIKROE, are you ready to answer the question from many many years ago ???


P.S. Once again I am convinced of the incompetence of compiler developers for PIC and PIC32.

Post Reply

Return to “mikroC PRO for PIC32 General”