eeprom.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // read and write eeprom of PIC18F2550 (and 18F2455, 18F4455, 18F4550)
  2. // EEPROM size is 256 bytes
  3. // (c) Raphael Wimmer. Licensed under GNU GPL v2 or higher
  4. // modified by Antoine Rousseau 17 jan 2010
  5. //#include <pic18fregs.h>
  6. void ee_write_byte(unsigned char address, unsigned char _data){
  7. EEDATA = _data;
  8. EEADR = address;
  9. // start write sequence as described in datasheet, page 91
  10. EECON1bits.EEPGD = 0;
  11. EECON1bits.CFGS = 0;
  12. EECON1bits.WREN = 1; // enable writes to data EEPROM
  13. INTCONbits.GIEH = 0; // disable interrupts
  14. //INTCONbits.GIEL = 0; // disable interrupts
  15. EECON2 = 0x55;
  16. EECON2 = 0x0AA;
  17. EECON1bits.WR = 1; // start writing
  18. while(EECON1bits.WR){
  19. _asm nop _endasm;}
  20. if(EECON1bits.WRERR){
  21. printf("ERROR: writing to EEPROM failed!\n");
  22. }
  23. EECON1bits.WREN = 0;
  24. INTCONbits.GIEH = 1; // enable interrupts
  25. //INTCONbits.GIEL = 1; // enable interrupts
  26. }
  27. unsigned char ee_read_byte(unsigned char address){
  28. EEADR = address;
  29. EECON1bits.CFGS = 0;
  30. EECON1bits.EEPGD = 0;
  31. EECON1bits.RD = 1;
  32. return EEDATA;
  33. }