#include<p18f452.h>
#include <my_i2c.h>
#pragma config LVP = OFF
#pragma config STVR = OFF
#pragma config WDT = OFF
#pragma config OSC = HS
void main(void)
{
char c;
PORTA = PORTB = PORTC = PORTD = PORTE = 0x00;
TRISA = 0;
TRISB = 0;
TRISD = 0;
TRISC = 0x18;
OpenI2C(MASTER, SLEW_OFF);
SSPADD = 19;
comienzaI2C();
PrepareWWord_I2C(0xa0,0x00,0x00);
for (c=0x01;c<=0x20;c++)
{
WWord_I2C(c);
}
FinishWWord_I2C();
AckPolling(0xa0);
comienzaI2C();
PrepareWWord_I2C(0xa0,0x00,0x20);
for (c=0x01;c<=0x20;c++)
{
WWord_I2C(c);
}
FinishWWord_I2C();
retardo_1ms(0xfe);
AckPolling(0xa0);
PrepareWWord_I2C(0xa0,0x00,0x20);
for (c=0x02;c<=0x40;c=c+0x02)
{
WWord_I2C(c);
}
FinishWWord_I2C();
retardo_1ms(0xfe);
AckPolling(0xa0);
c = 0x01;
comienzaI2C();
PrepareRWord_I2C(0xa0,0x00,0x00);
for (c=0x00;c<=0x3f;c++)
{
PORTB = RWord_I2C();
retardo_1ms(0x80);
}
FinishRWord_I2C();
IdleI2C();
PORTAbits.RA0 = 1;
while(1)
{
}
}
Este es el main, y estas las funciones como header file:
#include <funciones_i2c.h>
#include <my_Delays.h>
/**************************************
* *
* Escribe I2C Byte *
* *
**************************************/
unsigned char dato[7];
unsigned char escribeI2C(unsigned char dir_slv,
unsigned char dir_low,
unsigned char dir_high,
unsigned char dato )
{
WriteI2C(dir_slv);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_high);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_low);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dato);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
IdleI2C();
StopI2C();
while ( SSPCON2bits.PEN ); // wait until stop condition is over
return (0);
}
/**************************************
* *
* Lee I2C Random *
* *
**************************************/
unsigned char leeI2C( unsigned char dir_slv,
unsigned char dir_high,
unsigned char dir_low)
{
unsigned char read;
WriteI2C(dir_slv);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_high);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_low);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
RestartI2C();
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_slv+0x01);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
IdleI2C();
read = ReadI2C();
NotAckI2C();
while ( SSPCON2bits.ACKEN ); // wait until ACK sequence is over
StopI2C();
while ( SSPCON2bits.PEN ); // wait until stop condition is over
return (read);
}
/**************************************
* *
* Comienza I2C *
* *
**************************************/
void comienzaI2C(void)
{
IdleI2C();
StartI2C();
while ( SSPCON2bits.SEN ); // wait until start condition is over
}
/**************************************
* *
* Detiene I2C *
* *
**************************************/
void detenI2C(void)
{
IdleI2C();
StopI2C();
while ( SSPCON2bits.PEN ); // wait until stop condition is over
}
/***********************************************************************
************************************************************************
*
* Escribe I2C Word
* *********************************************************************
/***********************************************************************/
void PrepareWWord_I2C ( unsigned char dir_slv,
unsigned char dir_low,
unsigned char dir_high )
{
WriteI2C(dir_slv);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_high);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_low);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
IdleI2C();
}
void WWord_I2C(unsigned char Word)
{
WriteI2C(Word);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
IdleI2C();
}
void FinishWWord_I2C(void)
{
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
IdleI2C();
StopI2C();
while ( SSPCON2bits.PEN ); // wait until stop condition is over
}
/***********************************************************************
************************************************************************
* *
* Lee I2C Word *
* *
************************************************************************
/***********************************************************************/
unsigned char PrepareRWord_I2C( unsigned char dir_slv,
unsigned char dir_high,
unsigned char dir_low)
{
WriteI2C(dir_slv);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_high);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_low);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
RestartI2C();
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
WriteI2C(dir_slv+0x01);
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
IdleI2C();
return (0);
}
unsigned char RWord_I2C(void)
{
ReadI2C();
AckI2C();
return (SSPBUF);
}
void FinishRWord_I2C()
{
NotAckI2C();
while ( SSPCON2bits.ACKEN ); // wait until ACK sequence is over
StopI2C();
while ( SSPCON2bits.PEN ); // wait until stop condition is over
}
/********************************************************************
********************************************************************/
unsigned char AckPolling( unsigned char control )
{
IdleI2C(); // ensure module is idle
StartI2C(); // initiate START condition
while ( SSPCON2bits.SEN ); // wait until start condition is over
WriteI2C( control ); // write byte - R/W bit should be 0
IdleI2C(); // ensure module is idle
while ( SSPCON2bits.ACKSTAT ) // test for ACK condition received
{
RestartI2C(); // initiate Restart condition
while ( SSPCON2bits.RSEN ); // wait until re-start condition is over
WriteI2C( control ); // write byte - R/W bit should be 0
IdleI2C(); // ensure module is idle
}
StopI2C(); // send STOP condition
while ( SSPCON2bits.PEN ); // wait until stop condition is over
return ( 0 ); // return with no error
}