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.
7 years, 8 months ago.
can bus
hi guys;
I am trying to read the value of the potentiometer from the CAN bus line, I can read the values, but how can I read 16 bits in 10 system?
1 Answer
7 years, 8 months ago.
You can send various data types over CAN bus for example as follows:
Arduino code
... const unsigned int ID_SENSOR_VALUE = 0x201; const unsigned int ID_OUTPUT_VALUE = 0x202; const unsigned int ID_SENSOR_VOLTAGE = 0x203; ... union CanDataType { byte data[8]; // 8 bytes (max length) unsigned int sensorValue; // 2 bytes char outputValue; // 1 byte float sensorVoltage; // 4 bytes }; ... CanDataType canData; unsigned int sensorValue; // 2 bytes byte outputValue; // 1 byte float sensorVoltage; // 4 bytes ... void loop() { ... sensorValue = analogRead(analogInPin); outputValue = map(sensorValue, 0, 1023, 0, 255); sensorVoltage = (sensorValue * 5) / 1023.0f; ... canData.sensorValue = sensorValue; CAN.sendMsgBuf(ID_SENSOR_VALUE , 0, sizeof(canData.sensorValue), &canData); delay(1000); canData.outputValue = outputValue; CAN.sendMsgBuf(ID_OUTPUT_VALUE, 0, sizeof(canData.outputValue), &canData); delay(1000); canData.sensorVoltage = sensorVoltage; CAN.sendMsgBuf(ID_SENSOR_VOLTAGE, 0, sizeof(canData.sensorVoltage), &canData); delay(1000); }
LPC1768 code
#include "mbed.h" const uint16_t ID_SENSOR_VALUE = 0x201; const uint16_t ID_OUTPUT_VALUE = 0x202; const uint16_t ID_SENSOR_VOLTAGE = 0x203; Serial pc(USBTX, USBRX); DigitalOut led1(LED1); DigitalOut led2(LED2); CAN can(p30, p29); CANMessage msg; uint16_t sensorValue; // 2 bytes uint8_t outputValue; // 1 byte float sensorVoltage; // 4 bytes int main() { pc.baud(9600); can.frequency(250000); while(1) { if(can.read(msg)) { led1 = !led1; pc.printf("CAN message received\r\n"); pc.printf(" ID = 0x%.3x\r\n", msg.id); pc.printf(" Type = %d\r\n", msg.type); pc.printf(" Format = %d\r\n", msg.format); pc.printf(" Length = %d\r\n", msg.len); pc.printf(" Data ="); for(int i = 0; i < msg.len; i++){ pc.printf(" %.2x", msg.data[i]); } pc.printf("\r\n"); switch(msg.id) { case ID_SENSOR_VALUE: sensorValue = *((uint16_t*)msg.data); pc.printf("sensorValue = %d\r\n", sensorValue); break; case ID_OUTPUT_VALUE: outputValue = *((uint8_t*)msg.data); pc.printf("outputValue = %d\r\n", outputValue); break; case ID_SENSOR_VOLTAGE: sensorVoltage = *((float*)msg.data); pc.printf("sensorVoltage = %5.3fV\r\n", sensorVoltage); break; } } } }
f7==247(decimal) ı want to decimal data?
posted by EKREM KELES 12 Dec 2016