PIC18F4550 and CANSPI

Post your requests and ideas on the future development of mikroC.
Post Reply
Author
Message
Cvijo
Posts: 4
Joined: 18 Jul 2011 11:47

PIC18F4550 and CANSPI

#1 Post by Cvijo » 24 Jul 2011 02:12

Hi people! I hope that we will be able to help. I want to make communication between the CAN bus and the PC via USB by using PIC18F4550. USB communication is ok, but I have a problem when I want to initialize the external SPI interface. I use the MCP2515 as a transceiver. When I use the connection between the MCP2515 and PIC16F887 everything is fine. Now, when I want to use PIC18F4550 instead PIC16F886, I have to reconnect some connection, RC3, RC4, RC5 is substituted with RB1, RB0, RC7. Furthermore, when using the MicroC, this is not enough, because the program always stops at initializing the SPI interface to the MCP. What is wrong?


Code: Select all

unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags
unsigned char Rx_Data_Len;                                   // received data length in bytes
char RxTx_Data[8];                                           // can rx/tx data buffer
char Msg_Rcvd;                                               // reception flag
const long ID_1st = 12111, ID_2nd = 3;                       // node IDs
long Rx_ID;

// CANSPI module connections
sbit CanSpi_CS            at  RC0_bit;
sbit CanSpi_CS_Direction  at  TRISC0_bit;
sbit CanSpi_Rst           at  RC2_bit;
sbit CanSpi_Rst_Direction at  TRISC2_bit;
// End CANSPI module connections

void main(void){
 ADCON1 |= 0x0F;                         // Configure all ports with analog function as digital
 CMCON  |= 7;                            // Disable comparators

  Can_Init_Flags = 0;                                         //
  Can_Send_Flags = 0;                                         // clear flags
  Can_Rcv_Flags  = 0;                                         //

  Can_Send_Flags = _CANSPI_TX_PRIORITY_0 &                    // form value to be used
                   _CANSPI_TX_XTD_FRAME &                     //     with CANSPIWrite
                   _CANSPI_TX_NO_RTR_FRAME;

  Can_Init_Flags = _CANSPI_CONFIG_SAMPLE_THRICE &             // Form value to be used
                   _CANSPI_CONFIG_PHSEG2_PRG_ON &             //  with CANSPIInit
                   _CANSPI_CONFIG_XTD_MSG &
                   _CANSPI_CONFIG_DBL_BUFFER_ON &
                   _CANSPI_CONFIG_VALID_XTD_MSG;

  SPI1_Init();                                                     // initialize SPI1 module
  PORTD.B0 = 1; // THIS IS OK

  CANSPIInitialize(1,3,3,3,1,Can_Init_Flags);                      // Initialize external CANSPI module      <<-- THERE IS STOP !!!

  PORTD.B1 = 1;
  CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF);                // set CONFIGURATION mode
  CANSPISetMask(_CANSPI_MASK_B1,-1,_CANSPI_CONFIG_XTD_MSG);        // set all mask1 bits to ones
  CANSPISetMask(_CANSPI_MASK_B2,-1,_CANSPI_CONFIG_XTD_MSG);        // set all mask2 bits to ones
  CANSPISetFilter(_CANSPI_FILTER_B2_F4,ID_2nd,_CANSPI_CONFIG_XTD_MSG);// set id of filter B2_F4 to 2nd node ID

  CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF);

while(1){
...
}
}
P.S. I have EASYPic6

THANKS A LOT...
Attachments
CANSPI PIC16F.JPG
CANSPI PIC16F.JPG (40.54 KiB) Viewed 7409 times

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

Re: PIC18F4550 and CANSPI

#2 Post by janko.kaljevic » 27 Jul 2011 12:19

Hello Cvijo,

I have modified CSNSPI example from our compiler, for two PIC18F4550. So you will be able to use it, as a reference for your project.

Best regards.
Attachments
Can Spi P18F4550.rar
(98.38 KiB) Downloaded 750 times

TEJAS
Posts: 5
Joined: 28 Jan 2012 14:05

Re: PIC18F4550 and CANSPI

#3 Post by TEJAS » 28 Jan 2012 15:00

can anyone please tell me what is the Difference between CAN & CAN SPI ......confused

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

Re: PIC18F4550 and CANSPI

#4 Post by janko.kaljevic » 30 Jan 2012 12:34

Hello,

Some controllers have built in CAN modules and they only need CAN transceiver circuit like MPC2551.
If you want to use CAN communication with other controllers you will have to use CAN SPI, which implements stand alone CAN controller MCP2515.

Best regards.

TEJAS
Posts: 5
Joined: 28 Jan 2012 14:05

Re: PIC18F4550 and CANSPI

#5 Post by TEJAS » 30 Jan 2012 18:04

I am a student .......I want to implement my project on PIC18F458 using CAN protocol to control the intensity of light.....I cannot buy mikroC as I don't have enough money.....How can I decide if I am able to complete my project within 2 kB program memory

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

Re: PIC18F4550 and CANSPI

#6 Post by janko.kaljevic » 31 Jan 2012 10:22

Hello,

Demo limit is 2k words and if you take a look at the CAN example that's provided with compiler, you will see that you can build it under demo limit.
So I am sure that you will be able to make simple CAN project under demo limit.

Best regards.

TEJAS
Posts: 5
Joined: 28 Jan 2012 14:05

Re: PIC18F4550 and CANSPI

#7 Post by TEJAS » 31 Jan 2012 18:00

Is it possible to write a code in CAN in C program without using CAN function ................is there any sample code available on internet or any tutorial which can guide for actual programming of CAN without using the mikroC function

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

Re: PIC18F4550 and CANSPI

#8 Post by janko.kaljevic » 01 Feb 2012 11:00

Hello,

If you do not want to use CAN library, you can write your own functions that will implement CAN.
The best is to use your controller's datasheet as starting point.

Best regards.

Post Reply

Return to “mikroC Wish List”