• AVR Freaks

Hot!CAN Controller with Integrated Transceiver set with Arduino

Author
lorenrus
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2018/10/30 09:27:50
  • Location: 0
  • Status: offline
2020/05/24 06:46:21 (permalink)
0

CAN Controller with Integrated Transceiver set with Arduino

Hello everyone and good Sunday
I am checking the MCP25625 device with Arduino M0 Pro and in particular with its SPI.
I have to enable loopback mode by manipulating three bits of the CANCTRL register. to manipulate them you have to first send the statement, address, then a mask and then the data.
What happens to me is that as soon as I turn on I perform the reset and then I expect to see, in steps 44 and 45, the value of those registers to reset.
If I press for example 44 I actually have its reset value but if then I press immediately 45 I get a value that does not match the reset value; then if I reset again and I do the vice versa I see the correct reset value of point 45 but if then I push 44 I do not see it anymore.
And of course this does not allow me to understand whether I write or not and whether or not I write the bit for activating the loopback function correctly.
Someone can tell me where I'm wrong.
Thank you very much
/******* TEST CAN ***************/
 
  #define RESET_CAN_NEG 1
  #define CS_CAN_NEG 2
  #define CAN_INT_NEG 3
  #define FPGA_RX_CAN 8
  #define FPGA_TX_CAN 9

  #define CAN_STAND_BY A3


  SPISettings SPI_Settings(20000000, MSBFIRST, SPI_MODE0);

  unsigned int SPI_Received_Value = 0;

  unsigned int SPI_Received_Value_1 = 0;

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

  delay(2000);
 

  SPI.begin();
 
  pinMode(CS_CAN_NEG, OUTPUT);

  pinMode(RESET_CAN_NEG, OUTPUT);

  pinMode(CAN_STAND_BY, OUTPUT);

  digitalWrite(CS_CAN_NEG, HIGH);

  digitalWrite(RESET_CAN_NEG, HIGH);

  digitalWrite(CAN_STAND_BY, LOW);
}

void loop() {

  if(Serial.available()) {

    String Stringa = Serial.readString();

    int Dato = atoi(Stringa.c_str());
   
    delay(500);

/**** SPI ****/
   
    if(Dato == 42) {
       
        digitalWrite(CAN_STAND_BY, HIGH);

        Serial.println("Chip Reset");
       
        digitalWrite(RESET_CAN_NEG, LOW);

        delay(50);

        digitalWrite(RESET_CAN_NEG, HIGH);
       
        digitalWrite(CAN_STAND_BY, LOW);

        //digitalWrite(CS_CAN_NEG, LOW);

        //SPI.transfer(0b11000000);

        //digitalWrite(CS_CAN_NEG, HIGH);
    }

    if(Dato == 43) {
       
        Serial.println("Active LoopBack Mode");
       
        //digitalWrite(CAN_STAND_BY, HIGH);

        SPI.beginTransaction(SPI_Settings);

        digitalWrite(CS_CAN_NEG, LOW);
 
        SPI.transfer(0b00000101); // Istruction

        SPI.transfer(0xF); // Address

        SPI.transfer(0b11111111); // Mask

        SPI.transfer(0b01000000); // Data

        digitalWrite(CS_CAN_NEG, HIGH);

        SPI.endTransaction();

        //digitalWrite(CAN_STAND_BY, LOW);
       
        //SPI.end();
    }

    if(Dato == 44) {
       
        //SPI.begin();

        Serial.println("Read Register CAN Control Register");
       
        //digitalWrite(CAN_STAND_BY, HIGH);

        SPI.beginTransaction(SPI_Settings);
       
        digitalWrite(CS_CAN_NEG, LOW);


        SPI.transfer(0b00000011); // Istruction

        SPI.transfer(0xF); // Address

        SPI_Received_Value = SPI.transfer(0);


        digitalWrite(CS_CAN_NEG, HIGH);

        SPI.endTransaction();

        //digitalWrite(CAN_STAND_BY, LOW);

        Serial.println(SPI_Received_Value);



        //SPI.end();
    }

    if(Dato == 45) {
       
        //SPI.begin();

        Serial.println("Read Register CAN Status Register");
       
        //digitalWrite(CAN_STAND_BY, HIGH);

        SPI.beginTransaction(SPI_Settings);
       
        digitalWrite(CS_CAN_NEG, LOW);

        SPI.transfer(0b00000011); // Istruction

        SPI.transfer(0xE); // Address

        SPI_Received_Value_1 = SPI.transfer(0);

        digitalWrite(CS_CAN_NEG, HIGH);

        SPI.endTransaction();

        //digitalWrite(CAN_STAND_BY, LOW);

        Serial.println(SPI_Received_Value_1);

        //SPI.end();
    }
 }
}

#1

2 Replies Related Threads

    lorenrus
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2018/10/30 09:27:50
    • Location: 0
    • Status: offline
    Re: CAN Controller with Integrated Transceiver set with Arduino 2020/05/24 23:17:04 (permalink)
    1 (1)
    Hi
     
    Can you help me ? 
    #2
    lorenrus
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2018/10/30 09:27:50
    • Location: 0
    • Status: offline
    Re: CAN Controller with Integrated Transceiver set with Arduino 2020/06/29 00:14:54 (permalink)
    0
    I was able to program it with the SPI in Normal Mode. I wanted your advice.
    I have to send a CAN signal from card number one and send it to card number two where the MCP is present. Then I want this CAN signal to be sent back as it is to card number one, then a Loopback test.
    On the MCP I enter in pins 3 and 4, then to loop back I have to connect rxcan together with Rxd and Txcan cone txd ? Thank you
    #3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5