Internal Hardware Resources of PIC Microcontroller 16F84 (Part 3)[Copy link]
4 I/O port As a control device, the microcontroller must have data input and output. The input quantity may be temperature, pressure, speed, etc., while the output quantity may be switch quantity and data to ensure that the controlled process runs within the specified range. Both the input and output of data must pass through the relevant circuits inside the microcontroller, and then form an input/output (I/O) port with the pins. The PIC16F84 microcontroller chip has two I/O ports (PORTA and PORTB). Port A is a 5-bit port and port B is an 8-bit port, occupying a total of 13 pins. Each port consists of a latch (that is, the special function register 05H, 06H unit in the data memory), an output driver and an input buffer. When the I/O port is used as an output, the data can be latched; when used as an input port, the data can be buffered. RA4 in the 16F84 PORTA port is a Schmitt trigger input and an open drain output. The other RA port pins are TTL level inputs and full CMOS drive outputs. Port PORTB is an eight-bit bidirectional programmable I/O port. Although each port is also composed of latches, drivers, buffers, etc., the circuits are also different due to slightly different functions. Now take the circuit of RA0 ~ RA3 of PORTA port (see the left figure) as an example to explain its basic working principle. The I/O pin of RA port in the figure is defined by the data direction bit (register TRISA) to define the data flow direction. When the position of the TRISA register is "1", its output driver (composed of P-channel and N-channel MOS tubes connected in series) is in high impedance state, that is, both MOS tubes are cut off, and the I/O port is defined as input. At this time, data is input from the I/O terminal and goes to the D flip-flop through the TTL input buffer. When the read instruction is executed, this D flip-flop is enabled, and the data enters the data bus through the tri-state gate. When the position of TRISA is "0", the I/O port is defined as output, and the output level of the output latch is the output level of the I/O port. The result of reading the PORTA register is to read the level on the I/O pin, and the result of writing the PORTA register is to write to the I/O latch. All operations of writing I/O ports are a "read/modify/write" process, that is, read the I/O pin level first, then modify it by the program (give a value as required), and then put it into the I/O latch. The output of the PIC16F84 microcontroller can provide 20mA of current, so it can directly drive the LED. Each bit of PORTA and PORTB can be defined as input and output respectively. The following example of the PORTA port initialization program explains how to select the I/O port. CLRF PORTA; Port A is cleared BSF STATUS; The RPO position of the status register STATUS is 1, and BANK1 is selected. MOVLW 0xCF; Put the directional value ; 11001111 into the W working register MOVWF TRISA; Set RA (3~0) bits as input ; RA 5?4 bits as output ; TRISA 7?6 bits are not used When using the I/O port, you should pay attention to the following: (1) When an I/O port is required to be used as input and output at the same time, the output value will be uncertain. (2) The I/O pin output drive circuit is a CMOS complementary push-pull output. When it is in the output state, it cannot be connected to other output pins in "wired OR" or "wired AND", otherwise, the microcontroller will be burned out due to current overload. (3) After writing to the I/O port, it is not suitable to directly perform a read operation. Generally, it is required to add at least one NOP instruction between two consecutive write and read instructions. Example: MOVWF 6; write I/O NOP; stabilize I/O level MOVF 6, W; read I/O 5? Stack When the microcontroller executes a program, it often needs to execute a call subroutine. This raises a question: how to remember where the subroutine is called from so that it can return correctly after executing the subroutine. In addition, during the execution of the program, an interrupt may occur and the interrupt subroutine will be executed instead. At this time, how to remember where the interrupt was from so as to return? The method to meet the above functions is the "stack" technology. The "stack" is a stack area used to store temporary data. When the main program calls a subroutine, the microcontroller executes the CALL instruction or an interrupt occurs, and the address of the next instruction is automatically "pushed" to the stack area. When the subroutine ends and the microcontroller executes the return instruction, it automatically "pops" the contents of the stack area as the new address for the next instruction to be executed. The PIC16F84 microcontroller chip has an 8-level 13-bit wide (same width as PC) hardware stack, which occupies neither program storage space nor data storage space. When a CALL instruction is executed or an interrupt is responded to, the breakpoint address in the program counter PC is automatically pushed (PUSH) to protect it, and when the RETURN, RETLW or RETFIE instruction is executed, the breakpoint address in the stack will pop back (POP) to the program counter PC. Whether it is a PUSH or POP operation, it does not affect the contents of the PCLATH register.