Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

I2C with MPLAB C18 problem

Status
Not open for further replies.

Electronic2050

New Member
hi to all friends
anybody have example code for using i2c with mplab c18
for example connecting PIC18F452 to 24c16(serial EEPROM) and useing
i2c functions in mplab c18.
i need it very very urgent.
thanks
 
There's example code in the C18 libraries reference. I can't find an official URL at the moment.

It isn't all explained very well (I'm looking at the LCD code now) but the functions are documented.
 
thanks edeca
i had this manual before and use of the i2c function but i can not
earn any answer of it.
i need a example code.
i konw how driveing LCD have you any problem with LCD, can i help you?
 
OK, so what exactly is the problem? Do you need specific examples for the serial EEPROM?

I have never used one but I would assume that it simply needs the correct commands sending, probably using putcI2C.

If you are more specific about which part is causing you trouble (starting the I2C hardware, sending commands, receiving data etc.) then you might get a little more help from somebody who knows.
 
i never seen it but will check it out. I just wrote this and it is untested as i dont own that memory... does Isis have it ?

Here is my untested code made for the "AT24C164" from Atmel.
Code:
#include <p18f452.h>
#include "i2c.h"
#pragma config WDT = OFF, LVP = OFF, OSC = HS

unsigned char buffer;
unsigned char data;

void WriteEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char data);
void ReadEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char *buff);

void main(void)
{
    data = 'A';
    OpenI2C(MASTER, SLEW_ON);// Initialize I2C module
                             //SLEW_ON  = 400kHz Baud is using 5v
                             //SLEW_OFF = 100kHz Baud if under 5v

    WriteEEB(0x00,0x00,0x00,data);
    ReadEEB(0x00,0x00,0x00,&buffer);
  while(1) ;
}
void WriteEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char data)
{
    char DevAddr;
    DevAddr = ((deviceHard << 4) | block) | 0b10000000;

    StartI2C();                     // initiate START condition
    WriteI2C( DevAddr );
    while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    while(SSPCON2bits.ACKSTAT);
    WriteI2C( data );
    while(SSPCON2bits.ACKSTAT);
    StopI2C();
}
void ReadEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char *buff)
{
    char DevAddrR, DevAddrW;
    DevAddrR = ((deviceHard << 4) | block) & 0b01111111;
    DevAddrW = ((deviceHard << 4) | block) | 0b10000000;

    StartI2C();
    WriteI2C( DevAddrW );
    while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    while(SSPCON2bits.ACKSTAT);
    StartI2C();
    WriteI2C( DevAddrR );
    while(SSPCON2bits.ACKSTAT);
    *buff = ReadI2C();
    NotAckI2C();
    StopI2C();
}
 
Last edited:
dear jason
i use of your program and with ReadEEB function i send buffer to portd but it do not show anything on portd.
Code:
    TRISD=0x00;                  
    PORTD=0x00;
    data = 0x0a;

Code:
    PORTD = buffer;

can you revision this files.
thanks
 

Attachments

  • i2c.zip
    23.8 KB · Views: 544
For the 24C16 I think you need to change it to,

Code:
void WriteEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char data)
{
    char DevAddr;
    DevAddr = ( block[COLOR="Red"]<<1[/COLOR]) [COLOR="Red"]| 0b10100000[/COLOR];

    StartI2C();                     // initiate START condition
    WriteI2C( DevAddr );
    while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    while(SSPCON2bits.ACKSTAT);
    WriteI2C( data );
    while(SSPCON2bits.ACKSTAT);
    StopI2C();
}
void ReadEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char *buff)
{
    char DevAddrR, DevAddrW;
    DevAddrR = ( block[COLOR="red"]<<1[/COLOR]) [COLOR="red"]| 0b10100001[/COLOR];
    DevAddrW = ( block[COLOR="Red"]<<1[/COLOR]) | [COLOR="red"]0b10100000[/COLOR];

    StartI2C();
    WriteI2C( DevAddrW );
    while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    while(SSPCON2bits.ACKSTAT);
    StartI2C();
    WriteI2C( DevAddrR );
    while(SSPCON2bits.ACKSTAT);
    *buff = ReadI2C();
    NotAckI2C();
    StopI2C();
}

Mike.
 
not sure pommie setting it to 0b10100001 changes the address of A0:A2

addr-jpg.26345


How about:
Code:
#include <p18f452.h>
#include "i2c.h"
#pragma config WDT = OFF, LVP = OFF, OSC = HS

unsigned char buffer;
unsigned char data;

void WriteEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char data);
void ReadEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char *buff);

