Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
learn:courses:microprocessor-io-unit-1:start [2017/03/14 09:45] Marthalearn:courses:microprocessor-io-unit-1:start [2021/10/13 15:24] (current) Arthur Brown
Line 1: Line 1:
-====== Unit 1: Microprocessor IO ======+====== Unit 1: Microprocessor I/O ====== 
 +[[{}/learn/courses/start|Back to Course List]]
 ==Unit 1 Labs== ==Unit 1 Labs==
-  * [[https://reference.digilentinc.com/learn/courses/microprocessor-io-lab-1a/start|Lab 1a]] +  * [[/learn/courses/microprocessor-io-lab-1a/start|Lab 1a]] 
-  * [[https://reference.digilentinc.com/learn/courses/microprocessor-io-lab-1b/start|Lab 1b]]+  * [[/learn/courses/microprocessor-io-lab-1b/start|Lab 1b]]
  
  
 == Download This Document == == Download This Document ==
-  * {{ :learn:courses:microprocessor-io-unit-1:unit-1-io.pdf |Unit 1 PDF}}+{{ :learn:courses:microprocessor-io-unit-1:unit_1.pdf |Unit 1 PDF}}
  
  
 ===== 1. Introduction and Chapter Objectives ===== ===== 1. Introduction and Chapter Objectives =====
-The goal for this series of laboratory designs is to teach not only how to manage the PIC32 processor to solve engineering design problems, but also how to produce [[https://en.wikipedia.org/wiki/Verification_and_validation|dependable (validated) and sustainable (verified)]] software using best design practices. To that end, Unit 1 will begin by introducing engineering concepts that are important in developing these best practices. Although some of these concepts may seem trivial at first, they are important in measuring the ability of a system to meet design requirements and specifications, and will be implemented throughout Units 1-10+The goal for this series of laboratory designs is to teach not only how to manage the PIC32 processor to solve engineering design problems, but also how to produce [[https://en.wikipedia.org/wiki/Verification_and_validation|dependable (validated) and sustainable (verified)]] software using best design practices. To that end, Unit 1 will begin by introducing engineering concepts that are important in developing these best practices. Although some of these concepts may seem trivial at first, they are important in measuring the ability of a system to meet design requirements and specifications, and will be implemented throughout Units 1-7
  
 ---- ----
  
-===== 2. Objectives===== +===== 2. Objectives ===== 
-  How to develop a plan for a software-based microprocessor design. +  How to develop a plan for a software-based microprocessor design. 
-  Understand the management of basic microprocessor digital inputs and outputs. +  Understand the management of basic microprocessor digital inputs and outputs. 
-  Write an application that utilizes the Basys MX3 processor platform to perform basic calculator functions and display the results on a four digit seven-segment LED display.  +  Write an application that utilizes the Basys MX3 processor platform to perform basic calculator functions and display the results on a 4-digit 7-segment LED display.  
-  Become familiar with the circuits included on the Basys MX3 PCB.+  Become familiar with the circuits included on the Basys MX3 PCB. 
  
-===== 3. Basic Knowledge: ===== 
-  * Fundamentals of digital [[https://en.wikipedia.org/wiki/Combinational_logic|combinational]] and [[https://en.wikipedia.org/wiki/Sequential_logic|sequential logic]]. 
-  * How to interpret a schematic diagram and electric circuits. 
-  * How to write a computer program using the [[http://www.tutorialspoint.com/cprogramming/index.htm|C language]]. 
-  * How to launch a Microchip [[http://microchip.wikidot.com/tls0101:design-environment|MPLAB X project]]. 
  
-===== 4. Equipment Necessary: ===== +---- 
-==== Hardware==== + 
-  * Workstation computer running Windows 10 or higher, MAC OS, or Linux. +===== 3. Basic Knowledge ===== 
-  * Basys MX3 processor board with [[http://www.microchip.com/wwwproducts/en/PIC32MX370F512L|PIC32MX370F512L]] processor. +  - Fundamentals of digital [[https://en.wikipedia.org/wiki/Combinational_logic|combinational]] and [[https://en.wikipedia.org/wiki/Sequential_logic|sequential]] logic. 
-  * Micro USB cable.+  - How to interpret a schematic diagram and electric circuits. 
 +  - How to write a computer program using the [[http://www.tutorialspoint.com/cprogramming/index.htm|C language]]. 
 +  - How to launch a [[http://microchip.wikidot.com/tls0101:design-environment|Microchip MPLAB X]] project. 
 + 
 + 
 +---- 
 + 
 +===== 4. Equipment List ===== 
 +==== 4.1. Hardware ==== 
 +  - [[microprocessor/basys-mx3/start|Basys MX3 trainer board]] 
 +  - [[https://digilent.com/shop/usb-a-to-micro-b-cable/|Micro USB cable]] 
 +  - Workstation computer running Windows 10 or higher, MAC OS, or Linux
  
 In addition, we suggest the following instruments: In addition, we suggest the following instruments:
-  * Logic analyzer or oscilloscope (e.g. [[http://store.digilentinc.com/analog-discovery-2-100msps-usb-oscilloscope-logic-analyzer-and-variable-power-supply/|Digilent Analog Discovery 2]]). 
  
-==== Software==== +  - [[http://www.digilentinc.com/Products/Detail.cfm?NavPath=2,842,1018&Prod=ANALOG-DISCOVERY|Digilent Analog Discovery 2]] 
-The following programs must be installed on your development work station+ 
-  [[http://www.microchip.com/mplab/mplab-x-ide|MPLAB X IDE]]. +==== 4.2. Software ==== 
-      * [[http://microchip.wikidot.com/mplabx:installation|Installation help]]. +The following programs must be installed on your development workstation
-      * [[http://microchip.wikidot.com/tls0101:start|Getting started]]. +  [[http://www.microchip.com/mplab/mplab-x-ide|Microchip MPLAB X® v3.35 or higher]] 
-  [[http://www.microchip.com/xcdemo/xcpluspromo.aspx|XC32 Cross Compiler]]. +    * [[http://microchip.wikidot.com/mplabx:installation|Installation help]] 
-  [[http://www.microchip.com/SWLibraryWeb/product.aspx?product=PIC32%20Peripheral%20Library|PLIB Peripheral Library]].+    * [[http://microchip.wikidot.com/tls0101:start|Getting started]] 
 +  [[http://www.microchip.com/xcdemo/xcpluspromo.aspx|XC32 Cross Compiler]] 
 +  [[http://www.microchip.com/SWLibraryWeb/product.aspx?product=PIC32%20Peripheral%20Library|PLIB Peripheral Library]] 
 + 
 + 
 +----
  
 ===== 5. Project Takeaways ===== ===== 5. Project Takeaways =====
-  Know how to generate a microprocessor development project using MPLAB X. +  - Understand best practices for creating dependable and sustainable software. 
-  Know how to generate a config_bits file for the PIC32MX370 processor.  +  - Know how to generate a microprocessor development project using MPLAB X. 
-  Know how to configure the Microchip PIC32 processor pins as either digital inputs or outputs. +  Know how to generate a config_bits file for the PIC32MX370 processor.  
-  Understand the drive capability of a processor digital output.  +  Know how to configure the Microchip PIC32 processor pins as either digital inputs or outputs. 
-  Know how to write a C program for a specific application using an embedded system. +  Understand the drive capability of a processor digital output.  
-  Know how to make speed performance measure on a processor-based system. +  Know how to write a C program for a specific application using an embedded system. 
 +  Know how to make speed performance measure on a processor-based system.  
  
 ---- ----
Line 55: Line 68:
 ===== 6. Fundamental Concepts ===== ===== 6. Fundamental Concepts =====
 ==== 6.1. Project Planning ==== ==== 6.1. Project Planning ====
-[[https://en.wikipedia.org/wiki/Software_system|software-based system]] consists of a collection of electrically interconnected electronic hardware components, some of which require programming using a computer language. [[https://en.wikipedia.org/wiki/Software_design|Software-based system]] design is an engineering effort and requires a good process to obtain good results. The [[https://en.wikipedia.org/wiki/Systems_development_life_cycle|life cycle]] of system development uses a series of activities that, when considered in proper order, minimize design effortand achieve maximum benefit using the available resources of the software-based system. +A software-based system consists of a collection of electrically interconnected electronic hardware components, some of which require programming using a computer language. Software-based system design is an engineering effort and requires a good process to obtain good results. The life cycle of system development uses a series of activities that, when considered in proper order, minimize design effort and achieve maximum benefit using the available resources of the software-based system. 
  
 Coding is one of the last phases when developing software-based systems. There are many methods and tools available today to assist in code development planning. Not all software applications require exotic planning tools, although all applications do require some degree of planning. After my 25 years of teaching a course on microcontrollers, the most common mistake I have seen students make is writing code before they completely understand the problem. Even a simple plan helps to guide the developer to design a system that meets the stated requirements.  Coding is one of the last phases when developing software-based systems. There are many methods and tools available today to assist in code development planning. Not all software applications require exotic planning tools, although all applications do require some degree of planning. After my 25 years of teaching a course on microcontrollers, the most common mistake I have seen students make is writing code before they completely understand the problem. Even a simple plan helps to guide the developer to design a system that meets the stated requirements. 
Line 71: Line 84:
 | Evaluation    | Evaluation of reliability and functionality                                  | Recommendations for improvements                                               | Cost – benefit analysis               | | Evaluation    | Evaluation of reliability and functionality                                  | Recommendations for improvements                                               | Cost – benefit analysis               |
  
-In all lab exercises of this series, the purposes and requirements will be defined by a problem statement. In most labs, the required hardware is provided on the Basys MX3 trainer board. In some instances, additional hardware circuits will need to be constructed and connected to the trainer board+In all lab exercises of this series, the purposes and requirements will be defined by a problem statement. In most labs, the required hardware is provided on the Basys MX3 trainer board. In some instances, additional hardware such as motors will be needed. 
 + 
 +Software development will be the focus of the design phase for this series of labs. A significant amount of effort will be needed to understand the required resources of the PIC32MX370 processor along with the functionality of sensors, actuators, displays, and controls used in each lab, as well as the interconnection of these I/O devices to the PICMX370 processor. This includes application specific hardware, interconnection drawings, development tools, and testing instrumentation. Part of processor resource allocation is a table that designates the processor I/O pins and special functions such as timers, communications, and analog I/O. During the design phase, the developer must consider how the system will be tested or validated. Frequently, this requires identifying hardware instrumentation test points and test code that must be integrated with the system code
  
-Software development will be the focus of the design phase for this series of labs. A significant amount of effort will be needed to understand the required resources of the PIC32MX370 processor along with the functionality of sensors, actuators, displays, and controls used in each lab, as well as the interconnection of these IO devices to the PICMX370 processor. This includes application specific hardware, interconnection drawings, development tools, and testing instrumentation. Part of processor resource allocation is a table that designates the processor IO pins and special functions such as timers, communications, and analog IO. During the design phase, the developer must consider how the system will be tested or validated. Frequently, this requires identifying hardware instrumentation test points and test code that must be integrated with the system code. 
  
  
Line 80: Line 94:
  
 ==== 6.1.2. Concepts Planning Using Partitioning ==== ==== 6.1.2. Concepts Planning Using Partitioning ====
-Software and hardware engineering use the [[https://effectivesoftwaredesign.com/2011/06/06/divide-and-conquer-coping-with-complexity/|divide and conquer]] approach that divides a large problem into a series of small tasks that are easier to complete. For software engineering, this requires partitioning the problem into single-purpose functions that can be tested independently of other single-purpose functions. There are two common approaches to partitioning: those based on hardware such as IO devices or processor peripherals and partitions based on software functionality so that minimum interaction with or dependency on other partitions is necessary. +Software and hardware engineering use the [[https://effectivesoftwaredesign.com/2011/06/06/divide-and-conquer-coping-with-complexity/|divide and conquer approach]] that divides a large problem into a series of small tasks that are easier to complete. For software engineering, this requires partitioning the problem into single-purpose functions that can be tested independently of other single-purpose functions. There are two common approaches to partitioning: those based on hardware such as I/O devices or processor peripherals and partitions based on software functionality so that minimum interaction with or dependency on other partitions is necessary. 
  
 {{ :learn:courses:microprocessor-io-unit-1:figure-6-1.png?nolink |Figure 6.1. Data flow diagram - partitioning base on IO resources for Lab 1a.}} {{ :learn:courses:microprocessor-io-unit-1:figure-6-1.png?nolink |Figure 6.1. Data flow diagram - partitioning base on IO resources for Lab 1a.}}
Line 88: Line 102:
 //Figure 6.2. Control flow diagram for Lab 1a.// //Figure 6.2. Control flow diagram for Lab 1a.//
  
-==== 6.1.3. Concept Planning: Performance Evalueation - When and How to Test ====+A [[https://en.wikipedia.org/wiki/Control_flow_graph|control flow diagram]] (CFD), as the one shown in Fig. 6.2, describes the order in which the transactions occur. The CFD is useful when integrating the portioned elements in defining the order in which they are executed. One should be able to determine where each line of software code is executed on the CFD graph. The CFD is also useful during the testing phase to ensure that a scenario can be generated that causes the execution to transverse each path through the CFD.  
 + 
 +==== 6.1.3. Concept Planning: Performance Evaluation - When and How to Test ====
 Using the divide and conquer approach, each element in the partitioned project must be completely tested and validated before integrating it with another element. You don’t want to design new code based on the functionality of incorrectly functioning old code. It is a waste of time.  Using the divide and conquer approach, each element in the partitioned project must be completely tested and validated before integrating it with another element. You don’t want to design new code based on the functionality of incorrectly functioning old code. It is a waste of time. 
  
 After integration of all partitioned elements, system tests determine how well your design effort satisfies the stated requirements. One of the most straightforward ways is to make a table of the specified requirements to serve as a check-off list to include in your final documentation. After integration of all partitioned elements, system tests determine how well your design effort satisfies the stated requirements. One of the most straightforward ways is to make a table of the specified requirements to serve as a check-off list to include in your final documentation.
 +
  
  
Line 97: Line 114:
  
 ===== 7. Problem Statement ===== ===== 7. Problem Statement =====
-There are two lab assignments associated with this unit. Lab 1a is very limited in scope and complexity and focuses on defining the PIC32MX370 IO pins as digital inputs or outputs in a tutorial-like fashion. Lab 1b is more open-ended and asks the student to extend the knowledge gained in Lab 1a by using the slide switched, push buttons, and the four digit seven-segment LED display on the Basys MX3 trainer board to implement a basic calculator. Both labs emphasize the engineering approach to embedded system design. +There are two lab assignments associated with this unit. Lab 1a is very limited in scope and complexity and focuses on defining the PIC32MX370 I/O pins as digital inputs or outputs in a tutorial-like fashion. Lab 1b is more open-ended and asks the student to extend the knowledge gained in Lab 1a by using the slide switches, push buttons, and the 4-digit 7-segment LED display on the Basys MX3 trainer board to implement a basic calculator. Both labs emphasize the engineering approach to embedded system design. 
  
  
Line 104: Line 121:
 ===== 8. Background Information ===== ===== 8. Background Information =====
 ==== 8.1. The Development Environment ==== ==== 8.1. The Development Environment ====
-The reader is expected to have acquired the basic knowledge of programming a computer in C. Bus as a review, we offer web links to an online [[http://www.tutorialspoint.com/cprogramming/index.htm|C Tutorial]] and [[http://microchip.wikidot.com/tls0101:design-environment|Microchip MPLAB X IDE]]. Figure 8.1 illustrates a typical hardware configuration for microcontroller development. The workstation computer can be running any Windows®, MAC®, or Linux® operating system. The developer writes the program code on this workstation using the editor provided by the [[http://www.microchip.com/mplab/mplab-ide-home|MPLAB X IDE]] or a third party editor such as [[https://notepad-plus-plus.org/download/v6.9.1.html|NotePad++]]®. A cross compiler converts the C programs into a binary representation of the code that the PIC processor can execute. The file generated by the compiler is downloaded to the target application processor with the aid of a programmer/debugger module (included on board the Basys MX3).+The reader is expected to have acquired the basic knowledge of programming a computer in C. But as a review, we offer web links to an online [[http://www.tutorialspoint.com/cprogramming/index.htm|C Tutorial]] and [[http://microchip.wikidot.com/tls0101:design-environment|Microchip MPLAB X IDE]]. Figure 8.1 illustrates a typical hardware configuration for microcontroller development. The workstation computer can be running any Windows®, MAC®, or Linux® operating system. The developer writes the program code on this workstation using the editor provided by the [[http://www.microchip.com/mplab/mplab-ide-home|MPLAB X IDE]] or a third party editor such as [[https://notepad-plus-plus.org/download/v6.9.1.html|NotePad++]]®. A cross compiler converts the C programs into a binary representation of the code that the PIC processor can execute. The file generated by the compiler is downloaded to the target application processor with the aid of a programmer/debugger module (included on-board the Basys MX3).
  
-{{ :learn:courses:microprocessor-io-unit-1:figure-8-1.png?nolink |Figure 8.1. Integrated Development Hardware Diagram.}}+{{ :learn:courses:microprocessor-io-unit-1:unit_1_-_photo_1.jpg?nolink&400 |Figure 8.1. Integrated Development Hardware Diagram.}}
 //Figure 8.1. Integrated Development Hardware Diagram.// //Figure 8.1. Integrated Development Hardware Diagram.//
  
-The diagram in Fig. 8.1 shows the PC with the Basys MX3 unit. The Basys MX3 has a built-in programmer/debugger that allows a direct connection from the development work station computer to the Basys MX3 board. If the programmer/debugger is not built into the hardware platform, a separate programmer is required, such as the [[http://store.digilentinc.com/pickit-3-in-circuit-debugger/|PICkit 3]] or a [[http://store.digilentinc.com/chipkit-pgm-programmer-debugger-for-use-with-digilent-chipkit-platforms/|chipKIT Programmer]].  +The diagram in Fig. 8.1 shows the Basys MX3 unit. The Basys MX3 has a built-in programmer/debugger that allows a direct connection from the development workstation computer to the Basys MX3 board. If the programmer/debugger is not built into the hardware platform, a separate programmer is required, such as the PICkit 3 or a chipKIT Programmer.  
  
 ==== 8.2. General Notes of Interest ==== ==== 8.2. General Notes of Interest ====
Line 121: Line 138:
     #define _SUPPRESS_PLIB_WARNING     #define _SUPPRESS_PLIB_WARNING
 #endif #endif
-    #ifndef _DISABLE_OPENADC10_CONFIGPORT_WARNING + 
-        #define  _DISABLE_OPENADC10_CONFIGPORT_WARNING +#ifndef _DISABLE_OPENADC10_CONFIGPORT_WARNING 
-    #endif+    #define  _DISABLE_OPENADC10_CONFIGPORT_WARNING 
 +#endif
 </code> </code>
 <html> <html>
   <ol start="4">   <ol start="4">
-<li>Table 4-in the <a href="http://ww1.microchip.com/downloads/en/DeviceDoc/51686F.pdf">MPLAB XC32 C/C++ Compilers User’s Guide</a> notes that the legacy library option is automatically selected for new projects. If using MPLABX 3.40 or later, when starting a new project, this option must **not** be checked, as shown in Fig. 8.3</li></ol></html>+<li>Table 8.below, reproduced from the </html>[[http://ww1.microchip.com/downloads/en/DeviceDoc/51686F.pdf|MPLAB XC32 C/C++ Compilers User’s Guide]], notes that the legacy library option is automatically selected for new projects. If using MPLABX 3.40 or later, when starting a new project, this option must **not** be checked, as shown in Fig. 8.2. 
 + 
 +**Table 8.1. XC32 (Global Options) all Options Category.**
  
-{{ :learn:courses:microprocessor-io-unit-1:figure-8-2.png?nolink |Figure 8.2. XC32 Global Options Setting.}} +^ Option          ^ Description                                                                                                                                                                                    ^ Command Line                        ^ 
-//Figure 8.2XC32 Global Options Setting.//+Use legacy lib  | Check to use the Standard C library in the format before XC32 v1.12Uncheck to use the HTC LIBC versionThe legacy libc is the default for new projects created in MPLAB X v3.15 and later.  | -legacy-libc\\ &\\ -no-legacy-libc  |
  
-{{ :learn:courses:microprocessor-io-unit-1:figure-8-3.png?nolink |Figure 8.3. Disable “Use Legacy libc” to use plib.h.}} +{{ :learn:courses:microprocessor-io-unit-1:figure-8-3.png?nolink |Figure 8.2. Disable “Use Legacy libc” to use plib.h.}} 
-//Figure 8.3. Disable “Use Legacy libc” to use plib.h.//+//Figure 8.2. Disable “Use Legacy libc” to use plib.h.//
  
 <html> <html>
   <ol start="5">   <ol start="5">
-<li>Only the 8 individual LEDs labeled LD0 through LD7 are connected to contiguous IO port pins (see Table E.1 for Port A pins 0 through 7). These are the only IO pins that may be set as a group. All other pins must be individually assigned. The dispersion of pins and ports for common elements such as switches, buttons, and segment LEDs requires a <a href="https://en.wikipedia.org/wiki/Bit_banging">bit-banging</a> approach rather than reading or setting a group of bits with a single instruction.</li></ol></html>+<li>Only the 8 individual LEDs labeled LD0 through LD7 are connected to contiguous I/O port pins (see Table E.1 for Port A pins 0 through 7). These are the only I/O pins that may be set as a group. All other pins must be individually assigned. The dispersion of pins and ports for common elements such as switches, buttons, and segment LEDs requires a </html>[[https://en.wikipedia.org/wiki/Bit_banging|bit-banging]] approach rather than reading or setting a group of bits with a single instruction. 
  
 ==== 8.3. Microcontroller Resources ==== ==== 8.3. Microcontroller Resources ====
-Microcontrollers have three major resources: time access to the central processing unit (CPU execution time), program and data memory, and input and output (IO). Frequently, an application requires more of one of these three resources than the selected processor can provide. Microprocessor resource management involves compensating for a lack of one of these resources using excess capability in one or both of the other two. For example, if an application requires more IO than the processor has IO pins, the designer can implement a parallel pin multiplexing scheme that reduces the number of IO pins but requires additional memory and CPU time to execute the code. This concept will be explored when we interface the PIC32 processor with a four digit 7-segment LED display. +Microcontrollers have three major resources: time access to the central processing unit (CPU execution time), program and data memory, and input and output (I/O). Frequently, an application requires more of one of these three resources than the selected processor can provide. Microprocessor resource management involves compensating for a lack of one of these resources using excess capability in one or both of the other two. For example, if an application requires more I/O than the processor has I/O pins, the designer can implement a parallel pin multiplexing scheme that reduces the number of I/O pins but requires additional memory and CPU time to execute the code. This concept will be explored when we interface the PIC32 processor with a 4-digit 7-segment LED display. 
  
  
Line 159: Line 179:
 The following three steps are used to launch a new project: The following three steps are used to launch a new project:
   - Generate Lab 1 Project:   - Generate Lab 1 Project:
-    - Open the MPLAB X application on your work station computer.+    - Open the MPLAB X application on your workstation computer.
     - Start a new Microchip Embedded Standalone Project.     - Start a new Microchip Embedded Standalone Project.
-    - Select Device: PIC32MX370F512L<+    - Select Device: PIC32MX370F512L
     - Select Tool: Licensed Debugger MCU Alpha One     - Select Tool: Licensed Debugger MCU Alpha One
     - Select Toolchain: XC32 compiler     - Select Toolchain: XC32 compiler
Line 171: Line 191:
     - Right-click on the Header Files and select “New” -> “XC32 Header.h”. Name this file “config_bits.h”.     - Right-click on the Header Files and select “New” -> “XC32 Header.h”. Name this file “config_bits.h”.
     - Click on “Windows” -> “PIC Memory Views” -> “Configuration Bits”.     - Click on “Windows” -> “PIC Memory Views” -> “Configuration Bits”.
-    - Set the options as shown in Listing C.in the Appendix.+    - Set the options as shown in Listing B.in the Appendix.
     - Click on “Generate Source Code to Output”.     - Click on “Generate Source Code to Output”.
     - Enter “CTRL A” to highlight the text in the output window and “CTRL C” to copy the text.      - Enter “CTRL A” to highlight the text in the output window and “CTRL C” to copy the text. 
Line 187: Line 207:
       - Declare local functions other than the function main.        - Declare local functions other than the function main. 
       - Add a main function code as shown in Listing A.1. The return denotes an EXIT FAILURE because embedded systems code should never exit from the function main because it inherently has no place to return to.        - Add a main function code as shown in Listing A.1. The return denotes an EXIT FAILURE because embedded systems code should never exit from the function main because it inherently has no place to return to. 
-      - Complete the project requirements by adding header and source files as elements of building blocks. Each source file should have a header file that declares constants and software function prototypes that are used as interfaces with other project files. The project work is divided into independently testable units that can serve as libraries for other projects that use the same hardware resources.  For example, writing a decimal number to a four digit-seven segment display is a common operation. Hence a set of source-header files will be written to display a four-digit number. +      - Complete the project requirements by adding header and source files as elements of building blocks. Each source file should have a header file that declares constants and software function prototypes that are used as interfaces with other project files. The project work is divided into independently testable units that can serve as libraries for other projects that use the same hardware resources.  For example, writing a decimal number to a 4-digit 7-segment display is a common operation. Hencea set of source-header files will be written to display a 4-digit number. 
  
 ==== Listing A.1. Format of a Typical Main Function ==== ==== Listing A.1. Format of a Typical Main Function ====
 <code> <code>
-     int main(int argc, char** argv) + int main(int argc, char** argv) 
  {  {
  // Initialization code goes here  // Initialization code goes here
Line 202: Line 222:
                              // this line of code.                              // this line of code.
  } // End of main.c  } // End of main.c
- 
 </code> </code>
  
Line 208: Line 227:
  
 ===== Appendix B: Configuration Settings for the PIC32MX370F512L ===== ===== Appendix B: Configuration Settings for the PIC32MX370F512L =====
-==== Listing B.1. Configuration bits settings for BASYS MX3_A processor board ====+==== Listing B.1. Configuration bits Settings for Basys MX3 Processor Board ====
 <code> <code>
 /* ************************************************************************** */ /* ************************************************************************** */
Line 290: Line 309:
 ---- ----
  
-===== Appendix C: Basys MX3A Hardware Initialization ===== +===== Appendix C: Basys MX3 Hardware Initialization ===== 
-Listings C.1 and C.2 that are provided below initialize the Basys MX3 hardware inputs. Development time will be reduced by including these two files in future projects. The hardware.h header file contains an extensive use of macro instructions. Macro instructions are text replacement compiler directives that can result in better documentation and all but eliminate [[http://wiki.c2.com/?MagicNumber|magic numbers]] which are a well-known programming bad practice. Macro instructions can obfuscate code and can make it difficult when debugging; however, when properly used, macro instructions can improve documentation and facilitate code portability. The macro instructions used in the hardware.h header file are simple definitions, conditional compilation, and ternary operations all designed to improve readability of the code.      +Listings C.1 and C.2 that are provided below initialize the Basys MX3 hardware inputs. Development time will be reduced by including these two files in future projects. The hardware.h header file contains an extensive use of macro instructions. Macro instructions are text replacement compiler directives that can result in better documentation and all but eliminate [[http://wiki.c2.com/?MagicNumber|magic numbers]]which are a well-known programming bad practice. Macro instructions can obfuscate code and can make it difficult when debugging; however, when properly used, macro instructions can improve documentation and facilitate code portability. The macro instructions used in the hardware.h header file are simple definitions, conditional compilation, and ternary operations all designed to improve readability of the code.
  
 ==== Listing C.1. Hardware Initialization Header File ==== ==== Listing C.1. Hardware Initialization Header File ====
Line 334: Line 353:
 /* Comment out the following define statement when programmer is NOT used to  /* Comment out the following define statement when programmer is NOT used to 
 * allow using BTNL and BTNU as user inputs. */ * allow using BTNL and BTNU as user inputs. */
-   #define DEBUG_MODE  /* Inputs from push buttons BTNL and BRNU are not useable */+   #define DEBUG_MODE  /* Inputs from push buttons BTNL and BTNU are not useable */
  
 /* This included file provides access to the peripheral library functions and /* This included file provides access to the peripheral library functions and
Line 376: Line 395:
    #define Set_SW7_in() TRISBbits.TRISB9  = 1    #define Set_SW7_in() TRISBbits.TRISB9  = 1
  
-/* The following macro instruction seta the processor pins for all 8 switch inputs */+/* The following macro instruction sets the processor pins for all 8 switch inputs */
    #define Set_All_Switches_Input();  { Set_SW0_in(); Set_SW1_in(); Set_SW2_in();\    #define Set_All_Switches_Input();  { Set_SW0_in(); Set_SW1_in(); Set_SW2_in();\
                                   Set_SW3_in(); Set_SW4_in(); Set_SW5_in();\                                   Set_SW3_in(); Set_SW4_in(); Set_SW5_in();\
Line 511: Line 530:
  
   @File Name   @File Name
-   PICmx370.c+   hardware.c
  
  @Summary  @Summary
Line 530: Line 549:
  
 #include "hardware.h" #include "hardware.h"
-#include "switches.h" 
 #include <plib.h> #include <plib.h>
  
Line 577: Line 595:
        
    Set_All_LEDs_Output(); /* Configure all Basys MX3 LED0 – LED7 as outputs */    Set_All_LEDs_Output(); /* Configure all Basys MX3 LED0 – LED7 as outputs */
-   Set_All_LEDs_Off(); /* Set all Basys MX3 LED0 – LED7 off */+   Set_All_LEDs_Off();         /* Set all Basys MX3 LED0 – LED7 off */
    Set_All_Switches_Input(); /* Configure all Basys MX3 slide switches as inputs */    Set_All_Switches_Input(); /* Configure all Basys MX3 slide switches as inputs */
-   Set_All_PBs_Input(); /* Configure all Basys MX3 push buttons as inputs */+   Set_All_PBs_Input();         /* Configure all Basys MX3 push buttons as inputs */
        
 } /* End of hardware_setup */ } /* End of hardware_setup */
Line 612: Line 630:
 ---- ----
  
-===== Appendix D: Essential Elements of the PIC32 Microprocessor IO ===== +===== Appendix D: Essential Elements of the PIC32 Microprocessor I/O ===== 
-This section is not required to complete Labs 1a and 1b; however, it contains information important to forming a more complete view of microprocessor IO+This section is not required to complete Labs 1a and 1b; however, it contains information important to forming a more complete view of microprocessor I/O.  
 + 
 +Digital input and output is one of the most basic functions as a microprocessor can perform. A simplified block diagram of the control of an I/O processor pin is shown in Fig. D.1. The operation of the I/O pin is configured by bit values in the following four registers: the output drain control (ODC), the output latch (LAT), the output tri-state (TRIS), and, for pins that can be used for analog inputs, the analog-to-digital input configuration (AD1PCFG)
  
-Digital input and output is one of the most basic functions as a microprocessor can perform. A simplified block diagram of the control of an IO processor pin is shown in Fig. D.1. The operation of the IO pin is configured by bit values in the following four registers: the output drain control (ODC), the output latch (LAT), the output tri-state (TRIS), and, for pins that can be used for analog inputs, the analog to digital input configuration (AD1PCFG).  
  
 {{ :learn:courses:microprocessor-io-unit-1:figure-d-1.png?nolink |Figure D.1. Simplified block diagram for a PIC32 I/O pin.}} {{ :learn:courses:microprocessor-io-unit-1:figure-d-1.png?nolink |Figure D.1. Simplified block diagram for a PIC32 I/O pin.}}
-//Figure D.1. Simplified block diagram for a PIC32 I/O pin.(([[http://ww1.microchip.com/downloads/en/DeviceDoc/60001185E.pdf|PIC32MX330/350/370/430/450/470]] family data sheet, Chapter 12))//+//Figure D.1. Simplified block diagram for a PIC32 I/O pin, adapted from figure 12-1 in the PIC32 family data sheet.(([[http://ww1.microchip.com/downloads/en/DeviceDoc/60001185E.pdf|PIC32MX330/350/370/430/450/470]] family data sheet, Chapter 12))//
  
-Minimally, the TRIS registers must be set to configure a processor IO pin as an input or output by setting the bit in the register to 0 for output or 1 for input. By default, all pins that can serve as an analog input are configured to be so. Any analog input pin that is to be used for digital IO must also be designated to be a digital IO pin by clearing the appropriate analog select pin. The PIC32MX370 processor has five registers that are used to designate dual-functioning pins as digital or analog inputs. For example, the instruction to clear the analog select pin for PORT D bit 1 (RD1) is “ANSELDbits.ANSD1 = 0;".+Minimally, the TRIS registers must be set to configure a processor I/O pin as an input or output by setting the bit in the register to 0 for output or 1 for input. By default, all pins that can serve as an analog input are configured to be so. Any analog input pin that is to be used for digital I/O must also be designated to be a digital I/O pin by clearing the appropriate analog select pin. The PIC32MX370 processor has five registers that are used to designate dual-functioning pins as digital or analog inputs. For example, the instruction to clear the analog select pin for PORT D bit 1 (RD1) is “ANSELDbits.ANSD1 = 0;".
  
-If the bit in the TRIS register is set low to make the IO pin to function as an output, the voltage pin can be set high by writing a 1 to the appropriate bit in the LAT register. Setting the LAT register bit to a zero sets the output pin low (0V). The actual voltage at the output pin depends on the setting in the ODC register. The default configuration has all bits in the ODC register set to zero, which means the output can both source (supply output current) and sink (pull outputs low) current. If the bit in the ODC register is set high, then the output pin functions as open drain, which can sink current but not source current. +If the bit in the TRIS register is set low to make the I/O pin to function as an output, the voltage pin can be set high by writing a 1 to the appropriate bit in the LAT register. Setting the LAT register bit to a zero sets the output pin low (0V). The actual voltage at the output pin depends on the setting in the ODC register. The default configuration has all bits in the ODC register set to zero, which means the output can both source (supply output current) and sink (pull outputs low) current. If the bit in the ODC register is set high, then the output pin functions as open drain, which can sink current but not source current. 
  
-The maximum current capability of each conventional I/O pin is 15mA (sink or source), while the combined current of all I/O pins is 200mA subject to total power constraints. I/O pins have both open drain and active source output capability. The open drain capability provided by the ODC register is useful when interfacing with switch array keypads (see [[http://store.digilentinc.com/pmodkypd-16-button-keypad/|Pmod KYPD]] for an example keypad device). The outputs that drive the four LEDs on the Basys MX3 board are limited to approximately 5mA.  +The maximum current capability of each conventional I/O pin is 15mA (sink or source), while the combined current of all I/O pins is 200mA subject to total power constraints. I/O pins have both open drain and active source output capability. The open drain capability provided by the ODC register is useful when interfacing with switch array keypads (see [[https://digilent.com/shop/pmod-kypd-16-button-keypad/|Pmod KYPD]] for an example keypad device). The outputs that drive the four LEDs on the Basys MX3 board are limited to approximately 5mA. 
  
-The PORT register allows the state of pin to be read regardless if the pin is configured in the TRIS register to be an input or an output. If the voltage on the pin is above the high input threshold, the PORT bit is read as a logic one. Various processor pins have different thresholds. +The PORT register allows the state of pin to be read regardless if the pin is configured in the TRIS register to be an input or an output. If the voltage on the pin is above the high input threshold, the PORT bit is read as a logic one. Various processor pins have different thresholds.  
-  +If you are interested, refer to Table 30-8 of PIC32MX330/350/370/430/450/470 data sheet to determine the high and low thresholds for the pins being used. All digital input-only pins are 5V tolerant, meaning that a device that outputs 5V for logic high can be connected to digital input pins and will not damage the PIC32MX processor. For the PIC32MX7370, the maximum voltage that can be applied to I/O pins that can be used as either analog or digital I/O is VDD+0.3V, or 3.6V.  Exceeding this voltage limit will damage the processor.
-If you are interested, refer to Table 30-8 of PIC32MX330/350/370/430/450/470 data sheet to determine the high and low thresholds for the pins being used. All digital input-only pins are 5V tolerant, meaning that a device that outputs 5V for logic high can be connected to digital input pins and will not damage the PIC32MX processor. For the PIC32MX7370, the maximum voltage that can be applied to I/O pins that can be used as either analog or digital I/O is V<sub>DD</sub>+0.3V, or 3.6V.  Exceeding this voltage limit will damage the processor.+
  
 The following listings are two functionally equivalent examples of code that uses the input state of pin RD0 to set the output on RD1. The following listings are two functionally equivalent examples of code that uses the input state of pin RD0 to set the output on RD1.
Line 639: Line 657:
 { {
 // Initialization // Initialization
-    ANSELDbits.ANSD1 = 0;     // RD1 set to digital IO +    ANSELDbits.ANSD1 = 0;             // RD1 set to digital I/O 
-    TRISDbits.TRISD1 = 0;     // RD1 set to output  +    TRISDbits.TRISD1 = 0;             // RD1 set to output  
-    TRISDbits.TRISD0 = 1;     // RD2 set to input +    TRISDbits.TRISD0 = 1;             // RD0 set to input 
  
 // loop // loop
Line 647: Line 665:
     {     {
         LATDbits.LATD1 = PORTDbits.RD0; //Copy state of RD0 to RD1         LATDbits.LATD1 = PORTDbits.RD0; //Copy state of RD0 to RD1
-    {+    }
 } }
 </code> </code>
Line 657: Line 675:
 { {
 // Initialization // Initialization
-    ANSELDCLR = 0x02;    // RD1 set to digital IO +    ANSELDCLR = 0x02;    // RD1 set to digital I/O 
-    TRISDCLR = 0x02;   // RD1 set to output +    TRISDCLR = 0x02;           // RD1 set to output 
     TRISDSET = 0x01;    // RD0 set to input      TRISDSET = 0x01;    // RD0 set to input 
  
Line 665: Line 683:
     {     {
         if(PORTD & 0x01) // Read RD0 pin         if(PORTD & 0x01) // Read RD0 pin
-      LATDSET = 0x02; // Set RD1 pin high+      LATDSET = 0x02;         // Set RD1 pin high
         else         else
-      LATDCLR = 0x02; // Set RD1 pin low +      LATDCLR = 0x02;         // Set RD1 pin low 
-    {+    }
 } }
 </code> </code>
Line 675: Line 693:
 ---- ----
  
-===== Appendix E: PIC32MX370F512L Processor Pin Assignments for Basys MX3A =====+===== Appendix E: PIC32MX370F512L Processor Pin Assignments for Basys MX3 =====
 **Table E.1**. Processor IO Assignments. **Table E.1**. Processor IO Assignments.
 ^ CPU pin  ^ Port  ^ ALT                                    ^ Function      ^                          ^ ^ CPU pin  ^ Port  ^ ALT                                    ^ Function      ^                          ^
Line 700: Line 718:
 | 84       | RD7   | RPD7/PMD15/RD7                         | CF            | "                        | | 84       | RD7   | RPD7/PMD15/RD7                         | CF            | "                        |
 | 80       | RD13  | RPD13/RD13                             | CG            | "                        | | 80       | RD13  | RPD13/RD13                             | CG            | "                        |
-| 95       | RG14  | TRD2/RD14                              | CP            | "                        |+| 95       | RG14  | TRD2/RG14                              | CP            | "                        |
 | 93       | RE0   | PMD0/RE0                               | DB0           | Character LCD data       | | 93       | RE0   | PMD0/RE0                               | DB0           | Character LCD data       |
 | 94       | RE1   | PMD1/RE1                               | DB1           | "                        | | 94       | RE1   | PMD1/RE1                               | DB1           | "                        |
Line 743: Line 761:
 | 77       | RD2   | AN25/RPD2/RD2                          | LED8_R        | "                        | | 77       | RD2   | AN25/RPD2/RD2                          | LED8_R        | "                        |
 | 88       | RF1   | RPF1/PMD10/RF1                         | MODE          | Stepper motor            | | 88       | RF1   | RPF1/PMD10/RF1                         | MODE          | Stepper motor            |
-| 25       | RB0   | PGED1/AN0/RPB0/RB0                     | P32_PGC/BTNU  | Push Button              | +| 25       | RB0   | PGED1/AN0/RPB0/RB0                     | P32_PGC/BTNL  | Push Button              | 
-| 24       | RB1   | PGC1/AN1/RPB1/CTED12/RB1               | P32_PGD/BTNL  | "                        |+| 24       | RB1   | PGC1/AN1/RPB1/CTED12/RB1               | P32_PGD/BTNU  | "                        |
 | 57       | RG2   | SCL1/RG2                               | SCL           | I2C - Accelerometer      | | 57       | RG2   | SCL1/RG2                               | SCL           | I2C - Accelerometer      |
 | 56       | RG3   | SDA1/RG3                               | SDA           | "                        | | 56       | RG3   | SDA1/RG3                               | SDA           | "                        |
Line 763: Line 781:
 | 63       | RC12  | CLKI/RC12/OSC1                                                                | | 63       | RC12  | CLKI/RC12/OSC1                                                                |
 | 64       | RC15  | CLKO/RC15/OSC2                                                                | | 64       | RC15  | CLKO/RC15/OSC2                                                                |
 +
 +----
 +[[{}/learn/courses/microprocessor-io-lab-1a/start|Go to Lab 1a]]
 +[[{}/learn/courses/microprocessor-io-lab-1b/start|Go to Lab 1b]]
 +[[{}learn/courses/unit-2/start#unit_2elements_of_real-time_systems|Go to Unit 2]]
 +
 +{{tag>reference learn microprocessor courses basys-mx3}}