Menu English Ukrainian russian Home

Free technical library for hobbyists and professionals Free technical library


ENCYCLOPEDIA OF RADIO ELECTRONICS AND ELECTRICAL ENGINEERING
Free library / Schemes of radio-electronic and electrical devices

What is PIC? Encyclopedia of radio electronics and electrical engineering. Encyclopedia of radio electronics and electrical engineering

Free technical library

Encyclopedia of radio electronics and electrical engineering / Microcontrollers

Comments on the articleComments on the article

Introduction. The PIC16CXX are 8-bit RISC microcontrollers manufactured by Microchip Technology. This family of microcontrollers is characterized by low price, low power consumption and high speed.

Microcontrollers have a built-in program EEPROM, data RAM and are available in 18 and 28 output packages.

PIC OTP are user-programmable one-time controllers designed for fully tested and finished products that will not undergo further code changes. These controllers are available in cheap plastic cases with a preset type of external oscillator - quartz or RC.

For debugging programs and prototyping, a version of controllers with ultraviolet erasure is available. These controllers allow a large number of write/erase cycles and have a very short erasure time - typically 1-2 minutes.

However, the price of such controllers is significantly higher than those programmed once, so it is unprofitable to install them in serial production.

For products, the program of which can change, or contains any variable parts, tables, calibration parameters, keys, etc., an electrically erasable and reprogrammable PIC16C84 controller is available. It also contains an electrically flashable data ROM. It is this controller that we will use for experiments.

To get the most out of this article, you will need an IBM PC compatible personal computer, a programmer connected to the computer's parallel port, a PIC16C84 chip, a breadboard, 8 LEDs with resistors, a +5V power supply, and a socket for the chip.

We'll be typing little bits of PIC software, assembling them, putting them on the chip, and then watching the results on the LEDs. We will use the basic MICROCHIP mnemonics in this article so that you can more easily use the other application examples and listings provided in the Embedded Control Handbook.

PIC16CXX FAMILY We will begin a detailed description of the PIC family of microcircuits with those features and advantages that distinguish these microcontrollers from others. For data security applications, each PIC has a security bit that can be programmed to disable reading of program code and data ROM. When programming, the program code is first written, checked for correct writing, and then the security bit is set. If you try to read the chip with the security bit set, then for the PIC16C5X the upper 8 bits of the code will be read as 0, and the lower 4 bits will be scrambled 12 bits of the command. For PIC16C84, similarly, the 7 most significant bits will be read as zeros, and the 7 least significant bits will represent the scrambled 14 bits of the command. The PIC16C84 EEPROM cannot be read when the security bit is set. Figure 1 shows all currently produced microcontrollers and gives their brief characteristics. (Figure on page 2-1) The PIC family of microcontrollers has a very efficient instruction set consisting of only 35 instructions. All instructions are executed in one cycle, except for conditional jumps and instructions that change the program counter, which are executed in 2 cycles. One instruction execution cycle consists of 4 clock cycles. Thus, at a frequency of 4 MHz, the instruction execution time is 1 µs. Each instruction consists of 14 bits, divisible by the operation code and operand (possible manipulation with registers, memory locations and immediate data). The command set of the PIC16CXX microcontrollers is shown in Figure 2. (Table 7.2 on page 2-569) The high speed of command execution in the PIC is achieved by using a two-bus Harvard architecture instead of the traditional single-bus Von Neumann architecture. The Harvard architecture is based on a set of registers with separated buses and an address space for instructions and for data. A register set means that all software objects, such as I/O ports, memory cells, and a timer, are physically implemented hardware registers.

The data memory (RAM) for the PIC16CXX is 8 bits, the program memory (PROM) is 12 bits for the PIC16C5X and 14 bits for the PIC16CXX. Using the Harvard architecture allows you to achieve a high speed of bit, byte and register operations. In addition, the Harvard architecture allows for pipelined execution of instructions, when the current instruction is executed and the next one is read at the same time. In the traditional Von Neumann architecture, commands and data are transferred via a single shared or multiplexed bus, thereby limiting the possibilities of pipelining. Figure 3 shows a block diagram of the PIC16CXX dual bus structure. (Figure B on page 2-536) As you can see, the internal physical and logical components that make up the PIC16CXX are similar to any other microcontroller you may have worked with so far. Therefore, writing programs for the PIC is no more difficult than for any other processor. Logic, and only logic...

Of course, the Harvard architecture and the large command size make it possible to make the PIC code much more compact than for other microcontrollers and significantly increase the speed of program execution.

PIC REGISTER SET All software objects that the PIC can work with are physical registers. To understand how the PIC works, you need to understand what registers it has and how to work with each of them. Figure 4 shows all the registers of the PIC16C84. (pic 3.7.1 on p.

2-541) Let's start with a set of operational registers. This set consists of the indirect address register (f0), the timer/counter register (f1), the program counter (f2), the status word register (f3), the select register (f4), and the input/output registers (f5,f6).

It is imperative that you understand how to use these registers, as they represent the bulk of the microcontroller's software-accessible objects. Since we mostly need to understand "how to manage" and not "how it's done internally", we've included very simple examples showing possible uses for each register.

