Selectable CAN bus rate

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
beambase
Posts: 94
Joined: Wed Jul 29, 2009 5:15 pm
Has thanked: 6 times
Been thanked: 8 times
Contact:

Selectable CAN bus rate

Post by beambase »

Hello
I am working on a project where I try to have a selectable CAN bus rate. What I would like to do is to be able to read alternative values for the CNF1, CNF2 and CNF3 settings from EEPROM and send the values to the MCP2515 CAN controller.

The approach that I've been trying is this sequense.

Do a first standard Initiation of the CAN.

Read values from EEPROM

Set the CAN controller in configuration mode

Send the new CNF1, CNF2 and CNF3 values

End CAN controller configuration mode

So far I have tried to do custom C code for the CAN controller configuration and sending the values but I can't get it to work. It won't even compile.

Has anyone tried something like this and made it work?
Should I be using a different approach?
Ideas please.....

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: Selectable CAN bus rate

Post by Sean »

I have used a similar technique to reprogram the CAN receive filters and interrupt enable flags in a MIAC (after allowing standard initialization).

The following code was used in a C code block.

// Request config mode.
FCD_CAN0_CAN_Config(REQOP_CONFIG);

//Initialise all the CAN device registers
//enable SPI
clear_bit(MX_CAN_CS_PORT, CAN_CHIPSEL);

FCD_CAN0_SPI_SendByte(CAN_WRITE); //CAN write mode
FCD_CAN0_SPI_SendByte(0x00); //Address of filter 0 0x00

//Reprogram the CAN comms Filters
FCD_CAN0_SPI_SendByte( FCV_MOD_REQ >> 8 ); // RXF0SIDH 0x00
FCD_CAN0_SPI_SendByte(FCV_MOD_REQ); // RXF0SIDL 0x01

//disable SPI
set_bit(MX_CAN_CS_PORT, CAN_CHIPSEL);

delay_us(10);

clear_bit(MX_CAN_CS_PORT, CAN_CHIPSEL);

FCD_CAN0_SPI_SendByte(CAN_WRITE); //CAN write mode
FCD_CAN0_SPI_SendByte(0x08); //Address of filter 2 0x08

FCD_CAN0_SPI_SendByte( FCV_MOD_INT >> 8 ); // RXF2SIDH 0x08
FCD_CAN0_SPI_SendByte(FCV_MOD_INT); // RXF2SIDL 0x09

//disable SPI
set_bit(MX_CAN_CS_PORT, CAN_CHIPSEL);

delay_us(10);

clear_bit(MX_CAN_CS_PORT, CAN_CHIPSEL);

FCD_CAN0_SPI_SendByte(CAN_WRITE); //CAN write mode
FCD_CAN0_SPI_SendByte(0x2b); // Address of CANINTE 0x2B

FCD_CAN0_SPI_SendByte(RX1IE);

//disable SPI
set_bit(MX_CAN_CS_PORT, CAN_CHIPSEL);

//Put the CAN device into normal mode
FCD_CAN0_CAN_Config(REQOP_NORMAL);


Direct calls from C code to component functions require addition of the prefixes added by Flowcode

E.g. CAN_Config() is renamed FCD_CAN0_CAN_Config() in the final program.

If the compilation is failing due to due to unresolved function calls, or undefined variables, reading the C file produced by Flowcode should allow you to see the required representation.

The CAN component includes a large number of register and flag definitions which can also be viewed in the C file or Custom code windows.

Post Reply