Hi
I have trouble to convert the C++ driver for a Digital Tsic Temperature Sensor to Arduino.
I've worked a lot and it's almost done but one error with the Constructor.
error:
In function 'void setup()':
error: expected unqualified-id before '.' token In function 'void loop()':
I used the instrucktion on: http://www.arduino.cc/en/Hacking/LibraryTutorial
But I'm not sure.... I've play around but couldn't get it to work (see out-Commented code).
I think I need a Constructor.
-> in Sketch,
-> tsic.h,
-> tsic.cpp
how can I do that?????
Sketch:
#include <tsic.h>
//tsic tsic1(12, 13); //firstPin = VCC, secondPin = Signal
void setup(){
//TSIC_INIT();
tsic.TSIC_INIT(); // set data direction of IO-Pins
}
void loop(){
returnvalue = getTSicTemp(&temperatur); // pull the TSIC-Sensor
// conversion equation from TSic's data sheet
Temp_celsius = ((float)temperatur / 2047 * 200) - 50;
delay(1000);
}
tsic.h
#ifndef tsic_H
#define tsic_H
#include <inttypes.h>
#include "WProgram.h"
class tsic {
public:
//tsic(int Pin_VCC, int Pin_Sig);
void TSIC_INIT();
uint8_t getTSicTemp (uint16_t *temp_value16);
private:
};
#endif
tsic.cpp
#include "tsic.h"
extern "C" {
#include <stdio.h> //not needed yet
#include <string.h> //needed for strlen()
#include <inttypes.h>
#include "WConstants.h" //all things wiring / arduino
#include "WProgram.h"
}
//defines:
#define TSCI_POWER_PIN 12 // Where TSIC-Sensors VCC is connected
#define TSIC_SIGNAL_PIN 13 // Where TSIC-Sensors "Signal"-Pin is connected
#define TSIC_ON() digitalWrite(TSCI_POWER_PIN, HIGH) // Power up the TSIC-Sensor
#define TSIC_OFF() digitalWrite(TSCI_POWER_PIN, LOW) // Power down the TSIC-Sensor
#define TSIC_SIGNAL_HIGH digitalRead(TSIC_SIGNAL_PIN) //if TSIC_SIGNAL_PIN is high (=1)
#define TSIC_SIGNAL_LOW !digitalRead(TSIC_SIGNAL_PIN) //if TSIC_SIGNAL_PIN is low (=0)
//Variablen:
uint8_t returnvalue; // return value of getTSicTemp(*temp);
uint16_t temperatur; // 11-bit temperature value
uint8_t Temp_celsius; // converted temperature in °C
//Konstrucktor
/*tsic::tsic(int Pin_VCC, int Pin_Sig)
{
#define TSCI_POWER_PIN Pin_VCC // Where TSIC-Sensors VCC is connected
#define TSIC_SIGNAL_PIN Pin_Sig // Where TSIC-Sensors "Signal"-Pin is connected
}*/
void tsic::TSIC_INIT() {
pinMode(TSCI_POWER_PIN, OUTPUT); // sets the digital pin as output
pinMode(TSIC_SIGNAL_PIN, INPUT); // sets the digital pin as input
}
uint8_t tsic::getTSicTemp (uint16_t *temp_value16) {
uint16_t temp_value1 = 0;
uint16_t temp_value2 = 0;
uint8_t i;
uint16_t Temperature;
uint8_t parity;
TSIC_ON();
delayMicroseconds(60); // wait for stabilization
delayMicroseconds(60);
while (TSIC_SIGNAL_HIGH); // wait until start bit starts
// wait, TStrobe
while (TSIC_SIGNAL_LOW);
// first data byte
// read 8 data bits and 1 parity bit
for (i = 0; i < 9; i++) {
while (TSIC_SIGNAL_HIGH); // wait for falling edge
delayMicroseconds(60);
if (TSIC_SIGNAL_HIGH)
temp_value1 |= 1 << (8-i); // get the bit
else
while (TSIC_SIGNAL_LOW); // wait until line comes high again
}
// second byte
while (TSIC_SIGNAL_HIGH);
// wait, TStrobe
while (TSIC_SIGNAL_LOW);
// read 8 data bits and 1 parity bit
for (i = 0; i < 9; i++) {
while (TSIC_SIGNAL_HIGH); // wait for falling edge
delayMicroseconds(60);
if (TSIC_SIGNAL_HIGH)
temp_value2 |= 1 << (8-i); // get the bit
else
while (TSIC_SIGNAL_LOW); // wait until line comes high again
}
TSIC_OFF(); // switch TSic off
// check parity for byte 1
parity = 0;
for (i = 0; i < 9; i++)
if (temp_value1 & (1 << i))
parity++;
if (parity % 2)
return 0;
// check parity for byte 2
parity = 0;
for (i = 0; i < 9; i++)
if (temp_value2 & (1 << i))
parity++;
if (parity % 2)
return 0;
temp_value1 >>= 1; // delete parity bit
temp_value2 >>= 1; // delete parity bit
Temperature = (temp_value1 << 8) | temp_value2;
*temp_value16 = Temperature;
return 1; // parity is OK
}
the original whole C++ file:
/******************************************************************************
*
* AVR Code example to read a TSIC-206 / TSIC-306 digital temperatur probe
*
* Based on C++ Samplecode in
* "Tech Notes - ZACwireTM Digital Output, Rev. 2.3, October 17, 2006"
*
* Tested with ATMega8, avr-gcc (GCC) 4.3.0 and a TSIC-206
*
* see also discussion at http://www.mikrocontroller.net/topic/82087
*
******************************************************************************/
#define F_CPU 8000000UL // Clock of target system
#include <avr/io.h>
#include <util/delay.h>
/******************************************************************************
* Hardware Connection of the TSIC-Sensor
******************************************************************************/
#define TSIC_PORT PORTD // Port to use
#define TSIC_PIN PIND
#define TSIC_PORT_DDR DDRD
#define TSCI_POWER_PIN PD6 // Where TSIC-Sensors VCC is connected
#define TSIC_SIGNAL_PIN PD7 // Where TSIC-Sensors "Signal"-Pin is con.
/******************************************************************************
* FUNCTION MACROS
******************************************************************************/
// Define TSCI_POWER_PIN as output, TSIC_SIGNAL_PIN as input
#define TSIC_INIT() { TSIC_PORT_DDR |= (1<<TSCI_POWER_PIN); \
TSIC_PORT_DDR &= ~(1<<TSIC_SIGNAL_PIN); }
// Power up the TSIC-Sensor
#define TSIC_ON() TSIC_PORT |= (1<<TSCI_POWER_PIN)
// Power down the TSIC-Sensor
#define TSIC_OFF() TSIC_PORT &= ~(1<<TSCI_POWER_PIN)
//#define TSIC_SIGNAL (TSIC_PORT & (1<<TSIC_SIGNAL_PIN))
// Low/High Signal of the TSIC-Sensor, e.g. "if(TSIC_SIGNAL_HIGH)..."
#define TSIC_SIGNAL_HIGH TSIC_PIN & ( 1 << TSIC_SIGNAL_PIN )
#define TSIC_SIGNAL_LOW !( TSIC_PIN & ( 1 << TSIC_SIGNAL_PIN ))
/******************************************************************************
* Function : getTSicTemp(*temp_value16);
* Description : reads from the TSic its output value
* Parameters : pointer for return value
* Returns : 1: reading sucessfull, 0: parity error
******************************************************************************/
uint8_t getTSicTemp (uint16_t *temp_value16) {
uint16_t temp_value1 = 0;
uint16_t temp_value2 = 0;
uint8_t i;
uint16_t Temperature;
uint8_t parity;
TSIC_ON();
_delay_us(60); // wait for stabilization
_delay_us(60);
while (TSIC_SIGNAL_HIGH); // wait until start bit starts
// wait, TStrobe
while (TSIC_SIGNAL_LOW);
// first data byte
// read 8 data bits and 1 parity bit
for (i = 0; i < 9; i++) {
while (TSIC_SIGNAL_HIGH); // wait for falling edge
_delay_us(60);
if (TSIC_SIGNAL_HIGH)
temp_value1 |= 1 << (8-i); // get the bit
else
while (TSIC_SIGNAL_LOW); // wait until line comes high again
}
// second byte
while (TSIC_SIGNAL_HIGH);
// wait, TStrobe
while (TSIC_SIGNAL_LOW);
// read 8 data bits and 1 parity bit
for (i = 0; i < 9; i++) {
while (TSIC_SIGNAL_HIGH); // wait for falling edge
_delay_us(60);
if (TSIC_SIGNAL_HIGH)
temp_value2 |= 1 << (8-i); // get the bit
else
while (TSIC_SIGNAL_LOW); // wait until line comes high again
}
TSIC_OFF(); // switch TSic off
// check parity for byte 1
parity = 0;
for (i = 0; i < 9; i++)
if (temp_value1 & (1 << i))
parity++;
if (parity % 2)
return 0;
// check parity for byte 2
parity = 0;
for (i = 0; i < 9; i++)
if (temp_value2 & (1 << i))
parity++;
if (parity % 2)
return 0;
temp_value1 >>= 1; // delete parity bit
temp_value2 >>= 1; // delete parity bit
Temperature = (temp_value1 << 8) | temp_value2;
*temp_value16 = Temperature;
return 1; // parity is OK
}
/******************************************************************************
* Test application
******************************************************************************/
int main (void) {
uint16_t temperatur; // 11-bit temperature value
uint8_t returnvalue; // return value of getTSicTemp(*temp);
uint8_t Temp_celsius; // converted temperature in °C
TSIC_INIT(); // set data direction of IO-Pins
while(1){
returnvalue = getTSicTemp(&temperatur); // pull the TSIC-Sensor
// conversion equation from TSic's data sheet
Temp_celsius = ((float)temperatur / 2047 * 200) - 50;
}
}
thanks for Comments!!
The Post was totally edited, so the next 3 Answers don't don't fit anymore