[PIC] EEprom is reading alternate characters

Status
Not open for further replies.

MachaniGoutham

Newbie level 5
Joined
Nov 10, 2011
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,458

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <proc/p32mx450f128h.h>
#include "uart_fun.h"
// PIC32MX450F128H Configuration Bit Settings
// Configuration Bit settings
// SYSCLK = 48 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 48 MHz (SYSCLK / FPBDIV)
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
#pragma config FPLLMUL = MUL_16, FPLLIDIV = DIV_2, FPLLODIV = DIV_4, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1, JTAGEN = OFF, FSOSCEN = OFF
#pragma config ICESEL = ICS_PGx1
#define SYS_FREQ (16000000L)
#define EEPROM_ID 0xA0
#define BRG_VAL           0x04E
#define ON                1
#define OFF               0
#define STARTI2C1         I2C1CONbits.SEN
#define STOPI2C1          I2C1CONbits.PEN
#define RESTARTI2C1       I2C1CONbits.RSEN
#define ACKI2C1           I2C1CONbits.ACKEN
#define TRANSMITSTATUS_BF I2C1STATbits.TBF
#define RECIEVESTATUS_BF  I2C1STATbits.RBF
#define R_WSTATUS         I2C1STATbits.R_W
#define I2CIDLEBUS        STARTI2C1|STOPI2C1|ACKI2C1|TRANSMITSTATUS_BF|RECIEVESTATUS_BF
 
//function initiates I2C1 module to baud rate BRG
void I2C1_Init()
{
 
   // I2CBRG = 78 for 8Mhz OSCI with PPL with 100kHz I2C clock
   I2C1BRG = BRG_VAL;
   I2C1CONbits.I2CEN = OFF; // Disable I2C Mode
   I2C1CONbits.DISSLW = ON; // Disable slew rate control
   IFS1=0x00;                   // Clear Interrupt
   I2C1CONbits.I2CEN=ON;    // Enable I2C Mode
   I2C1CONbits.SIDL=OFF;        // Module Continues in Idle mode
   I2C1CONbits.A10M=OFF;        // 0=I2CxADD is a 7-bit slave address
   I2C1CONbits.ACKDT = ON;        // setting Ack by  Master operationI
   ACKI2C1 = OFF;
   I2C1RCV=0x00;            // read buffer to clear buffer full
 
    Uart1SendString("I2C Initalized");Uart1Send(0x0D); Uart1Send(0x0A);
}
 
//function iniates a start condition on bus
void I2C1_Start(void)
{
   STARTI2C1= ON;           //Initiate Start condition
   while(STARTI2C1);         //wait for automatic clear before proceding
   Uart1SendString("I2C Started");Uart1Send(0x0D); Uart1Send(0x0A);
}
 
//stop the I2C bus to Idle
void I2C1_Stop(void)
{
    //initiate stop bit
    STOPI2C1= ON;
   //wait for hardware clear of stop bit
   while (STOPI2C1);
   Uart1SendString("I2C Stoped");Uart1Send(0x0D); Uart1Send(0x0A);
}
 
//restart i2c bus
void I2C1_Restart(void)
{
 
    RESTARTI2C1=ON; //Initiate restart condition
 
   //the hardware will automatically clear restart bit
   //wait for automatic clear before proceding
   while (RESTARTI2C1);
 
Uart1SendString("I2C Restarted");Uart1Send(0x0D); Uart1Send(0x0A);
}
 
//I2C Idle state
void IdleI2C()
{
    if(I2CIDLEBUS);
    Uart1SendString("I2C @ IDLE STATE");Uart1Send(0x0D); Uart1Send(0x0A);
}
 
// write I2C
void WriteI2C1(unsigned char data)
{
    unsigned char x;
    IdleI2C();
 
    I2C1TRN = data; // load the outgoing data byte
    x=I2C1TRN;
    while (I2C1STATbits.TRSTAT);   // wait untill TBF-1 transmit is in progress...0-transmit Completed
Uart1SendString("I2C Write completed");Uart1Send(x);
 
Uart1Send(0x0D); Uart1Send(0x0A);
}
 
 //function reads data I2C,
unsigned char ReadI2C1(void)    //does not reset bus!!!
{
  unsigned char data;
 
   I2C1CONbits.RCEN = 1;      //set I2C module to receive
 
   while(I2C1CONbits.RCEN){};
   I2C1STATbits.I2COV=0;    // update to I2CRCV register clearing manually
 
   while(I2C1STATbits.RBF){ // wait to clear recive buffer
      data = I2C1RCV;          //get data from I2CRCV register
   }
 
Uart1SendString("Read Completed");
Uart1Send(0x0D); Uart1Send(0x0A);
 return data;               //return data
 
}
 
// ACK I2C1
void Ackl2C (void)
{
 I2C1CONbits.ACKDT = OFF;        // setting Ack by  Master operationI
 ACKI2C1 = ON;                 //Ack enabled
 while(ACKI2C1);              // wait Nack cleared
}
 
