CAN and UART issue

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Cecchi
Posts: 20
Joined: 17 Aug 2017 06:37

CAN and UART issue

#1 Post by Cecchi » 19 Oct 2017 04:02

Hi.

I've been burning my neurons on this issue for the past 3 days and can't seem to make any progress.
I'm involved in a big project in which my PIC 18f4580 has to:

-Read I2C data from 4 accelerometers
-Read 3 analog potentiometers
-Send UART data
-Communicate via CAN

Everything worked fine, until I implemented the CAN part. It makes everything freeze and nothing works...
After many hours testing what could the problem be, I found out that, for some reason, CAN and one of my functions won't get along and that results in the whole system stopping. This is the function:

Code: Select all

unsigned char dadoS[10];

void RF_Send(unsigned int dadoI){
     if(dadoI<10){
          IntToStr(dadoI,dadoS);
          Ltrim(dadoS);
          UART1_Write_Text("000");
          UART1_Write_Text(dadoS);
     }
     if(dadoI<100 && dadoI>=10){
          IntToStr(dadoI,dadoS);
          Ltrim(dadoS);
          UART1_Write_Text("00");
          UART1_Write_Text(dadoS);
     }
     if(dadoI<1000 && dadoI>=100){
          IntToStr(dadoI,dadoS);
          Ltrim(dadoS);
          UART1_Write('0');
          UART1_Write_Text(dadoS);
     }
     if(dadoI>=1000){
          IntToStr(dadoI,dadoS);
          Ltrim(dadoS);
          UART1_Write_Text(dadoS);
     }
}
If I comment my CAN_Configuration() function, everything works fine including the RF_Send() function. But, if I try to leave CAN_Configuration() working, the only way the system will not freeze is if I comment or remove the RF_Send() lines...

Here's my CAN_Configuration() function:

Code: Select all

unsigned char Can_Init_Flags;
unsigned long ID_2nd = 12111;

void CAN_Configuration(){
     T1CON.TMR1ON = 0;                      //Desabilita o Timer 1

     Can_Init_Flags = 0;                                       //
     Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &              // form value to be used
                      _CAN_CONFIG_PHSEG2_PRG_ON &              // with CANInit
                      _CAN_CONFIG_XTD_MSG &
                      _CAN_CONFIG_DBL_BUFFER_ON &
                      _CAN_CONFIG_VALID_XTD_MSG;

     CANInitialize(1,4,5,2,8,Can_Init_Flags);                   // Initialize CAN module
     CANSetOperationMode(_CAN_MODE_CONFIG,0xFF);                // set CONFIGURATION mode
     CANSetMask(_CAN_MASK_B1,-1,_CAN_CONFIG_XTD_MSG);           // set all mask1 bits to ones
     CANSetMask(_CAN_MASK_B2,-1,_CAN_CONFIG_XTD_MSG);           // set all mask2 bits to ones
     CANSetFilter(_CAN_FILTER_B2_F4,ID_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B2_F4 to 2nd node ID
     CANSetOperationMode(_CAN_MODE_NORMAL,0xFF);                // set NORMAL mode
     
     T1CON.TMR1ON = 1;                      //Habilita o Timer 1
     PIR1.TMR1IF = 0;                       //Zerando flag do Timer1
     TMR1H = 0X3C;                          //Byte mais significativo
     TMR1L = 0XAF;                          //Byte menos significativo
}
Now some really weird facts:

-If I substitute the RD_Send() functions by raw 100 lines of UART1_Write_Text("Random text") it works just fine.
-If I put one line of IntToStr() in any part of the program, everything freezes (only if CAN is uncommented. Otherwise IntToStr works just fine).

I really ran out of ideas. If anyone could help me, I would be really grateful.

Notes:
PIC 18f4580
MCP 2551 as CAN transceiver
Using Timer1 for interruption to switch cases inside a switch, so I can cycle between functions

paulfjujo
Posts: 1558
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: CAN and UART issue

#2 Post by paulfjujo » 19 Oct 2017 09:28

hello,


i don't lnow if this will solve your problem ,but could participate to a more concise program

Use WordToStr with unsigned int dadoI instead of IntToStr
because string size used by conversion is 7 chars by signed int +32767\0 else 6 chars by unsigned int 65535\0
even dados size is 10 chars wide ...

\0 = string terminator

Cecchi
Posts: 20
Joined: 17 Aug 2017 06:37

Re: CAN and UART issue

#3 Post by Cecchi » 19 Oct 2017 18:07

hello,


i don't lnow if this will solve your problem ,but could participate to a more concise program

Use WordToStr with unsigned int dadoI instead of IntToStr
because string size used by conversion is 7 chars by signed int +32767\0 else 6 chars by unsigned int 65535\0
even dados size is 10 chars wide ...

\0 = string terminator
Thank you for the reply. But that didn't work aswell.
It seems like the whole system does not boot up. It's not like it freezes when it reaches a certain line. It's like everything doesn't even start.

To back that theory up, I tried lighting LEDs up at the start of the program (when I turn CAN and RF_Send on together) and it won't show any sign of life.

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: CAN and UART issue

#4 Post by dusan.poluga » 20 Oct 2017 14:32

Hi,

Do you know the exact line that the program stops executing ?
Have you tired running the debugger ?

Best Regards,
Dusan Poluga.

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

Re: CAN and UART issue

#5 Post by p.erasmus » 23 Oct 2017 19:22

Hi

Just be aware that if your CAN bus Hardware is not functioning the CAN will not confihure and your code will hang inside the configurations section of the code

(1) make sure you have 2 CAN nodes connected to form a real bus
(2) inspect the CAN bus hardware (connections (MCP2551) wiring etc .

Had this issue with the CAN library many times :D
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

Cecchi
Posts: 20
Joined: 17 Aug 2017 06:37

Re: CAN and UART issue

#6 Post by Cecchi » 03 Nov 2017 04:16

dusan.poluga wrote:Hi,

Do you know the exact line that the program stops executing ?
Have you tired running the debugger ?

Best Regards,
Yes, I do. UART is not stopping the program anymore. I did some tinkering on all the configurations available in CAN and now the line that stops the program is a function I created that reads data from the I2C bus.
Also, MikroC shows no bugs whatsoever.
p.erasmus wrote:Hi

Just be aware that if your CAN bus Hardware is not functioning the CAN will not confihure and your code will hang inside the configurations section of the code.

(1) make sure you have 2 CAN nodes connected to form a real bus
(2) inspect the CAN bus hardware (connections (MCP2551) wiring etc .

Had this issue with the CAN library many times :D
I only have 1 node in my bus (actually I didn't create a whole bus at all at the moment, as I'm just trying to configure CAN and, without any more nodes, it worked by itself (without any other functions in the code) and all the wiring is OK.

Post Reply

Return to “mikroC PRO for PIC General”