Skip to content

Commit 0f1eeb6

Browse files
SMBus note
1 parent c6e5575 commit 0f1eeb6

File tree

4 files changed

+82
-14
lines changed

4 files changed

+82
-14
lines changed

docs/source/gui.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ Graphical interface
33

44
*EasyMCP2221 Workbench* is a graphical application to configure and play with MCP2221 and MCP2221A chips based on EasyMCP2221 library.
55

6-
https://github.com/electronicayciencia/EasyMCP2221-GUI
7-
6+
`EasyMCP2221 Workbench GitHub <https://github.com/electronicayciencia/EasyMCP2221-GUI>`_ (source code, screenshots and pre-compiled binaries for Windows available).
87

98
.. figure:: https://github.com/electronicayciencia/EasyMCP2221-GUI/raw/main/screenshots/main_window.png
109

docs/source/history.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ V1.7.1
1212

1313
I2C:
1414
* Set I2C default speed to 100kHz. In some chips, the default speed is 500kHz and can cause trouble with some slave devices or noisy buses. You can adjust it with :func:`I2C_speed` function.
15-
* Add clock frequency parameter in SMBus class.
15+
* Added clock frequency parameter in SMBus class.
1616

1717
Misc:
1818
* Added function :func:`revision` to get the mayor and minor hardware and firmware revision.

docs/source/smbus.rst

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Usage:
1212
1313
from EasyMCP2221 import SMBus
1414
15-
bus = SMBus(1)
15+
bus = SMBus()
1616
1717
1818
or
@@ -21,11 +21,19 @@ or
2121
2222
from EasyMCP2221 import smbus
2323
24-
bus = smbus.SMBus(1)
24+
bus = smbus.SMBus()
25+
26+
27+
.. note::
28+
29+
To use other mcp functions in addition to SMBus, do not create a new MCP Device.
30+
It will interfere with existing bus resulting in unpredictable behavior.
31+
Always re-use ``bus.mcp`` object (see `example 2`).
2532

2633

27-
Example
28-
-------
34+
35+
Example 1: Basic weather station
36+
--------------------------------
2937

3038
In this example, we are using a library from `Pimoroni/BME280 <https://github.com/pimoroni/bme280-python>`_ to read Temperature, Barometric Pressure and Relative Humidity from a BME280 sensor.
3139

@@ -69,6 +77,70 @@ Output:
6977
...
7078
7179
80+
81+
Example 2: Real Time Clock with LCD
82+
-----------------------------------
83+
84+
This is a digital clock with two I2C chips:
85+
86+
* DS1307 as RTC
87+
* LCD display based on with PCF8574 I2C adapter.
88+
89+
It also shows how to re-use ``mcp`` object to further configure MCP2221.
90+
91+
Main loop:
92+
93+
- DS1307 is configured as 1Hz square oscillator.
94+
- MCP2221's GP2 is configured as Interrupt on Change.
95+
- The rising edge of DS1307's output triggers the update cycle.
96+
97+
Full code on `EasyMCP2221 examples/clock <https://github.com/electronicayciencia/EasyMCP2221/tree/master/examples/clock>`_
98+
99+
100+
.. code-block:: python
101+
102+
from EasyMCP2221 import SMBus
103+
from lcd_driver import LCD
104+
from DS1307 import DS1307
105+
106+
# Create SMBus and instances
107+
bus = SMBus()
108+
lcd = LCD(bus, addr=0x3F)
109+
ds = DS1307(bus, addr=0x68)
110+
111+
bus.mcp.I2C_speed(100_000) # DS1307 only supports 100kHz
112+
113+
bus.mcp.set_pin_function(
114+
gp0 = "GPIO_IN", # unused
115+
gp1 = "IOC", # trigger update LCD each second
116+
gp2 = "DAC", # simulate backup battery
117+
gp3 = "LED_I2C") # i2c traffic indicator
118+
119+
bus.mcp.DAC_write(21) # about 3.28V with 5V Vcc
120+
bus.mcp.IOC_config(edge = "rising")
121+
122+
# Initialization after a complete power loss
123+
if ds.halted():
124+
ds.write_now()
125+
ds._write(0x07, 0b0001_0000) # sqwe 1Hz
126+
print("RTC initialized with current timestamp")
127+
else:
128+
print("RTC was already initialized")
129+
130+
lcd.clear()
131+
132+
# Update only when GP1 changes using Interrupt On Change
133+
while True:
134+
if bus.mcp.IOC_read():
135+
bus.mcp.IOC_clear()
136+
(year, month, day, dow, hours, minutes, seconds) = ds.read_all()
137+
138+
lcd.display_string("%02d/%02d/20%02d" % (day, month, year), 1)
139+
lcd.display_string("%02d:%02d:%02d" % (hours, minutes, seconds), 2)
140+
141+
142+
143+
72144
Full reference
73145
--------------
74146

examples/clock/clock.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
bus.mcp.I2C_speed(100_000) # DS1307 only supports 100kHz
1818

1919
bus.mcp.set_pin_function(
20-
gp0 = "GPIO_OUT", # updating monitor
21-
gp1 = "IOC", # update LCD each second
22-
gp2 = "GPIO_IN", # unused
23-
gp3 = "DAC") # simulate backup battery
20+
gp0 = "GPIO_IN", # unused
21+
gp1 = "IOC", # trigger update LCD each second
22+
gp2 = "DAC", # simulate backup battery
23+
gp3 = "LED_I2C") # i2c traffic indicator
2424

2525
bus.mcp.DAC_write(21) # about 3.28V with 5V Vcc
2626
bus.mcp.IOC_config(edge = "rising")
@@ -39,10 +39,7 @@
3939
while True:
4040
if bus.mcp.IOC_read():
4141
bus.mcp.IOC_clear()
42-
bus.mcp.GPIO_write(gp0 = True) # updating start
4342
(year, month, day, dow, hours, minutes, seconds) = ds.read_all()
4443

45-
#print(year, month, day, dow, hours, minutes, seconds)
4644
lcd.display_string("%02d/%02d/20%02d" % (day, month, year), 1)
4745
lcd.display_string("%02d:%02d:%02d" % (hours, minutes, seconds), 2)
48-
bus.mcp.GPIO_write(gp0 = False) # updating end

0 commit comments

Comments
 (0)