RASP-FT232H-MPSSE-Usage-GPIO

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

Il est facile de contrôler les broches GPIO de la carte FT232H en utilisant la bibliothèque "Python GPIO d'Adafruit". Maintenant que cette bibliothèque est installée, nous allons en démontrer l'usage avec des exemples simples. Nous allons faire clignoter une LED et lire une entrée digitale.

Nous allons commencer avec les composants suivants:

  • Une carte FT232H avec connecteur assemblé.
  • Une LED (couleur de votre choix).
  • Une résistance avec une valeur située entre 330 Omhs et 1000 Ohms (elle limitera le courant à travers la LED).
  • Des fils de prototypage et un breadboard.

Connectez les éléments comme suit:

  • La broche C0 du FT232H vers l'anode de la LED (broche +, la patte la plus longue)
  • La cathode de la LED (broche -, la patte la moins longue sur une des pattes de la résistance.
  • L'autre patte de la résistance sur la broche GND (la masse) du FT232H.

Ensuite, nous allons utiliser un fil pour simuler un bouton (enfoncé ou non)

  • La broche D7 du FT232H vers ma broche GND du FT232H.
    Ce pontage permettra de passer de créer:
    • Une valeur LOW (niveau bas) lorsque D7 est branché à la masse
    • Une valeur HIGH (niveau haut) lorsque D7 est branché sur +5V.

Vous pouvez voir ce montage sur la photo suivante:

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com

Dans la configuration ci-dessus, la broche C0 sera une broche de sortie qui allumera/eteindra la LED (en fonction du niveau logique de la sortie C0. La broche D7 sera une entrée digitale utilisée pour savoir si nous sommes au niveau haut (HIGH, une tension entre 3 et 5V) ou au niveau bas (la masse/GND).

Nous allons maintenant créer un fichier nommé gpio_test.py avec un éditeur de texte et nous allons remplir ce fichier avec le code suivant:

# import de la bibliothèque standard python time.
import time

# Import des modules GPIO et FT232H.
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.FT232H as FT232H

# Désactivation temporaire du pilote FTDI série sur les plateformes Mac et Linux.
FT232H.use_FT232H()

# Créer un objet FT232H (qui capture le premier périphérique FT232H disponible).
ft232h = FT232H.FT232H()

# Configurer les entrée et sortie digitales en utilisant ma méthode setup()
# Note: les broches de 0 a 15 correspondent au broches D0 à D7 puis C0 à C7 de la carte.
ft232h.setup(7, GPIO.IN)   # Make pin D7 a digital input.
ft232h.setup(8, GPIO.OUT)  # Make pin C0 a digital output.

# Bouche infinie qui change l'état de la LED (allumé/éteind) et lit l'état de l'entrée.
# Presser Ctrl-C pour quitter le programme
print 'Press Ctrl-C to quit.'
while True:
	# Mettre la broche C0 au niveau haut (HIGH) pour allumer la LED.
	ft232h.output(8, GPIO.HIGH)
	# Attendre 1 seconde.
	time.sleep(1)
	# Mettre la broche C0 au niveau base (LOW) pour eteindre la LED.
	ft232h.output(8, GPIO.LOW)
	# Attendre 1 seconde.
	time.sleep(1)
	# Lire l'etat de la broche D7 et afficher si le niveau est haut (HIGH) ou bas (LOW).
	level = ft232h.input(7)
	if level == GPIO.LOW:
		print 'Pin D7 is LOW!'
	else:
		print 'Pin D7 is HIGH!'

Sauvez le fichier puis ouvrez un terminal de commande.

Utilisez la commande cd pour vous rendre dans le répertoire où le fichier gpio_test.py est stocké.

Exécutez le script en exécutant la commande suivante sous Windows:

python gpio_test.py

Ou la commande suivante sous Mac OSX ou Linux (nous utilisons sudo pour avoir les droits administrateurs):

sudo python gpio_test.py

Vous devriez voir la LED commencer à clignoter une fois par seconde et afficher régulièrement l'état de la broche D7.

Par exemple, si la broche D7 est connectés sur la masse, vous devriez voir:

Press Ctrl-C to quit.
Pin D7 is LOW!
Pin D7 is LOW!
Pin D7 is LOW!

Essayez de déplacer le fil que vous avez branché sur D7 de la masse (GND) à 5 Volts. Vous devriez voir les lectures de la valeur d'entrée passer à 'HIGH' (niveau haut):

Pin D7 is HIGH!
Pin D7 is HIGH!
Pin D7 is HIGH!

Basculez de nouveau le fil de +5Volts vers la masse (GND) et la valeur lue repassera de nouveau vers 'LOW' (niveau bas).

Let's look a little more closely at the code to understand how reading and writing digital GPIO works.

# Import standard Python time library.
import time

# Import GPIO and FT232H modules.
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.FT232H as FT232H

First the required modules are loaded for this script. The time module will be used to delay for a short period of time.

The Adafruit_GPIO and Adafruit_GPIO.FT232H modules will be imported with shorter names using the 'as' keyword. These modules have all the logic for reading and writing GPIO on the FT232H.

# Temporarily disable the built-in FTDI serial driver on Mac & Linux platforms.
FT232H.use_FT232H()

Next the use_FT232H() function is called to temporarily disable any FTDI serial drivers. This command is necessary on Mac or Linux platforms because the libftdi library will interfere with the built-in FTDI serial drivers.

You don't really need to run this command on Windows because the FTDI serial driver was disabled using the Zadig tool, however it can't hurt to call the function as it will do nothing on Windows.

# Create an FT232H object that grabs the first available FT232H device found.
ft232h = FT232H.FT232H()

Now an FT232H object is created and assigned to the ft232h variable. This will detect the first available FT232H device connected to the computer and initialize its MPSSE for use with GPIO.

Make sure the use_FT232H() function was previously called or else this function will fail!

# Configure digital inputs and outputs using the setup function.
# Note that pin numbers 0 to 15 map to pins D0 to D7 then C0 to C7 on the board.
ft232h.setup(7, GPIO.IN)   # Make pin D7 a digital input.
ft232h.setup(8, GPIO.OUT)  # Make pin C0 a digital output.

Next the setup() function is called on the FT232H object. This function takes two parameters, the first is the pin number and the second is either GPIO.IN or GPIO.OUT to set the pin as a digital input or output.

Remember the pin numbers are 0 to 7 for D0 to D7, and 8 to 15 for C0 to C7.

# Loop turning the LED on and off and reading the input state.
print 'Press Ctrl-C to quit.'
while True:
	# Set pin C0 to a high level so the LED turns on.
	ft232h.output(8, GPIO.HIGH)
	# Sleep for 1 second.
	time.sleep(1)
	# Set pin C0 to a low level so the LED turns off.
	ft232h.output(8, GPIO.LOW)
	# Sleep for 1 second.
	time.sleep(1)

Now an infinite loop is entered and the LED is turned on and off using the output() function on the FT232H object. This function takes two parameters, the first is the pin number and the second is GPIO.HIGH/True to set the pin to a high level (3.3 volts), or GPIO.LOW/False to set the pin to a low level (ground).

	# Read the input on pin D7 and print out if it's high or low.
	level = ft232h.input(7)
	if level == GPIO.LOW:
		print 'Pin D7 is LOW!'
	else:
		print 'Pin D7 is HIGH!'

Finally the digital input is read using the input() function on the FT232H object. This function takes one parameter, the pin number to read. The function will return GPIO.LOW/False if the input is at a low level (below about 0.8 volts), and GPIO.HIGH/True if the input is at a high level (above about 0.8 volts, up to 5V max).

That's all there is to use GPIO on the FT232H board! You can use these GPIO pins to turn on and off devices or LEDs, or read switches or pins from other chips.

Be aware that the output pins on the FT232H are only designed to source a few milliamps of current (up to about 16mA per pin). If you need to drive devices that take a lot of current, look into using transistors to switch higher amounts of current.


Source: Adafruit FT232H Breakout Add a serial protocol 'swiss army knife' to your computer and talk directly to devices with SPI, I2C, serial UART, GPIO's, and more!
Créé par Toni DiCola pour AdaFruit Industries.

Traduction réalisée par Meurisse D pour MCHobby.be.

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com