Custom rasberry pi interfaces design and buid hardware interfaces for the rspberry pi

229 72 0
Custom rasberry pi interfaces design and buid hardware interfaces for the rspberry pi

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

TECHNOLOGY IN AC TION™ Custom Raspberry Pi Interfaces Design and build hardware interfaces for the Raspberry Pi — Warren Gay Custom Raspberry Pi Interfaces Design and build hardware interfaces for the Raspberry Pi Warren Gay Custom Raspberry Pi Interfaces: Design and build hardware interfaces for the Raspberry Pi Warren Gay St Catharines, Ontario, Canada ISBN-13 (pbk): 978-1-4842-2405-2 DOI 10.1007/978-1-4842-2406-9 ISBN-13 (electronic): 978-1-4842-2406-9 Library of Congress Control Number: 2017931068 Copyright © 2017 by Warren Gay This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Trademarked names, logos, and images may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein Managing Director: Welmoed Spahr Editorial Director: Todd Green Acquisitions Editor: Aaron Black Development Editor: James Markham Technical Reviewer: Massimo Nardone Coordinating Editor: Jessica Vakili Copy Editor: Kim Wimpsett Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover image designed by Freepik Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc) SSBM Finance Inc is a Delaware corporation For information on translations, please e-mail rights@apress.com, or visit http://www.apress.com/rights-permissions Apress titles may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/978-1-4842-2405-2 For more detailed information, please visit http://www.apress.com/source-code Printed on acid-free paper Contents at a Glance About the Author���������������������������������������������������������������������������� xiii About the Technical Reviewer��������������������������������������������������������� xv ■Chapter ■ 1: Introduction������������������������������������������������������������������ ■Chapter ■ 2: 3V/5V Signal Interfacing����������������������������������������������� ■Chapter ■ 3: VGA LCD Monitors������������������������������������������������������� 25 ■Chapter ■ 4: I2C LCD Displays��������������������������������������������������������� 35 ■Chapter ■ 5: MC14490 and Software Debouncing��������������������������� 55 ■Chapter ■ 6: PCF8591 ADC�������������������������������������������������������������� 67 ■Chapter ■ 7: Potentiometer Input Controls�������������������������������������� 91 ■Chapter ■ 8: Rotary Encoders�������������������������������������������������������� 103 ■Chapter ■ 9: More Pi Inputs with 74HC165����������������������������������� 129 ■Chapter ■ 10: More Pi Outputs with 74HC595������������������������������� 141 ■Chapter ■ 11: MCP23017 I/O Port Extender���������������������������������� 153 ■Chapter ■ 12: MPD/MPC Hardware Controls��������������������������������� 169 ■Chapter ■ 13: Custom Keypads����������������������������������������������������� 191 Index���������������������������������������������������������������������������������������������� 213 iii Contents About the Author���������������������������������������������������������������������������� xiii About the Technical Reviewer��������������������������������������������������������� xv ■Chapter ■ 1: Introduction������������������������������������������������������������������ Raspberry Pi and Zero�������������������������������������������������������������������������� Why GPIO Is Important����������������������������������������������������������������������������� What to Purchase������������������������������������������������������������������������������������ Software to Download����������������������������������������������������������������������������� Let’s Begin����������������������������������������������������������������������������������������������� ■Chapter ■ 2: 3V/5V Signal Interfacing����������������������������������������������� 7400 Series (TTL)������������������������������������������������������������������������������������ 3.3V Logic������������������������������������������������������������������������������������������������ Voltage Dividers�������������������������������������������������������������������������������������� 7400 Series Derivative Families������������������������������������������������������������ 11 Unused CMOS Inputs����������������������������������������������������������������������������� 12 Converting 5V to 3V Input: 74LVC245���������������������������������������������������������������������� 12 Converting 5V to 3V Input: 74LVC244���������������������������������������������������������������������� 14 CD4049/CD4050����������������������������������������������������������������������������������������������������� 16 Input Protection Diodes������������������������������������������������������������������������������������������ 16 v  ■ Contents Converting Volts to Volts with the HCT Family��������������������������������� 19 74HCT245��������������������������������������������������������������������������������������������������������������� 20 74HCT244��������������������������������������������������������������������������������������������������������������� 22 Switching Speed����������������������������������������������������������������������������������� 22 Summary����������������������������������������������������������������������������������������������� 23 Bibliography������������������������������������������������������������������������������������������ 24 ■Chapter ■ 3: VGA LCD Monitors������������������������������������������������������� 25 VGA Converters�������������������������������������������������������������������������������������� 25 Resolution and Refresh Rates��������������������������������������������������������������� 26 /boot/config.txt�������������������������������������������������������������������������������������� 27 Confirming Resolution��������������������������������������������������������������������������� 33 Summary����������������������������������������������������������������������������������������������� 33 Bibliography������������������������������������������������������������������������������������������ 34 ■Chapter ■ 4: I2C LCD Displays��������������������������������������������������������� 35 LCD Module 1602A�������������������������������������������������������������������������������� 35 I2C Serial Interface�������������������������������������������������������������������������������� 36 I2C Module Configuration���������������������������������������������������������������������� 37 I2C Module Output��������������������������������������������������������������������������������� 38 I2C Module Input����������������������������������������������������������������������������������� 41 PCF8574P Chip�������������������������������������������������������������������������������������� 42 Volts to Volts������������������������������������������������������������������������������������ 42 Attaching the I2C Serial Module������������������������������������������������������������ 44 Displaying Data������������������������������������������������������������������������������������� 46 Reading from the LCD1602������������������������������������������������������������������� 48 Class LCD1602�������������������������������������������������������������������������������������� 51 I2C Baud Rate���������������������������������������������������������������������������������������� 53 Profit and Loss�������������������������������������������������������������������������������������� 53 Summary����������������������������������������������������������������������������������������������� 54 vi  ■ Contents ■Chapter ■ 5: MC14490 and Software Debouncing��������������������������� 55 Hardware: MC14490������������������������������������������������������������������������������ 56 Chip Operation��������������������������������������������������������������������������������������� 57 Capacitor C1������������������������������������������������������������������������������������������� 57 Experiment�������������������������������������������������������������������������������������������� 58 More Inputs������������������������������������������������������������������������������������������� 61 Software Debouncing���������������������������������������������������������������������������� 62 Experiment�������������������������������������������������������������������������������������������� 64 Summary����������������������������������������������������������������������������������������������� 66 ■Chapter ■ 6: PCF8591 ADC�������������������������������������������������������������� 67 The YL-40 PCB��������������������������������������������������������������������������������������� 67 Voltage Range��������������������������������������������������������������������������������������� 70 I2C Bus�������������������������������������������������������������������������������������������������� 70 I2C Addresses���������������������������������������������������������������������������������������� 70 DAC (AOUT)�������������������������������������������������������������������������������������������� 70 Removing YL-40 LED D1������������������������������������������������������������������������ 73 Hacking YL-40 I2C Address������������������������������������������������������������������� 74 I2C Bus Setup���������������������������������������������������������������������������������������� 75 Reading from PCF8591�������������������������������������������������������������������������� 76 Experiment������������������������������������������������������������������������������������������������������������� 76 Writing to the DAC��������������������������������������������������������������������������������� 77 Experiment������������������������������������������������������������������������������������������������������������� 77 Experiment������������������������������������������������������������������������������������������������������������� 78 Limitations��������������������������������������������������������������������������������������������� 79 Extending Voltage Range����������������������������������������������������������������������� 79 Repairing the Temp Sensor������������������������������������������������������������������� 80 Conversion to Celsius���������������������������������������������������������������������������� 83 vii  ■ Contents Reading Temperature���������������������������������������������������������������������������� 84 Experiment������������������������������������������������������������������������������������������������������������� 84 The YL-40 LDR��������������������������������������������������������������������������������������� 85 Experiment������������������������������������������������������������������������������������������������������������� 85 1N914 Experiment��������������������������������������������������������������������������������� 85 Software������������������������������������������������������������������������������������������������ 89 Potential Experiments��������������������������������������������������������������������������� 90 Summary����������������������������������������������������������������������������������������������� 90 Bibliography������������������������������������������������������������������������������������������ 90 ■Chapter ■ 7: Potentiometer Input Controls�������������������������������������� 91 Potentiometers�������������������������������������������������������������������������������������� 91 Voltage Dividers������������������������������������������������������������������������������������ 93 ADC Circuit�������������������������������������������������������������������������������������������� 95 Pot Resistance��������������������������������������������������������������������������������������� 95 Taper����������������������������������������������������������������������������������������������������� 96 Effect of ADC Bits���������������������������������������������������������������������������������� 96 Experiment�������������������������������������������������������������������������������������������� 97 Applying Potentiometer Controls����������������������������������������������������������� 99 Selection Resolution���������������������������������������������������������������������������� 101 Summary��������������������������������������������������������������������������������������������� 101 Bibliography���������������������������������������������������������������������������������������� 102 ■Chapter ■ 8: Rotary Encoders�������������������������������������������������������� 103 Keyes KY-040 Rotary Encoder������������������������������������������������������������� 103 The Switch������������������������������������������������������������������������������������������ 105 Operation��������������������������������������������������������������������������������������������� 107 Voltage������������������������������������������������������������������������������������������������ 108 viii  ■ Contents Evaluation Circuit�������������������������������������������������������������������������������� 108 Interfacing to the Pi����������������������������������������������������������������������������� 110 Experiment������������������������������������������������������������������������������������������ 112 Experiment������������������������������������������������������������������������������������������ 114 Sequence Errors���������������������������������������������������������������������������������� 114 Experiment������������������������������������������������������������������������������������������ 115 FM Dial 1��������������������������������������������������������������������������������������������� 116 FM Dial 2��������������������������������������������������������������������������������������������� 117 Class Switch���������������������������������������������������������������������������������������� 119 Main Routine��������������������������������������������������������������������������������������� 126 Summary��������������������������������������������������������������������������������������������� 127 Bibliography���������������������������������������������������������������������������������������� 127 ■Chapter ■ 9: More Pi Inputs with 74HC165����������������������������������� 129 74HC165 Pinout����������������������������������������������������������������������������������� 129 Function Table������������������������������������������������������������������������������������� 131 Breadboard Experiment����������������������������������������������������������������������� 132 Program����������������������������������������������������������������������������������������������� 134 Logic Analyzer View����������������������������������������������������������������������������� 136 Profit and Loss������������������������������������������������������������������������������������ 137 Even More Inputs�������������������������������������������������������������������������������� 138 Other Bit Counts���������������������������������������������������������������������������������� 139 Combining GPIOs��������������������������������������������������������������������������������� 139 Chip Enable����������������������������������������������������������������������������������������� 139 CD4012B��������������������������������������������������������������������������������������������� 140 Summary��������������������������������������������������������������������������������������������� 140 ix  ■ Contents ■Chapter ■ 10: More Pi Outputs with 74HC595������������������������������� 141 74HC165 Pinout����������������������������������������������������������������������������������� 141 Function Table������������������������������������������������������������������������������������� 142 Breadboard Experiment����������������������������������������������������������������������� 143 Experiment Run����������������������������������������������������������������������������������� 146 Input and Output���������������������������������������������������������������������������������� 148 Additional Outputs������������������������������������������������������������������������������� 151 Profit and Loss������������������������������������������������������������������������������������ 152 Summary��������������������������������������������������������������������������������������������� 152 ■Chapter ■ 11: MCP23017 I/O Port Extender���������������������������������� 153 MCP23017������������������������������������������������������������������������������������������� 153 Wiring�������������������������������������������������������������������������������������������������� 155 Output GPIO Experiment���������������������������������������������������������������������� 157 Input Experiment��������������������������������������������������������������������������������� 158 Software Operations���������������������������������������������������������������������������� 159 I2C Header Files���������������������������������������������������������������������������������������������������� 160 Opening the I2C Driver������������������������������������������������������������������������������������������ 161 I2C Write��������������������������������������������������������������������������������������������������������������� 161 I2C Read��������������������������������������������������������������������������������������������������������������� 162 Configuration�������������������������������������������������������������������������������������������������������� 163 Interrupt Capability������������������������������������������������������������������������������ 164 Interrupt Profit and Loss���������������������������������������������������������������������� 168 Summary��������������������������������������������������������������������������������������������� 168 x Chapter 13 ■ Custom Keypads The two-digit hexadecimal number shown is effectively the “scan code” that you saw previously using the i2cget utility Recall that the left four bits select a row, and the right four represent the column selected The program then looks up the scan code to translate that into a “key name,” like S1 The main Program Listing 13-1 shows the source code for the main program Lines 113 to 119 open the I2C bus driver, saving the file descriptor i2c_fd This file descriptor provides you with a handle to the driver Listing 13-1.  The main Program of keypad.cpp 108 int 109 main(int argc,char **argv) { 110     uint8_t keypad, row, col; 111 112     // Open the I2C bus 113     i2c_fd = open(i2c_device,O_RDWR); 114     if ( i2c_fd == -1 ) { 115         fprintf(stderr,"%s: opening %s\n", 116             strerror(errno), 117             i2c_device); 118         exit(1); 119     } 120 121     for (;;) { 122         // Scan each keypad row: 123         for ( row=0x10; row != 0; row second is C++ type std::string To return a C-styled string, you convert that by invoking the method c_str() This pointer value remains valid as long as the values in keymap are left unmodified Line 101 is in place for scan codes that you don’t have defined in keymap You might think that you have them all covered, but if you press more than one key simultaneously, other scan codes are possible For example, the scan code 0xE6 is returned if you simultaneously press S1 and S4 204 Chapter 13 ■ Custom Keypads The i2c_write Function Listing 13-3 shows the i2c_write function This function is used to write one byte of information to the PCF8574 peripheral chip Listing 13-3.  The i2c_write Function 037 void 038 i2c_write(int addr,uint8_t byte) { 039     struct i2c_rdwr_ioctl_data msgset; 040     struct i2c_msg iomsgs[1]; 041     int rc; 042 043     iomsgs[0].addr = unsigned(addr);// Address 044     iomsgs[0].flags = 0;            // Write 045     iomsgs[0].buf = &byte;          // Buffer 046     iomsgs[0].len = 1;              // byte 047 048     msgset.msgs = iomsgs;           // The message 049     msgset.nmsgs = 1;               // message 050 051     rc = ioctl(i2c_fd,I2C_RDWR,&msgset); 052     if ( rc == -1 ) { 053         fprintf(stderr, 054             "%s: writing to I2C address 0x%02X\n", 055             strerror(errno), 056             i2c_keypad_addr); 057         exit(1); 058     } 059 } The Linux I2C driver requires you to use two structures • struct i2c_rdwr_ioctl_data • struct i2c_msg The first structure is a header record that describes aspects of the transfer that apply to the entire transaction You see this in lines 048 and 049, where you save the pointer to the first transaction (line 048) and then the number of consecutive transactions to be performed (line 049) The i2c_msg structure defines the aspects of each I/O transfer to be performed In the listing, line 043 first defines the I2C address involved Line 044 defines this operation as a write operation (this is because of the lack of the I2C_M_RD flag, which will be seen in the i2c_read function later) Line 045 defines where the buffer is located (the variable byte) Finally, the transfer will involve one byte (line 046) With the structures fully populated, the communication with the driver occurs through the ioctl(2) system call in line 051 The following are the arguments involved in the call: • The driver’s file descriptor (i2c_fd) 205 Chapter 13 ■ Custom Keypads • The ioctl(2) command value (I2C_RDWR macro) • Pointer to the associated data for this operation (address of msgset) The system call returns -1 if the operation failed for any reason, leaving the error code in the thread-safe value of errno Line 052 checks the status returned and reports the error in lines 053 to 056 if it fails and then exits the program in line 057 If the operation succeeds, you simply return from the function as “mission accomplished.” The i2c_read Function A companion to the i2c_write function is the i2c_read function In this application program, its mission is to simply read one byte of data (from the PCF8574 peripheral) Listing 13-4 shows the code for this function Listing 13-4.  The i2c_read Function 065 uint8_t 066 i2c_read(int addr) { 067     struct i2c_rdwr_ioctl_data msgset; 068     struct i2c_msg iomsgs[1]; 069     uint8_t byte; 070     int rc; 071 072     iomsgs[0].addr = unsigned(addr);// I2C Address 073     iomsgs[0].flags = I2C_M_RD;     // Read 074     iomsgs[0].buf = &byte;          // buffer 075     iomsgs[0].len = 1;              // byte 076 077     msgset.msgs = iomsgs;           // The message 078     msgset.nmsgs = 1;               // message 079 080     rc = ioctl(i2c_fd,I2C_RDWR,&msgset); 081     if ( rc == -1 ) { 082             fprintf(stderr, 083                     "%s: reading I2C address 0x%02X\n", 084                     strerror(errno), 085                     i2c_keypad_addr); 086             exit(1); 087     } 088     return byte;                    // Return read byte 089 } The operation of the i2c_read function is nearly identical to the i2c_write function The one difference is that the flag I2C_M_RD is provided in line 073 to indicate that this will be a read operation The only other difference is that the data byte is transferred from the peripheral to the local variable byte The operation is initiated in line 080, checked in lines 081 to 086 When the operation is successful, the value byte is returned in line 088 206 Chapter 13 ■ Custom Keypads Combination Lock To finish off this chapter, let’s create a practical keypad application—the combination lock For this project, let’s add two more components to the breadboard • A green LED to indicate unlock success • A red LED to indicate that the attempted combination has failed For the LEDs, refer to Figure 13-2, using the circuit shown on the right (the LED being sinked) Connect the green LED to the second PCF8574 (IC2) port P0 and the red LED to IC2’s P1 The only thing left is to determine the resistors needed The voltage drop VF of an LED varies with the color of the device [1] A green LED is listed between 1.9 and volts, while a red LED is 1.6 to volts Since you’re using a +3.3V supply, this means you only have a difference of VCC – VF = 3.3 – 1.9 = 1.4 Volts for green LEDs and 3.3 – 1.6 = 1.7 Volts for red The dropping resistor you need can be determined with the following: R= VCC - VF I LED Many LEDs will use about 10mA, which is well within the PCF8574 sinking capability So, calculate the following: R= 3.3 - 1.4 = 190W 0.010 The closest E12 resistance value to this is 180Ω If you have trouble finding of that value, you can safely go up to 200Ω or even go lower With R2 of Figure 13-2 set to a value of 180Ω, the resistor wired to the +3.3V side, and the other end wired to the LED, connect the green LED cathode to IC2’s P0 Do the same for the red LED, except that the LED cathode goes to P1 Before you present the software, let’s prove that the hardware is good Turn on the green LED (note that the I2C address is 0x21 this time for IC2) $ i2cset -y 0x21 0xFE If everything is wired correctly, the green LED should light, and the red one should remain dark Now enable the red LED $ i2cset -y 0x21 0xFD The red LED should light, and the green one should be dark Finally, let’s light both $ i2cset -y 0x21 0xFC To turn them both off again, write the value 0xFF to the port Note that only the last two bits actually matter here as far as the LEDs are concerned 207 Chapter 13 ■ Custom Keypads Combination Lock With the LEDs installed and tested, you can turn your attention to the combination lock software The program must implement a state machine based upon key press events The state machine you’re going to use is as follows: The initial state is that no keys have pressed yet Clear the buffer of key codes Wait for a key press Proceed to the next step when it arrives Add a key code to the buffer Have four keys been pressed? If not, go to back to step If four keys have been pressed, is the combination correct? If not, light the red LED and go to step The combination is correct: light the green LED to announce the unlocking of the device Has a key been pressed? If so, turn off the green LED and return to step Otherwise, continue to wait The program combo.cpp located in the /keypad subdirectory implements this overall procedure, except that it checks the combination code as the keys are entered You’ll examine that later Figure 13-7 illustrates my breadboard arrangement Don’t be intimidated by the number of wires Most of the wires (most of them white) are wires to ground for the PCF8574 address pins The remaining white wires take connections from the keypad to IC1 Figure 13-7.  A breadboarded circuit for the keypad.cpp and combo.cpp programs 208 Chapter 13 ■ Custom Keypads With the hardware ready, run the program combo, as shown here: $ /combo *** LOCKED *** code[0] = 7D (S14) code[1] = BB (S11) code[2] = DD (S6) code[3] = ED (S2) *** FAILED *** *** LOCKED *** code[0] = BE (S9) code[1] = D7 (S8) code[2] = DD (S6) code[3] = DB (S7) *** UNLOCKED *** *** LOCKED *** ^C The session output helps display state information as the code runs A full implementation would provide some kind of feedback for each key as it is entered You could, for example, use the remaining ports of IC2 to light an LED for each of the four codes This is left as an exercise for you Initially, the session reports that the lock is “locked.” As each key is pressed, the scan code and the key name are displayed (for demonstration purposes) At the end of four codes, the lock either fails to unlock (red LED) or becomes unlocked (green LED) The first attempt shown in the session output has an entry of S14, S11, S6, and S2 before the red LED comes on and the session reports “failed.” At this point, the state machine restarts, allowing entry of a new code The red LED is permitted to stay on until the entry of the first key code You’ll see that in the code later in this chapter In the second attempt, keys S9, S8, S6, and S7 are pressed to successfully unlock the device The green LED will remain lit until another (any) key is pressed At that point, the green LED is turned off, and the device enters a locked state once again The main Program The main program has changed to implement a key press state machine This is presented in Listing 13-5 Listing 13-5.  The combo.cpp Main Program 140 int 141 main(int argc,char **argv) { 142     static uint8_t code[4] = { 143         // 9, 8,    6     7: 144         0xBE, 0xD7, 0xDD, 0xDB 145     }; 146     uint8_t keypad, kx; 209 Chapter 13 ■ Custom Keypads 147     bool matched; 148 149     // Open the I2C bus 150     i2c_fd = open(i2c_device,O_RDWR); 151     if ( i2c_fd == -1 ) { 152         fprintf(stderr,"%s: opening %s\n", 153             strerror(errno), 154             i2c_device); 155         exit(1); 156     } 157 158     i2c_write(i2c_led_addr,0xFF);   // Both LEDs off 159 160     for (;;) { 161         puts("*** LOCKED ***"); 162 163         // Wait for entry of all four codes: 164         matched = true; 165         for ( kx=0; kx

Ngày đăng: 14/09/2020, 16:35

Mục lục

    Contents at a Glance

    About the Technical Reviewer

    Raspberry Pi 3 and Zero

    Why GPIO Is Important

    Chapter 2: 3V/5V Signal Interfacing

    Converting 5V to 3V Input: 74LVC245

    Converting 5V to 3V Input: 74LVC244

    Converting 3 Volts to 5 Volts with the HCT Family

    Chapter 3: VGA LCD Monitors

    Resolution and Refresh Rates