I2C and pic18FX550

General discussion on mikroC.
Post Reply
Author
Message
Compaq
Posts: 38
Joined: 27 Jul 2005 19:38
Location: Slovenia
Contact:

I2C and pic18FX550

#1 Post by Compaq » 21 Sep 2005 20:31

Helo!

I want to connect eeprom 24C512 with Pic18F2550.
If I use pic16F876 works fine, but on 18F.... :( .
All test on EasyPic3.
SDA....RB0, SCL....RB1
PortB--->Pull Up
XT=4MHz

Code: Select all

void main(){

unsigned int addr=0;
char i;
do{

porta=0;
trisa=0;
adcon1=0x0f;
cmcon=0x07;
portb=0;
trisb=0;
lata =0;
latb =0;
I2C_Init(100000);
    for (addr=0;addr<4;addr++)
     {
    I2C_Start();
    I2C_Wr(0xA2);
    I2C_Wr(hi(addr));
    I2C_Wr(lo(addr));
    I2C_Wr(addr);
    I2C_Stop();
    delay_ms(20);
    }

    delay_ms(2000);
  for (addr=0;addr<4;addr++)
  {
  I2C_Start() ;
  I2C_Wr(0xA2) ;
  I2C_Wr(hi(addr));
  I2C_Wr(lo(addr));
  I2C_Repeated_Start();
  I2C_Wr(0xA3);
  i = I2C_Rd(0);
  porta = i;
  I2C_Stop() ;
  delay_ms(1000);
    }
}while(1);
}
Happening on PORTA: 3 second 0010011 then fot the moment 0x00...
So three times the same value and false.

I don't know what i'm doing wrong :? .

Thank's, Jan

Compaq
Posts: 38
Joined: 27 Jul 2005 19:38
Location: Slovenia
Contact:

#2 Post by Compaq » 21 Sep 2005 23:00

Now I write my own I2C library and works fine.
Frequency=48MHz :D

Jan

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#3 Post by zristic » 22 Sep 2005 08:05

Can we see that library and compare it to ours?

Compaq
Posts: 38
Joined: 27 Jul 2005 19:38
Location: Slovenia
Contact:

#4 Post by Compaq » 23 Sep 2005 07:26

The names of functions are in Slovenian language, but I added some comments :) .
EEprom must be bigger then 2K, because addres is 16Bit. All three chip-select pin connect to GND.

Code: Select all

void I2CInit(void)              //Initializes I2C
{
	TRISB=TRISB | 0x03;
	SSPSTAT=0x80;
	SSPADD=0x0A;
	SSPCON2=0;
	SSPCON1=0x28;
	PIR1.F3=0;
        PIR2.F3=0;
}

void I2C_Koncaj(void)          //Issues STOP signal
{
        PIR1.F3=0;
	SSPCON2.F2=1;
	while (PIR1.f3==0){}
}

void I2C_Zacni(void)           //Issues START signal
{
        PIR1.F3=0;
        SSPCON2.F0=1;
	while (PIR1.F3==0){}
}

void I2C_Obnovi(void)          //Issues repeated START signal
{
        PIR1.F3=0;
        SSPCON2.F1=1;
	while (PIR1.F3==0){}
}


void I2C_ZapisiPodatek(char hex)    //Write data
{
        PIR1.F3=0;
	SSPBUF=hex;
	while (PIR1.F3==0){}
}

char I2C_PreberiPodatek(void)       //Read data
{
	char _podatek;
        PIR1.F3=0;
        SSPCON2.F3=1;
	while (PIR1.F3==0){}
	_podatek=SSPBUF;
        PIR1.F3=0;
        SSPCON2.F4=1;
	while (PIR1.F3==0){}
	return _podatek;
}

void I2C_ZacniPosiljanje(unsigned int address)      // Start send data
{
	I2C_Zacni();
	I2C_ZapisiPodatek(0xA0);
	I2C_ZapisiPodatek(Hi(address));
	I2C_ZapisiPodatek(Lo(address));
}

void I2C_ZacniBranje(unsigned int address)          // Start read data
{
	I2C_Zacni();
	I2C_ZapisiPodatek(0xA0);
	I2C_ZapisiPodatek(Hi(address));
	I2C_ZapisiPodatek(Lo(address));
	I2C_Obnovi();
	I2C_ZapisiPodatek(0xA1);
        SSPCON2.F5=1;
}
//****************************************************************************//
//**********************Main function for I2C*********************************//
void I2C_Zapisi(unsigned int addr, unsigned char pod)   //Write
{
        I2C_ZacniPosiljanje(addr);
        I2C_ZapisiPodatek(pod);
        I2C_Koncaj();
        Delay_ms(5);
}

unsigned char I2C_Preberi(unsigned int addr)            //Read
{
        unsigned char podatek;
        I2C_ZacniBranje(addr);
        podatek=I2C_PreberiPodatek();
        I2C_Koncaj();
        return podatek;
}

Simple example:

Code: Select all


/***************************************
Project name: Test I2C

Done by: Jan & Jure Medvesek, 23.09.2005

Description:
Write some data to EEPROM 24FC512,
then reads it and displays it on PORTC.
A0,A1,A2--->connect to GND

Test configuration:
     MCU:             P18F2550
     Dev.Board:       EasyPIC3
     Oscillator:      XT, 48.0000MHz
     Modules:         EEPROM (24C512)
     SW:              mikroC v2.1

**************************************/

void main()
{
 unsigned int addr=0;
 unsigned char data=55;
 
     ADCON1 = 0x0c;       // Set pins as digital I/O
     trisc = 0x00;        // Set portc as outputs
     trisb = 0x00;        // Set portb as outputs
     portb = 0;
     latb = 0;
     latc = 0;
     I2CInit();           // I2C init
     Delay_ms(1000);
     I2C_Zapisi(addr,data);   // Write data(55) to address(0)
     data = 0;
     data = I2C_Preberi(addr);   // Read data(55) from address(0)
     POTRC = data;
}
Jan

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#5 Post by zristic » 23 Sep 2005 08:34

Thank you very much indeed. We will analyze this solution of yours and will let you know of the results. Good work... :D

Compaq
Posts: 38
Joined: 27 Jul 2005 19:38
Location: Slovenia
Contact:

#6 Post by Compaq » 23 Sep 2005 12:58

Ok, thank's. Now I'm going to test sequential read. I hope that it will work.

Jan

pratik345
Posts: 15
Joined: 18 Mar 2010 04:24

Re: I2C and pic18FX550

#7 Post by pratik345 » 14 May 2010 04:52

try with i2c_int with 400000

pratik345
Posts: 15
Joined: 18 Mar 2010 04:24

Re: I2C and pic18FX550

#8 Post by pratik345 » 16 May 2010 11:03

but how to define hi and lo function for it....

Post Reply

Return to “mikroC General”