The University of Texas at Arlington Lecture 5 CSE 3442/5442
LCD Discussed in Chapter 12 RS, R/W, E Signals Are Used to Send/Receive Data on D0-D7 2
PIC PROGRAMMING IN C CHAPTER 7 Chapter 7 discusses the special C constructs or statements for using C with the PIC. We will continue to study the assembler instructions that help understand the PIC architecture but will do labs and programming in C. As we go through Chapter 4, accessing the I/O ports with assembler, we will discuss the related C statements for performing the same function in Chapter 7. 3
Chapter 4 PIC I/O PORT PROGRAMMING Ports are not only used for simple I/O, but also can be used for other functions such as ADC, timers, interrupts, and serial communication pins. Figure 4-1 shows the alternate functions for the PIC18F458 pins. 4
PIC18F458 Pin Diagram RA0-RA5 RB0-RB7 RA6 CSE 3442/4442 Lecture 3 5
Number of Individual Port Pins For example, for the PIC18F458, Port A has 7 pins; Ports B, C, and D each have 8 pins; and Port E has only 3 pins. Each port has three SFRs associated with it. -- PORTx, TRISx, and LATx (LATch). 6
Using PIC18F458 A-E Ports for Input/Output Each of the Ports A-E in the PIC18F458 can be used for input or output. The TRISx SFR is used solely for the purpose of making a given port an input or output port. To make a port an output, write Os to the TRISx register. Or, to output data to any of the pins of the Port B, first put Os into the TRISB register to make it an output port. Then send the data to the Port B SFR. 7
Addresses of SFR, PORTx, TRISx (TRIState), and LATx (LATch). See Table 4-2 PORTA F80H PORTB F81H TRISA F92H 8
Recall in Lecture 3 - SFR Window in MPLAB Simulator 9
Example Using Port A as Input In order to make all the bits of Port A an input, TRISA must be programmed by writing 1 to all the bits. In the next slide code, Port A is configured first as an input port by writing all 1 s to register TRISA, and then data is received from Port A and saved in some RAM location of the filereg: 10
PORT A as Input MYREG EQU 0X20 ;Program location MOVLW B 11111111 ;All 1 s to WREG MOVWF TRISA ;Port A as input port (1 for In) MOVF PORTA,W ;move from filereg of Port A to WREG MOVWF MYREG ;save in filereg of MYREG 11
PORT B as Input In order to make all the bits of Port B an input, TRISB must be programmed by writing 1 to all the bits. In the following code, Port B is configured first as an input port by writing all 1 s to register TRISB, and then data is received from Port B and saved in some RAM location of the filereg: MYREG EQU OX25 ; save it here MOVLW B'llllllll' ;WREG = 11111111 (binary) MOVWF TRISB ; make Port B an input port (1 for In) MOVF PORTB,W ; move from filereg of Port B to WREG MOVWF MYREG ; save it in filereg 12
Using C to Set Port B as Input and Read Port B #include <P18F452.h> // #include <P18cxxx.h> ; adds all of the header files // needed for PIC18 family void main(void) { unsigned char mybyte; TRISB = 0xFF; port B as input mybyte = PORTB; read Port B ; and save in mybyte. 13
Dual role of Ports The PIC18 multiplexes an analog-to-digital converter through Port A to save I/O pins. The alternate functions of the pins for PortA are shown in Table 4-3. ADC is discussed in Chapter 13. Since many projects use an ADC, typically do not use Port A for simple I/O functions. The PICI8 multiplexes some other functions through Port B, C, and D to save pins. 14
Figure 4-1 PICF458 Pin Diagram 15
QuickFlash Board 16
Note: Bit 4 of Port A is Open Drain QuickFlash Board Note: Pull Up to resistor to VDD 17
Dual Role of Ports A&B 18
Dual Role of Ports C&D 19
Using Ports as Both Input & Output To use the pins of Ports as both input and output ports; each bit must be connected externally to the pin by enabling the bits of register TRISB. Due to timing issues, do not have two I/O operations one right after the other. The following code will read from PortC and send to Port B: 20
Reading from Port C and sending to Port B: CLRF TRISB ; Clear TRISB output port SETF TRIBC ; Set Port C input port L4 MOVF PORTC,W ;read from Port C NOP ; delay one instruction MOVWF PORTB ;send to Port B BRA L4 ;repeat 21
Pipeline Activity for Both Fetch and Execute 22
Pipeline for Read Followed by Write I/O 23
C Program to Read From B and Send to C (Example 7-10) 24
C Program to Read From C and Output to B or D (Example 7-11) 25
Outputting (Writing) 0 to a Pin in the PIC18 26
Outputting (Writing) 1 to a Pin in the PIC18 27
Inputting (Reading) 0 from a Pin in the PIC18 28
Inputting (Reading) 1 from a Pin in the PIC18 29
LATx Register Role in Reading a Port or Latch 30
Example of Interfacing PIC to LED/Switches on QuickFlash Potentiometer LED s Push Button Switch CSE 3442/4442 Lecture 6 31
Chapter 4 Working with I/O ports - C Program to Read From C and Output to B or D (Example 7-11) CSE 3442/4442 Lecture 6 32
Single-Bit Addressing Review Assembler Instructions Table 4-8 BSF, BCF, BTG BIT Set/Clear/Toggle BTFSC, BTFSS Bit test, skip if clear/set Single-Bit Addressing RA0, RA5..RA8, RB0..RB8 CSE 3442/4442 Lecture 6 33
Example 7-14 pp 263 CSE 3442/4442 Lecture 6 34
Assignment for Tuesday, September 10 Finish Reading Chapter 4 Read Chapter 12, pages 473-481 HW#2 Due Thursday September, 12. 35