Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Code to read temperature problem

Status
Not open for further replies.

hssn601

Junior Member level 2
Junior Member level 2
Joined
Apr 26, 2011
Messages
24
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Visit site
Activity points
1,420
Hi All,

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

Sensor Model:am2315

Sensor info:

screenshot_5.png




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,rx_byte1,rx_byte2,rx_byte3; 
  
  
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 */ 
        
        i2c_write(0x03);  
      
          
           
          
        // reading humidity 
        rx_byte = i2c_read(0x00);  
     
          
        rx_byte1 = i2c_read(0x01);  
        
          
        
          
          
          
        //read temp 
        rx_byte2 = i2c_read(0x02);  
       
          
        rx_byte3 = i2c_read(0x03);  
        
          
        
        printf ("rx_byte1 = 0x%2.2X\r   0x%2.2X\r \n", rx_byte, rx_byte1);       
        printf ("rx_byte1 = 0x%2.2X\r   0x%2.2X\r \n", rx_byte2, rx_byte3);   
        
          
          
          
        
          
          
        i2c_stop();  
      
        printf ("End\n");  
    }  
  
  
  
  
}
 

Attachments

  • AM2315.pdf
    2.9 MB · Views: 99
Last edited:

Re: Connecting temperature sensor on i2c c18 and ccs

Hello

Why are you putting delays at the end of each command for I2C comunication i.e.
Code:
 i2c_start(); delay_ms(50);
. During the communication stop using printf. Once you are done with communication display results then. Also the what clock settings are you using for I2C? I cannot see that in above code.

Enjoy!
 

Re: Connecting temperature sensor on i2c c18 and ccs

Hello

Why are you putting delays at the end of each command for I2C comunication i.e.
Code:
 i2c_start(); delay_ms(50);
. During the communication stop using printf. Once you are done with communication display results then. Also the what clock settings are you using for I2C? I cannot see that in above code.

Enjoy!

ok i got it actually i am using i2c 1st time can you tell me what clock settings i have to use.

thanks
 

Re: Connecting temperature sensor on i2c c18 and ccs

mikroC Code not tested.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
I2C1_Init(100000);         // initialize I2C communication
I2C1_Start();              // issue I2C start signal
I2C1_Wr(0xB8);             // send byte via I2C  (device address + W)
I2C1_Wr(0x03);                // send byte (address of EEPROM location)
I2C1_Wr(0x00);             // send data (data to be written)
I2C1_Wr(0x00);
I2C1_Stop();               // issue I2C stop signal
 
I2C1_Start();              // issue I2C start signal
I2C1_Wr(0xB9);             // send byte via I2C  (device address + W)
FC = I2C1_Rd(1);       // Read the data (acknowledge)
RN = I2C1_Rd(1);       // Read the data (acknowledge)
RHL = I2C1_Rd(1);       // Read the data (acknowledge)
RHH = I2C1_Rd(1);       // Read the data (acknowledge)
TEMPL = I2C1_Rd(1);       // Read the data (acknowledge)
TEMPH = I2C1_Rd(1);       // Read the data (acknowledge)
RHL = I2C1_Rd(1);       // Read the data (acknowledge)
RHH = I2C1_Rd(1);       // Read the data (acknowledge)
CRC = I2C1_Rd(1);       // Read the data (acknowledge)
I2C1_Stop();               // issue I2C stop signal

 
Last edited:

Re: Connecting temperature sensor on i2c c18 and ccs

ok i got it actually i am using i2c 1st time can you tell me what clock settings i have to use.

You must be aware of I2C protocol. I normally use 100Kb/s transfer rate. To do this you must read out the formula in microcontroller datasheet to set this rate. for pic18f25x i use the following function.

Code:
//some where in header file put these two 

#define FOSC 4000000
#define Baud 100000 

void I2Cinitilise(void)
{
	SSPCON1 = 0x28;	// I2C ENB, FOSC / (4 * (SSPADD+1)) 
	SSPADD = ((FOSC/4)/Baud)-1;
	SSPIF = 0;	
}

or you can read the manual for C18 and see how to set these clock rates.
Enjoy!!! :cool:
 

Re: Connecting temperature sensor on i2c c18 and ccs

mikroC Code not tested.

why you have readed rhl and rhh twice.

Code:
RHL = I2C1_Rd(1);       // Read the data (acknowledge)
RHH = I2C1_Rd(1);       // Read the data (acknowledge)

and send
Code:
I2C1_Wr(0x00);             // send data (data to be written)
I2C1_Wr(0x00);

