Modifications

Sauter à la navigation Sauter à la recherche
3 209 octets ajoutés ,  31 décembre 2016 à 11:50
Ligne 271 : Ligne 271 :     
Notice how later lines combine multiple LEDs to get different colors, like calling {{fname|set_color(1.0, 0.0, 1.0)}} to combine '''red''' and '''blue''' LEDs for a '''magenta/violet''' color.
 
Notice how later lines combine multiple LEDs to get different colors, like calling {{fname|set_color(1.0, 0.0, 1.0)}} to combine '''red''' and '''blue''' LEDs for a '''magenta/violet''' color.
 +
 +
== Avec un MCP23017 + LCD ==
 +
Si vous avez suivi le plan de montage avec le MCP23017 alors c'est cette partie du tutoriel qui vous intéresse.
 +
 +
{{traduction}}
 +
The '''char_lcd_mcp.py''' file in the library's examples folder demonstrates the usage of a character LCD (non-RGB version) with an MCP23017 IO extender. Run the script by executing this command inside the examples directory:
 +
 +
sudo python char_lcd_mcp.py
 +
 +
You should see a simple demo of the LCD displaying text, cursors, and scrolling just like the '''char_lcd.py''' demo described on the previous page.
 +
 +
If you open '''char_lcd_mcp.py''' in a text editor you can see how to use the character LCD library with the MCP chip. Below is an overview of the file:
 +
 +
<syntaxhighlight lang="python">
 +
import math
 +
import time
 +
 +
import Adafruit_CharLCD as LCD
 +
import Adafruit_GPIO.MCP230xx as MCP
 +
</syntaxhighlight>
 +
 +
First the required modules are imported. Notice the addition of the Adafruit_GPIO.MCP230xx module (imported under the name MCP). This MCP230xx class will be used to talk to the MCP chip and interface with the LCD.
 +
 +
<syntaxhighlight lang="python">
 +
# Define MCP pins connected to the LCD.
 +
lcd_rs        = 0
 +
lcd_en        = 1
 +
lcd_d4        = 2
 +
lcd_d5        = 3
 +
lcd_d6        = 4
 +
lcd_d7        = 5
 +
lcd_backlight = 6
 +
# Optionally if the backlight is not controllable then set:
 +
# lcd_backlight = None
 +
 +
# Define LCD column and row size for 16x2 LCD.
 +
lcd_columns = 16
 +
lcd_rows    = 2
 +
 +
# Alternatively specify a 20x4 LCD.
 +
# lcd_columns = 20
 +
# lcd_rows    = 4
 +
</syntaxhighlight>
 +
 +
Next the LCD pins are defined in the script. '''Note that these pin numbers are the MCP chip GPIO pin numbers, and NOT Raspberry Pi/BeagleBone Black pin numbers!'''
 +
 +
<syntaxhighlight lang="python">
 +
# Initialize MCP23017 device using its default 0x20 I2C address.
 +
gpio = MCP.MCP23017()
 +
 +
# Alternatively you can initialize the MCP device on another I2C address or bus.
 +
# gpio = MCP.MCP23017(0x24, busnum=1)
 +
</syntaxhighlight>
 +
 +
Next an instance of the MCP23017 class is created. By default this class will use the I2C address 0x20 (default for MCP chips) and appropriate I2C bus for the running development board. However if you need to override the address or bus number you can see in the commented line how these are passed as parameters.
 +
 +
Also note if you're using an MCP23008 chip you can instead create an instance of the '''MCP23008''' class. This class is exactly the same as the '''MCP23017''' class, but only supports 8 GPIO pins.
 +
 +
Now the Adafruit_CharLCD class instance is created. The big difference with this line and previous examples is that the MCP23017 class (created with the name 'gpio') is passed in as the gpio parameter. By passing in an explicit gpio parameter, the char LCD class will use that GPIO class for talking to the LCD instead of the default development board GPIO pins.
 +
 +
The rest of the example code is exactly the same as the non-MCP character LCD example. You only need to change the setup and initialization of the character LCD class to use the MCP IO extender with an LCD!
 +
 +
{{ambox|text=Note you can also use an RGB character LCD with an MCP IO extender, however the MCP IO extender does NOT support PWM control of the backlight!}}
    
== L'exemple Horloge + IP ==
 
== L'exemple Horloge + IP ==
29 917

modifications

Menu de navigation