void main(void)
{
    data = 'A';
    OpenI2C(MASTER, SLEW_ON);// Initialize I2C module
                             //SLEW_ON  = 400kHz Baud is using 5v
                             //SLEW_OFF = 100kHz Baud if under 5v

    WriteEEB(0x00,0x00,0x00,data);
    ReadEEB(0x00,0x00,0x00,&buffer);
  while(1) ;
}
void WriteEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char data)
{
    char DevAddr;
    DevAddr = (((deviceHard << 4) | block) | 0b10000000) & 0b11111110;

    StartI2C();                     // initiate START condition
    WriteI2C( DevAddr );
    while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    while(SSPCON2bits.ACKSTAT);
    WriteI2C( data );
    while(SSPCON2bits.ACKSTAT);
    StopI2C();
}
void ReadEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char *buff)
{
    char DevAddrR, DevAddrW;
    DevAddrR = ((deviceHard << 4) | block) | 0b10000001;
    DevAddrW = (((deviceHard << 4) | block) | 0b10000000) & 0b11111110;

    StartI2C();
    WriteI2C( DevAddrW );
    while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    while(SSPCON2bits.ACKSTAT);
    StartI2C();
    WriteI2C( DevAddrR );
    while(SSPCON2bits.ACKSTAT);
    *buff = ReadI2C();
    NotAckI2C();
    StopI2C();
}

i just noticed the zip ill check it now
 

Attachments

  • addr.jpg
    addr.jpg
    7.8 KB · Views: 3,363
Last edited:
i will be honest im not sure if those parts in isis follow the same rules as the datasheet for the "AT24C164" But the latest code i posted should work if you remove the ACK commands. I noticed that MCC18 when it Writes it includes that in it,

Code:
void WriteEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char data)
{
    char DevAddr;
    DevAddr = (((deviceHard << 4) | block) | 0b10000000) & 0b11111110;

    StartI2C();                     // initiate START condition
    WriteI2C( DevAddr );
    //while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    //while(SSPCON2bits.ACKSTAT);
    WriteI2C( data );
    //while(SSPCON2bits.ACKSTAT);
    StopI2C();
}
void ReadEEB(unsigned char deviceHard,unsigned char block,unsigned char address,unsigned char *buff)
{
    char DevAddrR, DevAddrW;
    DevAddrR = ((deviceHard << 4) | block) | 0b10000001;
    DevAddrW = (((deviceHard << 4) | block) | 0b10000000) & 0b11111110;

    StartI2C();
    WriteI2C( DevAddrW );
    //while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    //while(SSPCON2bits.ACKSTAT);
    StartI2C();
    WriteI2C( DevAddrR );
    //while(SSPCON2bits.ACKSTAT);
    *buff = ReadI2C();
    NotAckI2C();
    StopI2C();
}
 
Thanks pommie i tried this:
Code:
#include <p18f452.h>
#include <i2c.h>

#pragma config WDT = OFF, LVP = OFF, OSC = HS

unsigned char buffer;
unsigned char data;

void WriteEEB(unsigned char block,unsigned char address,unsigned char data);
void ReadEEB(unsigned char block,unsigned char address,unsigned char *buff);

void main(void)
{
/////////////////////////////////////
    TRISD=0x00;                  
	PORTD=0x00;
    data = 0x01;
/////////////////////////////////////
    OpenI2C(MASTER, SLEW_OFF);// Initialize I2C module
                             //SLEW_ON  = 400kHz Baud is using 5v
                             //SLEW_OFF = 100kHz Baud if under 5v

    WriteEEB(0x00,0x00,data);
    ReadEEB(0x00,0x00,&buffer);
/////////////////////////////////////
    PORTD = buffer;
/////////////////////////////////////
 	
  while(1) ;

}
void WriteEEB(unsigned char block,unsigned char address,unsigned char data)
{
    char DevAddrW;
    DevAddrW = (0b10100000 | (block<<1)) & 0b11111110;

    StartI2C();                     // initiate START condition
    WriteI2C( DevAddrW );
    //while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    //while(SSPCON2bits.ACKSTAT);
    WriteI2C( data );
    //while(SSPCON2bits.ACKSTAT);
    StopI2C();
}
void ReadEEB(unsigned char block,unsigned char address,unsigned char *buff)
{
    char DevAddrR, DevAddrW;
    DevAddrR = (0b10100001 | (block << 1));
    DevAddrW = (0b10100000 | (block << 1)) & 0b11111110;

    StartI2C();
    WriteI2C( DevAddrW );
    //while(SSPCON2bits.ACKSTAT);
    WriteI2C( address );
    //while(SSPCON2bits.ACKSTAT);
    StartI2C();
    WriteI2C( DevAddrR );
    //while(SSPCON2bits.ACKSTAT);
    *buff = ReadI2C();
    NotAckI2C();
    StopI2C();
}

The addresses are fine now but i get 0xFF as the buffer/PORTD. ANy thoughts?

Please note i turned off SLEW (SLEW_OFF) because the part in ISIS is the 100khz version.
 
Last edited:
PIC 18F2455 and EEPROM 24LC1025

HI TO ALL!

I work with PIC18F2455 and I want to connect EEPROM 24LC1025.

I make a program to AD CONVERTER. Now I need to save the values from AD
in the external memory EEPROM 24LC1025. I2C Protocol.

Please, I need a source code in MPLAB C18 COMPILER.

Thanks.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top