|
12 | 12 |
|
13 | 13 | from EasyMCP2221 import SMBus |
14 | 14 |
|
15 | | - bus = SMBus(1) |
| 15 | + bus = SMBus() |
16 | 16 |
|
17 | 17 |
|
18 | 18 | or |
|
21 | 21 |
|
22 | 22 | from EasyMCP2221 import smbus |
23 | 23 |
|
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`). |
25 | 32 |
|
26 | 33 |
|
27 | | -Example |
28 | | -------- |
| 34 | + |
| 35 | +Example 1: Basic weather station |
| 36 | +-------------------------------- |
29 | 37 |
|
30 | 38 | 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. |
31 | 39 |
|
@@ -69,6 +77,70 @@ Output: |
69 | 77 | ... |
70 | 78 |
|
71 | 79 |
|
| 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 | +
|
72 | 144 | Full reference |
73 | 145 | -------------- |
74 | 146 |
|
|
0 commit comments