Show Posts
Pages: 1 ... 14 15 [16] 17 18 19
226  Forum 2005-2010 (read only) / Interfacing / Re: Humidity and pressure over SPI on: June 08, 2009, 08:44:29 am
Code:
//for pressure

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait for the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}


char read_register(char register_name)
{
    char in_byte;
    register_name <<= 2;
    register_name &= B11111100; //Read command
  
    digitalWrite(SLAVESELECT,LOW); //Select SPI Device
    spi_transfer(register_name); //Write byte to device
    in_byte = spi_transfer(0x00); //Send nothing, but we should get back the register value
    digitalWrite(SLAVESELECT,HIGH);
    delay(10);
    return(in_byte);
  
}

float read_register16(char register_name)
{
    byte in_byte1;
    byte in_byte2;
    float in_word;
    
    register_name <<= 2;
    register_name &= B11111100; //Read command

    digitalWrite(SLAVESELECT,LOW); //Select SPI Device
    spi_transfer(register_name); //Write byte to device
    in_byte1 = spi_transfer(0x00);    
    in_byte2 = spi_transfer(0x00);
    digitalWrite(SLAVESELECT,HIGH);
    in_word = UBLB(in_byte1,in_byte2);
    return(in_word);
}

void write_register(char register_name, char register_value)
{
    register_name <<= 2;
    register_name |= B00000010; //Write command

    digitalWrite(SLAVESELECT,LOW); //Select SPI device
    spi_transfer(register_name); //Send register location
    spi_transfer(register_value); //Send value to record into register
    digitalWrite(SLAVESELECT,HIGH);
}

227  Forum 2005-2010 (read only) / Interfacing / Humidity and pressure over SPI on: June 08, 2009, 08:44:16 am
I tried to put the code from humidity and from the pressure Sketch together. They are connected both over SPI.
At the moment the Sketch doesn't work. I think I have to set one as Slave but I don't know how.

Could you pls help me? Thx!

Code:
//humidity
#include <avr/io.h>

#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e        //000 1111 0

// pins dat - PB4, clk - PB5
#define SETSCK1 PORTB|=(1<<PB5)
#define SETSCK0 PORTB&=~(1<<PB5)
#define SCKOUTP DDRB|=(1<<DDB5)

#define SETDAT1 PORTB|=(1<<PB4)
#define SETDAT0 PORTB&=~(1<<PB4)
#define GETDATA (PINB&(1<<PINB4))

#define DMODEIN DDRB&=~(1<<DDB4)
#define PULLUP1 PORTB|=(1<<PINB4)
#define DMODEOU DDRB|=(1<<DDB4)

#define S_PULSLONG delayMicroseconds(3)
#define S_PULSSHORT delayMicroseconds(1)


char s_measure(unsigned int *p_value, unsigned char mode);
void calc_sth11(float *p_humidity ,float *p_temperature);
float calc_dewpoint(float h,float t);
void serialPrintFloat( float f);




//PRESSURE

#define SLAVESELECT 10
#define SPICLOCK 13
#define DATAOUT 11      //MOSI
#define DATAIN 12       //MISO

#define UBLB(a,b)  ( ( (a) << 8) | (b) )
#define UBLB19(a,b) ( ( (a) << 16 ) | (b) )


#define REVID 0x00      //ASIC Revision Number
#define OPSTATUS 0x04   //Operation Status
#define STATUS 0x07     //ASIC Status
#define START 0x0A      //Constant Readings
#define PRESSURE 0x1F   //Pressure 3 MSB
#define PRESSURE_LSB 0x20 //Pressure 16 LSB
#define TEMP 0x21       //16 bit temp

char rev_in_byte;          
int temp_in;
unsigned long pressure_lsb;
unsigned long pressure_msb;
unsigned long temp_pressure;
unsigned long pressure;








void setup(){
  //pressure  
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device  

  SPCR = B01010011;
  clr=SPSR;
  clr=SPDR;
  delay(10);
  Serial.begin(9600);
  delay(500);
  write_register(0x03,0x09);


  //Humidity
  Serial.begin(19200);
}

void loop()
{
  pressure_calculate();
  delay(5000);
  humidity_calculate();
  delay(5000);
}



