46 lines
813 B
C
46 lines
813 B
C
#include "device_address.h"
|
|
|
|
uint8_t EEPROM_ReadByte(uint32_t adr)
|
|
{
|
|
while (EECR & (1<<EEPE));
|
|
EEAR = adr;
|
|
EECR |= (1<<EERE);
|
|
return EEDR;
|
|
}
|
|
|
|
void EEPROM_WriteByte(uint32_t adr, uint8_t value)
|
|
{
|
|
while (EECR & (1<<EEPE));
|
|
EEAR = adr;
|
|
EEDR = value;
|
|
EECR |= (1<<EEMPE);
|
|
EECR |= (1<<EEPE);
|
|
}
|
|
|
|
uint8_t get_device_address(void)
|
|
{
|
|
if(is_valid_device_address())
|
|
{
|
|
return EEPROM_ReadByte(0);
|
|
}
|
|
else
|
|
{
|
|
return get_default_address();
|
|
}
|
|
}
|
|
|
|
void write_device_address(uint8_t address)
|
|
{
|
|
EEPROM_WriteByte(0, address);
|
|
EEPROM_WriteByte(1, ~address);
|
|
}
|
|
|
|
uint8_t get_default_address(void)
|
|
{
|
|
return DEFAULT_MODBUS_ADDRESS;
|
|
}
|
|
|
|
int is_valid_device_address(void)
|
|
{
|
|
return ((EEPROM_ReadByte(0) ^ EEPROM_ReadByte(1)) == 0xFF);
|
|
} |