Hi guys,
Im using PIC24FJ64GA002 microcontroller. Working in I2C protocol interfacing PIC with EEPROM(). Im not working in physically. All work are doing in proteus simulation only. My environment IDE is MPLAB v8.92, XC16 compiler and proteus simulation. My EEPROM slave address in "0xa0"(all address pins(A0,A1,A2) are grounded). In proteus i can't see whether data is writing or not, so i removed the eeprom and placed I2C debugger. Im new to I2C debugger. I almost see some info on I2C debug window. Im trying to write letter 'M' in eeprom. I can't see letter 'M' in I2C debug window. It seems data 'M' didnt send to eeprom. I didnt find any flaw in my code.
My i2c.h:
My main.c:
I2C debug window:
Finally the schematic:
Thanks...
Im using PIC24FJ64GA002 microcontroller. Working in I2C protocol interfacing PIC with EEPROM(). Im not working in physically. All work are doing in proteus simulation only. My environment IDE is MPLAB v8.92, XC16 compiler and proteus simulation. My EEPROM slave address in "0xa0"(all address pins(A0,A1,A2) are grounded). In proteus i can't see whether data is writing or not, so i removed the eeprom and placed I2C debugger. Im new to I2C debugger. I almost see some info on I2C debug window. Im trying to write letter 'M' in eeprom. I can't see letter 'M' in I2C debug window. It seems data 'M' didnt send to eeprom. I didnt find any flaw in my code.
My i2c.h:
Code:
#include "p24FJ64GA002.h"
#include "stdio.h"
void init_i2c()
{
///Initiate I2C1 (Slave Device)
I2C1CONbits.I2CEN = 1; // I2C enable
I2C1CONbits.I2CSIDL = 0; // I2C stop in idle
I2C1CONbits.SCLREL = 0; // I2C clock release
I2C1CONbits.STREN = 0; // I2C clock stretch enable
I2C1CONbits.IPMIEN = 0; // I2C Peripheral Management
I2C1CONbits.A10M = 0; // I2C address size = 7 bit
I2C1CONbits.SMEN = 0; // I2C address size = 7 bit
I2C1CONbits.SMEN = 0; // I2C SMBUS enable
I2C1MSK = 0x0000; // I2C SMBUS enable
I2C1BRG = 37; //Baud Rate
I2C1ADD = 0xa0;
_SI2C1IF = 0;
_SI2C1IE = 1;
//SendUARTStr("I2C Initiated ");
}
My main.c:
Code:
#include "p24FJ64GA002.h"
#include <stdio.h>
#include "uart.h"
#include <stdint.h>
#include "i2c.h"
unsigned char I2CFlag,db,I2C2Rcv,C_I2CTimeout,I2CTimer;
#define C_I2CTimeOut 30
/* Main function*/
int main (void)
{
TRISA=0x0000;
//Set input port B
TRISB = 0xFFFF;
//Assign uart pins///////////
RPINR18bits.U1RXR = 10;
// Tx:RP13(Pin)
RPOR6bits.RP13R = 3;
////////////////////////////
/////Initiate UART////////////////////////////////////////////
InitUART();/////////////////////////////////////////////////////
//////////////
//////////////////////////////////////////////////////////////
SendUARTStr("PIC24F connected");
Timer(10);
SendUARTStr("Uart initiated");
Timer(10);
init_i2c();
Timer(10);
//SendUARTStr("I2C Initiated");
SaveEEPW(0xa0,'M');
Timer(5);
uint16_t Addr=0xa0;
// ReadEEPW(Addr);
while(1)
{
}
}
/* End of Main function*/
/* Function WriteRTC() - Writes a byte into RTC register */
void WriteI2C(unsigned char DeviceAddress, uint16_t Addr, unsigned char db)
{
uint32_t i;
unsigned char WriteState;
I2CTimer = WriteState = 0;
IFS1 &= ~0x0002; // Master I2C interrupt flag MI2C2IF bit is set to = 1
I2CFlag = 1;
I2C1CON |= 0x0001; // SEN = 1;
I2CTimer = 0;
//Restart_WDT(); // SEN = 1
while(I2CFlag)
{
if(I2CTimer > C_I2CTimeout){ // Exit routine 0=30
I2CFlag = 0;
// Error to be handled here
}
if(IFS1 & 0x0002){ // SSP1IF
IFS1 &= ~0x0002; // Master I2C interrupt flag
I2CTimer = 0;
if(!WriteState){
I2C1TRN = (DeviceAddress & 0xa0); // Device Address + Write 0
if(DeviceAddress == 0xde)WriteState = 1; // Skip State 1 for RTC // Skip State 1 for RTC
}
else if(WriteState == 1)I2C1TRN = Make8(Addr, 1); // MSB Send Address
else if(WriteState == 2)I2C1TRN = Make8(Addr, 0); // LSB of Address
else if(WriteState == 3)I2C1TRN = db; // Write data
else if(WriteState == 4)I2C1TRN |= 0x04; // PEN = 1, Stop enable
else{WriteState = 98; I2CFlag = 0;} // end of process
WriteState++; // Next state
SendUARTStr(db);
}
}
Timer(2);
}
/* End Function WriteRTC() */
/* Function SaveEEPW() - Saves a Word into EEPROM */
void SaveEEPW(uint16_t EEPAddress, uint16_t Cx)
{ unsigned char i, *Ptr;
Ptr = &Cx;
i = 2;
do{
WriteI2C(0xa0, EEPAddress++, *Ptr++);
}while(--i);
}
/* End Function SaveEEPW() */
/* Make8 */
void Make8(uint16_t i,int j)
{
unsigned char loByte;
unsigned char hiByte;
loByte = i & 0x00ff;
hiByte = i >> 8;
if(j==0)
{
//printf("0x%2x",loByte);
return loByte;
}
if(j==1)
{
//printf("0x%2x",hiByte);
return hiByte;
}
}
/* End of Make8 */
I2C debug window:
Finally the schematic:
Thanks...