PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 19, 2004 3:59 pm |
|
|
I would try a more simple test program. Here is one that I made
for the TCN75, which is a similar chip. It displays this output:
Start
Reading Temperature
Temp = 26
Temp = 28
Temp = 28
Temp = 27
Temp = 27
Temp = 28
Temp = 27
Temp = 26
Temp = 26
Temp = 27
Temp = 27
Temp = 27
Temp = 27
Temp = 27
It's not perfect, but it doesn't jump around as much as your
output does. (I have a heater running near the board. That's
why it's so warm).
Code: | #include <18F458.H>
#fuses XT, NOPROTECT, PUT, NOBROWNOUT, NOWDT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, errors)
#define TCN75_SDA PIN_C0
#define TCN75_SCL PIN_C1
#use i2c(Master, SDA=TCN75_SDA, SCL=TCN75_SCL) // Software i2c
//------------------------------------------------------------------------
// DEFINES
// TCN75 i2c addresses
#define TCN75_I2C_WRITE_ADDR 0x90
#define TCN75_I2C_READ_ADDR 0x91
// TCN75 register addresses
#define TCN75_TEMP_REG_ADDR 0
#define TCN75_CONFIG_REG_ADDR 1
#define TCN75_THYST_REG_ADDR 2
#define TCN75_TSET_REG_ADDR 3
#define TCN75_CONFIG_VALUE 0x1A // From Microchip TB052 appnote
//---------------------------------------------------------------------------
// FUNCTION PROTOTYPES
void TCN75_init(void);
char TCN75_read_temperature(void);
//=========================================
void main()
{
char result;
printf("Start\n\r");
TCN75_init();
printf("Reading Temperature\n\r");
while(1)
{
result = TCN75_read_temperature();
printf("Temp = %d \n\r", result);
delay_ms(1000);
}
}
//=======================================
void TCN75_init(void)
{
output_float(TCN75_SDA);
output_float(TCN75_SCL);
i2c_start();
i2c_write(TCN75_I2C_WRITE_ADDR);
i2c_write(TCN75_CONFIG_REG_ADDR);
i2c_write(TCN75_CONFIG_VALUE);
i2c_stop();
}
//-----------------------------------------
// Read the temperature and return the integer
// portion as a char. The "1/2 degree" portion
// of the result is discarded and not returned.
// We don't need that level of precision.
char TCN75_read_temperature(void)
{
char msb;
char lsb;
i2c_start();
i2c_write(TCN75_I2C_WRITE_ADDR);
i2c_write(TCN75_TEMP_REG_ADDR);
i2c_start();
i2c_write(TCN75_I2C_READ_ADDR);
msb = i2c_read();
lsb = i2c_read(0);
i2c_stop();
return(msb);
}
// end of program |
|
|