void pressure_calculate(){



  rev_in_byte = read_register(REVID);

  pressure_msb = read_register(PRESSURE);
  pressure_msb &= B00000111;
  pressure_lsb = read_register16(PRESSURE_LSB);
  pressure_lsb &= 0x0000FFFF;


  pressure = UBLB19(pressure_msb, pressure_lsb);
  pressure /= 4;

  Serial.print("PRESSURE ");
  Serial.println(pressure, DEC);

}



void humidity_calculate(){
  byte error;
  byte rh;
  unsigned int humival_raw,tempval_raw;
  float humival_f, tempval_f, dew_point_f;
  char str[15];

  error=s_measure( &tempval_raw,0); //measure temperature

  if (error==0){
    error=s_measure( &humival_raw,1); //measure humidity
  }

  if(error!=0){ //in case of an error
    if (error==1){
      Serial.println("SHT1x sensor communication error");
    }
    if (error==3){
      Serial.println("SHT1x sensor timeout error");
    }

    delay(5000);
    return;
  }

  humival_f = (float) humival_raw;
  tempval_f = (float) tempval_raw;

  calc_sth11(&humival_f, &tempval_f);
  dew_point_f=calc_dewpoint(humival_f,tempval_f); //calculate dew point

  Serial.print("Temperature = ");
  serialPrintFloat(tempval_f);
  Serial.print("C");        
  Serial.print(" Rel Humidity = ");
  serialPrintFloat(humival_f);
  Serial.print("%");
  Serial.print(" Dew point = ");
  serialPrintFloat(dew_point_f);
  Serial.println("C");
}


//for humidity

// writes a byte on the Sensibus and checks the acknowledge
char s_write_byte(unsigned char value)
{
  unsigned char i=0x80;
  unsigned char error=0;
  DMODEOU;
  while(i){ //shift bit for masking
    if (i & value) {
      SETDAT1; //masking value with i , write to SENSI-BUS
    }
    else{
      SETDAT0;
    }
    SETSCK1; //clk for SENSI-BUS
    S_PULSLONG;
    SETSCK0;
    S_PULSSHORT;
    i=(i>>1);
  }
  DMODEIN; //release DATA-line
  PULLUP1;
  SETSCK1; //clk #9 for ack
  S_PULSLONG;
  if (GETDATA){ //check ack (DATA will be pulled down by SHT11)
    error=1;
  }
  S_PULSSHORT;
  SETSCK0;
  return(error); //error=1 in case of no acknowledge
}

// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
unsigned char s_read_byte(unsigned char ack)
{
  unsigned char i=0x80;
  unsigned char val=0;
  DMODEIN; //release DATA-line
  PULLUP1;
  while(i){ //shift bit for masking
    SETSCK1; //clk for SENSI-BUS
    S_PULSSHORT;
    if (GETDATA){
      val=(val | i); //read bit
    }
    SETSCK0;
    S_PULSSHORT;
    i=(i>>1);
  }
  DMODEOU;
  if (ack){
    //in case of "ack==1" pull down DATA-Line
    SETDAT0;
  }
  else{
    SETDAT1;
  }
  SETSCK1; //clk #9 for ack
  S_PULSLONG;
  SETSCK0;
  S_PULSSHORT;
  DMODEIN; //release DATA-line
  PULLUP1;
  return (val);
}

// generates a sensirion specific transmission start
// This is the point where sensirion is not I2C standard conform and the
// main reason why the AVR TWI hardware support can not be used.
//       _____         ________
// DATA:      |_______|
//           ___     ___
// SCK : ___|   |___|   |______
void s_transstart(void)
{
  //Initial state
  SCKOUTP;
  SETSCK0;
  DMODEOU;
  SETDAT1;
  //
  S_PULSSHORT;
  SETSCK1;
  S_PULSSHORT;
  SETDAT0;
  S_PULSSHORT;
  SETSCK0;
  S_PULSLONG;
  SETSCK1;
  S_PULSSHORT;
  SETDAT1;
  S_PULSSHORT;
  SETSCK0;
  S_PULSSHORT;
  //
  DMODEIN; //release DATA-line
  PULLUP1;
}

// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//      _____________________________________________________         ________
// DATA:                                                     |_______|
//          _    _    _    _    _    _    _    _    _        ___    ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|  |___|   |______
void s_connectionreset(void)
{
  unsigned char i;
  //Initial state
  SCKOUTP;
  SETSCK0;
  DMODEOU;
  SETDAT1;
  for(i=0;i<9;i++){ //9 SCK cycles
    SETSCK1;
    S_PULSLONG;
    SETSCK0;
    S_PULSLONG;
  }
  s_transstart(); //transmission start
}

