Page 240 - Mechatronics with Experiments
P. 240

226   MECHATRONICS
                              microprocessor. High level languages, such as C, attempt to relieve the programmer from
                              the details of tedious programming in assembly language. For instance, in order to perform
                              a long mathematical expression in assembly language, we would have to write a sequence
                              of add, subtract, and multiply operations. Whereas in C we can directly type the expression
                              in a single statement. Although the basic mathematical and logical operations are expressed
                              the same way for different processors in C, the specific hardware capabilities of a processor
                              cannot be made totally generic. For instance, one must understand the interrupt structure in
                              a particular microcontroller in order to be able to make use of it even with C programming
                              language. Quite often, I/O related operations require access to certain registers that are
                              specific to each microcontroller design. Therefore, a good programmer needs to understand
                              the architecture of a particular microcontroller even if a high level language such as C is
                              used as the programming language.


                              4.3.3 I/O Peripherals of PIC 18F452
                              The input/output (I/O) hardware connection to the outside world is provided by the pins of
                              the microcontroller. Figure 4.6 shows the pins of PIC 18F452 and their functions.


                              I/O Ports  There are five ports on PIC 18F452: PORTA (7-pin), PORTB (8-pin), PORTC
                              (8-pin), PORTD (8-pin), and PORTE (3-pin). Each port has three registers for its operation.
                              They are:
                                  TRISx register, where “x” is the port name A, B, C, D, or E. This is the data direction

                                  or setup register. The value of the TRISx register for any port determines whether
                                  that port acts an input (i.e., reads the value present on the port pins to the data register
                                  of the port) or an output (writes contents of the port data register to the port pins).
                                  Setting a TRISx register bit (= 1) makes the corresponding PORTx pin an input.
                                  Clearing a TRISx register bit (= 0) makes the corresponding PORTx pin an output.
                                  PORTx register. This is the data register for the port. When we want to read the status

                                  of the input pins, this register is read. When we write to this register, its content is
                                  written to the output latch register.
                                  LATx register (output latch). The content of the latch register is written to the port.

                              As an example, the code below sets all pins of PORT C as output, and all pins of PORT B
                              as input.

                              TRISC = 0; /∗ Binary 00000000 ∗/
                              PORTC = value ; /∗ write value variable to port C ∗/

                              TRISB = 255; /∗ Binary 11111111 ∗/
                              value = PORTB ; /∗ read the data in port B to variable value. ∗/

                              The MPLAB C18 compiler provides C-library functions to setup and use I/O ports. For
                              example, Port B interrupt-on-change and pull-up resistor functions can be enabled/disabled
                              using these functions.

                              #include <portb.b>

                              OpenPORTB (PORTB_CHANGE_INT_ON & PORTB_PULLUPS_ON  ) ;
                                              /∗ Configure interrupts and internal pull-up resistors on PORTB ∗/
                              ClosePORTB ();  /∗ Disable  ................................................... ∗/
   235   236   237   238   239   240   241   242   243   244   245