Author: Chengdu Ding Jinyuan
Although there are many varieties of PIC series microcontrollers, the data memory settings of the internal hardware resources of each product are still very regular. The author takes PIC16C71A and PIC16C63/65/65A as examples to check the structure of their on-chip data memory, find out their characteristics and explain the main functions of some registers for users to quickly program. Table 1 and Table 2 are the resource tables of the on-chip data memory of PIC16C71A and PIC16C63/65/65A products respectively. The resources of the on-chip data memory structure of other series products are very similar to those in Table 1 and Table 2. The only difference is the type and number of on-chip functional components (PIC16C57/58 has 4 storage banks). The author introduces the characteristics and main functions of Table 1 and Table 2 from the following aspects.
1? Unified addressing All kinds of data memory of PIC series microcontrollers work and address in register mode. Special registers include timing register TMRO, selection register OPTION (also known as option register), program counter PCL, status register STATUS, indirect addressing registers INDF and FSR, port I/O registers (such as PORTA, PORTB...) and corresponding port I/O control registers (also known as port I/O data direction registers, such as TRIAS, TRISB...), holding register PCLATH and interrupt control register INTCON, etc. The above special registers are common to PIC16C63/65/65A and PIC16C71A. They not only have the same register names and functions, but also the same register addresses. If you look at other PIC microcontrollers, such as PIC16C62/62A/64/64A, PIC16C71/72/73/73A/74/74A, PIC16C8X..., their special register names are the same as the above, and their addresses are also exactly the same as the above. It can be seen that although there are many varieties of PIC series microcontrollers, it is not difficult to learn after mastering their rules.
Different types of PIC microcontrollers have different internal resources in data memory only in terms of function type and quantity. For example, the PIC16C71A type has 18 pins and its main function is to have an 8-bit A/D conversion unit with 4 A/D channel analog inputs. Therefore, in Table 1, the dedicated registers related to its A/D conversion unit are ADRES (used to store the numerical results of A/D conversion), A/D control register ADCON0 (used to control the operation of the A/D converter) and A/D control register ADCON1 (used to control the function of selecting the A/D pin). For the PIC16C65/65A type, it has 40 pins and its functions are stronger than those of the PIC16C71A. Therefore, the types of dedicated registers in the data memory table 2 are much more than those in Table 1.
Each register unit of the dedicated register has a corresponding fixed purpose. They can be divided into two categories: one is used for CPU operation (such as INDF and FSR, STATUS, PCL...); the other is used to control the operation of peripheral function chips.
?
When learning about the data memory of a PIC microcontroller, it is necessary not only to understand the functions of each register unit, but also to call them to achieve the purpose of programming when compiling a program. The following author will use programming examples to illustrate their uses.
2? Indirect addressing registers INDF and FSR The indirect addressing register INDF, located at the top of the PIC data memory and at address 00 (the smallest address code), is an empty register. It only has an address code and is not a real register physically. Its function often works in conjunction with the register FSR (also known as the register selection register) to achieve the purpose of indirect addressing. When learning about the special registers INDF and FSR for the first time, it is helpful to remember the following logical relationship for programming: Any instruction using the register INDF logically accesses the RAM pointed to by the register FSR, that is, indirectly addresses (accesses) INDF (itself), and the content of FSR should be read out. The following simple program is an example of clearing the contents of the registers at addresses 20h to 2Fh of the RAM using indirect addressing.
MOVLW 0x20; 20h→w, pointer to RAM unit
; initialize
MOVWF FSR; 20h→FSR, FSR points to RAM
LOOP CLRF INDF; clear INDF, that is, clear
the unit pointed to by FSR content; 20h→2Fh
INCF FSR; (pointer) FSR content plus 1
BTFSS FSR, 4; judge (instruction) FSR D3 bit, if it is zero
; execute the next loop instruction; if it is 1, jump
; execute.
GOTO LOOP; jump to LOOP (loop)
CONTINUE…; the function has been completed, continue to execute the program.
It can be seen from the above instructions that due to the cooperation of registers INDF and FSR, the purpose of clearing the registers of RAM address 20h~2Fh is achieved. Since the number of instructions to complete the above functions is small, this will simplify the instruction system and streamline the instruction set of PIC microcontrollers.
Note: The above instructions are easy to understand, so there is no need to repeat them, but one of the judgment instructions "BTFSS FSR, 4" is more critical. This instruction is to ensure that when the upper limit value of the RAM address unit 2Fh is selected in the question, the corresponding binary number is 00101111B, and the 4th bit of FSR is exactly 1. Therefore, a judgment instruction is used in the above instructions; BTFSS FSR, 4, to determine whether the D3 bit value of FSR is 1. If it is not 1 but 0, the next loop instruction GOTO LOOP is executed, so that the address in FSR is continuously increased by 1 until the D3 bit of register FSR is 1, at which time its content represents the RAM address of 2Fh.
It can be seen from this that when learning the dedicated registers in the data memory of the PIC microcontroller, it is not necessary to learn the dedicated registers of each product. You only need to learn their common points first, and then select the dedicated register of a product model for detailed analysis. When conditions permit, perform necessary related instruction operations, and you can fully master the microcontroller technology.
3?A/D conversion register Here is an extract of some source program instructions that I downloaded from the Internet, which uses the PIC16F877 chip (with Flash memory) to complete the A/D conversion, and uses it to illustrate the usage of the A/D conversion register in the instructions. Here I first quote part of the source program. The comments in the source program are the comments added by the author according to the role of the instructions in the program (not the direct function of a certain instruction). This is what beginners should pay attention to when reading the following instructions. The source code list of the A/D converter is as follows:
DEMO 877?ASM
List P=16F877
org 0x00; Reset vector
NOP; No operation
Start Banksel PORTC; Select
the storage location of PORTC
(actually Bank0)
CLRF PORTC; Clear the RC port
MOVLW B′01000001′; A/D conversion clock selection
FOSC/8, turn on
the A/D converter
MOVWF ADCON0; Set the A/D conversion
; operation part parameters
Banksel OPTION_REG; Select
the storage location of OPTION (actually Bank1)
MOVLW B′10000111′; Set the prescaler TM
; R0, division rate 1:256
MOVWF OPTION; Complete the setting of the previous instruction
CLRF TRISC; Set the RC port (8 bits)
; for output
MOVLW B′0001110′; Select analog channel 1
; (RA1/AN1)
MOVWF ADCON1; The analog reference voltage
VREF is the chip power supply voltage, select channel 1 (RA1/AN1) to complete
main...
To read the above instructions, readers also need to understand the following necessary supplementary knowledge.
(1) About using PIC16F877 as an A/D converter. The PIC16F877 microcontroller is an 8-bit A/D converter with multi-channel analog input. The above source program is an experimental program using this PIC product for A/D conversion. The purpose of the experiment is to use PIC16F877 to realize an 8-bit A/D conversion of one channel, and output the conversion result in binary form through the RC port and then display it by LED. The circuit principle of the experiment is shown in the attached figure (the PIC16F877 A/D conversion hardware circuit diagram drawn by the author based on the source program).
(2) Compile special register instructions related to the A/D converter. To understand the above source program, you should follow the following clues: 1) Understand the writing convention of the source program; 2) Select the RC port and clear it; 3) Give the A/D control register ADCON0 each position number to select the sampling time of the A/D bit during A/D conversion, that is, the A/D clock selection in the comments; 4) Select the special register Option and give it each position number to select the prescaler TMR0 and determine its division rate (1:256); 5) Set the RC port as output to ensure LED display; 6) Give the A/D controller ADCON1 each position number to determine the RA1 port of PIC1F877 as the input channel of the analog quantity. And select the chip power supply as the reference voltage. If readers need to understand the detailed functions of each bit of the A/D control register ADCON0 and ADCON1, please refer to the detailed introduction of the PIC microcontroller books.
The main program (main) of A/D conversion has about 13 items, which will be introduced in the later part of this edition of the PIC series of special topics combined with the programmer of the experimental board. Other special registers of PIC series microcontrollers, such as TRISA, STATUS Bank, etc., have been introduced in the previous issues of this newspaper on PIC.