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 in PIC18F4520

Status
Not open for further replies.

hmk

New Member
Dear all,

I am trying to use I2C module in PIC 18F4520 but I got some troubles. I can not produce a 100kHz pulse on SCL pin of the PIC. I use MPLAB C18 and its standard library to program.
This is the function from i2c.h: OpenI2C(MASTER,SLEW_OFF); (Config I2C Mode Master, Clock 100Khz). However, when I use osciloscope to measure the frequency of the pulse on SCL, it generates a nearly 500kHz on this pin.
Can anyone help me? Thanks

hmk
 
You could write to SSPADD after you call OpenI2C. Personally, I wrote my own routines, I can post them if it would help.

Mike.
 
Hi Mike,

Thank you for your help but I have tried that way and it still did not work. When I demo the circuit by Proteus, it only runs in write process but can not receive data from ds1307. Here is a part of my code for this, can you read and comment?

void nhanACK()
{
while(SSPCON2bits.ACKSTAT);
SSPCON2bits.ACKSTAT = 1;
}
void write_i2c_all(void)
{
StartI2C(); // Start I2C
WriteI2C(0xD0); // Write mode
nhanACK();
WriteI2C(0x00); // address 0x00
nhanACK();
WriteI2C(second1&0x7f); // Write second
nhanACK();
WriteI2C(minute1); // Write minute
nhanACK();
WriteI2C(hour1); // Write hour
nhanACK();
WriteI2C(day1); // Write day
nhanACK();
WriteI2C(date1); // Write date
nhanACK();
WriteI2C(month1); // Write month
nhanACK();
WriteI2C(year1); // Write year
nhanACK();
WriteI2C(0x90);
nhanACK();
StopI2C();
}

void read_i2c_all(void)
{
StartI2C(); // Start I2C
WriteI2C(0xD0); // Write mode
nhanACK();
WriteI2C(0x00); // address 0x00
nhanACK();

RestartI2C(); // Start I2C
WriteI2C(0xD1); // Read mode
nhanACK();
second = ReadI2C(); // Read second
AckI2C(); // ACK
minute = ReadI2C(); // Read minute
AckI2C(); // ACK
hour = ReadI2C(); // Read hour
AckI2C(); // ACK
day = ReadI2C(); // Read day
AckI2C(); // ACK
date = ReadI2C(); // Read date
AckI2C(); // ACK
month = ReadI2C(); // Read month
AckI2C(); // ACK
year = ReadI2C(); // Read year
NotAckI2C(); // Not ACK
Delay10TCYx(1);
StopI2C(); // Stop I2C

}
void main (void)
{
int k;
TRISD=0x00;//so thu 1
OpenI2C(MASTER,SLEW_OFF); // Config I2C Mode Master, Clock 100Khz
SSPADD = 9;
second1 = 0x00; minute1 = 0x00;
hour1 = 0x00; day1 = 0x05;
date1 = 0x08; month1 = 0x08;
year1 = 0x08;

write_i2c_all();
Delay10TCYx(50);
while(1)
{
read_i2c_all();
delay_us(10);
PORTD=ma_bcd[second];
delay10ms();
}

}
 
You could write to SSPADD after you call OpenI2C. Personally, I wrote my own routines, I can post them if it would help.

Mike.

hi Pommie
i have problem with i2c functions in mplab c18 and connecting pic18F452 to
a serial eeprom such as 24C16 have you any example code.
thanks.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top