Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 12th October 2009, 11:15 PM   #1
Default LM75 Help please

need some help

i got a LM75(BD) and can only get so far.. its I2C and so far i can read from it i think heh

Im using the below code to split the 2 byte data (temperature) into 1 byte then into 3 bytes...

Code:
     temp3 = ReadLM75BD();                    //get 2 bytes (int)
    temp4 = temp3 >> 8;                      // erase the LSByte (the LSBit is not important i assume)
    tH = (temp4 / 100) + 0x30;               // divide by 100 to get hundreds #
    tT = ((temp4 % 100) / 10) + 0x30;        // divide by 100 and use remainder to divide by 10  to get tens #
    tO = (((temp4 % 100) % 10) % 10) + 0x30; //use remainder on of temp4/100 then divide that byt 10 and with that remainder divide it by ten and use last remainder  as ones...
and code to get info:
Code:
unsigned int ReadLM75BD(void){
unsigned int tmp;
char result;

    i2c_start(); //Send Start
    i2c_write(RAdd); //Send our Device Address with the Write BIT

    result = i2c_ack2(); //Test for ACK

    tmp = i2c_read(); //Read 1 Byte from device and save in tmp
    result = i2c_ack2(); //Test for ACK

    tmp = tmp << 8;
    tmp |= i2c_read(); //Read 1 Byte from device and save in tmp

    result = i2c_ack(); //Test for NACK
    i2c_stop(); //Send Stop
    return tmp;
}
My entire code is quite long. Not sure if i should post it heh... the I2C is in my PDF cheat sheet and worked before so i assume its not the code for that or the hardware since i get a int like 0x02FF and such...

I assume it the way i convert the stuff or something or read the bytes in..

EDIT:

Here is some valuable info which i dont understand much:
""

Last edited by AtomSoft; 12th October 2009 at 11:16 PM.
AtomSoft is offline  
Old 12th October 2009, 11:53 PM   #2
Default

I get a steady reading of 0x01 or 0x00 and it increases with my finger on it.

It says the default register is TEMP so i assume thats what im reading.

Im reading D15 to D8 as my temperature but heh its unlikely the temperature is 0-1C here heh...

I must be doing something wrong... when i first start the program i get a reading of about 25C
AtomSoft is offline  
Old 13th October 2009, 01:52 AM   #3
Default

decided to use the built in i2c stuff:
Code:
unsigned char ReadLM(void){
unsigned char tmpC;
unsigned char result;

    StartI2C();

    WriteI2C(WAdd);
    AckI2C();

    WriteI2C(0x00);
    AckI2C();

    RestartI2C();

    WriteI2C(RAdd);
    AckI2C();

    tmpC = ReadI2C();
    //AckI2C();

    //result = getcI2C();
    NotAckI2C();

    StopI2C();

    return tmpC;
    
}
Still no good. I get a steady 127 (0x7F)

Last edited by AtomSoft; 13th October 2009 at 01:52 AM.
AtomSoft is offline  
Old 13th October 2009, 01:57 AM   #4
Default

Jason,

You need to make your variables signed.

Code:
signed int ReadLM75BD(void){
signed int tmp;
char result;

    i2c_start(); //Send Start
    i2c_write(RAdd); //Send our Device Address with the Write BIT

    result = i2c_ack2(); //Test for ACK

    tmp = i2c_read(); //Read 1 Byte from device and save in tmp
    result = i2c_ack2(); //Test for ACK

    tmp = tmp << 8;
    tmp |= i2c_read(); //Read 1 Byte from device and save in tmp

    result = i2c_ack(); //Test for NACK
    i2c_stop(); //Send Stop
    return (tmp>>7);
}
The function will then return a signed value that is twice the actual temperature. To display it you would do,
Code:
signed int temp;        //must be signed
    temp=ReadLM75BD();  //get temperature
    if(temp<0){         //if negative
        PutLCD('-');    //put minus sign
        temp=-temp;     //and negate temperature
    }
    else{               //else
        PutLCD(' ');    //put a space
    }
    PutNumber(temp/2);  //put the 3 digit number
    PutLCD('.')         //put decimal point
    if(temp&1)          //if there was a half degree
        PutLCD('5');    //display it
    else
        PutLCD('0');    //or dont
You could do it all unsigned but it makes more sense signed. If you prefer unsigned let me know.

Mike.
Pommie is offline  
Old 13th October 2009, 02:04 AM   #5
Default

If you have read a single byte as in your last code you may have hung the I2C bus. Power your circuit down and try again.

Mike.
Pommie is offline  
Old 13th October 2009, 02:22 AM   #6
Default

