Modifications

Sauter à la navigation Sauter à la recherche
7 447 octets ajoutés ,  13 mars 2017 à 20:43
Page créée avec « {{RASP-FT232H-NAV}} Using the GPIO pins on the FT232H board is easy with the Python GPIO library that was installed. To demonstrate the usage I'll show a simple example o... »
{{RASP-FT232H-NAV}}

Using the GPIO pins on the FT232H board is easy with the Python GPIO library that was installed. To demonstrate the usage I'll show a simple example of blinking an LED and reading a digital input.

To get started you'll need the following parts:
* Assembled FT232H breakout board.
* One LED of any color.
* A small resistor between ~330-1000 Ohms to limit current through the LED.
* Jumper wires & breadboard.

Connect the parts as follows:
* FT232H C0 to the LED anode (longer leg)
* LED cathode (shorter leg) to one leg of the resistor.
* Other resistor leg to FT232H GND.
* FT232H D7 to FT232H GND. This jumper wire will switch between reading a low value or high value on the D7 input by moving it between FT232H GND and 5V.

You can see a photo of this setup below:

{{ADFImage|RASP-FT232H-MPSSE-Usage-GPIO.jpg}}

With this configuration pin '''C0''' will be a digital output that controls if the LED is on or off, depending on the level of the '''C0''' output. Pin '''D7''' will be a digital input that reads if it's at a high level (3-5 volts) or low level (ground).

Now create a file named '''gpio_test.py''' in a text editor and fill it with the following Python code:

<syntaxhighlight lang="python">
# Import standard Python time library.
import time

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

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

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

# 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.

# 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)
# 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!'
</syntaxhighlight>

Save the file and then open a command line terminal and navigate to the folder with gpio_test.py. Run the script by executing on Windows:

<syntaxhighlight lang="bash">
python gpio_test.py
</syntaxhighlight>

Or on Mac OSX or Linux run the script as root by executing:

<syntaxhighlight lang="bash">
sudo python gpio_test.py
</syntaxhighlight>

You should see the LED start to blink once a second, and the state of the D7 input is also printed. For example if D7 is connected to ground you'll see:

<syntaxhighlight lang="bash">
Press Ctrl-C to quit.
Pin D7 is LOW!
Pin D7 is LOW!
Pin D7 is LOW!
</syntaxhighlight>

Try moving the jumper wire for D7 from ground to 5 volts. You should see the input start to read a high value:

<syntaxhighlight lang="bash">
Pin D7 is HIGH!
Pin D7 is HIGH!
Pin D7 is HIGH!
</syntaxhighlight>

Swap the jumper wire between ground and 5 volts to see the input value change.

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

<syntaxhighlight lang="python">
# Import standard Python time library.
import time

# Import GPIO and FT232H modules.
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.FT232H as FT232H
</syntaxhighlight>

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.

<syntaxhighlight lang="python">
# Temporarily disable the built-in FTDI serial driver on Mac & Linux platforms.
FT232H.use_FT232H()
</syntaxhighlight>

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.

<syntaxhighlight lang="python">
# Create an FT232H object that grabs the first available FT232H device found.
ft232h = FT232H.FT232H()
</syntaxhighlight>

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!

<syntaxhighlight lang="python">
# 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.
</syntaxhighlight>

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 <font color="orange">'''0 to 7'''</font> for <font color="orange">'''D0 to D7'''</font>, and <font color="teal">'''8 to 15'''</font> for <font color="teal">'''C0 to C7'''</font>.

<syntaxhighlight lang="python">
# 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)
</syntaxhighlight>

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).

<syntaxhighlight lang="python">
# 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!'
</syntaxhighlight>

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.

{{RASP-FT232H-TRAILER}}
29 973

modifications

Menu de navigation