f0...INDIRECT ADDRESS REGISTER IND0 Indirect address register f0 does not physically exist. It uses the f4 select register to indirectly select one of the 64 possible registers.

Any instruction that uses f0 actually accesses the data register pointed to by f4.

f1... TMR0 TIMER/COUNTER REGISTER The TMR0 timer/counter register can be written to and read from like any other register. TMR0 can be increased by an external signal applied to the RTCC pin, or by an internal frequency corresponding to the command frequency.

The main use of the timer/counter is to count the number of external events and measure time. The signal from an external or internal source can also be pre-divided using the PIC's built-in programmable divider.

f2...PCL PROGRAM COUNTER The program counter (PC) is used to generate a sequence of program ROM cell addresses containing 14-bit instructions. The PC has a 13-bit capacity, which allows you to directly address 8Kx14 ROM cells. For the PIC16C84 however, only 1K cells are physically available. The lower 8 bits of the PC can be written and read through the f2 register, the upper 5 bits are loaded from the PCLATCH register, which has address 0Ah.

f3... STATUS REGISTER The status word register is similar to the PSW register found in most microprocessors. It contains the carry, decimal carry, and zero bits, as well as enable mode bits and paging bits.

f4...FSR SELECT REGISTER As already mentioned, the FSR select register is used in conjunction with the indirect addressing register f0 to indirectly select one of the 64 possible registers. Physically involved are 36 user RAM registers located at addresses 0Ch-2Fh and 15 service registers located at different addresses.

f5, f6... I/O REGISTERS PORTA, PORTB Registers f5 and f6 correspond to the two I/O ports available on the PIC16C84. Port A has 5 bits PA4-PA0, which can be individually programmed as inputs or outputs using the TRISA register at address 85h. Port B has 8 bits PB7-PB0 and is programmed using the TRISB register with address 86h. Setting 1 in the TRIS register bit programs the corresponding port bit as an input. When reading the port, the immediate state of the output is read, when writing to the port, the write occurs in the buffer register.

f8, f9...EEDATA, EEADR EEPROM REGISTERS The PIC16C84 has a built-in 64-byte EEPROM that can be read and written using the EEDATA data register and the EEADR address register. Writing a new byte lasts about 10 ms and is controlled by a built-in timer. The control of writing and reading is carried out through the EECON1 register, which has the address 88h. For additional control over the recording, the EECON2 register, which has the address 89h, is used.

GENERAL PURPOSE REGISTERS General purpose registers are static RAM located at addresses 0Ch-2Fh. A total of 16 RAM cells can be used in the PIC84C36.

SPECIAL REGISTERS W, INTCON, OPTION Finally, let's look at the special PIC registers. These include the working register W, used as the accumulator register in most commands, and the INTCON and OPTION registers. The INTCON interrupt register (address 0Bh) is used to control interrupt modes and contains interrupt enable bits from various sources and interrupt flags. The OPTION mode register (address 81h) is used to set the signal sources for the prescaler and timer/counter, as well as to set the division factor of the prescaler, the active edge of the signal for RTCC and the interrupt input. In addition, using the OPTION register, terminating resistors can be enabled for port B bits programmed as inputs.

WDT WDT Watchdog The WDT watchdog is designed to prevent catastrophic consequences from accidental program failures. It can also be used in timing applications such as a missed pulse detector. The idea of ​​using a watchdog timer is to regularly reset it under the control of a program or an external influence before its time delay expires and the processor does not reset. If the program is running normally, then the CLRWDT watchdog reset command should run regularly to prevent the processor from resetting. If the microprocessor accidentally went beyond the limits of the program (for example, from strong interference in the power circuit) or got stuck in some part of the program, the watchdog reset command most likely will not be executed within a sufficient time, and a complete reset of the processor will occur, initializing all registers and bringing the system into working condition.

The watchdog timer in the PIC16C84 does not require any external components and runs on the built-in RC oscillator, and the generation does not stop even in the absence of the processor clock frequency. Typical watchdog period is 18 ms. You can connect a prescaler to the watchdog timer and increase its period up to 2 seconds.

Another function of the watchdog timer is to turn on the processor from the low power mode, in which the processor is transferred by the SLEEP command. In this mode, the PIC16C84 consumes very little current - about 1 µA. You can switch from this mode to the working mode either by an external event of pressing a button, triggering a sensor, or by a watchdog timer.

CLOCK GENERATOR Four types of clock generator can be used for microcontrollers of the PIC family: XT quartz resonator HS high-frequency quartz resonator LP micro-consuming RC crystal RC circuit chip programming process. If the XT, HS and LP options are set, a quartz or ceramic resonator or an external clock source is connected to the microcircuit, and if the RC option is set, a resistor and a capacitor are connected. Of course, a ceramic and, especially, a quartz resonator is much more accurate and stable, but if high timing accuracy is not needed, the use of an RC generator can reduce the cost and size of the device.

RESET CIRCUIT The PIC family of microcontrollers use an internal power-on reset circuit in combination with a oscillator start timer, eliminating the need for a traditional resistor and capacitor in most situations.

