Connecting temperature sensor on i2c c18 and ccs

Status
Not open for further replies.

hssn601

Member
Hi All,

i have developed a code to read temperature but its not working please help to correct it.

Sensor Model:am2315

Sensor info:





Code:
#include <18f4525.h> 
#include <stdlib.h> 
#fuses INTRC_IO, NOWDT, PUT , BROWNOUT, NOLVP, NOPROTECT, NOMCLR 
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(master, sda=PIN_C4, scl=PIN_C3, FORCE_HW) 
unsigned char rx_byte;
unsigned char rx_byte1;
void main(void) 
{ 
SET_TRIS_A(0x00); 
int a=0;
while (a<2){ 
output_high(PIN_A1);
delay_ms(500); 
output_low(PIN_A1); 
delay_ms(100); 
a++;
} 
while (1) 
{ 
printf ("Start i2c \n"); 
i2c_start(); 
delay_ms(50); 
i2c_write(0xB8);  /* Device Address */ 
delay_ms(50); 
i2c_write(0x03); 
delay_ms(100); 
// reading humidity
rx_byte = i2c_read(0x00); 
delay_ms(50); 
rx_byte1 = i2c_read(0x01); 
delay_ms(50); 
printf ("rx_byte1 = 0x%2.2X\r  0x%2.2X\r \n", rx_byte, rx_byte1);
//read temp
rx_byte = i2c_read(0x00); 
delay_ms(50); 
rx_byte1 = i2c_read(0x01); 
delay_ms(50); 
printf ("rx_byte1 = 0x%2.2X\r\n", rx_byte1);
printf ("rx_byte1 = 0x%2.2X\r\n", rx_byte1);
i2c_stop(); 
printf ("End\n"); 
} 
}
 

Attachments

  • AM2315.pdf
    2.9 MB · Views: 202
You are not reading for multiple reads... Read the manual for streaming multiple bytes or write a wrapper function to grab one byte at a time

C:
i2c_start();
i2c_write(0xB8);
i2c_write(0x3); 
i2c_start();
i2c_write(0xB9);
data1 = i2c_read(TRUE);
data2 = i2c_read(TRUE);    // Keep this high until you read the last byte
data3 = i2c_read(TRUE);
data4 = i2c_read(FALSE);  // this tells the slave to stop sending!!
i2c_stop();
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…