// resets the sensor by a softreset
char s_softreset(void)
{
  s_connectionreset(); //reset communication
  //send RESET-command to sensor:
  return (s_write_byte(RESET)); //return=1 in case of no response form the sensor
}


// makes a measurement (humidity/temperature) with checksum
// p_value returns 2 bytes
// mode: 1=humidity  0=temperature
// return value: 1=write error, 2=crc error, 3=timeout
//
char s_measure(unsigned int *p_value, unsigned char mode)
{
  unsigned error;
  unsigned char i=0;
  s_transstart(); //transmission start
  *p_value=0;
  if(mode){
    mode=MEASURE_HUMI;
  }
  else{
    mode=MEASURE_TEMP;
  }
  if(s_write_byte(mode)){
    return(1);
  }
  // normal delays: temp i=70, humi i=20
  while(i<240){
    delay(3);
    if (GETDATA==0){
      i=0;
      break;
    }
    i++;
  }
  if(i){
    // or timeout
    return(3);
  }
  i=s_read_byte(1); //read the first byte (MSB)
  *p_value=(i<<8)|s_read_byte(0); //read the second byte (LSB) and end with no-ack
  return(0);
}

//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [°C] and humidity [%RH]
// input :  humi [Ticks] (12 bit)
//          temp [Ticks] (14 bit)
// output:  humi [%RH]
//          temp [°C]
{
  const float C1=-4.0;              // for 12 Bit
  const float C2=+0.0405;           // for 12 Bit
  const float C3=-0.0000028;        // for 12 Bit
  const float T1=+0.01;             // for 14 Bit @ 5V
  const float T2=+0.00008;           // for 14 Bit @ 5V      

  float rh=*p_humidity;             // rh:      Humidity [Ticks] 12 Bit
  float t=*p_temperature;           // t:       Temperature [Ticks] 14 Bit
  float rh_lin;                     // rh_lin:  Humidity linear
  float rh_true;                    // rh_true: Temperature compensated humidity
  float t_C;                        // t_C   :  Temperature [°C]

  t_C=t*0.01 - 40;                  //calc. temperature from ticks to [°C]
  rh_lin=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH]
  rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity [%RH]
  if(rh_true>100)rh_true=100;       //cut if the value is outside of
  if(rh_true<0.1)rh_true=0.1;       //the physical possible range

  *p_temperature=t_C;               //return temperature [°C]
  *p_humidity=rh_true;              //return humidity[%RH]
}

//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input:   humidity [%RH], temperature [°C]
// output:  dew point [°C]
{
  float logEx,dew_point;
  logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
  dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
  return dew_point;
}
void serialPrintFloat( float f){
  Serial.print((int)f);
  Serial.print(".");
  int temp = (f - (int)f) * 100;
  Serial.print( abs(temp) );
}
228  Forum 2005-2010 (read only) / Interfacing / Re: Reset command on: February 09, 2009, 04:36:35 am
hmm yea the code does work, but
now i can't load any sketch to the arduino anymore..
It does reset itself that quick that the sketch
can't get uploaded...

What can I do?

Thx
Geko
229  Forum 2005-2010 (read only) / Interfacing / Xbee and Mesh Network on: September 04, 2008, 10:13:23 am
Hello

So I got some experience with the Xbee modules. They do work great.
But at the moment I only got two working so one is sending data to
the other one vice versa.
But now I would like to set up a network of Xbee's. For example I need
to put a Sensor far a way from the Computer. So I have to put several
Xbee's between the Sensor and Computer to transmit the Data. Do I
need for every Xbee a Microcontroller?

Or better, does someone have experience in this?

Thx
Geko
230  Forum 2005-2010 (read only) / Interfacing / Re: Ethernet Shield and MySQL on: February 01, 2009, 02:39:01 am
hey guys

That's exactly what I want to do, write to a MYSQL DB.
Different Arduinos with Sensors should write theire data
to an DB...
Can someone explain me how to set this up? Or give me
some tips?

