Hello,
I am trying to read the temperature from LM75 Thermometer, I use KEIL MCB4357 Development Board.The program is :
#include <br> #include "LED.h" #include "KBD.h" #include "GLCD.h" #include "TH_LM75.h" #include "I2C.h"
#define KEY_P4_0 1 #define KEY_P4_3 2 #define KEY_P4_4 4 #define KEY_WAKEUP 8
#define Font1 1 #define Font0 0
#define TH_I2C_ADDR 0x48 #define REG_TEMP 0x00 #define REG_CONF 0x01 #define REG_THYS 0x02 #define REG_TOS 0x03
int main (void) {
LED_Init(); KBD_Init (); GLCD_Init(); TH_Init(); I2C_Init();
LED_Val(0x55);
GLCD_Clear (White); GLCD_SetBackColor (Blue); GLCD_SetTextColor (White); GLCD_DisplayString (0, 0, 1, " IUT CACHAN GEII2 "); GLCD_DisplayString (1, 0, 1, " Projet Quadricoptère "); GLCD_SetBackColor (White); GLCD_SetTextColor (Blue);
while (1) { if (KBD_GetKeys() & KEY_P4_0) { LED_On(1); GLCD_SetTextColor(Black); } else { LED_Off(1); GLCD_SetTextColor(LightGrey); } GLCD_DisplayString (4, 9, 1, "P4_0");
if (KBD_GetKeys() & KEY_P4_3) { LED_On(2); GLCD_SetTextColor(Black); } else { LED_Off(2); GLCD_SetTextColor(LightGrey); } GLCD_DisplayString (4, 14, 1, "P4_3");
if (KBD_GetKeys() & KEY_P4_4) { LED_On(3); GLCD_SetTextColor(Black); } else { LED_Off(3); GLCD_SetTextColor(LightGrey); } GLCD_DisplayString (5, 9, 1, "P4_4");
if (KBD_GetKeys() & KEY_WAKEUP) LED_On(4); else LED_Off(4);
TH_GetTemp(&Temp); } }
And finally i try to call this function but i don't know can i call, and anyone to help me to activate I2C for reading temperature :
uint16_t TH_GetTemp (TH_DATA *ThDat) { uint8_t val[2];
if (I2C_RdData (TH_I2C_ADDR, REG_TEMP, val, 2) == 0) { ThDat->Temp = (val[0] << 8) | (val[1] & 0x80) ; return (0); } return (1); }
#include #include "LED.h" #include "KBD.h" #include "GLCD.h" #include "TH_LM75.h" #include "I2C.h" #define KEY_P4_0 1 #define KEY_P4_3 2 #define KEY_P4_4 4 #define KEY_WAKEUP 8 #define Font1 1 #define Font0 0 #define TH_I2C_ADDR 0x48 #define REG_TEMP 0x00 #define REG_CONF 0x01 #define REG_THYS 0x02 #define REG_TOS 0x03 int main (void) { LED_Init(); KBD_Init (); GLCD_Init(); TH_Init(); I2C_Init(); LED_Val(0x55); GLCD_Clear (White); GLCD_SetBackColor (Blue); GLCD_SetTextColor (White); GLCD_DisplayString (0, 0, 1, " Hello "); GLCD_DisplayString (1, 0, 1, " Good Morning "); GLCD_SetBackColor (White); GLCD_SetTextColor (Blue); while (1) { if (KBD_GetKeys() & KEY_P4_0) { LED_On(1); GLCD_SetTextColor(Black); } else { LED_Off(1); GLCD_SetTextColor(LightGrey); } GLCD_DisplayString (4, 9, 1, "P4_0"); if (KBD_GetKeys() & KEY_P4_3) { LED_On(2); GLCD_SetTextColor(Black); } else { LED_Off(2); GLCD_SetTextColor(LightGrey); } GLCD_DisplayString (4, 14, 1, "P4_3"); if (KBD_GetKeys() & KEY_P4_4) { LED_On(3); GLCD_SetTextColor(Black); } else { LED_Off(3); GLCD_SetTextColor(LightGrey); } GLCD_DisplayString (5, 9, 1, "P4_4"); if (KBD_GetKeys() & KEY_WAKEUP) LED_On(4); else LED_Off(4); TH_GetTemp(&Temp); } }
uint16_t TH_GetTemp (TH_DATA *ThDat) { uint8_t val[2]; if (I2C_RdData (TH_I2C_ADDR, REG_TEMP, val, 2) == 0) { ThDat->Temp = (val[0] << 8) | (val[1] & 0x80) ; return (0); } return (1);
You don't seem to be a very caring person. Or why did you make a second post with totally failed formatting? Didn't you see some issues on the preview?
By the way: What is the intended result of the following line?
ThDat->Temp = (val[0] << 8) | (val[1] & 0x80) ;
?
Don't you find it meaningful to make use of some form of source code comments to inform a reader that the upper byte will store a temperature in degrees Celsius and the lower byte will store a fractional temperature where the most significant bit represents 0.5°C and the other bits are always zero?
And you seem to have questions about the code but you don't manage to actually write any real question. And you don't seem able to tell if you have written the code or where you have copied it from. You also make use of header files, but doesn't allow us to know the actual contents of these header files.
You call TH_GetTemp() but doesn't seem to care about the return value.
You make use of a variable named Temp, but doesn't show us any declaration of this variable. And we aren't supposed to know the data type of TH_DATA.
What issues did you have with reading the processor documentation about the I2C interface, or looking at the sample code available for the processor? Or were you just as caring about that part of your task, i.e. you just "forgot" to read it?
So - how much time do you have left before it's time to hand in this school project?