Simply connect the MCLR input to a power source. If surges or surges are likely to occur when the power is turned on, it is best to use a 100-300 ohm series resistor. If the power ramp-up is very slow (less than 70 ms), or you are running at very low clock speeds, then a traditional resistor and capacitor reset circuit must be used.

FROM THEORY TO PRACTICE...

We briefly got acquainted with the main elements that make up the microcontrollers of the PIC family. Now let's move on to practical exercises.

We will write short programs, assemble them, write them into a microcircuit and see what happens.

To do this, we need the following things: - chip PIC16C84; - assembler MPALC; - PROPIC programmer; - power supply of direct or alternating current 18-36 V; - prototyping board with display device.

The schematic diagram of the display device, which we will use to demonstrate the operation of the basic PIC16C84 commands, is shown in Figure 5. (should be drawn) As you can see, the device consists simply of 8 LEDs with current-limiting resistors and frequency-setting elements. Each output of microcontrollers of the PIC family can directly control the LED without additional amplifiers.

What is PIC?

Let's start by describing the basic code that will be used in our examples. When you start writing code for your project, the header section (all code up to the ORG 0 line) should be specific to your application. The header section defines logical names for all resources used in the project - ports, bit and byte variables and registers. Our header also sets the I/O ports so that all bits of ports A and B will be set as outputs after executing the following commands: MOVLW INITA MOVWF TRISA MOVLW INITB MOVWF TRISB When the power is turned on, the PIC16C84 sets all bits of ports A and B to input and starts execute the program from address 000h. The basic code is shown in Figure 6. (need to draw) Something like this: ; Sample base code for the demo program ; LIST P=16C84, E=2 ; ; Header section ; ; description of operating registers TMR0 EQU 01h PC EQU 02h STATUS EQU 03h FSR EQU 04h ; I/O registers CNTRLPORT EQU 05h DATAPORT EQU 06h ; RAM locations SCRATCH EQU 0Ch DIGIT EQU 0Dh ; register bits STATUS C EQU 0h DC EQU 1h Z EQU 2h PD EQU 3h TO EQU 4h RP EQU 5h ; control registers TRISA EQU 85h TRISB EQU 86h ; initialization words for I/O ports INITA EQU B'00000000' INITB EQU B'00000000' ; ; Working section ; ; start of executable code ORG 0 GOTO BEGIN ; ORG 100h BEGIN MOVLW INITA MOVWF TRISA MOVLW INITB MOVWF TRISB ; ; Paste the example code here ; END ; Refer back to Figure 4 if necessary while we discuss the basic code. First, all lines beginning with ";" are treated as comments by the assembler. Let's move on to the expression TMR0. We told the assembler that every time the word TMR0 is encountered, it should substitute the value 01h (01 hex). The word "EQU" means equality. Thus, we have assigned TMR0 the value 1h. As you can see from Figure 4, the TMR0 register does indeed have address 1h. You can use 01h every time you want to address the TMR0 register, but this will be much more difficult to debug because you have to remember that 01h means RTCC all the time. You may also have data equal to 01h. The use of symbolic names eliminates ambiguity and makes the source text easier to read. You can also see the expressions for the PC, STATUS and FSR registers. The name PC corresponds to register address 02h, the name STATUS corresponds to register address 03h, the name FSR corresponds to register address 04h, and so on. We have also given names for the I/O ports, CNTRLPORT (05h) and DATAPORT (06h). RAM cells can also have names. We chose the names "SCRATCH" for the cell at 0Ch and "DIGIT" for the cell at 0Dh. If you read to the end of this text, you will see that we do not use the PC directly anywhere, although this name is defined. There is no mistake in this - you can define names and then not use them, although, of course, you cannot use a name if it has not been previously defined. Don't worry too much about this - the assembler's job is to check the text against all the rules, and you'll get error messages if something doesn't match. Not only can you name registers, you can also name individual bits within registers. Pay attention to the section that sets the STATUS register. Figure 7 shows what the STATUS register consists of. (Figure 3.9.1 on page 2-544) Symbol C is set to 0h because C, or CARRY, is bit zero of the STATUS status word. Every time we need to check the CARRY bit (bit 0), we will use the predefined character "C". Every time we want to access bit 2, or the ZERO bit, we will use the character "Z" instead of 02h. You can define the full bit structure of a register, even if you don't use all of them afterwards. Now it's clear to us how registers are described, and we can move on to the executable code. Before starting the executable code, we must set the ORG expression to 0. This is a pointer to the assembler that the code following this expression starts at EEPROM address zero. The "ORG" expression is used to allocate code segments to different addresses within the EEPROM size. Another ORG expression occurs before the BEGIN label at address 100h, as given by ORG expression 100h. The executable code must end with an END directive, meaning that there are no executable commands following this directive. On power up, the PIC16C84 jumps to address 000h. The first instruction to be executed by the processor is the GOTO BEGIN command, which will transfer control to address 100h and further work will continue from this address. BEGIN is a user-selectable label name (labels must always start at the first line position) that the assembler uses as a reference. During operation, the assembler determines the location of the BEGIN label and remembers that if this name is encountered again, the label's address will be substituted for it. The CALL and GOTO commands use labels for links in the source text. Now let's look at the following commands executed by the processor. The MOVLW INITA instruction loads the value assigned to the name INITA into the working register W. This value is specified in the header and equals B'00000000', which is 00h. The characters B' mean that the data is given in binary format. One could write 0 (decimal) or 0h (hexadecimal) in the same place and get the same result. Binary representation is more convenient to use in those cases when an operation with bits in a register is expected. The following MOVWF TRISA instruction loads the value from the working register W into the TRISA port A configuration control register. Setting a bit of this register to 0 specifies that the corresponding port A bit is an output. In our case, all bits of port A are set by outputs. Note that port A has only 5 bits, and the upper 3 bits of the value written to the TRISA register, which also has 5 bits, are not used. If we wanted, for example, to set the low bit of port A as an input, we would set the value of INITA to B'00000001' in the register description section. If during the course of the program we need to redefine the assignment of individual port bits, for example, during bidirectional transmission, then it is most convenient to specify all the necessary configuration words in the description section, as we did for INITA and INITB. The next two commands MOVLW INITB and MOVWF TRISB define the configuration of port B. We could save money by not writing the MOVLW INITB command, because in our case INITB is also 0h. However, we did not do this, because it can lead to hard-to-find errors if we subsequently need to change the assignment of any one bit. Instead of changing only one bit in one port, two bits with the same number in two ports will change. Therefore, while the program is not finished, it is not desirable to make such savings, although at the end, at the stage of code optimization, such repetitions can be removed. What have we already done? 1. With the EQU lines, we told the assembler which symbol names we were going to use. 2. We have set the reset vector to address 000h. 3. We have set the starting address of the program execution from the label BEGIN to the address 100h. 4. We have configured all bits of ports A and B as outputs. Now we can insert the example code between the header and the end of our base code instead of the commented line "Insert example code here". We will replace this line with real commands, assemble the resulting program, write it to a microcircuit, rearrange the microcircuit on a prototyping board with an indication device and see what happens. FIRST PROGRAM For the first program, we only need three commands: MOVLW k MOVWF f GOTO k We have already used these commands in the header of our basic code. The MOVLW instruction loads a byte literal or constant into the W work register. The following MOVWF instruction moves a byte from the working register W to the specified register f. The GOTO command transfers control to address k. The following program writes the value 01010101 to the working register W and then writes its contents to port B. After starting this program, you will see the glow of four LEDs. MOVLW B'01010101' ;load 01010101 into register W MOVWF DATAPORT ;write W into port B (DATAPORT) GOTO $ ;loop forever The assembler directive "$" means the current value of the program counter (PC). Therefore, the GOTO $ command means jumping to where we currently are. Such a loop is infinite, since there is no way (other than interruption) to get out of it. The GOTO $ command is often used to stop code while debugging. ASSEMBLY We will use the macro assembler MPALC, since it is freely distributed by MICROCHIP, and contains all the features we need. If you prefer to use another assembler, use its description to assemble our program. The command line to run the MPALC macro assembler is very simple: MPALC <source_text> [options] where <source_text> is the name of the file containing the source text to be assembled, and there can be quite a lot of options. The full description of the options is given by the assembler on the option /? At first, we do not need to specify any options. The only thing we still need to do is specify the type of processor for which our program is written and the type of errors that the assembler should produce. This is done using the LIST assembler directive (Fig. 6). The default source file extension is .ASM. Another extension must be explicitly specified. So, take the EXAMPLE.ASM file containing the text shown in Figure 6, and run the assembler: MPALC EXAMPLE As a result of the assembler, files with the following extensions are created: * OBJ object file * LST listing file * ERL error and warning file * SYM symbol file The object file is created in hexadecimal format and contains code that must be written to the chip. The listing file contains the complete listing of the program along with the boot code. The error and warning file records all errors and warnings that occur during the assembly process. They are also present in the listing file. The table of symbolic labels, which is written to a symbolic file, is intended for further work with the debugger. After processing our program, the assembler should have issued the message "No errors were found by the assembler", meaning that no errors were found. The error file should not have been created. If your assembler generated any error messages, or the EXAMPLE.OBJ, EXAMPLE.LST and EXAMPLE.SYM files were not created, check again if you did everything correctly. PROGRAMMING You now have an object file, EXAMPLE.OBJ, which must be written to the chip. Recording is carried out with the help of the programmer and the PROPIC program. The command line for running the PROPIC program is similar to the line for running the assembler: PROPIC <object_file> [options] where <object_file> is the name of the object file created by the assembler, and the options determine the mode of writing to the chip. The list of options is given by the option /? As in the case of assembler, at first we do not need to specify any options. Take the programmer, connect its cable to the printer port connector (to the first printer port if your machine has 2 or 3 printer ports).