Thx in advanced
Geko
231  Forum 2005-2010 (read only) / Interfacing / Re: phone and Arduino some progress but need help on: August 26, 2008, 02:20:00 am
Now I got a problem with the SoftSerial. i tired to send codes
to my phone over the SoftSerial but without success.
Code:
/*
 Example of SSerial2Mobile libary

Sends a SMS and an email
The SMS phone number and the email address need to be changed to something valid.

 created 21 June 2008
 by Gustav von Roth
*/


#include <SoftwareSerial.h>
#include <t630.h>
#include <AFSoftSerial.h>

AFSoftSerial mySerial =  AFSoftSerial(3, 2);

char message[20]="Hallo Test 2";
char number[20]="0123456789";

t630 phone = t630(2,3);

void setup() {
  pinMode(13, OUTPUT);
  mySerial.begin(4800);
  phone.sendTxt(number,message);  
}
void loop(){ }

This code should be ok or not?

Thx,
Geko
232  Forum 2005-2010 (read only) / Interfacing / Re: phone and Arduino some progress but need help on: August 24, 2008, 12:44:33 pm
nice, so at the moment I can  turn off the phone by this code smiley

Code:
void setup() {
 
  Serial.begin(4800);
  Serial.println("AT+CFUN=0");
  }
void loop(){ }

So what I've done..
I connected know the wires directly to the phone and that was the solution!

I always tried to connect through this phone connector:

But it didn't work. So is this connector maybe a TTL to Serial converter?

Thx a lot for your great help!!!!


Geko

233  Forum 2005-2010 (read only) / Interfacing / Re: phone and Arduino some progress but need help on: August 24, 2008, 10:26:17 am
I got a Sony Ericsson. I got the connectors here http://pinouts.ru/CellularPhones-P-W/erics_t28_pinout.shtml ....
234  Forum 2005-2010 (read only) / Interfacing / phone and Arduino some progress but need help on: August 23, 2008, 04:08:55 am
Hello

At the moment I'm trying to get my Sony Ericsson working on the Arduino.
So what have I done:
I got a Serial cable for the phone. I plugged it into my PC and connected through the Terminal. At commands work great. The I cuted the cable to see the wires. Ok there were 6 wires. But 3 where useless.
I got now 3 cable to the serial port and it does still work. I found out that one is the RX cable. (To test that I sent the code to turn off the phone. First with this cable connected and the disconnected this cable. Both time it worked but once I didn't get the OK message from the phone so it should be the RX cable… Should be right or did I miss something?)

Now how do I detected the Ground cable and the TX? ok 3 cables so 50:50 smiley

But one of the important question:
When I like to connect the phone with those 3 wires to the Arduino do I have to use a Serial to TTL converter? I think so because as I tried to connected my Garmin GPS I had to use this as well… or am I wrong


Thx,
Geko
235  Forum 2005-2010 (read only) / Interfacing / Re:  Thermocouple on: October 01, 2008, 02:35:20 pm
Would be very nice!

In another thread you've mentioned that you've used a balloon for something. Did you also measure the temperature at high altitude?


Thx
Geko
236  Forum 2005-2010 (read only) / Interfacing / Thermocouple on: October 01, 2008, 11:07:09 am
Hy

How can I connect a  T-Thermocouple ([ch8722]185C to +300C) to arduino?
Maxim doesn't sell those kind of converter or did I miss something?

Thx
Geko
237  Forum 2005-2010 (read only) / Interfacing / Re: Temperature Sensor on: October 03, 2008, 09:23:45 am
-55 deg is more or less enough -80 would be much better but I haven't found a solution for the arduino yet to measure that low temperature...
Do you have any ideas?

Geko
238  Forum 2005-2010 (read only) / Interfacing / Re: Temperature Sensor on: September 26, 2008, 01:29:59 am
hmm Ok thx a lot...
I got the DS18B20 and this one goes to -55C..

I have to checkout those Thermocouples...

Thx
Geko
239  Forum 2005-2010 (read only) / Interfacing / Temperature Sensor on: September 24, 2008, 04:01:51 pm
Hello

There are a lot of temperature Sensor that can measure high temperature but I do need one that can measure low temperate at around -70 Celsius.
(It should be accurate between +30 and -70 Celsius)
Does someone know one?

Thx
Geko
240  Forum 2005-2010 (read only) / Interfacing / Re: Temperature Sensor DS18B20 and producing some heat on: October 01, 2008, 11:07:49 am
Thx for the advise! I will check those Heat Sinks...

Geko
Pages: 1 ... 14 15 [16] 17 18 19