|
|
View previous topic :: View next topic |
Author |
Message |
respected
Joined: 16 May 2006 Posts: 95
|
HDC1000 Reading sensor |
Posted: Thu Jan 08, 2015 3:38 pm |
|
|
This is my arduino code and it works
Code: |
#include <Wire.h>
long value1;
long value2;
long totalvalue;
float temperature;
void setup(){
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(0x40);
Wire.write(0x10);
Wire.write(0x00); //
Wire.endTransmission();
}
void loop(){
temp_measure();
Serial.print("\n\r");
Serial.print(temperature);
delay(500);
}
float temp_measure(){
Wire.beginTransmission(0x40);
Wire.write(0x00);
delay(10);
Wire.requestFrom(0x40,2);
value1 = Wire.read(); //
value2 = Wire.read(); //
Wire.endTransmission();
totalvalue =((value1<<8)|value2);
temperature= (totalvalue*0.0025177001953125)-40;
} |
This is PIC code. It doesn't work. It comes data0= 0xff or 0xf7 or different value. But this is not true. It has to be temperature value. Can anyone help me please? Where is mistake?
Thanks
Code: |
#include <18F46J50.h>
#fuses HSPLL,PLL3,CPUDIV2,NOWDT,NODEBUG
#use delay(clock=24MHz,crystal=12MHz)
#use i2c(master,sda=pin_b5, scl=pin_b4, slow)
#use rs232(baud=115200,parity=N,xmit=PIN_A2, bits=8,stream=USART2)
long totaltemp;
float temperature;
char strstore[20];
void hdc_setup (void)
{
i2c_start();
i2c_write(0x40);
i2c_write(0x80);
i2c_write(0x02);
i2c_write(0x10);
i2c_write(0x00);
i2c_stop();
delay_ms(20);
}
void read_hdc(void)
{
int8 data[2]={0,0};
int i;
i2c_start();
i2c_write(0x80);
i2c_write(0x00);
i2c_write(0x81);
for(i=0;i<2;i++)
{
data[i]=i2c_read();
}
i2c_stop();
totaltemp=make16(data[0],data[1]);
temperature=(totaltemp*0.0025177)-40;
sprintf(strstore,"Temp= %f",temperature);
fputs(strstore,USART2);
delay_ms(250);
}
void main()
{
hdc_setup();
while (TRUE)
{
read_hdc();
} //while
}//main
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 08, 2015 3:57 pm |
|
|
Quote: | void hdc_setup (void)
{
i2c_start();
i2c_write(0x40);
i2c_write(0x80);
i2c_write(0x02);
i2c_write(0x10);
i2c_write(0x00);
i2c_stop();
delay_ms(20);
}
|
Arduino uses 7-bit format for the slave address. CCS uses 8-bit format.
You need to fix the CCS code. If you already did this with the 0x80, then
delete the 0x40 line.
Quote: |
void read_hdc(void)
{
int8 data[2]={0,0};
int i;
i2c_start();
i2c_write(0x80);
i2c_write(0x00);
i2c_write(0x81);
for(i=0;i<2;i++)
{
data[i]=i2c_read();
}
i2c_stop(); |
The last read needs a NACK. You don't need a for() loop for two reads. |
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
Posted: Thu Jan 08, 2015 4:45 pm |
|
|
I deleted 0x40 and i wrote
Code: |
i2c_start();
i2c_write(0x80);
i2c_write(0x00);
i2c_write(0x81);
data[0]=i2c_read();
data[1]=i2c_read();
i2c_stop();
|
but same result |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 08, 2015 5:03 pm |
|
|
You need a NACK on the last read, as shown in bold below:
Quote: |
data[0]=i2c_read();
data[1]=i2c_read(0);
|
|
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 09, 2015 12:54 pm |
|
|
Post a sample of your irregular results. |
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
Posted: Fri Jan 09, 2015 2:49 pm |
|
|
22:55:14.856> data0= 255
22:55:14.856> data1= 255
22:55:14.856> temp= 124.99
22:55:15.792> data0= 255
22:55:15.792> data1= 255
22:55:15.792> temp= 124.99
22:55:16.544> data0= 31
22:55:16.544> data1= 31
22:55:16.544> temp= -19.94
22:55:17.292> data0= 0
22:55:17.292> data1= 0
22:55:17.292> temp= -40.00
22:55:18.230> data0= 253
22:55:18.230> data1= 123
22:55:18.230> temp= 123.37
22:55:19.169> data0= 239
22:55:19.169> data1= 95
22:55:19.169> temp= 114.28
22:55:20.045> data0= 63
22:55:20.045> data1= 127
22:55:20.045> temp= 0.92
Serial port terminal screen |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9247 Location: Greensville,Ontario
|
|
Posted: Fri Jan 09, 2015 3:07 pm |
|
|
You need to post the program you used to display those results.
That device has a specific setup . Also the last 2 bits are NOT part of the 'math' probably need to have the 14 bits right shifted then the math according to the datasheet.
Jay |
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
Posted: Fri Jan 09, 2015 3:11 pm |
|
|
Code: |
#include <18F46J50.h>
#fuses HSPLL,PLL3,CPUDIV2,NOWDT,NODEBUG
#use delay(clock=24MHz,crystal=12MHz)
#use i2c(master,sda=pin_b5, scl=pin_b4, slow)
#use rs232(baud=115200,parity=N,xmit=PIN_A2, bits=8,stream=USART2)
long totaltemp;
float temperature;
char strstore1[20];
char strstore2[20];
char strstore3[20];
void hdc_setup (void)
{
i2c_start();
// i2c_write(0x40);
i2c_write(0x80);
i2c_write(0x02);
i2c_write(0x10);
i2c_write(0x00);
i2c_stop();
delay_ms(20);
}
void read_hdc(void)
{
int8 data[4]={0,0,0,0};
i2c_start();
i2c_write(0x80);
i2c_write(0x00);
i2c_write(0x81);
data[0]=i2c_read();
data[1]=i2c_read(0);
i2c_stop();
totaltemp=make16(data[0],data[1]);
temperature=(totaltemp*0.0025177)-40;
sprintf(strstore1,"data0= %u",data[0]);
sprintf(strstore2,"data1= %u",data[1]);
sprintf(strstore3,"temp= %f",temperature);
fputs(strstore1,USART2);
fputs(strstore2,USART2);
fputs(strstore3,USART2);
delay_ms(750);
}
void main()
{
hdc_setup();
while (TRUE)
{
read_hdc();
} //while
}//main |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 09, 2015 5:11 pm |
|
|
The HDC1000 has two address pins, ADR0 and ADR1. What connections
do you have to these two pins ?
What is the Vdd voltage on your PIC, on the HDC1000, and on the i2c
pullup resistors ?
What is the value of the i2c pullup resistors ?
What happens if you write a program to read a fixed value from the
hdc1000, such as the manufacturer ID word at address 0xFE ?
The data sheet says the ID word is 0x5449 (on page 14). Do you
always get that word, or do you sometimes get 0xFFFF ? |
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
Posted: Sat Jan 10, 2015 4:12 pm |
|
|
I tried with TI Sensor Evaluation Module. and I read codes with logic analyser
for 0x5449 i read
0x80+ack 0xfe+ack 0x81+ack 0x54+ack 0x49+nak
and i wrote pic mcu code
Code: | i2c_start();
i2c_write(0x80);
i2c_write(0xFE);
i2c_write(0x81);
data[0]=i2c_read();
data[1]=i2c_read(0);
i2c_stop();
|
i read code with logic analyser
1. 0x80+ack 0xfe+ack 0x81+ack 0x54+ack 0x48+nak
2. 0x80+ack 0x7f+ack 0x40+ack 0x95+ack 0x11+nak
3. 0x80+ack 0xfe+ack 0x81+ack 0x25+ack 0x82+nak |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 10, 2015 9:59 pm |
|
|
I think there is something fundamentally wrong, such as you are
connecting capacitors to SDA and SCL, or the pullups are the wrong
value, or the power supply is bad.
Post a link to a schematic of your PIC to HDC1000 circuit.
How to post an image on the CCS forum:
Use http://www.postimage.com or http://www.imageshack.com
or some other free image hosting service.
Upload your schematic to that website. Preferably a .JPG file or some
other commonly used image format.
Then post a link to your image here in the forum. |
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
Posted: Sun Jan 11, 2015 11:03 am |
|
|
https://imageshack.com/i/f0tyuVnUj
I broke sensor and cpu board. Unfortunately pullup resistors was mcu side.
I added pullup (4K7) and It works now.
I sent code
Code: |
i2c_start();
i2c_write(0x80);
i2c_write(0xFE);
i2c_write(0x81);
data[0]=i2c_read();
data[1]=i2c_read(0);
i2c_stop();
|
answer by sensor
data[0]=84 (0x54)
data[1]=73 (0x49)
Thanks for helping for everyone( especially PCM Programmer)
ADR0 and ADR1 default value is 0(GND)
I added 4K7 pullup registor for DRDYn. Special registers are ok. all of data are true. But temperature data and humidity data are coming 0xff.
I will try to continue.
HDC1000 data sheet says
Data ready, active low, open-drain; requires a pull-up resistor to VDD. If not used tie to GND or no connect.
Code: |
i2c_start();
i2c_write(0x80);
i2c_write(0x00);
delay_ms(13);// I saw in the logic analyser signal
i2c_write(0x81);
data[0]=i2c_read();
data[1]=i2c_read(0);
i2c_stop(); |
Everyting is OK.
Last edited by respected on Sun Jan 11, 2015 3:40 pm; edited 1 time in total |
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
Posted: Sun Jan 11, 2015 3:07 pm |
|
|
This is full code.
Code: |
#include <18F46J50.h>
#fuses HSPLL,PLL3,CPUDIV2,NOWDT,NODEBUG
#use delay(clock=24MHz,crystal=12MHz)
#use i2c(master,sda=pin_b5, scl=pin_b4, slow=100000)
#use rs232(baud=115200,parity=N,xmit=PIN_A2, bits=8,stream=USART2)
long totaltemp;
float temperature;
char strstore1[20];
char strstore2[20];
char strstore3[20];
void hdc_setup (void)
{
i2c_start();
i2c_write(0x80);
i2c_write(0x02);
i2c_write(0x10);
i2c_write(0x00);
i2c_stop();
delay_ms(20);
}
void read_hdc(void)
{
int8 data[4]={0,0,0,0};
i2c_start();
i2c_write(0x80);
i2c_write(0x00);
delay_ms(13);
i2c_write(0x81);
data[0]=i2c_read();
data[1]=i2c_read(0);
i2c_stop();
totaltemp=make16(data[0],data[1]);
temperature=(totaltemp*0.0025177)-40;
sprintf(strstore1,"data0= %u",data[0]);
sprintf(strstore2,"data1= %u\r\n",data[1]);
sprintf(strstore3,"temp= %f",temperature);
fputs(strstore1,USART2);
fputs(strstore2,USART2);
fputs(strstore3,USART2);
delay_ms(500);
}
void main()
{
hdc_setup();
while (TRUE)
{
read_hdc();
} //while
}//main |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9247 Location: Greensville,Ontario
|
|
Posted: Sun Jan 11, 2015 7:43 pm |
|
|
nice to hear you got it 'up and running'.
Yes, I2C does need pullups ! too bad TI put them next to their micro and not the temp/hum sensor. It would have saved you some time BUT you learned a LOT of valuable 'hands on' experience.
jay |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|