' EEPROM routines ' Larry Bradley, Dec 29, 2003 Function eRead(WREG As uByte) Dim ret As uByte #If PROCESSOR = 18F1320 Dim saveGIE As uByte saveGIE = INTCON 'Save GIE #endif EEADR = WREG EECON1 = 0 #If PROCESSOR = 18F1320 GIE = 0 'Disable interrupts #endif RD=1 ret=EEDATA #If PROCESSOR = 18F1320 ' Restore GIE btfsc saveGIE.7 bsf GIE #endif End Function ret Function eReadW(address As uByte) ' Read 2 bytes Dim ret As uInteger ret.Byte1 = eRead(address) ret.Byte0 = eRead(address+1) End Function ret Function eReadS(address As uByte) ' Read 3 bytes Dim ret As uShort ret.Byte2 = eRead(address) ret.Byte1 = eRead(address+1) ret.Byte0 = eRead(address+2) End Function ret Function eReadL(address As uByte) ' Read 4 bytes Dim ret As uLong ret.Byte3 = eRead(address) ret.Byte2 = eRead(address+1) ret.Byte1 = eRead(address+2) ret.Byte0 = eRead(address+3) End Function ret Function eReadFP(address As uByte) ' Read 4 bytes Dim ret As Single ret.Byte3 = eRead(address) ret.Byte2 = eRead(address+1) ret.Byte1 = eRead(address+2) ret.Byte0 = eRead(address+3) End Function ret Sub zeeWrite(address As uByte, WREG As uByte) Dim saveGIE As uByte EEDATA=WREG EEADR=address EEPGD = 0 CFGS = 0 WREN = 1 saveGIE = INTCON 'Save GIE GIE = 0 movlw 0x55 movwf EECON2 movlw 0xAA movwf EECON2 WR = 1 ' Restore GIE btfsc saveGIE.7 bsf GIE While WR=1: Wend WREN = 0 EEIF = 0 ' Turn off EEPROM interrupt flag End Sub Sub ewrite(address As uByte, wreg As uByte Call zeeWrite(address,wreg) End Sub Sub ewrite(address As uByte, bin As uInteger) Call zeeWrite(address,bin.Byte1) Call zeeWrite(address+1,bin.Byte0) End Sub Sub ewrite(address As uByte, bin As uShort) Call zeeWrite(address,bin.Byte2) Call zeeWrite(address+1,bin.Byte1) Call zeeWrite(address+2,bin.Byte0) End Sub Sub ewrite(address As uByte, bin As uLong) Call zeeWrite(address,bin.Byte3) Call zeeWrite(address+1,bin.Byte2) Call zeeWrite(address+2,bin.Byte1) Call zeeWrite(address+3,bin.Byte0) End Sub Sub ewrite(address As uByte, bin As Single) Call zeeWrite(address,bin.Byte3) Call zeeWrite(address+1,bin.Byte2) Call zeeWrite(address+2,bin.Byte1) Call zeeWrite(address+3,bin.Byte0) End Sub Sub EEPROMRefresh() ' This reads and rewrites all 256 EEPROM bytes. It needs to be done whenever the total ' number of writes to the EEPROM exceeds 1 million. Dim i As uByte, temp As uByte For i = 0 To 255 temp = eRead(i) eWrite(i,temp) Next i End Sub