in particular I don't understand what make : ctrl &= ~(0x1 << (alarm_num - 1));
Maybe the whole statement needs to clear bit in ctrl register but I need to understand the single parts of statement for example what make (alarm_num - 1).
Many thanks to all
This is saying, based on the alarm number, which bit in the register to set to zero.
The tilda (~) is a bit inversion symbol. The (<<) is a left shift operation. The idea there is to create a bit mask which just set the desired bit to the desired value when "anded" with its original value.
Edit
For example, the bitmask to set A2IE in the control register to 0 when "anded" with the original value in the register (that is we touch only the desired bit and leave everything else as it is) looks like this: 0b11111101
To get there, we subtract 1 from the alarm number because the alarm numbers start from 1 and the register bit numbers start from zero. Then we left shift a '1' one bit to yield 0b00000010. Then we invert all the bits to yield 0b11111101. Thus we have the bit mask.
Good question. The data sheet appears to have given these alarms names like A1, A2 etc. but could have easily used names like A0, A1 etc.
I suppose it is something to do with the first alarm, the second alarm etc. Anyway, it is just a naming convention.
Well, you can see what has happened. A real time clock needs to manage data like days, hours, minutes seconds etc. However, it uses byte registers (8 bits). But the numerical representation of say minutes, which has a value range of 0 to 59, requires only 7 bits. So there is one spare bit there. So a design decision has been made to tuck one of the alarm control bits in that spare bit instead of creating a new register. It is the same with most of the other time/date variables also. That is why there is this huge struggle to get at all the data relevant to the alarm settings.
is simply saying return the value of the bit labeled RS1 (bit 3) in the register named CONTROL.
It means, extract the value of bit 1 from the variable alarm_mode.
0x02 is equivalent to 0b0000010. Bits are numbered from right to left, starting at 0.
The & operator is a bitwise AND. 0 & 0 = 0; 0 & 1 = 0 ; 1 & 0 = 0 ; 1 & 1 = 1 ;
But, for an even better explanation, find a tutorial on bit manipulation.
quote="manu68, post:7, topic:890755"]
ok.
But, now if I see
uint8_t A1M4 = (alarm_mode & 0x08) << 4
following the same way , extract the value of bit 7 from the variable alarm_mode and then shift left four positions. So "Alarm_mode" value is 1 or 0,correct?
and
uint8_t DY_DT = (alarm_mode & 0x10) what bit extract?
thanks
[/quote]
Close. Anding with 4 (B00000100)picks up bit 2 which is INTCN and checks if the bit is set for an alarm interrupt. If not set, it returns false so you know the alarm is not set in the way the code intends.
Bit 2: Interrupt Control (INTCN). This bit controls the
INT/SQW signal. When the INTCN bit is set to logic 0,
a square wave is output on the INT/SQW pin. When the
INTCN bit is set to logic 1, then a match between the timekeeping registers and either of the alarm registers activates the INT/SQW output (if the alarm is also enabled).
The corresponding alarm flag is always set regardless of
the state of the INTCN bit. The INTCN bit is set to logic 1
when power is first applied.