;=============================================================================== ; Title: Soldering Station EEPROM library ; ; Author: Rob Jansen, Copyright (c) 2021..2021, all rights reserved. ; ; Revisions ; --------- ; 2021-04-17 : Initial version. ; ; Description: Stores and reads data from the EEPROM. Data can be written and ; read as bytes or words. ; ; Sources: - ; ; ================== Constant and variable declarations ======================= ; EEPROM start addresses const byte EEPROM_ADDRESS_PRESET_1 = 0 ; Setting 1 temperature. const byte EEPROM_ADDRESS_PRESET_2 = 2 ; Setting 2 temperature. const byte EEPROM_ADDRESS_CALIBRATION_LOW = 4 ; 200 degrees calibration value. const byte EEPROM_ADDRESS_CALIBRATION_HIGH = 6 ; 450 degrees Calibration value. const byte EEPROM_ADDRESS_OPERATION_MODE = 8 ; Last chosen operation mode. ; EEPROM special values; const word EEPROM_NO_WORD_DATA = 0xFFFF const byte EEPROM_NO_BYTE_DATA = 0xFF ; ========================= Functions and Procedures ========================== ; Read one byte from the given EEPROM address and return it function eeprom_read_byte(byte in ee_address) return byte is EEADRL = ee_address ; The following two bits must be cleared for EEPROM data access. EECON1_EEPGD = FALSE EECON1_CFGS = FALSE ; Activate Read EECON1_RD = TRUE return EEDATL end function ; Write one byte to the EEPROM on the given address. procedure eeprom_write_byte(byte in ee_address, byte in ee_data) is var bit interrupt_save ; It is important to disable all interrupts during write ; The routine waits until the Write cycle is complete. EEADRL = ee_address EEDATL = ee_data ; The following two bits must be cleared for EEPROM data access. EECON1_EEPGD = FALSE EECON1_CFGS = FALSE ; Writes have to be enabled. EECON1_WREN = TRUE ; Interrupt must be disabled before special write sequence is activated. interrupt_save = INTCON_GIE INTCON_GIE = FALSE ; Do the special write sequence to write data to EEPROM. EECON2 = 0x55 EECON2 = 0xAA EECON1_WR = TRUE ; Interrupt can be enabled again. INTCON_GIE = interrupt_save ; It is safe to disable next writes. EECON1_WREN = FALSE ; Wait for write to complete, write bit will be cleared when write is done. while EECON1_WR loop end loop end procedure ; Read one word from the given EEPROM address and return it function eeprom_read_word(byte in ee_address) return word is var word ee_word var byte ee_byte_low at ee_word var byte ee_byte_high at ee_word + 1 ee_byte_low = eeprom_read_byte(ee_address) ee_byte_high = eeprom_read_byte(ee_address + 1) return ee_word end function ; Write one word to the EEPROM on the given address. procedure eeprom_write_word(byte in ee_address, word in ee_data) is var byte ee_byte_low at ee_data var byte ee_byte_high at ee_data + 1 eeprom_write_byte(ee_address, ee_byte_low) eeprom_write_byte(ee_address + 1, ee_byte_high) end procedure