Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 1 month ago.
CAN bus monitoring
Dear community members, I am working on (Controller Area Network) analyzer for monitoring CAN bus on vehicles (not for diagnostics, just to see simple message frames with identifiers, etc.) with stm32f103rb MCU This is my code but I cant receive any frame in my Terminal usart, does anyone have any experience? I really appreciate it because of about 7 days stuck in and I'm using (STM-P103 BOARDS) with KEIL IDE. Thank you very much.
/* Includes --------------*/
- include "stm32f10x.h"
- include "USART.h"
- include "CAN.h"
- include <stdio.h>
unsigned int val_Tx = 0, val_Rx = 0; /* Globals used for display */
void Delay (uint32_t Time);
/*----------------
fputc
----------------*/ int fputc(int ch, FILE *f) { return (USART2_SendChar(ch)); }
/*----------------
initialize CAN interface
----------------*/ void can_Init (void) {
CAN_setup (); /* setup CAN Controller */ CAN_wrFilter (33, STANDARD_FORMAT); /* Enable reception of msgs */
/* ! COMMENT LINE BELOW TO ENABLE DEVICE TO PARTICIPATE IN CAN NETWORK ! */ CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM); Loopback, Silent Mode (self-test)
CAN_start (); /* start CAN Controller */
CAN_waitReady (); /* wait til tx mbx is empty */ }
*****************/
int main(void)
{
int i;
char str[50];
SystemInit();
USART2_Init();
USART2_SendString("\n\r-----");
USART2_SendString("\n\r");
USART2_SendString("\n\r");
USART2_SendString("\n\r");
USART2_SendString("\n\r");
can_Init (); /* initialize CAN interface */
CAN_TxMsg.id = 33; /* initialize msg to send */ for (i = 0; i < 8; i++) CAN_TxMsg.data =0; CAN_TxMsg.len = 1; CAN_TxMsg.format = STANDARD_FORMAT; CAN_TxMsg.type = DATA_FRAME;
while (1) {
if (CAN_TxRdy) { /* tx msg on CAN Ctrl */ CAN_TxRdy = 0;
CAN_TxMsg.data[0] = val_Tx++; /* data[0] = ADC value */ CAN_wrMsg (&CAN_TxMsg); /* transmit message */ USART2_SendString("\r\nTransfer "); sprintf(str,"%i ", CAN_TxMsg.data[0]); USART2_SendString(str); }
Delay (100); /* delay for 10ms */ 0 Receive 11:49:09:845 Data frame Standard frame 00000130 5 05 f0 fc ff ff format; 0 - STANDARD, 1- EXTENDED IDENTIFIER type; 0 - DATA FRAME, 1 - REMOTE FRAME if (CAN_RxRdy) { /* rx msg on CAN Ctrl */ USART2_SendString("\r\nReceive "); if(CAN_RxMsg.type == 0) { USART2_SendString("DATA FRAME "); } else { USART2_SendString("REMOTE FRAME "); } if(CAN_RxMsg.format == 0) { USART2_SendString("STANDARD "); } else { USART2_SendString("EXTENDED IDENTIFIER "); } sprintf(str,"%x ", CAN_RxMsg.id); USART2_SendString(str);
sprintf(str,"%x ", CAN_RxMsg.len); USART2_SendString(str); for (i=0;i<CAN_RxMsg.len ;i++) { sprintf(str,"%x ", CAN_RxMsg.data); USART2_SendString(str); }
CAN_RxRdy = 0;
}
}
}
-------------------