twice
 

Re: Connecting temperature sensor on i2c c18 and ccs

It was a mistake. Mentio the PIC and Fosc you are using. I will compile a code and post it.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
I2C1_Start();              // issue I2C start signal
I2C1_Wr(0xB9);             // send byte via I2C  (device address + W)
FC = I2C1_Rd(1);       // Read the data (acknowledge)
RN = I2C1_Rd(1);       // Read the data (acknowledge)
RHL = I2C1_Rd(1);       // Read the data (acknowledge)
RHH = I2C1_Rd(1);       // Read the data (acknowledge)
TEMPL = I2C1_Rd(1);       // Read the data (acknowledge)
TEMPH = I2C1_Rd(1);       // Read the data (acknowledge)
CRC = I2C1_Rd(1);       // Read the data (acknowledge)
I2C1_Stop();

 

Re: Connecting temperature sensor on i2c c18 and ccs

It was a mistake. Mentio the PIC and Fosc you are using. I will compile a code and post it.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
I2C1_Start();              // issue I2C start signal
I2C1_Wr(0xB9);             // send byte via I2C  (device address + W)
FC = I2C1_Rd(1);       // Read the data (acknowledge)
RN = I2C1_Rd(1);       // Read the data (acknowledge)
RHL = I2C1_Rd(1);       // Read the data (acknowledge)
RHH = I2C1_Rd(1);       // Read the data (acknowledge)
TEMPL = I2C1_Rd(1);       // Read the data (acknowledge)
TEMPH = I2C1_Rd(1);       // Read the data (acknowledge)
CRC = I2C1_Rd(1);       // Read the data (acknowledge)
I2C1_Stop();


i am using pic 24F16KA102 with ccs compailer

here is my code:
Code:
#include<24F16KA102.h>
#device ADC=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOBSS                    //No boot segment
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
#FUSES FRC                      //Internal Fast RC Oscillator
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOPR                     //Pimary oscillaotr disabled
#FUSES OSCIO                    //OSC2 is general purpose output
#FUSES POSCFREQ_H            
#FUSES SOSC_HIGH             
#FUSES NOCKSFSM                 //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
//#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WINDIS                   //Watch Dog Timer in non-Window mode
#FUSES NOBROWNOUT               //No brownout reset
#FUSES PUT                      //Power Up Timer
#FUSES BORV_LOW              
#FUSES MCLR                     //Master Clear pin enabled
#FUSES ICSP1                    //ICD uses PGC1/PGD1 pins
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES DSWDT2147483648       
#FUSES DSWDTCK_LPRC          
#FUSES RTCCK_SOSC            
#FUSES DSBOR                 
#FUSES DSWDT                 
#FUSES NOALTI2C                 //I2C mapped to alternate pins

#device ADC=10

#use delay(clock=8000000)
#opt 9

#use rs232(UART1,baud=9600,parity=N,bits=8,stream=sbuff)
#use i2c(Master,Fast,sda=PIN_B9,scl=PIN_B8,force_hw)//disable hardware i2c for sht15 as the library uses bit banging
#use standard_io(B)

#include <string.h>

#ZERO_RAM

static int tick100ms;


#int_TIMER1
void  TIMER1_isr(void) //100mSec overflow
{
   set_timer1( 0x3CB0 );

   tick100ms++;
   if( tick100ms == 10)
   {
      tick100ms=0;
      output_toggle(PIN_B15);
   }

}


void main()
{


   setup_timer1(TMR_INTERNAL | TMR_DIV_BY_8 ); 
   setup_wdt(WDT_OFF);
   enable_interrupts(INT_TIMER1);
   
   fprintf( sbuff, "\r\nBoot: OK\r\n\r\n");
 
 
  
   
    
   
 unsigned char fc,rn,rhl,rhh,templ,temph,crc;
   while(TRUE)
   {
      
      i2c_start(); // Issues a start command when in the I2C master mode.
 
      i2c_write(0xB8); // Sends a single byte over the I2C interface.
      i2c_write(0x03);
      i2c_write(0x00);
      i2c_write(0x00);
      
     i2c_stop(); //Issues a stop command when in the I2C master mode.
     
     delay_ms(100);
     
    I2C_Start();              // issue I2C start signal
    i2c_write(0xB9);             // send byte via I2C  (device address + W)
    FC = i2c_read(1);       // Read the data (acknowledge)
    RN = i2c_read(1);       // Read the data (acknowledge)
    RHL = i2c_read(1);       // Read the data (acknowledge)
    RHH = i2c_read(1);       // Read the data (acknowledge)
    TEMPL = i2c_read(1);       // Read the data (acknowledge)
    TEMPH = i2c_read(1);       // Read the data (acknowledge)
    CRC = i2c_read(1);  
      
      
     
     
 

     
     
     delay_ms(3000);        //delay 500 ms between reading to prevent self heating of sensor         
   }

}
 