thanks guys BUT.... no luck... after a power off (complete no power) then power on and code alterations i have this code
Code:
	while(1){

    LCD_LINE(2);
    LCD_STR((unsigned rom char*)"Temp: ");

    temp=ReadLM75BD();      //get temperature
    if(temp<0){             //if negative
        LCD_DATA('-',1);    //put minus sign
        temp=-temp;         //and negate temperature
    } else {                //else
        LCD_DATA(' ',1);    //put a space
    }

    tH = ((temp/2) / 100) + 0x30;
    tT = (((temp/2) % 100) / 10) + 0x30;
    tO = ((temp/2) % 10) + 0x30;

    LCD_DATA(tH,1);
    LCD_DATA(tT,1);
    LCD_DATA(tO,1);

    LCD_DATA('.',1);        //put decimal point
    if(temp&1)              //if there was a half degree
         LCD_DATA('5',1);   //display it
    else
         LCD_DATA('0',1);   //or dont

	}
MY LCD shows 127.5
AtomSoft is offline  
Old 13th October 2009, 02:54 AM   #7
Default

The LM75 is returning 0x00ff and so I guess it's not working. Can you go back to the code that returned 25C?

Mike.
Pommie is offline  
Old 13th October 2009, 03:12 AM   #8
Default

right now it shows 127c only and dosnt chang but im using c18 i2c maybe i should return to my bit bang method
AtomSoft is offline  
Old 13th October 2009, 04:13 AM   #9
Default

hey guys funny stuff. Im using SOIC to DIP and when i disconnect the ground i get 27.5C
AtomSoft is offline  
Old 13th October 2009, 04:18 AM   #10
Default

Code:
while(1){

    LCD_LINE(2);
    LCD_STR((unsigned rom char*)"Temp: ");

    temp = 0;
    temp=ReadLM75BD();      //get temperature
    if(temp<0){             //if negative
        LCD_DATA('-',1);    //put minus sign
        temp=-temp;         //and negate temperature
    } else {                //else
        LCD_DATA(' ',1);    //put a space
    }

    temp = 1.8*temp+32;

    tH = ((temp/2) / 100) + 0x30;
    tT = (((temp/2) % 100) / 10) + 0x30;
    tO = ((temp/2) % 10) + 0x30;

    LCD_DATA(tH,1);
    LCD_DATA(tT,1);
    LCD_DATA(tO,1);

    LCD_DATA('.',1);        //put decimal point
    if(temp&1)              //if there was a half degree
         LCD_DATA('5',1);   //display it
    else
         LCD_DATA('0',1);    //or dont

     LCD_DATA('F',1);    //or dont

    DelayMS(250);
    DelayMS(250);
    DelayMS(250);
    DelayMS(250);
}
The above shows me the degrees in fahrenheit . Which seems to be correct heh.
AtomSoft is offline  
Old 13th October 2009, 04:19 AM   #11
Default

I can't really follow what you are doing. I have to assume the I2C functions are doing more than just a basic write and read. This is my function for returning the temp bytes from the LM75 using bit banged code on the MSP430. I'll just post the read function and maybe you can figure out what it's doing differently.

Code:
unsigned int get_temp( void)
{
	unsigned char buf[2];
	volatile unsigned int temp;

	i2cm_start( );
	
	if( i2cm_out( 0x90 | device ) ) //send write control byte
		return 9999;
	
	else if( i2cm_out( 0x00 ) )//send config command byte
		return 9999;
	
	i2cm_start( );//device wants start again to read
	
	if( i2cm_out( 0x91 | device ) ) //send read control byte
		return 9999;
	
	i2cm_in( buf, 2 ); //input the config byte to 'buf'

	i2cm_stop( );
	//device was not busy or failed...
	//set sucsess flag if needed before return

	temp = buf[0] << 8 | buf[1];
	temp >>= 5;
	return temp;
}
__________________
Mark Higgins
DirtyLude is online now  
Old 13th October 2009, 04:21 AM   #12
Default

I guess I sat on my message too long. Is it possibly the device address? If you have the address select pins grounded, that might be affected by taking away ground.

EDIT: Are you sure its correct? 27.5 Celsius is really pretty hot for indoors.
__________________
Mark Higgins

Last edited by DirtyLude; 13th October 2009 at 04:24 AM.
DirtyLude is online now  
Old 13th October 2009, 04:31 AM   #13
Default

64.5F in my room it says right now. I guess i need a digital temp sensor to compare with heh
AtomSoft is offline  
Old 13th October 2009, 05:06 AM   #14
Default

This line,
Code:
    temp = 1.8*temp+32;
needs to be,
Code:
    temp = 1.8*temp+64;
As the temperature is times 2. But this would make your room temperature around 80F so I suspect something is still wrong.

A better way would be,
Code:
    temp*=9;
    temp/=5;
    temp+=64;
Using 1.8 may make the compiler use floating point!!

Mike.
Pommie is offline  
Old 13th October 2009, 12:27 PM   #15
Default

Every where i look its 32 not 64

Temperature Conversion from Celsius to Fahrenheit
AtomSoft is offline  
Reply

Tags
lm75

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
interfacing Atmel Microcontroller AT89C51 with LM75 umar abdul sattar Micro Controllers 0 28th June 2003 06:27 AM



All times are GMT. The time now is 01:18 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker