|
|
View previous topic :: View next topic |
Author |
Message |
freddayz
Joined: 05 Oct 2009 Posts: 9
|
I2C Problem with MAX6642 sensor and 16F917 |
Posted: Thu Oct 08, 2009 10:13 am |
|
|
I am using PIC16F917 with compiler 4.071
I am talking/getting data from MAX6642 Temp Sensor via I2C, I am the Master and Temp Sensor is slave.
I cannot communicate with the device using the following code. What can my problem be?
This is my include stuff:
Code: | // Include files
#include "16F917.h"
#device adc=10
#include "LBA_App_Main.h"
// PIC Configuration
#use delay ( clock = 8MHZ, type = internal )
//#use rs232 ( baud=38400, xmit=PIN_B7, rcv=PIN_B5, ERRORS )
#fuses HS // Crystal Osc (> 4 MHz)
#fuses NOWDT // No watch Dog Timer
#fuses NOPROTECT // Code not protected from reading
#fuses NOPUT // No Power Up Timer
#fuses NOBROWNOUT // No Brownout Reset
#fuses NOIESO // Internal External Switch Over mode disabled
#fuses FCMEN // Fail-safe clock monitor enabled,
#fuses NOMCLR // Master Clear pin used for I/O
//#fuses INTRC_IO
#use i2c ( MASTER, sda = SDA_PIN, scl = SCL_PIN) // cfg device as master |
Header stuff:
Code: | // I2C variables
#define MAX6642_I2C_WRITE 0x90 // Address is 1001 000 , the write bit is 0 per I2C standard
#define MAX6642_I2C_READ 0x91 // Address is 1001 000 , the write bit is 1 per I2C standard
#define MAX6642_LOC_TEMP 0x00 // Read local Temperature
#define MAX6642_REM_TEMP 0x01 // Read remote temperature
#define MAX6642_STATUS 0x02 // Read Status Byte
#define MAX6642_CFG_BYTE 0x03 // Read CFG Byte
#define MAX6642_READ_LOC_HL 0x05 // Read Local High Limit
#define MAX6642_READ_REM_HL 0x07 // Read Remote High Limit
#define MAX6642_WRITE_CFG 0x09 // Write CFG Byte
#define MAX6642_WRITE_LOC_HL 0x0B // Write Local High Limit
#define MAX6642_WRITE_REM_HL 0x0D // Write Remote High Limit
#define MAX6642_SINGLE_SHOT 0x0F // Single Shot
#define MAX6642_LOC_TEMP2 0x11 // Read local extended temperature
#define MAX6642_REM_TEMP2 0x10 // Read remote extended temperature
#define MAX6642_DEVID 0xfe // Read MFG ID
#define MAX6642_TEMP_INVALID 0xff // Invalide Temperature |
PIC Init:
Code: | void Process_Initialization ( void )
{
// PIC Port Initialization
setup_lcd(LCD_DISABLED);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
Disable_Interrupts(INT_CCP1);
Disable_Interrupts(INT_CCP2);
Disable_Interrupts(INT_COMP);
Disable_Interrupts(INT_COMP2);
setup_comparator(NC_NC_NC_NC);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
// I2C Init
output_high(SCL_PIN);
output_high(SDA_PIN);
delay_us(1000); // Wait
} |
Temp Sensor CFG:
Code: | void TempSensor_Start(void)
{
// Set Configuration for Temp Sensor
i2c_start();
i2c_write(MAX6642_I2C_WRITE); // Write to Temp Sensor
i2c_write(MAX6642_WRITE_CFG); // Write to CFG Register
i2c_write(0x00); // 0(7) 0(6) 0(5) 0(4) 0(3) 0(2) 0(1) 0(0)
// Enables Alerts, Run Mode, Reads both temps, single fault
i2c_stop();
// // Set Limits
// i2c_start();
// i2c_write(MAX6642_I2C_WRITE); // Write to Temp Sensor
// i2c_write(WRITE_REM_HL); // Write to CFG Register
// i2c_write(0x00); // 0(7) 0(6) 0(5) 0(4) 0(3) 0(2) 0(1) 0(0)
// // Enables Alerts, Run Mode, Reads both temps, single fault
// i2c_stop();
} |
Get Data Function:
Code: | void I2C_Messaging(void )
{
m_Rem_Temp = 0;
m_Loc_Temp = 0;
i2c_start();
i2c_write(MAX6642_I2C_WRITE); // insert write address for MAX6642
i2c_write(MAX6642_REM_TEMP); // Pointer Byte
i2c_start(); // Restart
i2c_write(MAX6642_I2C_READ); // Read Address for MAX6642
Rem_Temp = i2c_read(); // Read from Slave and insert into REM_TEMP
i2c_start();
i2c_write(MAX6642_I2C_WRITE); // insert write address for MAX6642
i2c_write(MAX6642_LOC_TEMP); // Pointer Byte
i2c_start(); // Restart
i2c_write(MAX6642_I2C_READ); // Read Address for MAX6642
Loc_Temp = i2c_read(); // Read from Slave and insert into REM_TEMP
i2c_stop();
/* if (Loc_Temp > 0x64)
{
m_Loc_Temp = 1;
}
if (Rem_Temp > 0x64)
{
m_Rem_Temp = 1;
}*/
} |
Main
Code: | void main ()
{
m_Rem_Temp = 0;
m_Loc_Temp = 0;
Process_Initialization();
if(Power_On_Good) //always = 1
{
m_PWR_GOOD = 1;
TempSensor_Start(); //Enable communication on I2C to Temp Sensor
}
do
{
//do processes here
I2C_Messaging();
} while (1);
} |
What can my problem be?
Thanks in Advance |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 08, 2009 12:52 pm |
|
|
You are trying to make a driver for the MAX6642.
Quote: |
void Process_Initialization ( void )
{
// PIC Port Initialization
setup_lcd(LCD_DISABLED);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
Disable_Interrupts(INT_CCP1);
Disable_Interrupts(INT_CCP2);
Disable_Interrupts(INT_COMP);
Disable_Interrupts(INT_COMP2);
setup_comparator(NC_NC_NC_NC);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
// I2C Init
output_high(SCL_PIN);
output_high(SDA_PIN);
delay_us(1000); // Wait
}
|
Delete all the lines in bold. You don't need to disable the CCP modules
or the interrupts. They are already disabled when the PIC is turned on.
The hardware inside the PIC disables them upon power-on reset.
The exception is that the LCD module is enabled on power-up. CCS
inserts code to disable it, but on some early compiler versions they don't
do it. So it doesn't hurt to disable it, as you have done in your code.
Don't enable INT_SSP interrupts. They are not needed. You don't have
a #int_ssp routine. Don't enable global interrupts. Don't set the SCL
and SDA pins high. They are initialized by the #use i2c() library code.
Quote: | void I2C_Messaging(void )
{
m_Rem_Temp = 0;
m_Loc_Temp = 0;
i2c_start();
i2c_write(MAX6642_I2C_WRITE); // insert write address for MAX6642
i2c_write(MAX6642_REM_TEMP); // Pointer Byte
i2c_start(); // Restart
i2c_write(MAX6642_I2C_READ); // Read Address for MAX6642
Rem_Temp = i2c_read(0); // Last read must do a NACK
i2c_stop(); // Must do a stop here
i2c_start();
i2c_write(MAX6642_I2C_WRITE); // insert write address for MAX6642
i2c_write(MAX6642_LOC_TEMP); // Pointer Byte
i2c_start(); // Restart
i2c_write(MAX6642_I2C_READ); // Read Address for MAX6642
Loc_Temp = i2c_read(0); // Last read must do a NACK
i2c_stop();
|
Your code to read the registers has some things wrong with it. See the
corrections shown in bold above. Note that a "NACK" is done by giving
the i2c_read() function a parameter of 0, as shown above. |
|
|
freddayz
Joined: 05 Oct 2009 Posts: 9
|
|
Posted: Thu Oct 08, 2009 1:21 pm |
|
|
Thanks alot for your help....will see if that corrects the problem. |
|
|
freddayz
Joined: 05 Oct 2009 Posts: 9
|
|
Posted: Thu Oct 15, 2009 4:34 pm |
|
|
That did correct the problem...the other issue I had was with the I2C Slave...it wasn't cfg properly |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|