Skip to main content
added 795 characters in body
Source Link

EDIT: @Juraj fixed the Issue, changes at the end of the post

/*
Library for DS28EC20, built on top of OneWire.h lib
*/


#ifndef DS28EC20_h
#define DS28EC20_h


#include "Arduino.h"
#include "OneWire.h"
#include "DS28EC20.h"

#define WRITE_SCRATCHPAD 0xF                                              // commands für DS28EC20
#define READ_SCRATCHPAD 0xAA
#define COPY_SCRATCHPAD 0x55
#define READ_MEMORY 0xF0
#define EXTENDED_READ_MEMORY 0xA5

#define READ_ROM 0x33
#define MATCH_ROM 0x55
#define SEARCH_ROM 0xF0
#define SKIP_ROM 0xCC
#define RESUME 0xA5



class DS28EC20
{
    public:
     DS28EC20(uint8_t); // Constructor, add DS28EC20 to "pin"
     bool write_scratchpad(uint8_t *data, uint8_t leng, uint_fast16_t adress);
     bool write_memory(uint16_t adress);
     void read_memory(uint16_t adress,uint8_t *data, uint8_t leng);
    
     private:    
                                                        

};

Fix:

In main.cpp call Object with corresponding pin:

DS28EC20 One(31);

In DS28EC20.cpp add

OneWire Wire;

after includes, change the constructor aswell:

DS28EC20::DS28EC20(uint8_t pin)
{
  Wire.begin(pin);  
}

In DS28EC20.h you need to change the class to:

class DS28EC20
{
    public:
     
     DS28EC20(uint8_t pin); // Constructor, add DS28EC20 to "pin"
     bool write_scratchpad(uint8_t *data, uint8_t leng, uint_fast16_t adress);
     bool write_memory(uint16_t adress);
     void read_memory(uint16_t adress,uint8_t *data, uint8_t leng);
    
     private:    
     OneWire Wire;                                                   

};
/*
Library for DS28EC20, built on top of OneWire.h lib
*/


#ifndef DS28EC20_h
#define DS28EC20_h


#include "Arduino.h"
#include "OneWire.h"
#include "DS28EC20.h"

#define WRITE_SCRATCHPAD 0xF                                              // commands für DS28EC20
#define READ_SCRATCHPAD 0xAA
#define COPY_SCRATCHPAD 0x55
#define READ_MEMORY 0xF0
#define EXTENDED_READ_MEMORY 0xA5

#define READ_ROM 0x33
#define MATCH_ROM 0x55
#define SEARCH_ROM 0xF0
#define SKIP_ROM 0xCC
#define RESUME 0xA5



class DS28EC20
{
    public:
     DS28EC20(uint8_t); // Constructor, add DS28EC20 to "pin"
     bool write_scratchpad(uint8_t *data, uint8_t leng, uint_fast16_t adress);
     bool write_memory(uint16_t adress);
     void read_memory(uint16_t adress,uint8_t *data, uint8_t leng);
    
     private:    
                                                        

};

EDIT: @Juraj fixed the Issue, changes at the end of the post

/*
Library for DS28EC20, built on top of OneWire.h lib
*/


#ifndef DS28EC20_h
#define DS28EC20_h


#include "Arduino.h"
#include "OneWire.h"
#include "DS28EC20.h"

#define WRITE_SCRATCHPAD 0xF                                              // commands für DS28EC20
#define READ_SCRATCHPAD 0xAA
#define COPY_SCRATCHPAD 0x55
#define READ_MEMORY 0xF0
#define EXTENDED_READ_MEMORY 0xA5

#define READ_ROM 0x33
#define MATCH_ROM 0x55
#define SEARCH_ROM 0xF0
#define SKIP_ROM 0xCC
#define RESUME 0xA5



class DS28EC20
{
    public:
     DS28EC20(uint8_t); // Constructor, add DS28EC20 to "pin"
     bool write_scratchpad(uint8_t *data, uint8_t leng, uint_fast16_t adress);
     bool write_memory(uint16_t adress);
     void read_memory(uint16_t adress,uint8_t *data, uint8_t leng);
    
     private:    
                                                        

};

Fix:

In main.cpp call Object with corresponding pin:

DS28EC20 One(31);

In DS28EC20.cpp add

OneWire Wire;

after includes, change the constructor aswell:

DS28EC20::DS28EC20(uint8_t pin)
{
  Wire.begin(pin);  
}

In DS28EC20.h you need to change the class to:

class DS28EC20
{
    public:
     
     DS28EC20(uint8_t pin); // Constructor, add DS28EC20 to "pin"
     bool write_scratchpad(uint8_t *data, uint8_t leng, uint_fast16_t adress);
     bool write_memory(uint16_t adress);
     void read_memory(uint16_t adress,uint8_t *data, uint8_t leng);
    
