Chapter+1 +the+adventure+begins


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Chapter+1 +the+adventure+begins

  1. 1. Chapter 1 The Adventure Begins Creating the first project and saying “Hello to the world”
  2. 2. The Plan <ul><li>The first project with MPLAB IDE </li></ul><ul><ul><li>The project window </li></ul></ul><ul><ul><li>The editor </li></ul></ul><ul><ul><li>The output window </li></ul></ul><ul><li>A first statement </li></ul><ul><li>A first complete program </li></ul><ul><ul><li>Controlling I/Os: Ports and Pins </li></ul></ul><ul><li>Building the project </li></ul><ul><li>Using the MPLAB SIM simulator </li></ul><ul><li>The first debugging experience </li></ul><ul><li>Hello World! </li></ul>
  3. 3. Preparation <ul><li>The following tools will be used in this lesson: </li></ul><ul><li>MPLAB IDE, Integrated Development Environment (v8.00 or later, free) </li></ul><ul><li>MPLAB SIM, free software simulator (included in MPLAB installation) </li></ul><ul><li>MPLAB C32, C compiler (free Student Edition) </li></ul><ul><li>The following pieces of documentation will be used during this lesson: </li></ul><ul><li>PIC32MX Datasheet –DS61143 (latest rev.) </li></ul><ul><li>PIC32MX Family Reference Manual – DS61120 Section 12. I/O Ports </li></ul><ul><li>Make sure they are available and/or installed and ready to use on your computer. </li></ul><ul><li>You can download them from Microchip web site at: </li></ul><ul><li>And </li></ul>
  4. 4. The New Project Set Up <ul><li>Launch MPLAB IDE </li></ul><ul><li>Follow the “New Project Set Up” Checklist to create a new project using the Project Wizard </li></ul>
  5. 5. The New Project Setup Checklist <ul><li>Select the PIC32MX360F512L device and click Next. </li></ul><ul><li>Select the PIC32 C-Compiler Tool Suite and click Next </li></ul><ul><li>Click the Browse button and create a new folder. Name the new folder “Hello”, and inside it create the project file “Hello World”, then click Next. </li></ul><ul><li>Click Next to proceed to the following dialog box since there is no need to copy any source files from any previous projects or directories. </li></ul><ul><li>Click on Finish to complete the project set up </li></ul>
  6. 6. The Project Window <ul><li>If not automatically visible, open the Project Window: </li></ul><ul><ul><li>Select “View-> Project” from the main menu </li></ul></ul>Note: The project window can be made “dockable” so that it will stick to one of the edges of the screen (left)
  7. 7. The Output Window <ul><li>If not automatically visible, open the Output Window: </li></ul><ul><ul><li>Select “View-> Output” from the main menu </li></ul></ul>Note: The output window can be made “dockable” so that it will stick to one of the edges of the screen (bottom)
  8. 8. The Editor Window <ul><li>Open a new editor window by selecting </li></ul><ul><ul><li>“ File->New”, or </li></ul></ul><ul><ul><li>CTRL+N keyboard shortcut, or </li></ul></ul><ul><ul><li>by clicking on the corresponding button in MPLAB standard toolbar. </li></ul></ul>
  9. 9. Creating a Source File <ul><li>Type the following three lines: /* ** Hello Embedded World! */ </li></ul><ul><li>Select “File ->Save As” </li></ul><ul><ul><li>Save the file as: “Hello1.c”. </li></ul></ul><ul><li>Now right click with your mouse on the editor window to bring up the editor’s context menu </li></ul><ul><ul><li>Select the “Add To Project” item. </li></ul></ul><ul><li>This will make the Hello1.c file the main source file in your project </li></ul>
  10. 10. Saving the Project <ul><li>Select “Project ->Save Project” </li></ul><ul><li>Save the project as “Hello World.mcp” </li></ul>
  11. 11. The First Statement: #include <ul><li>Add a first C statement: </li></ul><ul><li>#include <p32xxxx.h> </li></ul><ul><li>Which will actually include a file called “ p32mx360f512l.h” whose content looks like: </li></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>extern volatile unsigned int WDTCON __attribute__((section(&quot;sfrs&quot;))); </li></ul></ul><ul><ul><li>typedef union { </li></ul></ul><ul><ul><li>struct { </li></ul></ul><ul><ul><li>unsigned WDTCLR:1; </li></ul></ul><ul><ul><li>unsigned WDTWEN:1; </li></ul></ul><ul><ul><li>unsigned SWDTPS0:1; </li></ul></ul><ul><ul><li>unsigned SWDTPS1:1; </li></ul></ul><ul><ul><li>unsigned SWDTPS2:1; </li></ul></ul><ul><ul><li>unsigned SWDTPS3:1; </li></ul></ul><ul><ul><li>unsigned SWDTPS4:1; </li></ul></ul><ul><ul><li>unsigned :7; </li></ul></ul><ul><ul><li>unsigned FRZ:1; </li></ul></ul><ul><ul><li>unsigned ON:1; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>... </li></ul></ul>
  12. 12. The main() function <ul><li>Add the following lines of code: </li></ul><ul><ul><li>main() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>There can be only one main() function </li></ul><ul><li>The curly {} brakets </li></ul><ul><li>When is it executed </li></ul><ul><li>What happens after it is executed </li></ul>
  13. 13. I/O PINS <ul><li>I/O pins can be configured as: </li></ul><ul><ul><li>Digital Inputs </li></ul></ul><ul><ul><li>Digital Ouputs (Push Pull) </li></ul></ul><ul><ul><li>Digital Outputs (Open Drain) </li></ul></ul><ul><ul><li>Analog Inputs </li></ul></ul><ul><ul><li>Dedicated inputs or outputs for a number of peripherals </li></ul></ul>
  14. 14. PORTA and PORTB <ul><li>Different PORTs group pins with different functions </li></ul><ul><li>PORTB for example contains a number of pins that can be configured as analog inputs to the Analog to Digital Converter (ADC) . </li></ul><ul><li>PORTA contains a number of pins that can be used for the JTAG interface, TRACE function, and the I2C interface </li></ul><ul><li>Refer to the specific device datasheet for a detailed list of each PORT/pin capabilities </li></ul>
  15. 15. TRIS registers <ul><li>TRIS registers control the direction of each pin (Input/Output) </li></ul><ul><li>TRISA, TRISB… each port has a corresponding tris register </li></ul><ul><ul><li>Setting a bit to 1 configures a pin as Input </li></ul></ul><ul><ul><li>Clearing a bit to 0 configure the corresponding pin as an output </li></ul></ul>
  16. 16. The Watch Window <ul><li>Once a debugging tool (MPLAB SIM) is selected </li></ul><ul><li>Open the Watch Window </li></ul><ul><ul><li>To inspect the content of a variable (symbol) or any of the special function registers (SFR) </li></ul></ul><ul><ul><li>Select the desired output format(s) </li></ul></ul>
  17. 17. Compiling and Linking <ul><li>A compiler transforms the C source code (.c) and all inlcuded (.h) files into a relocatable code object (.o) </li></ul><ul><li>The linker takes all the relocatable code objects (.o) and libraries (.lib) and assembles them into an executable (.hex) file </li></ul>
  18. 18. Using the Simulator <ul><li>Follow the SetUp Checklist </li></ul><ul><li>Learn the basic debugging options offered by the Simulator </li></ul><ul><ul><li>Reset </li></ul></ul><ul><ul><li>Single Step (Over/In) </li></ul></ul><ul><ul><li>Animation </li></ul></ul><ul><ul><li>Running </li></ul></ul><ul><ul><li>Halting </li></ul></ul>
  19. 19. Debugging: Hello World <ul><ul><li>#include <p32xxxx.h> </li></ul></ul><ul><ul><li>main() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>// configure all PORTB pins as output </li></ul></ul><ul><ul><li>TRISB = 0; // all PORTB as output </li></ul></ul><ul><ul><li>AD1PCFG = 0xffff; // all PORTB as digital </li></ul></ul><ul><ul><li>PORTB = 0xff; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Set all pins of PORTA as outputs and then turn them on </li></ul></ul><ul><ul><li>Notice how the JTAG port takes precedence unless disabled </li></ul></ul><ul><ul><li>Now Try using PORTB </li></ul></ul><ul><ul><li>Notice how by default the pins are configured as analog inputs and always read as 0 unleas re-configured </li></ul></ul>
  20. 20. Analog Pin Functions Multiplexing <ul><li>The Analog Pins control: AD1PCFG </li></ul>
  21. 21. Summary <ul><li>In this lesson we learned: </li></ul><ul><li>How to create a new project </li></ul><ul><li>How to create our first C source file </li></ul><ul><li>How to build a project using the MPLAB C32 compiler </li></ul><ul><li>About PINs and PORTs </li></ul><ul><li>How to configure and control simple digital output pins </li></ul><ul><li>How to configure and use the MPLAB SIM simulator </li></ul>
  22. 22. Advanced Material
  23. 23. The Disassembly Window <ul><li>If you want to see what happens at the machine instruction level: </li></ul><ul><ul><li>Open the disassembly window </li></ul></ul>
  24. 24. The Memory Gauge <ul><li>If you want to see how much memory RAM and FLASH is being used by the project </li></ul><ul><li>Open the Memory Gauge Window </li></ul>
  25. 25. Notes for the PIC MCU Experts <ul><li>The PIC32 PORTS are not necessarily 32-bit large. In fact most PORTS are 16-bit at the most. </li></ul><ul><li>The PIC32 PORTS are designed to be compatible with the 8-bit and 16-bit PIC PORTS </li></ul><ul><li>I/O PORT control in C is easy </li></ul><ul><li>Use the LATx registers to control directly the output latches </li></ul>
  26. 26. Tips and Tricks <ul><li>Interfacing to 5V input and output signals is possible with some caution: </li></ul><ul><ul><li>Digital Input pins are 5V tolerant </li></ul></ul><ul><ul><li>Digital Output pins can be configured as Open Drain </li></ul></ul><ul><ul><li>Use the ODCx registers to configure an output pin for Open Drain mode. </li></ul></ul><ul><ul><li>Watch Out! Pins that are multiplexed with analog functions are NOT 5V tolerant! </li></ul></ul>
  27. 27. Suggested Excercises <ul><li>If you have the Explorer16 board and an in circuit debugger: </li></ul><ul><ul><li>Use the MPLAB REAL ICE Debugging or the MPLAB ICD2 Debugging checklists to help you prepare the project for debugging. </li></ul></ul><ul><ul><li>Insert the instructions required to disable the JTAG port. </li></ul></ul><ul><ul><li>Test the PortA example, connecting the Explorer16 board and checking the visual output on LED0-7. </li></ul></ul><ul><li>If you have the PIC32 Starter Kit: </li></ul><ul><ul><li>Use the PIC32 Starter Kit Debugging checklist to help you prepare the project for debugging. </li></ul></ul><ul><ul><li>Modify the code to operate on PortD, but do NOT disable the JTAG port. </li></ul></ul><ul><ul><li>Test the code by checking the visual output on LED0-2 on the PIC32 Starter Kit itself. </li></ul></ul><ul><li>In both cases you can: </li></ul><ul><ul><li>Test the PortB example by connecting a voltmeter (or DMM) to pin RB0, if you can identify it on your board, and watching the needle move, between 0 and 3.3V, as you single step through the code. </li></ul></ul>
  28. 28. Recommended Readings <ul><li>Kernighan, B. & Ritchie, D. </li></ul><ul><li>The C Programming Language </li></ul><ul><li>Prentice-Hall, Englewood Cliffs, NJ </li></ul><ul><li>When you read or hear a programmer talk about the “K&R” … they mean this book! </li></ul><ul><li>Also known as “the white book”, the C language has evolved quite a bit since the first edition was published in 1978! </li></ul><ul><li>The second edition (1988) includes the more recent ANSI C standard definitions of the language </li></ul><ul><li>The MPLAB C32 compiler adheres to the ISO/IEC 9899:1990 (also known as C90) standard </li></ul>
  29. 29. Online Resources <ul><li>http:// </li></ul><ul><li>This is a Wiki-book on C programming and as such it is a bit of a work in progress. It’s convenient if you don’t mind doing all your reading online. </li></ul><ul><li>Hint: look for the chapter called “A taste of C” to find the omnipresent “Hello World!” example. </li></ul>