Last edited:


Re: Connecting temperature sensor on i2c c18 and ccs

That was a mistake which happened while copying and pasting code. Remove two lines after this line.


Code C - [expand]
1
TEMPH = I2C1_Rd(1);       // Read the data (acknowledge)



The data is printed on UART1.
 

Re: Connecting temperature sensor on i2c c18 and ccs

That was a mistake which happened while copying and pasting code. Remove two lines after this line.


Code C - [expand]
1
TEMPH = I2C1_Rd(1);       // Read the data (acknowledge)



The data is printed on UART1.

and what about

Code:
 I2C1_Write(0x00);             // send data (data to be written)
     I2C1_Write(0x00);

actually i am using i2c first time thats why asking silly questions.
 

Re: Connecting temperature sensor on i2c c18 and ccs

Page no. 10

Start + (I2C Addr + W) + Function code (0x03) + Start address (0x00) + Register number (0x00) + Stop

Start + (I2C Addr + R) + Continuously read Sensor data returned + Stop

https://github.com/adafruit/Adafruit_AM2315


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void readDevice2(){
     I2C1_Start();              // issue I2C start signal
     I2C1_Write(0x5C);             // send byte via I2C  (device address + W)
     I2C1_Write(0x03);                // send byte (address of EEPROM location)
     I2C1_Write(0x00);             // send data (data to be written)
     I2C1_Write(0x04);
     I2C1_Stop();               // issue I2C stop signal
 
     I2C1_Start();              // issue I2C start signal
     I2C1_Write(0x5C);             // send byte via I2C  (device address + W)
     I2C1_Write(0x03);                // send byte (address of EEPROM location)
     I2C1_Write(0x00);             // send data (data to be written)
     I2C1_Write(0x04);
     I2C1_Stop();
     
     I2C1_Start();              // issue I2C start signal
     I2C1_Write(0x5C);             // send byte via I2C  (device address + W)
     dummy = I2C1_Read(1);       // Read the data (acknowledge)
     dummy = I2C1_Read(1);       // Read the data (acknowledge)
     FC = I2C1_Read(1);       // Read the data (acknowledge)
     RN = I2C1_Read(1);       // Read the data (acknowledge)
     RHL = I2C1_Read(1);       // Read the data (acknowledge)
     RHH = I2C1_Read(1);       // Read the data (acknowledge)
     TEMPL = I2C1_Read(1);       // Read the data (acknowledge)
     TEMPH = I2C1_Read(1);       // Read the data (acknowledge)
     I2C1_Stop();
 
     RH = RHH << 8;
     RH |= RHL;
 
     TEMP = TEMPH << 8;
     TEMP |= TEMPL;
 
}

 
Last edited:
Re: Connecting temperature sensor on i2c c18 and ccs

Page no. 10

Start + (I2C Addr + W) + Function code (0x03) + Start address (0x00) + Register number (0x00) + Stop

Start + (I2C Addr + R) + Continuously read Sensor data returned + Stop

https://github.com/adafruit/Adafruit_AM2315


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void readDevice2(){
     I2C1_Start();              // issue I2C start signal
     I2C1_Write(0x5C);             // send byte via I2C  (device address + W)
     I2C1_Write(0x03);                // send byte (address of EEPROM location)
     I2C1_Write(0x00);             // send data (data to be written)
     I2C1_Write(0x04);
     I2C1_Stop();               // issue I2C stop signal
 
     I2C1_Start();              // issue I2C start signal
     I2C1_Write(0x5C);             // send byte via I2C  (device address + W)
     I2C1_Write(0x03);                // send byte (address of EEPROM location)
     I2C1_Write(0x00);             // send data (data to be written)
     I2C1_Write(0x04);
     I2C1_Stop();
     
     I2C1_Start();              // issue I2C start signal
     I2C1_Write(0x5C);             // send byte via I2C  (device address + W)
     dummy = I2C1_Read(1);       // Read the data (acknowledge)
     dummy = I2C1_Read(1);       // Read the data (acknowledge)
     FC = I2C1_Read(1);       // Read the data (acknowledge)
     RN = I2C1_Read(1);       // Read the data (acknowledge)
     RHL = I2C1_Read(1);       // Read the data (acknowledge)
     RHH = I2C1_Read(1);       // Read the data (acknowledge)
     TEMPL = I2C1_Read(1);       // Read the data (acknowledge)
     TEMPH = I2C1_Read(1);       // Read the data (acknowledge)
     I2C1_Stop();
 
     RH = RHH << 8;
     RH |= RHL;
 
     TEMP = TEMPH << 8;
     TEMP |= TEMPL;
 
}



