Hallo allemaal,
Ik probeer de on-board CANBUS te activeren met behulp van de meegeleverde code (zie code hieronder). Wij krijgen hem op geen enkele manier aan de praat.
Dit board is aangesloten op een tweede ontwikkelboard. Dit is de EasyPIC-v7. Hierop draait ook de meegeleverde code (voor PIC uiteraard) maar dan voor Node2.
Ik heb tevens een CANBUS analizer eraan gekoppeld. Er zou dus communicatie moeten zijn maar dat is er dus niet.
Heeft iemand een idee waarom er geen communicatie is?
Hieronder staat de code waarop de STM32F107VCT6 draait.
c code:
/* Project name:
CAN_1st (CAN Network demonstration with mikroE's CAN-1 module)
* Copyright:
(c) MikroElektronika, 2011.
* Revision History:
20110511(TL):
- initial release;
* Description:
This code demonstrates how to use CAN library functions and procedures.
It is used together with the CAN_2nd example (on second MCU), and it can
be used to test the connection of MCU to the CAN network.
This node initiates the communication with the 2nd node by sending some
data to its address. The 2nd node responds by sending back the data incre-
mented by 1. This (1st) node then does the same and sends incremented data
back to 2nd node, etc.
With minor adjustments, it should work with any other MCU that has a CAN module.
* Test configuration:
MCU: STM32F107VC
http://www.st.com/internet/com/technical_resources/technical_literature/datasheet/cd00220364.pdf
Dev.Board: EasyMx PRO v7 for STM32(R) ARM(R)
http://www.mikroe.com/eng/products/view/852/easymx-pro-v7-for-stm32/
Oscillator: HSE-PLL, 72.000MHz
Ext. Modules: None.
SW: mikroC PRO for ARM
http://www.mikroe.com/eng/products/view/752/mikroc-pro-for-arm/
* NOTES:
- Turn on PORTE LEDs at SW10 and turn on CAN RX and TX switches at SW12.
*/
unsigned long Can_Init_Flags;
unsigned char 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;
void main() {
GPIO_Digital_Output(&GPIOE_BASE, 0xFF00);
GPIOE_ODR = 0;
Can_Init_Flags = 0; //
Can_Send_Flags = 0; // clear flags
Can_Rcv_Flags = 0; //
Can_Send_Flags = _CAN_TX_XTD_FRAME & // with CANWrite
_CAN_TX_NO_RTR_FRAME;
Can_Init_Flags = _CAN_CONFIG_AUTOMATIC_RETRANSMISSION & // form value to be used
_CAN_CONFIG_RX_FIFO_NOT_LOCKED_ON_OVERRUN & // with CANInit
_CAN_CONFIG_TIME_TRIGGERED_MODE_DISABLED &
_CAN_CONFIG_TX_FIFO_PRIORITY_BY_IDINTIFIER &
_CAN_CONFIG_WAKE_UP;
CAN1InitializeAdvanced(1,5,4,4,1,Can_Init_Flags, &_GPIO_MODULE_CAN1_PD01); // Initialize CAN module
CAN1SetOperationMode(_CAN_OperatingMode_Initialization); // set CONFIGURATION mode
CANSetFilterScale32(0, _CAN_FILTER_ENABLED & _CAN_FILTER_ID_MASK_MODE & _CAN_FILTER_XTD_MSG, ID_2nd, -1);
CAN1SetOperationMode(_CAN_OperatingMode_Normal); // set NORMAL mode
RxTx_Data[0] = 9; // set initial data to be sent
CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags); // send initial message
while(1) { // endless loop
Msg_Rcvd = CAN1Read(0, &Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
if ((Rx_ID == ID_2nd) && Msg_Rcvd) { // if message received check id
GPIOE_ODR = RxTx_Data[0] << 8; // id correct, output data at PORTE
RxTx_Data[0]++ ; // increment received data
Delay_ms(10);
CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags); // send incremented data back
}
}
}
Hieronder staat de code voor de tweede Node op de EasyPIC-v7 board
c code:
/*
* Project name:
Can_Spi_2nd (CAN Network demonstration with mikroE's CAN-SPI module)
* Copyright:
(c) MikroElektronika, 2012.
* Description:
This project is a simple demonstration of using CAN-SPI module; with minor
adjustments, it should work with any other MCU that has a SPI module.
This code demonstrates how to use CANSPI library functions and procedures.
It is used together with the CanSpi_1st example (on second MCU), and it can
be used to test the connection of MCU to the CAN network.
This node receives data, increments it by 1 and send it back to the 1st node.
* Test configuration:
MCU: PIC18F45K22
http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf
Dev.Board: EasyPIC7
http://www.mikroe.com/easypic/
Oscillator: HS-PLL, 32.00000 MHz
Ext. Modules: ac:CAN_SPI_Board on PORTC
SW: mikroC PRO for PIC
http://www.mikroe.com/mikroc/pic/
* NOTES:
- The indicator of good communication in this example is, that the 1st
node always displays even data on PORTB, whilst the 2nd node always
displays odd data.
- Consult the CAN standard about CAN bus termination resistance.
- Turn on LEDs on PORTB SW3.2
*/
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 = 0xA1, ID_2nd = 0xA2; // 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() {
ANSELB = 0; // Configure AN pins as digital I/O
ANSELC = 0;
SLRCON = 0; // Set output slew rate on all ports at standard rate
LATB = 0; // clear PORTB
TRISB = 0; // set PORTB as output
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 &
_CANSPI_CONFIG_LINE_FILTER_OFF;
SPI1_Init(); // initialize SPI1 module
CANSPIInitialize(1,3,3,3,1,Can_Init_Flags); // initialize external CANSPI module
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_F3,ID_1st,_CANSPI_CONFIG_XTD_MSG); // set id of filter B2_F3 to 1st node ID
CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF); // set NORMAL mode
while (1) { // endless loop
Msg_Rcvd = CANSPIRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
if ((Rx_ID == ID_1st) && Msg_Rcvd) { // if message received check id
LATB = RxTx_Data[0]; // id correct, output data at PORTC
RxTx_Data[0]++ ; // increment received data
CANSPIWrite(ID_2nd, RxTx_Data, 1, Can_Send_Flags); // send incremented data back
}
}
}