Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: FlexCAN with extended ID's

  1. #1
    Junior Member
    Join Date
    Dec 2016
    Posts
    18

    FlexCAN with extended ID's

    I am having trouble getting a T3.6 to read CAN frames when the CAN ID is in extended format. With standard format ID's I have no problem.

    Can someone tell me how to set up FlexCAN for extended ID's

  2. #2
    Junior Member
    Join Date
    Nov 2016
    Posts
    15
    Code:
    CAN_filter_t allPassFilter;
      allPassFilter.id = 0;
      allPassFilter.ext = 1;
      allPassFilter.rtr = 0;
    
      for (uint8_t filterNum = 4; filterNum < 16; filterNum++) {
        Can0.setFilter(allPassFilter, filterNum);
      }
    an example sketch:
    Code:
    #include <FlexCAN.h>
    
    
    /* BLINK */
    int blink_led_pin = 13;
    unsigned long blink_last_millis = 0;
    int blink_on_interval = 1000;
    int blink_off_interval = 1000;
    boolean blink_state = false;
    
    CAN_message_t inMsg;
    
    void setup() {
      Serial.begin(115200);
      Can0.begin();
      pinMode(blink_led_pin, OUTPUT);
    
      Serial.println("Start");
    
    
      CAN_filter_t allPassFilter;
      allPassFilter.id = 0;
      allPassFilter.ext = 1;
      allPassFilter.rtr = 0;
    
      for (uint8_t filterNum = 4; filterNum < 16; filterNum++) {
        Can0.setFilter(allPassFilter, filterNum);
      }
    
      
    }
    
    
    void loop() {
      blink_led();
      while (Can0.available()) {
        Can0.read(inMsg);
        print_can_message(inMsg);
      }
    
    }
    
    void blink_led() {
      if ((!blink_state && (millis() - blink_last_millis) > blink_off_interval)  || (blink_state && (millis() - blink_last_millis) > blink_on_interval)) {
        blink_last_millis = millis();
        blink_state = !blink_state;
        digitalWrite(blink_led_pin, blink_state);
      }
    }
    
    void print_can_message(CAN_message_t msg) {
    
      uint8_t nodeid = msg.id & 0x7F;
      uint8_t pdo = msg.id >> 7;
    
      Serial.print(millis());
      Serial.print(" msgid: ");
      Serial.print(msg.id);
      Serial.print(" node: ");
      Serial.print(nodeid);
      Serial.print(" function code: ");
      Serial.print(pdo);
      Serial.print(": ");
      for (int i = 0; i < 8; i++) {
        Serial.print(msg.buf[i]);
        Serial.print(" ");
      }
      Serial.println();
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •