【问题标题】:Converting Analog input to digital for CAN on the mbed LPC1768在 mbed LPC1768 上将模拟输入转换为 CAN 的数字输入
【发布时间】:2014-08-12 09:49:03
【问题描述】:

我需要代码帮助,将 mbed LPC1768 上的 AnalogIn 输入转换为 CAN 控制器使用的数字。我使用的示例语法是

if(can1.write(CANMessage(1337, &counter, 2))) {
..........
}

其中“counter”是要传输的数据,我将其定义为带符号的 int(但示例将其定义为 char)。但我不断收到一条错误消息

Error: No instance of constructor "mbed::CANMessage::CANMessage" matches the argument list in "project_test.cpp"

控制器 CANMessage 语法为

CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {

  len    = _len & 0xF;
  type   = _type;
  format = _format;
  id     = _id;
  memcpy(data, _data, _len);
}

我真的不明白控制器的语法以及如何应用它。任何解释方面的帮助将不胜感激。谢谢

【问题讨论】:

    标签: can-bus mbed


    【解决方案1】:

    由于 CANMessage 只接受 char* 作为数据参数,因此您可以将带符号的 int 值(即 4 个字节)转换为 unsigned char,如下所示:

    unsigned char buf[0x8];
    buf[0]=value & 0x000000ff;
    buf[1]=(value >> 8)  & 0x000000ff;
    buf[2]=(value >> 16) & 0x000000ff;
    buf[3]=(value >> 24) & 0x000000ff;
    

    然后

    if (can1.write(CANMessage(1337, &buf, 8))) {
    ..........
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多