Google Chat: zj734465502@gmail.com
+86-0755-88291180
sales01@spotpear.com
dragon_manager@163.com
services01@spotpear.com
manager01@spotpear.com
WhatsApp:13246739196
RS485 CAN Shield Designed for NUCLEO/XNUCLEO
Two RS485 CAN Shield
Two STM32 development board, we use Waveshare Xnucleo-F103RB board (with STM32F103R chip) in this manual.
Some jumper wire.
The demo program, divided into sending and receiving program, is based on mbed frame + STM32 library.
The CAN driver is written based on STM32 library, packaged into the two file CAN.cpp and CAN.h.
At the beginning of the program, function CAN_Config() is called to initiate related registers.
At the side of sending program, the message to be sent will be saved into the Mailbox TxMessage, then it will be sent by calling CAN_Transmit(CAN1, &TxMessage).
At the side of receiving program, the message received will be saved into the Mailbox RxMessage by calling CAN_Receive(CAN1, CAN_FIFO0, &RxMessage).
The sending side program sets RS485_E to high level, which will make RS485 into sending status. Messages will be sent by function RS485.printf, through RS485 serial port.
The receiving side program enables reception interruption, and sets RS485_E to low level, which will set RS485 to receiving status. Then, RX interrupt handler will scan received message via RS485.scanf.
Connection:
CAN: After related registers are initiated. the message to be sent will be saved into the Mailbox, and then it will be sent by driver functions.
RS485: Set RS485_E to high level, which will make RS485 into sending status. Messages will be sent through RS485 serial port.
#include "mbed.h" #include "CAN.h" Serial pc(D1,D0); //serial print message Serial RS485(D8, D2); //RS485_TX RS485_RX DigitalOut RS485_E(D7); //RS485_E CanTxMsg TxMessage; uint8_t TransmitMailbox = 0; int i =0,j=0; int main() { CAN_Config(); //CAN initiation RS485_E = 1; //enable RS485 sending status /* TxMessage */ //Setting message of Txmessage TxMessage.StdId = 0x10; TxMessage.ExtId = 0x1234; TxMessage.RTR=CAN_RTR_DATA; TxMessage.IDE=CAN_ID_STD; TxMessage.DLC=8; TxMessage.Data[0] = 'C'; TxMessage.Data[1] = 'A'; TxMessage.Data[2] = 'N'; TxMessage.Data[3] = ' '; TxMessage.Data[4] = 'T'; TxMessage.Data[5] = 'e'; TxMessage.Data[6] = 's'; TxMessage.Data[7] = 't'; pc.printf( "**** This is a RS485_CAN_Shield Send test program ****\r\n"); while(1) { RS485.printf("ncounter=%d ",j); //RS485 sending wait(1); TransmitMailbox = CAN_Transmit(CAN1, &TxMessage); //CAN sending i = 0; while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFFF)){ i++; } if(i == 0xFFF){ pc.printf("\r\can send fail\r\n"); //Send Timeout, fail } else{ pc.printf("\r\nCAN send TxMessage successfully \r\n"); //Send successfully } pc.printf("\r\nRS485 send: counter=%d\r\n",j++); //Print message pc.printf("The CAN TxMsg: %s\r\n",TxMessage.Data); wait(1); } }
CAN: After related registers are initiated. The receiving side will check if FIFO data exist. If yes, the received message will be saved in the mailbox RxMessage and printed.
RS485: Enable RS485 reception interruption and set RS485_E to low for setting RS485 to receving status. Then, interrupt service routine will scan received message via RS485.scanf.
#include "mbed.h" #include "CAN.h" Serial pc(D1,D0); //serial print message Serial RS485(D8, D2); //RS485_TX RS485_RX DigitalOut RS485_E(D7); //RS485_E CanRxMsg RxMessage; //RxMessage char s[1024]; void callback() //RS485 RX interrupt handler { //Note: you need to actually read from the serial to clear the RX interrupt RS485.scanf("%s",s); //Save received messages pc.printf("\r\nRS485 Receive:%s \r\n",s); //Print Received messages } int main() { CAN_Config(); //CAN initiation RS485.attach(&callback); //Open RS485 reception interruption RS485_E = 0; //Enable receiving status pc.printf( "**** This is a can receive test program ****\r\n"); while(1) { while(CAN_MessagePending(CAN1, CAN_FIFO0) < 1) // Message waiting { } CAN_Receive(CAN1, CAN_FIFO0, &RxMessage); //CAN data reception pc.printf("The CAN RxMsg: %s\r\n",RxMessage.Data); //Print received messages } }
The serial port of sending side will print:
**** This is a RS485_CAN_Shield Send test program **** CAN send TxMessage successfully RS485 send: counter=0 The CAN TxMsg: CAN Test CAN send TxMessage successfully RS485 send: counter=1 The CAN TxMsg: CAN Test CAN send TxMessage successfully RS485 send: counter=2 The CAN TxMsg: CAN Test
The serial port of receiving side will print:
**** This is a can receive test program **** RS485 Receive:ncounter=0 The CAN RxMsg: CAN Test RS485 Receive:ncounter=1 The CAN RxMsg: CAN Test RS485 Receive:ncounter=2 The CAN RxMsg: CAN Test RS485 Receive:ncounter=3 The CAN RxMsg: CAN Test