LATBbits.LATBx = 0; //where LATBx = the SDAx pin
LATBbits.LATBx = 1;
This must be done AFTER the module is enabled, and your TRISB must be set such that the SDAx pin is configured as an output
Que tal AKENAFAB?No me sirve :cry:,ya probe eso y las demás propuestas sin resultado.
En el link del foro de microchip que pusiste recomiendan que hagas esto:Código: C
LATBbits.LATBx = 0; //where LATBx = the SDAx pin LATBbits.LATBx = 1; This must be done AFTER the module is enabled, and your TRISB must be set such that the SDAx pin is configured as an output
TRISBbits.TRISB9=1;
TRISBbits.TRISB8=1; LATA=0; // Latch port
LATBbits.LATB8=1;
LATBbits.LATB9=1;
TRISA=0;
//TRISB=0; //
TRISBbits.TRISB9=0;
TRISBbits.TRISB8=0;Yo en mi codigo no toco siquiera los tris. Y aun asi el I2C1 no funciona (como dije, comienza el start pero no termina, quedando el LSB de I2C1CON = 1)
Aun asi, volvere a verificar que no toco nignun tris.
Por otra parte, parece que el I2C2 si funciona
/*
* File: eeprom.c
* Author: danielfiguerola
*
* Created on 24 / agost / 2015, 21:58
*/
#include <stdio.h>
#include <stdlib.h>
#include <p24FJ64GB002.h>
#define FCY 4000000UL // Definir FCY (meitat de freqüencia del //rellotge (necessari per utilizar llibreria libpic per delays)
#include <libpic30.h> // Llibreria amb funcions de retard
#include "lcd_control.c" // Funcions control LCD
/*
*
*/
#define eeprom_i2c_w 0xA1
#define eeprom_i2c_r 0xA0
/////// Paraules de configuració/////////
_CONFIG1 (JTAGEN_OFF & FWDTEN_OFF); //Mòdul JTAG deshabilitat (RB5-RB9 i/o digitals) i WDT desactivat
_CONFIG2 (POSCMOD_NONE & OSCIOFNC_ON & FNOSC_FRC); //Oscil·lador primari desactivat, RA3 i/o digital, Oscil·lador FRC seleccionat
_CONFIG3 (SOSCSEL_IO); //RA4 i RB4 i/o digitals
//_CONFIG4 (RTCOSC_LPRC); //Es selecciona el LPRC intern com a rellotge de referència del mòdul RTCC
//// FUNCIONS/////
void i2c_stop()
{
I2C2CONbits.PEN = 1;
while (I2C2CONbits.PEN); //Espera que finalitzi condició de start
I2C2CONbits.RCEN = 0;
I2C2STATbits.IWCOL = 0;
I2C2STATbits.BCL = 0;
_MI2C2IF = 0;
//DelayuSec(10);
}
void inicialitzar_I2C()
{
unsigned char a;
I2C2BRG = 9; //A 8MHZ correspon a 100kHz
I2C2CONbits.I2CEN = 0; // Desactiva el modul
I2C2CONbits.DISSLW = 1; // Desactiva el slew rate control
_MI2C2IF = 0;
I2C2CONbits.I2CEN = 1; // Actibva el modul i2c
a = I2C2RCV; //Buidar el registre
i2c_stop();
}
void i2c_start()
{
I2C2CONbits.ACKDT = 0;
I2C2CONbits.SEN = 1; //Activa condició de start
while (I2C2CONbits.SEN); //Espera que finalitzi condició de start
__delay_us(2);
}
void i2c_restart()
{
I2C2CONbits.RSEN = 1;
while (I2C2CONbits.RSEN);
}
unsigned char escriure_byte_I2C(unsigned char dada8)
{
while (I2C1STATbits.TBF);
_MI2C2IF = 0; //Esborra bandera d'interrupció
I2C2TRN = dada8; //Automàticament TBF = 1
while (I2C2STATbits.TBF); //Espera que finalitzi transmissió i rebre ACK
while (I2C2STATbits.TRSTAT);
// _MI2C2IF = 0; //Esborra bandera d'interrupció
// {
// return (0);
// }
//else
// return (1);
if (I2C2STATbits.ACKSTAT == 1)
{
return(1);
}
return(0);
}
unsigned char rebre_byte_I2C ()
{
unsigned char dada=0;
_MI2C2IF = 0; //Esborra bandera d'interrupció
I2C2CONbits.RCEN = 1;
while (I2C2STATbits.RBF); //Espera que finalitzi transmissió
_MI2C2IF = 0; //Esborra bandera d'interrupció
dada = I2C2RCV;
return (dada);
}
void generar_ACK()
{
I2C2CONbits.ACKDT = 0;
I2C2CONbits.ACKEN = 1;
while (I2C2CONbits.ACKEN);
// while (_MI2C2IF !=1); //Espera que finalitzi transmissió
// _MI2C2IF = 0; //Esborra bandera d'interrupció
}
void generar_NACK()
{
I2C2CONbits.ACKDT = 1;
I2C2CONbits.ACKEN = 1;
//while (I2C2CONbits.ACKEN);
// while (_MI2C2IF !=1); //Espera que finalitzi transmissió
// _MI2C2IF = 0; //Esborra bandera d'interrupció
}
unsigned char escriure_word_i2c(unsigned int dada16)
{
unsigned char dadaL, dadaH, fet;
dadaL = (unsigned char) dada16&0x00FF;
dadaH = (unsigned char) dada16>>8;
fet = escriure_byte_I2C(dadaH);
if (fet == 0)
{
fet = escriure_byte_I2C(dadaL);
}
else
fet = 1;
return (fet);
}
unsigned int rebre_word_i2c()
{
unsigned int dada;
unsigned char dadaL, dadaH;
dadaH = rebre_byte_I2C ();
generar_ACK();
dadaL = rebre_byte_I2C ();
generar_ACK();
dada = dadaH <<8;
dada = dada || dadaL;
return (dada);
}
unsigned int rebre_ultim_word_i2c()
{
unsigned int dada;
unsigned char dadaL, dadaH;
dadaH = rebre_byte_I2C ();
generar_ACK();
dadaL = rebre_byte_I2C ();
generar_NACK();
dada = dadaH <<8;
dada = dada || dadaL;
return (dada);
}
int main()
{
unsigned char a=0, b=0, error=0;
unsigned char buf[5];
// AD1PCFG = 0xffff; //Tots els ports digitals menys AN10 (RB14) analògic
// TRISB = 0xE018; //Com a entrada: RB2 (DS18B20), RB3 (TSL235R), RB4 (polsador)
//RB13 (pluviòmetre), RB14 (HIH-4010) i RB15 (anemòmetre)
//Com a sortida: RB0 (activa reg), RB5, RB7 (RS i E de LCD) i RB8, RB9, RB10
//i RB11 (dades LCD)
_TRISB5 = 0;
_TRISB7 = 0;
_TRISB8 = 0;
_TRISB9 = 0;
_TRISB10 = 0;
_TRISB11 = 0;
ini_LCD_4bits(); //Inicialitza LCD en mode 4 bits
__delay_ms(1000); //Espera 1 segon
escriure_txt("Hola");
__delay_ms(2000);
a = 32;
b = 20;
inicialitzar_I2C();
LATBbits.LATB2 = 0; //where LATBx = the SDAx pin
LATBbits.LATB2 = 1;
i2c_start();
error = escriure_byte_I2C(eeprom_i2c_w);
error = escriure_byte_I2C(0x00);
error = escriure_byte_I2C(0x00);
error = escriure_byte_I2C(32);
if (error == 1)
{
escriure_txt("error");
__delay_ms(2000);
}
i2c_stop();
i2c_start();
error = escriure_byte_I2C(eeprom_i2c_w);
error = escriure_byte_I2C(0x00);
error = escriure_byte_I2C(0x00);
__delay_us(10);
if (error == 1)
{
escriure_txt("error");
__delay_ms(2000);
}
i2c_restart();
error = escriure_byte_I2C(eeprom_i2c_r);
a = rebre_byte_I2C ();
if (error == 1)
{
escriure_txt("error");
__delay_ms(2000);
}
generar_NACK();
escriure_cmd (0x01); //Borrar LCD
escriure_cmd(0x02);
sprintf (buf, "%d", a); //la humitat màxima
escriure_txt(buf);//
__delay_ms(3000);
while(1);
}