CAN bus communication send-receive

Hello all,
I am trying to communicate MCP2515 with Kvaser light V2 CAN bus analyzer. I have used MCP2515 shield 1.5 on arduino uno. I have gone through sample codes for can send as well as receive. I want send as well as receive so I have merged both programs.
Sending is working well but there is problem in receiving data.
I have attached my code so please find the attachment and help me.

#include <Wire.h>
#include <CAN.h>
#include <Adafruit_ADS1015.h>

 Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
//Adafruit_ADS1015 ads;     /* Use this for the 12-bit version */
int timer1_counter;
uint16_t adc0;
 uint8_t adch,adcl;
 #define ledPin 5
void setup(void) 
{
  Serial.begin(9600);
  while (!Serial);
  pinMode(ledPin, OUTPUT);
  // initialize timer1 
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;

  // Set timer1_counter to the correct value for our interrupt interval
  timer1_counter = 64930;   // preload timer 65536-16MHz/256/100Hz   10ms 64911
  //timer1_counter = 64911;   // preload timer 65536-16MHz/256/100Hz
  //timer1_counter = 64286;   // preload timer 65536-16MHz/256/50Hz
  //timer1_counter = 34286;   // preload timer 65536-16MHz/256/2Hz
 // timer1_counter = 62411;   // preload timer 65536-16MHz/256/20Hz   100msec
  
  TCNT1 = timer1_counter;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler 
  //TCCR1B |= (1 << CS11) | (1 << CS10);    // 64 prescaler 
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();             // enable all interrupts

  Serial.println("CAN Sender");
  Serial.println("Hello!");
  
  Serial.println("Getting single-ended readings from AIN0..3");
  Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");

  ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  
  
  ads.begin();
  // start the CAN bus at 500 kbps
  if (!CAN.begin(500E3)) {
    Serial.println("Starting CAN failed!");
    while (1);
  }
   
}

ISR(TIMER1_OVF_vect)        // interrupt service routine 
{
  TCNT1 = timer1_counter;   // preload timer
 digitalWrite(ledPin, digitalRead(ledPin) ^ 1);
   
 // send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
  //Serial.print("Sending extended packet ... ");
  CAN.beginExtendedPacket(0x0C00DAEF); //(PGN-0 Priority-3 src Addr- EF Dest Addr-DA)
  CAN.write(0xFF);
  CAN.write(adch);
  CAN.write(adcl);
  CAN.write(0xFF);
  CAN.write(0xFF);
  CAN.write(0xFF);
  CAN.write(0xFF);
  CAN.write(0xFF);
  CAN.endPacket();
  Serial.println(" ");
}

void loop(void) 
{
  adc0 = ads.readADC_SingleEnded(0);// 10v = 64255 = 8031.875 max value speed. adjust 10v = 32127 counts.
  adc0 = adc0 * 2;
  adch = adc0 >> 8;
  adcl = adc0 & 0x00FF;
  Serial.print("AIN0: "); Serial.println(adc0);
  Serial.println(adch);
  Serial.println(adcl);

  // try to parse packet
  int packetSize = CAN.parsePacket();

  if (packetSize) {
    // received a packet
    Serial.print("Received ");

    if (CAN.packetExtended()) {
      Serial.print("extended ");
    }

    if (CAN.packetRtr()) {
      // Remote transmission request, packet contains no data
      Serial.print("RTR ");
    }

    Serial.print("packet with id 0x");
    Serial.print(CAN.packetId(), HEX);

    if (CAN.packetRtr()) {
      Serial.print(" and requested length ");
      Serial.println(CAN.packetDlc());
    } else {
      Serial.print(" and length ");
      Serial.println(packetSize);

      // only print packet data for non-RTR packets
      while (CAN.available()) {
        Serial.print((char)CAN.read());
      }
      Serial.println();
    }

    Serial.println();
  }
  
}

Which Arduino CAN Library you are using ?? It's better if you could attach sending and receving codes seperately, it will be easy to help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.