title: Controlling the real-time clock module PCF8563 with python over i2c - understanding smbus
body: <p>I am trying to control a real-time clock module PCF8563 with python. I connected the module to my Raspberry Pi and I am able to read the clock from the module. I would like to use the internal alarm functionality of the module and control this feature from python.</p>
  
  <p>There are two Python libraries I know, <a href="https://github.com/switchdoclabs/RTC_SDL_PCF8563" rel="nofollow">one here</a> and <a href="https://github.com/abelectronicsuk/AlarmPi" rel="nofollow">one here</a>.</p>
  
  <p>The first one works great but lacks the support for the alarm functionality. The second one cover most of the functions but rely on quick2wire library which I do not know and do not want to use. I actually would like to improve the first one.</p>
  
  <p>I found also a <a href="http://tronixstuff.com/2013/08/13/tutorial-arduino-and-pcf8563-real-time-clock-ic/" rel="nofollow">good documented how-to</a> related to this module, but sadly it is for Arduino.</p>
  
  <p>My current problem is, how can I <strong>read and set specific bits</strong> from the bytes I get from the registries over the i2c bus. With:</p>
  
  <pre><code>import smbus
  
  bus = smbus.SMBus(0)
  returndata = bus.read_byte_data(0x51, 0x01)
  </code></pre>
  
  <p>I get one byte from registry 0x01. I need to translate the return vale from bcd to decimal, but how can I get the third bit of this byte ? And how can I change it.</p>
  
  <p>Taken from the Arudino how-to:</p>
  
  <blockquote>
    <p>... check bit 3 of the register at 0x01 (the “AF” alarm flag bit). If it’s 1 – it’s alarm time! Then you can turn the alarm off by setting that bit to zero. Using hardware, first set bit 1 of register 0x01 to 1 – then whenever an alarm occurs, current can flow into pin 3 of the PCF8563.</p>
  </blockquote>