0% found this document useful (0 votes)
6 views2 pages

Untitled Document (8)

The document contains C code for initializing and using the CAN (Controller Area Network) interface on a microcontroller. It includes functions for sending and receiving CAN messages, specifically for requesting and downloading SDO (Service Data Objects) related to velocity readings. The code configures the necessary peripherals and handles message formatting for communication with a specified node ID.

Uploaded by

ee23b086
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Untitled Document (8)

The document contains C code for initializing and using the CAN (Controller Area Network) interface on a microcontroller. It includes functions for sending and receiving CAN messages, specifically for requesting and downloading SDO (Service Data Objects) related to velocity readings. The code configures the necessary peripherals and handles message formatting for communication with a specified node ID.

Uploaded by

ee23b086
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdint.

h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_can.h"
#include "driverlib/sysctl.h"
#include "driverlib/can.h"
#include "driverlib/pin_map.h"

#include "driverlib/gpio.h"
#include "utils/uartstdio.h"

tCANMsgObject canMessage;
uint8_t dataBuffer[8];

void CAN_Init(void) {

SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

GPIOPinConfigure(GPIO_PB4_CAN0RX);
GPIOPinConfigure(GPIO_PB5_CAN0TX);
GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);
CANInit(CAN0_BASE);
CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);
CANEnable(CAN0_BASE);
}

void reqforvelreading(uint16_t index, uint8_t subIndex, uint8_t nodeID) {

dataBuffer[0] = 0x40; // for sdo data getting


dataBuffer[1] = index & 0xFF; //index should be 6041 for status word
dataBuffer[2] = (index >> 8) & 0xFF;
dataBuffer[3] = subIndex; // sub index is also 00
dataBuffer[4] = dataBuffer[5] = dataBuffer[6] = dataBuffer[7] = 0x00;
canMessage.ui32MsgID = 0x600 + nodeID;
canMessage.ui32MsgLen = 8;
canMessage.pui8MsgData = dataBuffer;
CANMessageSet(CAN0_BASE, 3, &canMessage, MSG_OBJ_TYPE_TX);
UARTprintf("SDO request sent");
}
void ReceivecANMess(void) {
tCANMsgObject receivedMessage;
uint8_t receivedData[8];
receivedMessage.pui8MsgData = receivedData;

if (CANMessageGet(CAN0_BASE, 4, &receivedMessage, true)) {

for (int i = 0; i < receivedMessage.ui32MsgLen; i++) {


UARTprintf("Data ", i, receivedData[i]);
}
void SendSDODownload(uint16_t index, uint8_t subIndex, uint32_t data, uint8_t nodeID) {
dataBuffer[0] = 0x23; dataBuffer[1] = index & 0xFF; dataBuffer[2] =
(index >> 8) & 0xFF; \\ (index is 60ff)
dataBuffer[3] = subIndex; dataBuffer[4] = data & 0xFF; dataBuffer[5]
= (data >> 8) & 0xFF; dataBuffer[6] = (data >> 16) & 0xFF;
dataBuffer[7] = (data >> 24) & 0xFF; \\ subindex is 0x00
\\ data is the velocity setpoint
canMessage.ui32MsgID = 0x600 + nodeID;
canMessage.ui32MsgLen = 8;
canMessage.pui8MsgData = dataBuffer;
CANMessageSet(CAN0_BASE, 2, &canMessage, MSG_OBJ_TYPE_TX);
UARTprintf("SDO sent");
}

You might also like