Olimex Support Forum

Microcontrollers => PIC => Topic started by: Maxime Testori on March 07, 2015, 07:39:56 PM

Title: Bus I2C
Post by: Maxime Testori on March 07, 2015, 07:39:56 PM
Bonjour à tous

Je tiens à vous préciser que je ne suis pas spécialement doué en programmation et je suis une grosse merde en anglais...

J'ai besoin de vous parce que je ne sais pas utiliser le bus I2C.
Le but de mon travail est de mesurer une température, on m'impose d'utiliser PIC32-PINGUINO-MICRO qui possède un PIC32MX440F256H et pour X et Y raisons j'ai choisi comme capteur un TCN75A.
J'ai téléchargé les docs de ces composants et cherché des "cours" sur les bus I2C. Si j'ai bien compris, je dois configurer le PIC, puis le bus I2C. Ensuite il faut que je prenne le contrôle du bus et qu'après je fasse une lecture en mettant l'adresse du capteur...

S'il vous plait, dites moi si je suis à côté de la plaque et comme écrire tout ça parce que avec toute cette doc je suis totalement embrouillé...
Title: Re: Bus I2C
Post by: Lurch on March 09, 2015, 01:00:25 PM
Fortunately Google translates fairly well ...
I can't find the main procedure I used, but the MCP980x 12bit temperature chip is very similar - it even has the same hardware address as the TCN75, so these procedures might help a little as a start. Theses are for PIC32MX1xx, bu should be the same for PIC32MX4xx

/*************************************************************************
* File name : MCP9801.c
* Hardware : PIC32MX250F128
**************************************************************************
*                DESCRIPTION
*
*     MCP9801 Addr: 0x58 (<<1)
*     2,7-5,5V  12bit 400kHz
*     -----------------------
*
*      SDA  1    8  Vdd
*      SCL  2    7  A0
*    ALERT  3    6  A1
*      GND  4    5  A2
*
*  +125°C 0111 1101 0000 uuuu 7D0 2000 +125
*  +25.4375°C 0001 1001 0111 uuuu 197 407 +25.4375
*  +0.0625°C 0000 0000 0001 uuuu 001 1 +0.0625
*  0°C 0000 0000 0000 uuuu 000 0 0
*  –0.0625°C 1111 1111 1111 uuuu(2) 001(3) -1 -0.0625
*  –25.4375°C 1110 0110 1001 uuuu 197 -407 -25.4375
*  –55°C 1100 1001 0000 uuuu 370 -880 -55
*
*  CONFIG (default 9bit) =  0000 0000  set to:  0110 0000
*
*  Write CONFIG with I2C write:  ADDR  0x01  CONFIG
*
*************************************************************************/

#include "HardwareProfile.h"
#include "MCP9801.h"

/************************************************************************
* -------------------------- Local Variables ---------------------------
************************************************************************/

/*************************************************************************
* Function : BYTE i2cTemp_Init (void)
* Requires : I2C module must be configured
* Input    : None
* Output   : BYTE success = 0
* Overview : set CONFIG to 12bit
* Note     : None
*************************************************************************/
BYTE i2cTemp_Init (void)
{
StartI2C1(); // Send the Start Bit
IdleI2C1(); // Wait to complete

MasterWriteI2C1(TEMP_I2C_WADDR);
IdleI2C1(); // Wait to complete
// ACKSTAT = 0 when slave acknowledges, 1 when not acknowledged
if (I2C1STATbits.ACKSTAT)
return(1);

MasterWriteI2C1(0x01);
IdleI2C1(); // Wait to complete
// ACKSTAT = 0 when slave acknowledge2, 1 when not acknowledged
if (I2C1STATbits.ACKSTAT)
return(1);

MasterWriteI2C1(0x60);
IdleI2C1(); // Wait to complete
// ACKSTAT = 0 when slave acknowledge2, 1 when not acknowledged
if (I2C1STATbits.ACKSTAT)
return(1);

StopI2C1(); // Send the Stop condition
IdleI2C1(); // Wait to complete
}


/*************************************************************************
* Function : BYTE i2cTemp_read (WORD *temp)
* Requires : I2C module must be configured
* Input    : None
* Output   : BYTE success = 0
* Overview : set CONFIG to 12bit
* Note     : None
*************************************************************************/
BYTE i2cTemp_read (WORD *temp)
{
BYTE b1, b2;

StartI2C1(); // Send the Start Bit
IdleI2C1(); // Wait to complete

MasterWriteI2C1(TEMP_I2C_WADDR);
IdleI2C1(); // Wait to complete
// ACKSTAT = 0 when slave acknowledges, 1 when not acknowledged
if (I2C1STATbits.ACKSTAT)
return(1);

MasterWriteI2C1(0x00);
IdleI2C1(); // Wait to complete
// ACKSTAT = 0 when slave acknowledge2, 1 when not acknowledged
if (I2C1STATbits.ACKSTAT)
return(1);

// now send a start sequence again
RestartI2C1(); // Send the Restart condition
// wait for this bit to go back to zero
IdleI2C1(); // Wait to complete

// Device Address and RD Command
MasterWriteI2C1(TEMP_I2C_RADDR);
IdleI2C1(); // Wait to complete

b1 = MasterReadI2C1();
AckI2C1();
IdleI2C1(); //Wait to complete

b2 = MasterReadI2C1();
NotAckI2C1();
IdleI2C1(); //Wait to complete

StopI2C1(); // Send the Stop condition
IdleI2C1(); // Wait to complete

*temp = (WORD)b1<<8 | b2>>4;

return(0);
}


/*************************************************************************
* File name : MCP9801.h
* Hardware : PIC32MX220F032
*************************************************************************/

#ifndef _MCP9801_H_
#define _MCP9801_H_

/*************************************************************************
*                               D E F I N E S
*************************************************************************/

#define TEMP_I2C_DEV_ADDR 0x48

#define TEMP_I2C_WADDR (TEMP_I2C_DEV_ADDR << 1) | 0
#define TEMP_I2C_RADDR (TEMP_I2C_DEV_ADDR << 1) | 1

/*************************************************************************
*                          P R O T O T Y P E S
*************************************************************************/

/*************************************************************************
* Function : BYTE i2cTemp_Init (void)
* Requires : I2C module must be configured
* Input    : None
* Output   : BYTE success = 0
* Overview : set CONFIG to 12bit
* Note     : None
*************************************************************************/
BYTE i2cTemp_Init (void);

/*************************************************************************
* Function : BYTE i2cTemp_read (WORD *temp)
* Requires : I2C module must be configured
* Input    : None
* Output   : BYTE success = 0
* Overview : set CONFIG to 12bit
* Note     : None
*************************************************************************/
BYTE i2cTemp_read (WORD *temp);

#endif