Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

eeprom 16f684

Status
Not open for further replies.

neelam29

New Member
hi all
m designing a roject in which m using pic16f684 . want to save data into eeprom.below is the code .please help me find out the mistake. the value isnt saving...what might be the error....


/////////////////////////////////////////////////////////////
// Reads one byte from the EEprom at the specified address //
// and returns it //
/////////////////////////////////////////////////////////////
unsigned char ReadByteFromEE(unsigned char address)
{
unsigned char byte; // Variable hold the data that is read

EEADR = address; // Read from this address

//EEPGD = 0; // Point to EE memory
RD = 1; // Initiate a read cycle

byte = EEDAT; // Fetch byte from dataregister
return byte; // Return the read byte
}

////////////////////////////////////////////////////////////
// Writes one byte to the EEprom at the specified address //
////////////////////////////////////////////////////////////
void WriteByteToEE(unsigned char data, unsigned char address)
{

EEADR = 0x01;// address; // Address to write to
EEDAT = 0x02;// data; // Data to write

WREN = 1; // Enable writes to the EEProm
GIE = 0; // Disable interrupts during write
EECON2 = 0x55; // Write "password" to EECON2
EECON2 = 0xAA;
WR = 1; // Initiate a write cycle


while(!EEIF); // Wait for write to complete

WREN = 0; // Disable writes to EEProm
EEIF = 0; // Clear "write complete" flag
GIE = 1; // Disable interrupts during write

}

////////////////////////////////////////////////////////////////////////////////////////

thanks
 
well it seems you have it hard coded to save 0x01 at address 0x02 rather than using the data passed to your write function
 
Are you sure that EEIF is cleared before you write to EEPROM. You could change it to while(WR) as this bit gets cleared when the write is complete.

Edit, you should also check the config hasn't got the Data Protection bit set.

Mike.
 
Last edited:
I whipped out a 16f684 and tried this out, this worked fine for me:
Code:
// Device is a PIC16F684

#include <pic.h>

__CONFIG(HS & WDTDIS & PWRTDIS & MCLREN & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);

void init_pic16f684(void);
unsigned char ReadByteFromEE(unsigned char);
void WriteByteToEE(unsigned char, unsigned char);

void main()
{
  init_pic16f684();
  PORTC = ReadByteFromEE(0x00);
  WriteByteToEE(0x04, 0x00);
  while(1);   // infinite loop
}

void init_pic16f684()
{
  PORTA   = 0x00;    // Initialize PORTA
  PORTC   = 0x00;    // Initialize PORTC
  CMCON0  = 0x07;    // comparators off, Digital I/O
  ANSEL   = 0x00;    // Digital I/O
  TRISA   = 0x00;    // PORTA Outputs
  TRISC   = 0x00;    // PORTC Outputs
}

/////////////////////////////////////////////////////////////
// Reads one byte from the EEprom at the specified address //
// and returns it //
/////////////////////////////////////////////////////////////
unsigned char ReadByteFromEE(unsigned char address)
{
  unsigned char byte; // Variable hold the data that is read

  EEADR = address;    // Read from this address

  RD = 1;             // Initiate a read cycle

  byte = EEDAT;       // Fetch byte from dataregister
  return byte;        // Return the read byte
}

////////////////////////////////////////////////////////////
// Writes one byte to the EEprom at the specified address //
////////////////////////////////////////////////////////////
void WriteByteToEE(unsigned char data, unsigned char address)
{

  EEADR =  address; // Address to write to 0x00 to 0xFF
  EEDAT =  data;    // Data to write

  WREN = 1;         // Enable writes to the EEProm
  while(GIE)
    GIE = 0;        // Disable interrupts during write

  EECON2 = 0x55;    // Register not implemented on 16F684
  EECON2 = 0xAA;    // strange that this is required
  WR = 1;           // Initiate a write cycle


  while(!EEIF);     // Wait for write to complete

  WREN = 0;         // Disable writes to EEProm
  EEIF = 0;         // Clear "write complete" flag
  GIE = 1;          // Reenable interrupts

}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top