can anyone help with ds3231(rtc) + 24c32(eeprom)

Thread Starter

devjeetmandal

Joined May 7, 2017
48
hello everyone,

i am trying to interface at24c32 eeprom which is given with ds3231 rtc module.

here is my twi.c file
C:
void Twi_Init()
{
    TWSR=0x00;
    TWBR=0x46;
    TWCR=0x04;
}

void Twi_Start()
{
    TWCR = ((1<<TWINT) | (1<<TWSTA) | (1<<TWEN));
    while (!(TWCR & (1<<TWINT)));  
}


void Twi_Stop(void)
{
    TWCR = ((1<< TWINT) | (1<<TWEN) | (1<<TWSTO));
    _delay_us(100) ;
}


void Twi_Write(uint8_t value)
{
       TWDR = value ;
       TWCR = ((1<< TWINT) | (1<<TWEN));
       while (!(TWCR & (1 <<TWINT)));
}

uint8_t Twi_Read(uint8_t ack)
{
    TWCR = ((1<< TWINT) | (1<<TWEN) | (ack<<TWEA));
    while ( !(TWCR & (1 <<TWINT)));
    return TWDR;
}
here is how m accessing twi
C:
void ds3231_EEP_WriteData(unsigned char MEM_Address,unsigned char value)
{
    unsigned int temp;
    Twi_Start();
    Twi_Write(DS3231_EEP_W_Address);
    temp = MEM_Address >> 8;
    Twi_Write(temp);
    Twi_Write(MEM_Address);
    Twi_Write(value);
    //value = DecimalToBcd(value);
    Twi_Stop();
    _delay_ms(20);
}

unsigned char ds3231_EEP_ReadData(unsigned char MEM_Address)
{
    unsigned char TwiStatus;
    unsigned int temp;
    Twi_Start();
    Twi_Write(DS3231_EEP_W_Address);
    temp = MEM_Address >> 8;
    Twi_Write(temp);
    Twi_Write(MEM_Address);
    Twi_Start();
    Twi_Write(DS3231_EEP_R_Address);
    TwiStatus = Twi_Read(0);  // nack
    //TwiStatus = BcdToDecimal(TwiStatus);
    Twi_Stop();
    return TwiStatus;
}
and i am using following address
C:
#define DS3231_EEP_Address    0xA0
#define DS3231_EEP_W_Address  DS3231_EEP_Address | 0x00
#define DS3231_EEP_R_Address  DS3231_EEP_Address | 0x01

but i am not able to get the result. please help.
thank you

Mod edit: code tags
 
Last edited by a moderator:
Top