• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinoPythonUsing Python to connect RTC (I2C) to pcDuino2
Previous Next

Using Python to connect RTC (I2C) to pcDuino2

Posted by: admin , February 12, 2015

This was contributed by Wojtek.

Few days before I got my PcDuino and having some experiences before with other A10 boards, I tried to connect RTC module with DS1307 chip.
First I had to install i2c-tools and python-smbus library.

ubuntu@ubuntu:~$ sudo apt-get install i2c-tools python-smbus

Now, we can test if I2C buses are available on our board and what devices are on bus no. 2

ubuntu@ubuntu:~$ i2cdetect -l
i2c-0   i2c             sun4i-i2c.0                             I2C adapter
i2c-1   i2c             sun4i-i2c.1                             I2C adapter
i2c-2   i2c             sun4i-i2c.2                             I2C adapter
ubuntu@ubuntu:~$ i2cdetect -y 2
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

The device with address 0x68 is the DS1307. So, let’s play with python to initialise it and then read the date and time.
Small piece of code showing time and date initializing:

 

#!/usr/bin/python

import smbus

bus = smbus.SMBus(2)  # bus number 2

bus.write_byte_data(0x68, 0x00, 0x00) # set seconds and start clock
bus.write_byte_data(0x68, 0x01, 0x38) # set minutes
bus.write_byte_data(0x68, 0x02, 0x13) # set hours in 24 hour mode
bus.write_byte_data(0x68, 0x03, 0x04) # set day of week
bus.write_byte_data(0x68, 0x04, 0x07) # set date
bus.write_byte_data(0x68, 0x05, 0x03) # set month
bus.write_byte_data(0x68, 0x06, 0x13) # set year - last 2 digits

Clock is working, so we may read the time and date.

#!/usr/bin/python

import smbus

def BCD2Up( bcd ):    # function to cut 4 upper bits from byte
        return (str(bcd >> 4))

def BCD2Lo( bcd ):     # function to cut 4 lower bits from byte
        return (str(bcd & 0x0F))

bus = smbus.SMBus(2)  # bus number 2

czas = []
data = []

# read raw data from DS1307
sec = bus.read_byte_data(0x68, 0)
min = bus.read_byte_data(0x68, 1)
hour = bus.read_byte_data(0x68, 2)
day = bus.read_byte_data(0x68, 3)
date = bus.read_byte_data(0x68, 4)
month = bus.read_byte_data(0x68, 5)
year = bus.read_byte_data(0x68, 6)

#       print "RAW - ", hour, min, sec
#       print "RAW - ", day, date, month, year
# convert to strings
czas.append(BCD2Up(hour & 0x3F))
czas.append(BCD2Lo(hour & 0x3F))
czas.append(BCD2Up(min))
czas.append(BCD2Lo(min))
czas.append(BCD2Up(sec))
czas.append(BCD2Lo(sec))

data.append(BCD2Up(date))
data.append(BCD2Lo(date))
data.append(BCD2Up(month))
data.append(BCD2Lo(month))
data.append(BCD2Up(year))
data.append(BCD2Lo(year))

sc = czas[0] + czas[1] + ':' + czas[2] + czas[3] + ':' + czas[4] + czas[5]
sd = data[0] + data[1] + '/' + data[2] + data[3] + '/20' + data[4] + data[5]

print "Current time is: " + sc + "   " + sd

Share!
Tweet

admin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors