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);
}
}
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
}
-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