Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

http://www.edaboard.com/thread247597.html 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 analo g values and then I want to store these values in the internal EEPROM of the PIC 18. 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 ha ve written a program for the same. But when I read the data from the EEPROM, onl y the last value (i.e. highbyte and lowbyte) is stored in all the 32 memory loca tions. 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 o f the EEPROM and all I see is the last value written over all 32 memory location s. But what I want is 16 different values. Can anyone please help me? Here is my code: Code C - [collapse] #include <p18f8722.h> #include<delays.h> #include<stdio.h> #pragma #pragma #pragma #pragma config config config config OSC=HS // high speed oscillator WDT=OFF // watch dog off XINST = OFF 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 { // writes the "databyte" ress". EECON1bits.EEPGD = 0; EECON1bits.CFGS = 0; EEDATA = databyte; EEADR = address; EECON1bits.WREN = 1; int address, unsigned int databyte) value to EEPROM at the address given location in "add // Set to access EEPROM memory // Do not access Config registers // Load EEDATA with byte to be written // Load EEADR with address of location to write. // 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.

} unsigned char EEPROM_Read(unsigned int address) { // reads and returns the EEPROM byte value at the address given in "address". EECON1bits.EEPGD = 0; EECON1bits.CFGS = 0; EEADR = address; // execute the read EECON1bits.RD = 1; // Set to access EEPROM memory // Do not access Config registers // Load EEADR with address of location to write. // Set the RD bit to execute the EEPROM read

return EEDATA; } void write2eeprom(unsigned int byte) M { EEPROM_Write(ee_address, byte); ee_address=ee_address+1; if(ee_address==32) ee_address=0; } void ADInit() //ADC initialization { ADCON0=0b00000001; ADCON1=0b00000000; ADCON2=0b00000010; } float ReadAD() { // unsigned int highb,lowb,i=0; int i=0; do {

//function to write ADC values into EEPRO

// next address location

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 volta ge adresult=ad*5/1024; write2eeprom(highbyte); Delay1TCY(); write2eeprom(lowbyte); return (float)(ad); }while(1); } float readEEP() { 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; void writetoLCD(float volt) { 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]);} 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 ); } 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.

You might also like