here is my code and uc is just sending FF
Code:
#include<24F16KA102.h>

#device ADC=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOBSS                    //No boot segment
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
#FUSES FRC                      //Internal Fast RC Oscillator
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOPR                     //Pimary oscillaotr disabled
#FUSES OSCIO                    //OSC2 is general purpose output
#FUSES POSCFREQ_H            
#FUSES SOSC_HIGH             
#FUSES NOCKSFSM                 //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
//#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WINDIS                   //Watch Dog Timer in non-Window mode
#FUSES NOBROWNOUT               //No brownout reset
#FUSES PUT                      //Power Up Timer
#FUSES BORV_LOW              
#FUSES MCLR                     //Master Clear pin enabled
#FUSES ICSP1                    //ICD uses PGC1/PGD1 pins
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES DSWDT2147483648       
#FUSES DSWDTCK_LPRC          
#FUSES RTCCK_SOSC            
#FUSES DSBOR                 
#FUSES DSWDT                 
#FUSES NOALTI2C                 //I2C mapped to alternate pins

#device ADC=10

#use delay(clock=8000000)
#opt 9

#use rs232(UART1,baud=9600,parity=N,bits=8,stream=sbuff)
#use i2c(Master,Fast,sda=PIN_B9,scl=PIN_B8,fast=100000,force_hw)//disable hardware i2c for sht15 as the library uses bit banging
#use standard_io(B)

#include <string.h>

#ZERO_RAM

static int tick100ms;


#int_TIMER1
void  TIMER1_isr(void) //100mSec overflow
{
   set_timer1( 0x3CB0 );

   tick100ms++;
   if( tick100ms == 10)
   {
      tick100ms=0;
      output_toggle(PIN_B15);
   }

}


void main()
{


   setup_timer1(TMR_INTERNAL | TMR_DIV_BY_8 ); 
   setup_wdt(WDT_OFF);
   enable_interrupts(INT_TIMER1);
   
   fprintf( sbuff, "\r\nSERIAL  OK !!\r\n\r\n");
 
 
  
   
    
   
 //unsigned char fc,rn,rhl,rhh,templ,temph,crc;
 
 unsigned CHAR FC, RN, RHL, RHH, TEMPL, TEMPH, CRC, RH, TEMP;
   while(TRUE)
   {
      
      ; // Issues a start command when in the I2C master mode.
 /*
      i2c_write(0xB8); // device address
      i2c_write(0x03); //function
      i2c_write(0x00);// reg address
      i2c_write(0x00);// start 
      */
      
     
     i2c_start();
      i2c_write(0xB8); // device address
      i2c_write(0x03); //function
      i2c_write(0x00);// reg address
      i2c_write(0x00);// start 
      
     i2c_stop();
     
     
     
    I2C_Start();              // issue I2C start signal
    i2c_write(0xB9);             // send byte via I2C  
    FC = i2c_read(1);       // Read the data (acknowledge)
    RN = i2c_read(1);       // Read the data (acknowledge)
    RHL = i2c_read(1);       // Read the data (acknowledge)
    RHH = i2c_read(1);       // Read the data (acknowledge)
    TEMPL = i2c_read(1);       // Read the data (acknowledge)
    TEMPH = i2c_read(1);       // Read the data (acknowledge)
    CRC = i2c_read(1);  
    
    
   
     
    
     
     i2c_stop();
            
            
            
            
      
   printf ("HL = 0x%2.2X HH= 0x%2.2X \rTEMPL = 0x%2.2X TEMPH= 0x%2.8X \r\r ",RHH,RHL,TEMPL,TEMPH);
   //printf ("TEMPL = 0x%2.2X TEMPH= 0x%2.2X \r ",TEMPL,TEMPH);
     
     
 

     
     
     delay_ms(3000);        //delay 500 ms to prevent heating of sensor         
   }

}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top