The datasheet for the E2 interface, (their protocol), has code for interfacing with the processor that must be modified to work with specific processors: http://www.epluse.com/uploads/tx_EplusE ... sch_33.pdf (see the section that says "// adapt this code for your target processor !!!")
I've attempted to do this using pinMode and digitalWrite in the .h file I'm trying to create but the compiler is throwing errors saying the Arduino functions (pinMode, etc) aren't declared. I'm not a C coder so I've probably screwed up the file structure somehow. I would really appreciate some help with this. I'll post the library on the Arduino site if I can get it working.
The problem functions are at the very end. See below:
Code: Select all
/***************************************************************************/
// definitions
#define DELAY_FAKTOR 10
#define ACK 1
#define NAK 0
int SDA = 4;
int SCL = 5;
/***************************************************************************/
// variables
unsigned char rh_low;
unsigned char rh_high;
unsigned char temp_low;
unsigned char temp_high;
unsigned char checksum_03;
unsigned int rh_ee03= 0;
unsigned int temp_ee03= 0;
float rh = 0;
float temperature = 0;
/***************************************************************************/
// functions
char check_ack(void);
void send_ack(void);
void send_nak(void);
void E2Bus_start(void);
void E2Bus_stop(void);
void E2Bus_send(unsigned char);
void set_SDA(void);
void clear_SDA(void);
int read_SDA(void);
void set_SCL(void);
void clear_SCL(void);
unsigned char E2Bus_read(void);
void e2Delay(unsigned int value);
/***************************************************************************/
float RH_read(void);
float Temp_read(void);
unsigned char EE03_status(void);
int sdaPin = 9;
int sclPin = 10;
int ledPin = 13;
float Temp_read(void)
{
E2Bus_start();
E2Bus_send(0xA1);
if (check_ack()==ACK)
{
temp_low = E2Bus_read();
send_ack();
checksum_03 = E2Bus_read();
send_nak();
// terminate communication
E2Bus_stop();
if (((0xA1 + temp_low) % 256) == checksum_03) // checksum OK?
{
E2Bus_start();
E2Bus_send(0xB1); // MW2-high request
check_ack();
temp_high = E2Bus_read();
send_ack(); // terminate communication
checksum_03 = E2Bus_read();
send_nak();
E2Bus_stop();
// default value (error code)
// MW2-low request
if (((0xB1 + temp_high) % 256) == checksum_03) // checksum OK?
{
temp_ee03=temp_low+256*temp_high; //yes->calculate temperature
temperature=((float)temp_ee03/100) - 273.15;
// overwrite default (error) value
}
}
E2Bus_stop();
}
return temperature;
}
float RH_read(void)
{
rh = -1;
E2Bus_start();
E2Bus_send(0x81); // MW1-low request
if (check_ack()==ACK)
{
rh_low = E2Bus_read();
send_ack();
checksum_03 = E2Bus_read();
send_nak(); // terminate communication
E2Bus_stop();
if (((0x81 + rh_low) % 256) == checksum_03) // checksum OK?
{
E2Bus_start();
E2Bus_send(0x91); // MW1-high request
check_ack();
rh_high = E2Bus_read();
send_ack();
checksum_03 = E2Bus_read();
send_nak(); // terminate communication
E2Bus_stop();
if (((0x91 + rh_high) % 256) == checksum_03) // checksum OK?
{
rh_ee03=rh_low+256*(unsigned int)rh_high; // yes-> calculate humidity value
rh=(float)rh_ee03/100; // overwrite default (error) value
}
}
E2Bus_stop();
}
return rh;
}
unsigned char EE03_status(void)
{
unsigned char stat_ee03;
E2Bus_start();
E2Bus_send(0x71); //main command for STATUS request
if (check_ack()==ACK)
{
stat_ee03 = E2Bus_read();
send_ack();
checksum_03 = E2Bus_read();
send_nak();
E2Bus_stop();
if (((stat_ee03 + 0x71) % 256) == checksum_03)
return stat_ee03;
}
return 0xFF;
}
void E2Bus_start(void)
// send Start condition to E2 Interface
{
set_SDA();
set_SCL();
e2Delay(30*DELAY_FAKTOR);
clear_SDA();
e2Delay(30*DELAY_FAKTOR);
}
/*-------------------------------------------------------------------------*/
void E2Bus_stop(void)
// send Stop condition to E2 Interface
{
clear_SCL();
e2Delay(20*DELAY_FAKTOR);
clear_SDA();
e2Delay(20*DELAY_FAKTOR);
set_SCL();
e2Delay(20*DELAY_FAKTOR);
set_SDA();
e2Delay(20*DELAY_FAKTOR);
}
/*-------------------------------------------------------------------------*/
void E2Bus_send(unsigned char value)
{
unsigned char i;
unsigned char maske = 0x80;
for (i=8;i>0;i--)
{
clear_SCL();
e2Delay(10*DELAY_FAKTOR);
if ((value & maske) != 0)
{
set_SDA();
}
else
{
clear_SDA();
}
e2Delay(20*DELAY_FAKTOR);
set_SCL();
maske >>= 1;
e2Delay(30*DELAY_FAKTOR);
clear_SCL();
}
set_SDA();
}
/*-------------------------------------------------------------------------*/
unsigned char E2Bus_read(void)
{
unsigned char data_in = 0x00;
unsigned char maske = 0x80;
for (maske=0x80;maske>0;maske >>=1)
{
clear_SCL();
e2Delay(30*DELAY_FAKTOR);
set_SCL();
e2Delay(15*DELAY_FAKTOR);
if (read_SDA())
{
data_in |= maske;
}
e2Delay(15*DELAY_FAKTOR);
clear_SCL();
}
return data_in;
}
/*-------------------------------------------------------------------------*/
char check_ack(void)// check for acknowledge
{
int input;
e2Delay(30*DELAY_FAKTOR);
set_SCL();
e2Delay(15*DELAY_FAKTOR);
input = read_SDA();
e2Delay(15*DELAY_FAKTOR);
if(input == 1)
return NAK;
else
return ACK;
}
/*-------------------------------------------------------------------------*/
void send_ack(void)
// send acknowledge
{
clear_SCL();
e2Delay(15*DELAY_FAKTOR);
clear_SDA();
e2Delay(15*DELAY_FAKTOR);
set_SCL();
e2Delay(30*DELAY_FAKTOR);
clear_SCL();
set_SDA();
}
/*-------------------------------------------------------------------------*/
void send_nak(void)
// send NOT-acknowledge
{
clear_SCL();
e2Delay(15*DELAY_FAKTOR);
set_SDA();
e2Delay(15*DELAY_FAKTOR);
set_SCL();
e2Delay(30*DELAY_FAKTOR);
clear_SCL();
set_SDA();
}
/*-------------------------------------------------------------------------*/
void e2Delay(unsigned int value)
// delay- routine
{
while (--value != 0);
}
/*-------------------------------------------------------------------------*/
// adapt this code for your target processor !!!
void set_SDA(void) // set port-pin (SDA)
{
pinMode(sdaPin,OUTPUT);
digitalWrite(sdaPin,HIGH);
// SDA = 1;
}
void clear_SDA(void) // clear port-pin (SDA)
{
pinMode(sdaPin,OUTPUT);
digitalWrite(sdaPin,LOW);
// SDA = 0;
}
int read_SDA(void) // read SDA-pin status
{
pinMode(sdaPin,INPUT);
return digitalRead(sdaPin);
}
void set_SCL(void) // set port-pin (SCL)
{
pinMode(sclPin,OUTPUT);
digitalWrite(sclPin,HIGH);
// SCL = 1;
}
void clear_SCL(void) // clear port-pin (SCL)
{
pinMode(sclPin,OUTPUT);
digitalWrite(sclPin,LOW);
// SCL = 0;
}