Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

writing real time data into internal EEPROM of PIC microcontroller

Status
Not open for further replies.

apples1234

Newbie level 1
Joined
Apr 6, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,321
Hi. I am using PIC18 Explorer board (PIC18F8722), PICKit3 and C18 compiler. I am getting data from the potentiometer of the board, using the ADC to obtain analog values and then I want to store these values in the internal EEPROM of the PIC18. I am storing the 10 bit result obtained from the ADC into two bytes-one is 8-bit(highbyte) and the other is 2-bit (lowbyte). I want to store 16 such values in the EEPROM, that is a total of 32 bytes in consecutive memory locations. I have written a program for the same. But when I read the data from the EEPROM, only the last value (i.e. highbyte and lowbyte) is stored in all the 32 memory locations. I also tried reading the EEPROM through C18 compiler viewer. In MPLAB C18 click on View>EEPROM and then click on Programmer>Read. It shows the contents of the EEPROM and all I see is the last value written over all 32 memory locations. But what I want is 16 different values. Can anyone please help me? Here is my code:



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
#include <p18f8722.h>
#include<delays.h>
#include<stdio.h>
 
#pragma config OSC=HS // high speed oscillator
#pragma config WDT=OFF // watch dog off
#pragma config  XINST = OFF 
#pragma config  LVP = OFF
 
#pragma udata
 
#pragma code
 
float ad,adresult;
unsigned int EEPRead[32];
unsigned int ee_address=0;
unsigned int highbyte,lowbyte; 
 
void EEPROM_Write(unsigned int address, unsigned int databyte)
{ // writes the "databyte" value to EEPROM at the address given location in "address".
    [INDENT]EECON1bits.EEPGD = 0;   // Set to access EEPROM memory
    EECON1bits.CFGS = 0;    // Do not access Config registers
 
    EEDATA = databyte;      // Load EEDATA with byte to be written
    EEADR = address;        // Load EEADR with address of location to write.
 
    EECON1bits.WREN = 1;    // Enable writing
    
   //INTCONbits.GIE = 0;   // Disable interrupts
    EECON2 = 0x55;          // Begin Write sequence
    EECON2 = 0xAA;
    EECON1bits.WR = 1;      // Set WR bit to begin EEPROM write
    //INTCONbits.GIE = 1;   // re-enable interrupts
    
    while (EECON1bits.WR == 1)
    {   
 
    };
 
    EECON1bits.WREN = 0;    // Disable writing as a precaution.[/INDENT]
}
 
unsigned char EEPROM_Read(unsigned int address)
{ // reads and returns the EEPROM byte value at the address given in "address".
 
    [INDENT]EECON1bits.EEPGD = 0;   // Set to access EEPROM memory
    EECON1bits.CFGS = 0;    // Do not access Config registers
 
    EEADR = address;        // Load EEADR with address of location to write.
 
    // execute the read
    EECON1bits.RD = 1;      // Set the RD bit to execute the EEPROM read
 
    return EEDATA;[/INDENT]
}
void write2eeprom(unsigned int byte)   //function to write ADC values into EEPROM
{
    [INDENT]EEPROM_Write(ee_address, byte);             
    ee_address=ee_address+1;                // next address location
    if(ee_address==32)
        ee_address=0;
[/INDENT]
}   
 
void ADInit()   //ADC initialization
{
    [INDENT]ADCON0=0b00000001;
 
    ADCON1=0b00000000;
 
    ADCON2=0b00000010;[/INDENT]
}
float ReadAD()
{
[INDENT]//  unsigned int highb,lowb,i=0;
    int i=0;
    
    do
    {
        ADCON0bits.GO_DONE=1; // starts conversion
        while(ADCON0bits.GO_DONE==1); // wait, it's converting
        Delay10TCYx(0);
        highbyte=(unsigned int) ADRESH;
        lowbyte=(unsigned int) ADRESL;
        ad=highbyte*4+lowbyte/64;      //convert the int value into analog voltage
        adresult=ad*5/1024;
        write2eeprom(highbyte);
        Delay1TCY();
        write2eeprom(lowbyte);
        return (float)(ad);
    }while(1);
[/INDENT]   
}
float readEEP()
{
[INDENT]    float eepvalue,eepresult;
 
    for(ee_address=0;ee_address<32;ee_address++)
    {
        for(i=0;i<32;i=i++)
        {
            EEPRead[i]=EEPROM_Read(ee_address);
        }
    }
    for(i=0;i<32;i=i+2)
    {
        highbyte=EEPRead[i];
        lowbyte=EEPRead[i+1];
        eepvalue=highbyte*4+lowbyte/64;
        eepresult=max*5/1024;   
    
    }
    
    return (float) eepresult;   
[/INDENT]}
 
void writetoLCD(float volt)
{
    [INDENT]char buffer[2];
    char buffer_m[2];
    unsigned int voltin;
    unsigned int voltdec;
    char i=0;
 
    voltin=(unsigned int)volt;
    voltdec=(unsigned int)(((float)volt - (float)voltin) * 100000);
    sprintf(buffer,(const far rom char*)"%d.%01d%01d", voltin, voltdec);
    LCDLine_1();
    d_write('=');
    d_write(buffer[0]);
    d_write('.');
    d_write(buffer[1]);
    d_write(buffer[2]);
    d_write('V');
    LCDLine_2();
    d_write('e');
    d_write('e');
    d_write('p');
    d_write('=');
    d_write(buffer[0]);
    d_write('.');
    d_write(buffer[1]);
    d_write(buffer[2]);
[/INDENT]}
 
void main(void)
{
        float voltage = 0.0;
    float EEPvalue=0.0;
    
    TRISBbits.TRISB0 = 0;
 
    TRISD = 0;
 
    INTCON = 0;
 
    RCONbits.IPEN=0;   // disable priority system. 
 
    OSCCONbits.IRCF0 = 0;
    OSCCONbits.IRCF1 = 0;
    OSCCONbits.IRCF2 = 1;
 
    // Initialize the LCD display.
    LCDInit();
    
    // Initialize the A/D display.
 
    ADInit();
    do 
    {
        voltage = ReadAD();
        writetoLCD( voltage );
//      EEPvalue=readEEP();
//      writetoLCD(EEPvalue);
    }while ( 1 );
 
}

 
Last edited by a moderator:

hello apples1234,

what i found in your code is
in your EEPROM_write function you directly storing databyte which is 16 bit and EEDATA is holding only 8 bit (one byte)
so when you store the data on EEDATA only lower 8 bits get stored in EEPROM
you have to SHIFT data write by 8 (>>8) and also increment the address of eeprom to store the whole 16 bits.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top