What is PIC? What is PIC?

Take an 18...36 V DC or AC power supply capable of delivering up to 100 mA and connect it to the programmer. Prepare the PIC16C84 chip, but do not insert it into the programmer socket yet. Now you can start the programmer:

PROPIC EXAMPLE { should be inserted here about how the programmer's program works. } Now you have a programmed chip and you can see how it will work. TESTING

Take a breadboard and assemble on it the circuit shown in Figure 5. There are no critical parts in this circuit. All resistors can have a deviation from the nominal + -30%, LEDs - any with a rated current of not more than 10 mA. To install the PIC16C84 chip, use the socket. To power our circuit, you can use the same power supply that you used when programming the microcircuit. You can also use a 5 V laboratory power supply. In this case, the KR142EH5A stabilizer, diode bridge and electrolytic capacitor are not needed, and a 5 V voltage is supplied to point 2 in the diagram.

After the circuit is assembled, carefully check that everything is assembled correctly, the LEDs are installed in the correct polarity, the power to the microcircuit is coming to the correct pins and in the correct polarity. Take the programmed chip, insert it into the socket on the breadboard and turn on the power. 4 LEDs should light up (after one). Your first program is working! PIC COMMAND SET

Now that you have learned how to assemble a program, write it to a chip and test it on a breadboard, we can move on to describing the entire set of instructions for PIC family microcontrollers. We will continue to focus on the PIC16C84, although almost everything we will talk about is applicable to other microcontrollers of the PIC family. In the course of the description, we will make short programs in order to better understand how certain commands work. You can plug these programs into the base code, assemble them, put them on a chip, and plug the chip into a breadboard and see how it works. If everything is absolutely clear to you in the next paragraph, you can not try it, but go straight to the next paragraph. NOP

Let's start our description with the NOP command. It's hard to see the output of this command because it doesn't do anything. This instruction is usually used in time delay loops or to fine-tune the execution time of a specific section of the program. CLRW

This command clears the working register W. Let's add one line to our example and see that all the LEDs light up.

MOVLW B'01010101' ;load 01010101 into register W

CLRW ;clear register W

MOVWF DATAPORT ;write W to port B (DATAPORT)

GOTO $ ;loop forever CLRF f

CLRF does for any register what CLRW does for working register W. The following command will set port B to 0h.

CLRF DATAPORT ;clear port B (DATAPORT) SUBWF f,d ADDWF f,d

Subtract working register W from any register f. This command also sets the CARRY, DIGIT CARRY, and ZERO flags in the STATUS register. After executing the command, you can check for these signs and determine if the result is zero, positive, or negative. The character d after the comma means the address where the result of the command will be placed. If d=0, then the result is placed in the work register W, and if d=1, then the result is written into the register f used in the command.

In our example, the value 0FFh is loaded into the SCRATCH register, and the value 01h is loaded into the W register. Then the SUBWF command is executed and the result is displayed on the LEDs.

MOVLW 0FFh ;load 0FFh into register W

MOVWF SCRATCH ;load contents of W into SCRATCH register

MOVLW 01h ;load 01h into register W

SUBWF SCRATCH,0 ;subtract LEDs should display 11111110 where 1 is off and 0 is on.

The ADDWF command works in exactly the same way, adding the working register W to any register f and setting the same flags. The following example demonstrates how the ADDWF command works.

MOVLW 0h ;load 0 into register W

MOVWF SCRATCH ;load contents of W into SCRATCH register

MOVLW 1h ;load 01h into register W

ADDWF SCRATCH,0 ;do addition LEDs should display 00000001.

Note that there is a "0" in front of the FFh value in the subtraction example. The symbol "0" for assembler means that this is a number, not a label. If there were no symbol 0, then the assembler would start looking for a label with the name FFh, which does not exist in this program and, accordingly, an error would occur. the character "h" following the value 0FF means that the value is specified in hexadecimal format. SUBLW k ADDLW k

These two commands work in exactly the same way as described above, except that the operation is performed between the working register W and the byte constant specified in the command. The SUBLW command subtracts the working register W from the constant k, and the ADDLW command adds the working register W to the constant k. These commands also set the CARRY, DIGIT CARRY, and ZERO flags. The result of the command is placed in the working register W. The following example will decrement SCRATCH by 5.

MOVLW 0FFh ;load 0FFh into register W

MOVWF SCRATCH ;load contents of W into SCRATCH register

SUBLW 05h ;subtract 5 from working register

MOVWF SCRATCH ;load new contents of SCRATCH LEDs should display 11111010. DECF f,d INCF f,d

The DECF command decrements the given register by 1, and INCF increments the given register by 1. The result can be placed back in the given register (for d=1) or the working register W (for d=0). As a result of the execution of these commands, the ZERO sign can be set in the STATUS register. Here is an example of using these commands:

MOVLW 0FFh ;load 0FFh into register W

MOVWF SCRATCH ;load contents of W into SCRATCH register

DECF SCRATCH,0 ;decrement SCRATCH by 1

This example will increment SCRATCH from 0 to 1.

CLRF SCRATCH ;clear SCRATCH

INCF SCRATCH,0 ;Increment SCRATCH by 1 IORWF f,d ANDWF f,d XORWF f,d

These three commands perform logical operations OR, AND and EXCLUSIVE OR. The logical addition OR operation is most often used to set individual bits in registers. These bits are then reset by a logical AND operation. When an EXCLUSIVE OR operation is performed on identical bits, the result is 0. Therefore, the EXCLUSIVE OR operation is often used to check the status (set or clear) of certain bits in a register. The following procedure will set bit 1 on port B using the IORWF command:

CLRF DATAPORT ;clear port B

MOVLW B'00000010' ;set mask in register W

IORWF DATAPORT,1 ;set bits in port B by mask W

GOTO $ ;loop forever The LEDs should show 00000010. Now clear 2 bits with the ANDWF command:

MOVLW B'11111111' ;Load 0FFh into W register

MOVWF DATAPORT ;set all bits in port B

MOVLW B'00000101' ;set mask in register W

ANDWF DATAPORT,1 ; clear bits in port B by mask W

GOTO $ ;loop forever The LEDs should show 00000101.

Let's say we've used the SCRATCH register and want to know if it's equal to 04h. This is a good time to use the XORWF command:

MOVLW 04h ;load 04h into register W

MOVWF SCRATCH ;load register W in SCRATCH

XORWF SCRATCH,0 ; check if W and SCRATCH are equal Since SCRATCH and W are equal, the result of the XORWF operation is zero (all LEDs are lit). The STATUS register will set the ZERO bit, which the real program can then check and process. IORLW k ANDLW k XORLW k

These three commands perform the same actions as their counterparts described above, except that the operation is performed between the working register W and the mask specified in the command. The result of the command execution is placed in the working register W. For example:

MOVLW 0FFh ;load 0FFh into register W

ANDLW 040h ;leave 6th bit LEDs will show 01000000.

MOVLW 10h ;load 10h into register W

IORLW 09h ;set bits 0 and 3 LEDs will show 00011001.

MOVLW B'00100000' ;load 40h into register W

XORLW B'11111111' ;invert W LEDs show 11011111. MOVF f,d

This command is mainly used to move a register to the working register W (d=0). If you set d=1, then this command will load the register into itself, but the ZERO bit in the STATUS register will be set in accordance with the contents of the register. For example, we want to load into the SCRATCH register 0Fh, and then load the SCRATCH register into the working register W.

MOVLW 0Fh ;Load 0Fh into working register W

MOVWF SCRATCH ;load register W in SCRATCH

CLRW ;clear W

MOVF SCRATCH,0 ;load SCRATCH into W register

MOVF SCRATCH,1 The ZERO bit of the STATUS register will be set if the condition is met (SCRATCH = 0h). COMF f,d

This command inverts any given register. When d=0, the result is entered into the working register W, and when d=1, the contents of the specified register are inverted. As an example, let's invert the value 01010101:

MOVLW B'01010101' ;load 01010101 into register W

MOVWF SCRATCH ;load register W in SCRATCH

COMF SCRATCH,0 ;invert SCRATCH LEDs will show 10101010. DECFSZ f,d INCFSZ f,d

As you gain some experience with PIC assembler, you will use these commands very often. With d=1, the DECFSZ instruction decrements by one, and INCFZ increments the specified register by one and skips the next instruction if the register becomes zero. With d=0, the result is written to the W register and the next command is skipped if the working register W becomes zero. These commands are used to generate time delays, counters, loops, etc. Here is a typical example of using a loop: START

MOVLW 0FFh ;load FFh into register W

MOVWF SCRATCH ;load register W into SCRATCH LOOP

DECFSZ SCRATCH,1 ;decrement SCRATCH by 1

GOTO LOOP ;and loop back until = 0

MOVF DIGIT ;load DIGIT register in W

MOVWF DATAPORT ;output to LEDs

DECF DIGIT,1 ;decrement DIGIT register by 1

GOTO START ;go to start As a result, the LEDs will flash at different rates. The low order LED will blink most often, and the high order LED will blink the least. With a clock frequency of 4 MHz, the blinking frequency of the LED of the highest order will be approximately 8 Hz, and each next one will blink twice as often. Now let's figure out how we did it. The DECFSZ command here works in a delay loop consisting of two commands - DECFSZ and GOTO LOOP. Because we preloaded the SCRATCH register with 0FFh, this loop will execute 255 times until SCRATCH becomes zero. With a clock frequency of 4 MHz, this gives a delay of 1 µs/instruction * 2 instructions * 255 = 510 µs. In the DIGIT register, we did not write anything beforehand, so there could be any value that is displayed on the LEDs on the first pass. Then the DIGIT register is decremented by 1 and the loop repeats from the beginning. As a result, the DIGIT register iterates over all values ​​in 256 cycles, i.e. for approximately 130 ms.

The same code can be used with the INCFSZ instruction by changing the value loaded into the SCRATCH register from FFh ​​to 0h. The LEDs will flash in the same way if the DECF command is replaced with the INCF command. SWAF f,d

This command swaps nibbles in any register. As for other commands, when d=0 the result is written to the work register W, and when d=1 remains in the register. Here is a simple example of using this command:

MOVLW B'00001111' ;load 0Fh into register W

MOVWF SCRATCH ;load register W in SCRATCH

SWAPF SCRATCH,0 ;swap nibbles LEDs will show 11110000. RRF f,d RLF f,d

There are two shift instructions in the PIC assembler - a right shift through the CARRY bit of any RRF register, and a left shift through the CARRY bit of any RRF register. As for other commands, when d=0 the result of the shift is written to the W register, and when d=1 remains in the register. Shift instructions are used to perform multiplication and division operations, for serial data transfer, and for other purposes. In all cases, the bit shifted out of the 8-bit register is written to the CARRY bit in the STATUS register, and the CARRY bit is written to the other end of the register, depending on the direction of the shift. Left shift writes RLF CARRY to the least significant bit of the register, and right shift writes RRF CARRY to the most significant bit of the register.

CLRF STATUS ;clear STATUS register

MOVLW 0FFh ;load 0FFh into register W

MOVWF SCRATCH ;load register W in SCRATCH

RRF SCRATCH,0 ;shift right The LEDs should show 01111111 because CARRY has loaded into the high bit. Now let's move to the left:

CLRF STATUS ;clear STATUS register

MOVLW 0FFH ;load 0FFh into register W

MOVWF SCRATCH ;load register W in SCRATCH

RLF SCRATCH,1 ;shift left LEDs should show 11111110. BCF f,b BSF f,b

The clear BCF bit and set BSF bit commands are used to operate on individual bits in registers. Parameter b means the bit number with which the operation is performed, and can take values ​​from 0 to 7. Let's try to turn on the LED using the BCF command:

MOVLW 0FFh ;load 0FFh into register W

MOVWF DATAPORT ;turn off LEDs

BCF DATAPORT,7 ;clear bit 7 in port B

GOTO $ ;loop forever This will turn on the LED corresponding to bit 7. Recall we did the same thing with the use of a mask and the ANDWF command. The difference is that the ANDWF and IORWF instructions require the mask to be pre-formed and stored in some register, but at the same time they are able to set or clear several bits at the same time. The BCF and BSF instructions operate on only one bit. In addition, the BCF and BSF instructions do not change the STATUS register, so they are often used in cases where a subsequent check of the status register is not required. BTFSC f,b BTFSS f,b

Conditional jump instructions BTFSC and BTFSS check the status of a given bit in any register and skip the next instruction depending on the result. The BTFSC command skips the command if the specified bit is clear, and the BTFSS command if it is set. Here is a simple example:

MOVLW 0FFh ;load 0FFh into register W

MOVWF DATAPORT ;turn off LEDs

MOVLW B'00000001' ;load 01h into register W

MOVWF SCRATCH ;load register W into SCRATCH LOOP

BTFSS CNTRLPORT,0 ;check bit 0 in CNTRLPORT

GOTO LOOP ;wait until bit 0 is set

BCF DATAPORT,7 ;turn on LED

GOTO $ ;loop forever This example checks bit 0 of port A (chip pin 17) and, if this pin is set high, turns on the LED. Try replacing BTFSS with BTFSC in this example. The LED will turn on when bit 0 of port A goes low.

Earlier we mentioned the possibility of checking the status bits in the STATUS register. This is also done with the BTFSS and BTFSC commands:

;Check CARRY bit

BTFSS STATUS,C ; if C is set, skip GOTO

GOTO WHERE_EVER ; The ZERO bit is checked in the same way:

;Check bit ZERO

BTFSS STATUS,Z ; if Z is set, skip GOTO

GOTO WHERE_EVER ; It's safe to say that you will use these examples very often. CALL k RETURN

These two commands are intended for working with subroutines. The CALL command is used to jump to the subroutine at the address specified in the command, and the RETURN command is used to return from the subroutine. Both commands are executed in 2 cycles. The address at which the CALL command was located is stored in specially organized registers called the stack. These registers are not accessable and are used only for subroutine calls and returns. Stack depth, i.e. the number of special registers is 8. Therefore, no more than 8 nested subroutine calls can be made from the main program. After the subroutine returns, execution continues with the next instruction after the CALL. The W register and the STATUS register are not saved when a subroutine is called, so if necessary, they can be stored in separate memory locations. Here is a simple example of using a subroutine: START

BSF DATAPORT,7 ; turn off LED

CALL TURNON ;call subroutine

GOTO START ;go to start TURNON

BCF DATAPORT,7 ;turn on LED

RETURN ; return from the subroutine As a result, the LED will flash at a frequency of about 150 kHz. RETLW to RETFIE

There are two more commands for returning from subroutines. The RETLW command returns in the working register W the constant specified in this command, and the RETFIE command enables interrupts. The RETLW command is often used to create tables of values. Let the working register W contain an offset from the beginning of the table. Then you can get the desired element by the following procedure:

MOVLW 02h ;set offset

CALL SHOWSYM ;call subroutine

MOVWF DATAPORT ;output table element to port B

GOTO $ ;loop forever SHOWSYM

ADDWF PC ;calculate table offset

RETLW 0AAh ;1st element of the table

RETLW 0BBh ;2nd element of the table

RETLW 0CCh ;3rd table entry LEDs should display 10111011. SPECIAL COMMANDS

It remains for us to mention two special commands - CLRWDT and SLEEP. The CLRWDT command is intended to reset the watchdog timer, the purpose of which we have already discussed. This instruction must be present in such sections of the program so that the execution time of the program between two adjacent CLRWDT instructions does not exceed the watchdog timer. The SLEEP command is intended to put the processor into low power mode. After executing this command, the processor clock generator is turned off and the processor can be switched back to the operating mode either by the reset input, or by the watchdog timer, or by interruption. CONCLUSION

This article does not claim to be a complete description of the capabilities of the PIC16C84 microcontroller. To do this, you should read its technical description. Also, to understand all the possibilities of the MPALC assembler, macros, options, etc. It will be useful for you to read his manual. To correctly set all the necessary options, you should read the instructions for using the programmer. Application examples of microcontrollers will give you a solid basis for independent projects. If you have any questions, you can contact the regional support center for MICROCHIP products at the following address: Moscow, Rubtsovskaya nab. 3 office 502, tel. (095)-263-9930 Here they will always be ready to answer all your questions. You can also get new versions of software, application examples, reference information at the regional BBS by calling (095)-162-8405

AD micro BBS

Publication: N. Bolshakov, rf.atnn.ru

See other articles Section Microcontrollers.

Read and write useful comments on this article.

<< Back

Latest news of science and technology, new electronics:

Machine for thinning flowers in gardens 02.05.2024

In modern agriculture, technological progress is developing aimed at increasing the efficiency of plant care processes. The innovative Florix flower thinning machine was presented in Italy, designed to optimize the harvesting stage. This tool is equipped with mobile arms, allowing it to be easily adapted to the needs of the garden. The operator can adjust the speed of the thin wires by controlling them from the tractor cab using a joystick. This approach significantly increases the efficiency of the flower thinning process, providing the possibility of individual adjustment to the specific conditions of the garden, as well as the variety and type of fruit grown in it. After testing the Florix machine for two years on various types of fruit, the results were very encouraging. Farmers such as Filiberto Montanari, who has used a Florix machine for several years, have reported a significant reduction in the time and labor required to thin flowers. ... >>

Advanced Infrared Microscope 02.05.2024

Microscopes play an important role in scientific research, allowing scientists to delve into structures and processes invisible to the eye. However, various microscopy methods have their limitations, and among them was the limitation of resolution when using the infrared range. But the latest achievements of Japanese researchers from the University of Tokyo open up new prospects for studying the microworld. Scientists from the University of Tokyo have unveiled a new microscope that will revolutionize the capabilities of infrared microscopy. This advanced instrument allows you to see the internal structures of living bacteria with amazing clarity on the nanometer scale. Typically, mid-infrared microscopes are limited by low resolution, but the latest development from Japanese researchers overcomes these limitations. According to scientists, the developed microscope allows creating images with a resolution of up to 120 nanometers, which is 30 times higher than the resolution of traditional microscopes. ... >>

Air trap for insects 01.05.2024

Agriculture is one of the key sectors of the economy, and pest control is an integral part of this process. A team of scientists from the Indian Council of Agricultural Research-Central Potato Research Institute (ICAR-CPRI), Shimla, has come up with an innovative solution to this problem - a wind-powered insect air trap. This device addresses the shortcomings of traditional pest control methods by providing real-time insect population data. The trap is powered entirely by wind energy, making it an environmentally friendly solution that requires no power. Its unique design allows monitoring of both harmful and beneficial insects, providing a complete overview of the population in any agricultural area. “By assessing target pests at the right time, we can take necessary measures to control both pests and diseases,” says Kapil ... >>

Random news from the Archive

New series remote control 14.03.2005

Pogo Products has introduced a battery-free remote control. The EZPower Universal has a built-in handle that only needs to be twisted a little to generate the energy the device needs for seven days.

Thus, the remote control is not only "friendly" to the environment and to your hands, but it is also capable of operating devices such as TV, VCR, Cable, SAT, AUX and DVD player. The remote control will first appear in the US for about $25.

Other interesting news:

▪ The Alps are growing up

▪ Breathing bioaccumulators

▪ Agricultural sequestration to overcome the climate crisis

▪ Tank robots instead of firefighters

▪ Nanotablets with micromotor

News feed of science and technology, new electronics

 

Interesting materials of the Free Technical Library:

▪ section of the site Interesting facts. Selection of articles

▪ article Servant of the people. Popular expression

▪ article What is Magna Carta? Detailed answer

▪ caper article. Legends, cultivation, methods of application

▪ article Ignition block for VAZ-2108 and VAZ-2109. Encyclopedia of radio electronics and electrical engineering

▪ article Domestic field transistors and their foreign analogues. Encyclopedia of radio electronics and electrical engineering

Leave your comment on this article:

Name:


Email (optional):


A comment:





All languages ​​of this page

Home page | Library | Articles | Website map | Site Reviews

www.diagram.com.ua

www.diagram.com.ua
2000-2024