cmicrocontrollerreal-time-clock

PCF85063A enable second alarm register


I am developing an RTC library for Nuvoton M031SD2AE MCU. I use PCF85063A. I want to enable second alarm. In datasheet it's address is 0x0B.

This is the second alarm register.

RTC second alarm register

void set_second_alarm(void)
{
    uint8_t secondAlarmValue;
    I2C_ReadMultiBytesOneReg(I2C0,I2CADDR,SECONDALARM,secondAlarmValue,1);
    secondAlarmValue = (secondAlarmValue) & 0x7F;
    I2C_WriteMultiBytesOneReg(I2C0,I2CADDR,SECONDALARM,secondAlarmValue,1);
}

I tried to do something like this. Because datasheet says default value of that register is 1000 0000.However, I think it could cause error in future use when other bits get manipulated. I also get error because I2C_ReadMultiBytesOneReg function takes uint8_t rdata[] as parameter.

These are the I2C functions I use:

uint8_t I2C_WriteByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data);
uint32_t I2C_ReadMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t rdata[], uint32_t u32rLen);

Basically I want to write 0 to 7th bit of second alarm register to enable alarm. However, I couldn't figure out how to do it.

How can I write 0 to 7th bit of second alarm register ?


Solution

  • You have not shown the error message and the affected line. Most probably it says something like "you provided an integer, but a pointer to integers is expected." BTW, used as an argument, an array decays to a pointer to its first element.

    Instead of the expected address of the variable to store the values, and to read from, respectively, you provide the value. In C, you get the address of a variable by the operator &.

    Actually the multi-byte functions expect pointers to as many uint8_ts as the parameter u32rLen says.

    So this is the corrected version:

    void set_second_alarm(void)
    {
        uint8_t secondAlarmValue;
        I2C_ReadMultiBytesOneReg(I2C0, I2CADDR, SECONDALARM, &secondAlarmValue, 1);
        secondAlarmValue &= 0x7F;
        I2C_WriteMultiBytesOneReg(I2C0, I2CADDR, SECONDALARM, &secondAlarmValue, 1);
    }
    

    If you look close, I have simplified the clearing of bit 7. You can simplify many expressions with binary operators working on a single variable this way. Reread the related chapter of your beginner's C book.

    You did not explain, why you use multi-byte functions. Please consider to use the single-byte functions:

    void set_second_alarm(void)
    {
        uint8_t secondAlarmValue;
        secondAlarmValue = I2C_ReadByteOneReg(I2C0, I2CADDR, SECONDALARM);
        secondAlarmValue &= 0x7F;
        I2C_WriteByteOneReg(I2C0, I2CADDR, SECONDALARM, secondAlarmValue);
    }
    

    Final note: Please read the documentation of all functions to add a sensible error handling.