tlfong01 wrote: ↑
Wed Jul 25, 2018 7:49 am
This morning I shortened all the long names and the program now looks smart, as after a clean hair cut. I also walked through the program quickly and refactored things. Version 1.4 is listed below. I found more typo and format errors and would correct to Vision 1.5.
I have changed my mind not to update IOX (IO Expander) mcp23017 MCVNE to V1.5. Instead I write a MCVNE v1.5 for the RTC (Real Time Clock) DS3231.
The RTC MCVNE V1.5 uses the same I2C read/write device register functions in IOX MCVNE 1.4. Of course I can import the IOX functions to RTC MCVNE, but then RTC MCVNE is not complete. So I just copy the read/write/print functions (also the setup/debug notes) from IOX MCVNE to RTC MCVNE.
RTC MCVNE is very similar to IOX MCVNE, except the device address and device register address.
Now that I have no problem reading and writing a data byte to IOX or RTC devices, then I will have little problem configuring both devices to control anything, because all control is done by reading and writing registers. The programming is very tedious, but it is only register read and write, and it is not that difficult to google Arduino program examples and borrow or steal something for RpiZW. To morrow I will write simple operations such as reading RTC temperature, setting IOX output pin to High or Low etc.
Here is debugged, no problem, RTC MCVNE V1.5 (about 100 lines long. I number it 1.5 and not from 1.0, because it uses things from RTC MCVNE 1.4). It might take you one or two hours reading the complicated data sheet (read only the relevant paragraphs, not the whole date sheet, which might take you days to fully understand. Remember to eat the big elephant one bite at a time!)
Code: Select all
# KY019 DS3231 Real Time Clock Test Program V1.5 tlfong01 2018jul25hkt2230
# *** Short Description ***
# 1. read, write, print Day Register.
# *** Long Description ***
# 1. Read and print the old byte already in Day Register
# 2. Write a new byte to the register.
# 3. Read back the new byte.
# 4. Print the new byte.
# 5. Do it one more time.
# *** Setup/Debug Notes ***
# 1. To change I2c speed to 100Khz
# $ sudo nano /boot/config.txt
# modify the dtparam=i2c_arm_on line as below
# dtparam=i2c_arm=on, i2c_arm_baudrate=100000
# 2. I2c speeds list
# low speed = 50kHz ( 50000)
# standard speed = 100KHz (100000)
# maximum speed = 400KHz (400000)
# 3. I2C speed can only be changed at raspbain boot time. It cannot be changed using python.
# *** Import ***
# *** Config ***
# Raspberry Pi = Raspberry Pi Zero W V1.1
# Raspbian = Linux 9 (stretch 2018 April)
programTitle = 'DS3231 RTC Example Test Program V1.5'
timeNowStr = str(datetime.datetime.now())[0:16]
i2cCh1 = smbus.SMBus(1)
dataByte0x04 = 0x04
dataByte0x06 = 0x06
dvAddrByte = 0x68
dayRegAddrByte = 0x03
# *** Device Functions (copied from MCP23008 test program example) ***
def readDvRegOneByte (i2cPort, dvAddrByte, DvRegAddrByte):
readByte = i2cPort.read_byte_data ( dvAddrByte, DvRegAddrByte)
def writeDvRegOneByte (i2cPort, dvAddrByte, DvRegAddrByte, writeByte):
writeDviTwoBytes (i2cPort, dvAddrByte, DvRegAddrByte, writeByte)
def writeDviTwoBytes (i2cPort, dvAddrByte, dataByte1, dataByte2):
i2cPort.write_byte_data ( dvAddrByte, dataByte1, dataByte2)
def printDvRegOneByte (i2cPort, dvAddrByte, DvRegAddrByte, printTitle):
readByte = readDvRegOneByte (i2cPort, dvAddrByte, DvRegAddrByte)
print (printTitle, hex(readByte))
# *** Test Functions ***
printDvRegOneByte(i2cCh1, dvAddrByte, dayRegAddrByte, 'Old Byte =')
writeDvRegOneByte(i2cCh1, dvAddrByte, dayRegAddrByte, testDataByte)
printDvRegOneByte(i2cCh1, dvAddrByte, dayRegAddrByte, 'New Byte =')
print('*** TimeNow = ', timeNowStr, '***\n')
print('***', programTitle, 'Begin ***\n')
print('***', programTitle, 'End ***')
# *** Main ***
# *** Sample Output ***
*** TimeNow = 2018-07-25 22:30 ***
*** DS3231 RTC Example Test Program V1.5 Begin ***
Old Byte = 0x6
New Byte = 0x4
Old Byte = 0x4
New Byte = 0x6
*** DS3231 RTC Example Test Program V1.5 End ***
# *** End ***