Hi,
I am writing a program to display the data stored in EEPROM in UART terminal.Controller used by me is PIC32MX795F512L.
Communication between EEPROM(24aa01) and PIC32 is I2C.//Write a program to write the data to EEPROM.I am using MPLAB IDE v8.92.
Here is the code I wrote
#include<proc/p32mx795f512l.h>
//configure the system clock to 80MHz
#pragma config POSCMOD=HS,FNOSC=PRIPLL
#pragma config FPLLMUL=MUL_20
#pragma config FPLLIDIV=DIV_2
#pragma config FPLLODIV=DIV_1
#pragma config FWDTEN=OFF
#pragma config FPBDIV=DIV_2
//Enable the timer module to check whether the clock frequency is correct
void delay_ms(unsigned int d)
{unsigned int i;
for(i=0;i<d;i++)
{
PR1=313;
TMR1=0;
while(TMR1<PR1);
}
}
void Timer1_init()
{
T1CON=0;
T1CON=0X00008030;
}
//Enable I2C module
void I2C_Init(){
I2C1CON = 0;
I2C1BRG = 0x00c5 ; // 0x004f;
//Now we will initialise the I2C peripheral for Master Mode, No Slew Rate
//Control, and leave the peripheral switched off.
I2C1CON = 0x1200;
I2C1RCV = 0x0000;
I2C1TRN = 0x0000;
//Now we can enable the peripheral
I2C1CON = 0x9200;}
void UART_Init()
{
U2MODEbits.BRGH=0;
U2BRG=21;
U2MODEbits.UEN=0X00;
U2MODEbits.SIDL=0;
U2MODEbits.IREN=0;
U2MODEbits.PDSEL=0X00;
U2MODEbits.STSEL=0X00;
U2STAbits.UTXEN=1;
U2STAbits.URXEN=1;
U2MODEbits.ON=1;
}
void UART_Tx(unsigned char read)
{while(U2STAbits.UTXBF);
U2TXREG=read;
//delay_ms(200);
}
//Write data to EEPROM
unsigned char Write_EEPROM(unsigned char i,unsigned char b)
{
char f;
while(I2C1STATbits.TRSTAT==1);
I2C1CONbits.SEN=1;
while(I2C1CONbits.SEN==1);
I2C1TRN=0xA0;
while(I2C1STATbits.TBF==1);
while(I2C1STATbits.TRSTAT==1);
f = I2C1STATbits.ACKSTAT;
I2C1TRN=i;
while(I2C1STATbits.TBF==1);
while(I2C1STATbits.TRSTAT==1);
f=I2C1STATbits.ACKSTAT;
I2C1TRN=b;
while(I2C1STATbits.TBF==1);
while(I2C1STATbits.TRSTAT==1);
f=I2C1STATbits.ACKSTAT;
//I2C1CONbits.ACKEN=1;
I2C1CONbits.PEN=1;
while(I2C1CONbits.PEN==1);
}
// Read data from EEPROM
unsigned char Read_EEPROM(unsigned char i)
{
unsigned char k,x;
while(I2C1STATbits.TRSTAT==1);
I2C1CONbits.SEN=1;
while(I2C1CONbits.SEN==1);
I2C1TRN=0xA0;
while(I2C1STATbits.TBF==1);
while(I2C1STATbits.TRSTAT==1);
k=I2C1STATbits.ACKSTAT;
I2C1TRN=i;
while(I2C1STATbits.TBF==1);
while(I2C1STATbits.TRSTAT==1);
k=I2C1STATbits.ACKSTAT;
while(I2C1STATbits.TRSTAT==1);
I2C1CONbits.RSEN=1;
while(I2C1CONbits.RSEN==1);
I2C1TRN=0xA1;
while(I2C1STATbits.TBF==1);
while(I2C1STATbits.TRSTAT==1);
k=I2C1STATbits.ACKSTAT;
I2C1CONbits.RCEN=1;
UART_Tx(I2C1RCV);
//UART_Tx(x);
while(I2C1STATbits.TRSTAT==1);
I2C1CONbits.PEN=1;
while(I2C1CONbits.PEN==1);
}
//MAIN PROGRAM
void main()
{
AD1PCFG=0xffff;
Timer1_init();
I2C_Init();
LATB = 0;
UART_Init();
TRISD=0;
LATDSET=0x00;
TRISB=0;
LATBSET=0x00;
unsigned char b;
unsigned char i;
while(1)
{
LATB =0x00;
LATD=0;
b = 0x00;
// for(i = 0x00; i<0x100; i++) {
Write_EEPROM(0x01,0xff);
// b++;
delay_ms(5);
// }
// for(i = 0x00; i <0x100; i++){
// LATD = i;
Read_EEPROM(0x01);
delay_ms(100);
//}
//while(1);
}
}
Individual modules are working fine.When I club both I receive unknown character.
Please help resolving this issue.
Regards,
Parimala