Page 791 - Mechatronics with Experiments
P. 791

LABORATORY EXPERIMENTS  777
                             Application Software Description
                             The program code for all the experiments is written in C programming language and
                             compiled with MPLAB C18 C-compiler. The code can be typed in the MPLAB Integrated
                             Development Environment (IDE) using the built-in editor or in any standard ASCII editor
                             such as Notepad. The code is then compiled using the C-18 compiler in the MPLAB IDE.
                                  The program code must contain at least two sections: a setup section and logic section.
                             Depending on the state of input switches selected by the user, the program execution is
                             transferred to a particular section, and that section is executed. Points to be noted are:

                                1. Include proper header files provided with the C-18 compiler,
                                  if PIC 18F452 microcontroller is used

                                  #include <p18f452.h>
                                  #include <delays.h>
                                  or if PIC 18F4431 microcontroller is used

                                  #include <p18f4331.h>
                                  #include <delays.h>
                                2. Configure the correct ports as input and output using the TRISx command that
                                  matches your hardware interface choices for input and output. Clear any existing
                                  values on the ports by setting them to zero.
                                3. If you are using the “Watch” option while debugging in MPLAB to keep track of
                                  register values, note that the values are in hexadecimal (0-9, A-F).
                                4. As an example, if we wanted to send out a high signal on pin 0 and pin 5 of Port D,
                                  the code is:

                                  TRISD = 0; /* Set Port D as output */
                                  PORTD = 0; /* Clear existing Port D value */

                                  PORTD = 33; /* Decimal equivalent of 00100001 */
                                  /* Value for Port D in the Watch window in MPLAB is 21
                                  (hex equivalent of 33) */

                             Procedure

                                1. Assemble the circuit on the breadboard as shown in Figures 11.22 or 11.23.
                                2. Connect the four LEDs to each of four pins of PORTB of the PIC microcontroller on
                                  the demo board. The resistors in series with the LEDs are used to limit the maximum
                                  current through the LED, hence the current load (draw) from the PIC microcontroller
                                  per LED. Resistor values in the range of 100 Ω to 1 kΩ are typically used.
                                3. Connect four switches on the 8-pin DIP switch to pins on the PORTC of the PIC
                                  demo board. Notice the resistors (i.e., 10 kΩ or 100 kΩ) connecting the input ports
                                  to the ground. As a result of this connection, when the input switch is open (OFF),
                                  the input pin is at ground (OFF) level. When the input switch is closed (ON), the
                                  input pin is at high level (5 V) in ON state. This is called the pull-down resistor
                                  configuration, because the resistor “pulls-down” the pin to the ground. If we had
                                  swapped the location of the switches and resistors, the ON/OFF relationship would
                                  be reversed. In that case, when a switch is OFF (open), the corresponding input
                                  pin status would be high (ON). When the switch is ON (closed), the corresponding
   786   787   788   789   790   791   792   793   794   795   796