     private:    
     OneWire Wire;                                                   

};
Source Link

Initalizing library object in own library

I am developing an application to program OneWire ID Chips. I am using the OneWire library (https://github.com/PaulStoffregen/OneWire) and built my own Library for the DS28EC20 Chip on top of it.

It is working but it's not very elegant. I have issues initalizing the OneWire object in my own library. At the moment it is only working with a fixed pin declared in my own library.

I want to call a function in my code which initalizes the OneWire library aswell as my own library for the DS28EC20.

I am not using the Arduino IDE, I am using Platform.io for VSCode because it's more powerful.

It would be awesome if someone could help me, because I need to use multiple ID Chips in my application.

Because of unkown reasons I need to pass a variable to the constructor of the DS28EC20. Calling the OneWire library in there does not work...

I excluded the other functions in the DS28EC20.cpp file, I think they are not necessary rn

Thank you very much in advance!

  • pitchbent

main.cpp:

#include <Arduino.h>
#include <OneWire.h>
#include <DS28EC20.h>

uint8_t scratchpad[32];                                                   // Array for scratchpad
uint16_t location;                                                        //Memory location to store scratchpad in
//uint8_t memory[32];                                                       
bool status;                                                              // Status of the crc check


DS28EC20 One(5); 

void setup() {

  
  pinMode(13,OUTPUT);
  Serial.begin(115200);                                                   //begin serial connection 0
  location = 0x460;                                                       //Memory location
  
  for (uint8_t i = 0; i < 32; i++)                                        //filling array for scratchpad
  {
    scratchpad[i]=0x22;
  }
  Serial.println(scratchpad[2]);

  status = One.write_scratchpad(scratchpad,32,location);                  //write scratchpad
  Serial.println("Status:");                                              //print status
  Serial.println(status);

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

void loop() {



  // put your main code here, to run repeatedly:
}

DS28EC20.cpp:

#include "Arduino.h"
#include "OneWire.h"
#include "DS28EC20.h"


OneWire Wire(31);

DS28EC20::DS28EC20(uint8_t)
{
    
}
bool DS28EC20::write_scratchpad(uint8_t *data, uint8_t leng, uint_fast16_t adress)
{
  Serial.println("123");
  uint8_t TA1;                                                            // Var for adress Bytes
  uint8_t TA2;
  uint8_t crc_rec[6];                                                     // Array for recieved CRC Bytes
  uint8_t crc_array[leng+3];                                              // Array for calculation of CRC value

  TA1 = (adress & 0xFF);                                                  // calculation of TA1 & TA2
  TA2 = ((adress >> 8) & 0xFF);
 crc_array[0]=WRITE_SCRATCHPAD;                                            // The CRC sum is computed out of the command aswell as the adress byteys 
  crc_array[1]=TA1;                                                         // 
  crc_array[2]=TA2;   

  for (int i = 0; i < leng; i++)                                            // write data into crc array
  {
    crc_array[i+3]=data[i];
  }
 Wire.reset();                                                             // Reset
  Wire.write(SKIP_ROM);                                                     // Skip ROM
  Wire.write(WRITE_SCRATCHPAD);                                             // write Scratchpad

  Wire.write(TA1);                                                          // send adress bytes
  Wire.write(TA2);

  for(int i=0;i<leng; i++)                                                  // write data
  {
    Wire.write(data[i]);
  }

  Wire.read_bytes(crc_rec,6);                                               // safe CRC values sent from chip

  delay(10);
 Wire.reset();                                                             // Reset
if(Wire.check_crc16(crc_array,leng+3,&crc_rec[0]))                        // CRC Check
  {
    return true;
  }
  else
  {
    return false;
  }
  
}

DS28EC20.h

/*
Library for DS28EC20, built on top of OneWire.h lib
*/


#ifndef DS28EC20_h
#define DS28EC20_h


#include "Arduino.h"
#include "OneWire.h"
#include "DS28EC20.h"

#define WRITE_SCRATCHPAD 0xF                                              // commands für DS28EC20
#define READ_SCRATCHPAD 0xAA
#define COPY_SCRATCHPAD 0x55
#define READ_MEMORY 0xF0
#define EXTENDED_READ_MEMORY 0xA5

#define READ_ROM 0x33
#define MATCH_ROM 0x55
#define SEARCH_ROM 0xF0
#define SKIP_ROM 0xCC
#define RESUME 0xA5



class DS28EC20
{
    public:
     DS28EC20(uint8_t); // Constructor, add DS28EC20 to "pin"
     bool write_scratchpad(uint8_t *data, uint8_t leng, uint_fast16_t adress);
     bool write_memory(uint16_t adress);
     void read_memory(uint16_t adress,uint8_t *data, uint8_t leng);
    
     private:    
                                                        

};