// NACK I2C1
void Nackl2C (void)
{
 I2C1CONbits.ACKDT = ON;        // setting Ack by  Master operationI
 ACKI2C1 = ON;                 //Ack enabled
 
}
 
unsigned char EEByteWrite(unsigned char c, unsigned char address,  unsigned char data)
{
 
    IdleI2C();           // ensure I2C bus is idle
    I2C1_Start( );      // initiate START condition
 
        if (I2C1STATbits.BCL)  // test for bus collision
            return (-1);  // return with Bus Collision error
 
    WriteI2C1(c);  // write byte - R/W bit should be 0
    IdleI2C();   // ensure module is idle
 
        if (I2C1STATbits.ACKSTAT)  // test for NACK condition received
        return (-2);
 
    WriteI2C1(address);  // write word address for EEPROM
    IdleI2C();  // ensure module is idle
 
        if (I2C1STATbits.ACKSTAT)  // test for NACK condition received
        return (-2);
 
    WriteI2C1(data);  // data byte for EEPROM
    IdleI2C();  // ensure module is idle
 
    I2C1_Stop();  // send STOP condition
        if (I2C1STATbits.BCL)  // test for bus collision
        return (-1);  // return with Bus Collision error
 Uart1SendString("EEprom write Sucess");
return (0);  // return with no error
}
 
 
unsigned char EERandomRead( unsigned char c, unsigned char address)
{
   unsigned char edata;
 
   IdleI2C();      // make sure I2C module is idle
   I2C1_Start();   // assert a START condition
 
   WriteI2C1(c);  // write byte - R/W bit should be 0
   IdleI2C();   // ensure module is idle
 
        if (I2C1STATbits.ACKSTAT)  // test for NACK condition received
        return (-2);
 
   WriteI2C1(address);  // write word address for EEPROM
   IdleI2C();  // ensure module is idle
 
        if (I2C1STATbits.ACKSTAT)  // test for NACK condition received
        return (-2);
 
   I2C1_Restart();  // assert I2C bus restart condition
         if (I2C1STATbits.BCL)  // test for bus collision
            return (-1);  // return with Bus  Collision error
 
   WriteI2C1(0xA1);  // write 1 byte - R/W bit should be 1
   IdleI2C();  // ensure module is idle
 
        if (I2C1STATbits.ACKSTAT)  // test for NACK condition received
        return (-2);
 
   edata=ReadI2C1();
 
   Nackl2C ();
 
   while(ACKI2C1);
 
   I2C1_Stop();  // send STOP condition
 
      if (I2C1STATbits.BCL)  // test for bus collision
         return (-1);  // return with Bus Collision error
 
 Uart1SendString("EEprom read Sucess");
 Uart1Send(0x0D); Uart1Send(0x0A);
 
return edata;  // return with data
}
 
void DelayuSec(unsigned int n)     //loop nops for delay
{
   unsigned int j;
   while(n>0)
   {
      for(j=0;j < 10; j++);
         n--;
   }
}
 
 
 
void main()
{
    unsigned char Eodata[14],i,address;
    UART1PPSInit();
    UART1Initialise();
    Uart1SendString("uartworking");Uart1Send(0x0D); Uart1Send(0x0A);
 
    I2C1_Init();
 
     EEByteWrite(0xA0,0x00,'G');
    EEByteWrite(0xA0,0x01,'o');
   EEByteWrite(0xA0,0x02,'u');
   EEByteWrite(0xA0,0x03,'t');
    EEByteWrite(0xA0,0x04,'h');
    EEByteWrite(0xA0,0x05,'a');
    EEByteWrite(0xA0,0x06,'m');
    EEByteWrite(0xA0,0x07,'R');
     EEByteWrite(0xA0,0x08,'o');
EEByteWrite(0xA0,0x09,'c');
    EEByteWrite(0xA0,0x0A,'k');
    EEByteWrite(0xA0,0x0B,'z');
     EEByteWrite(0xA0,0x0C,'O');
     EEByteWrite(0xA0,0x0D,'n');
        Eodata[0]=EERandomRead(0xA0,address); Uart1Send(Eodata[0]);
    address=0x00;
    for(i=0;i<14;i++){
    Eodata[i]=EERandomRead(0xA0,address);
   address++;
    }
//     Uart1Send(0x0D); Uart1Send(0x0A);
//     for(i=0;i<14;i++){
//         Uart1Send(Eodata[i]);
//     }
//     Uart1Send(x);
      Uart1SendString(Eodata);
 
}

 
Last edited by a moderator:

Thanks for answering there is no error on code
but it executes alternate characters from eeprom
 

I was not thinking of syntax errors which the compiler finds but errors in the logic of the code where it may not be reading/writing the EEPROM correctly

I note you have UART output in WriteI2C1, I2C1_Stop, I2C1_Rstart, etc
outputing to a UART can slow things significantly (unless you are using UART transmit interrupts) - could these outputs be throwing the I2C timing out? try commenting them out?
 

What do you mean by alternate characters. Give example. You mean 'G', 'u' , 'h'... like that ?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…