The PIC microcontroller forms the brains of the robot. It controls and guides the robot based on the data from the detectors. The following is a brief preview of the PIC16F84.
The PIC16F84 is a low-cost, high-performance, CMOS, fully-static, 8-bit microcontroller. The PIC microcontroller employs an advanced RISC (Reduced Instruction Set Computing) architecture. It has an eight-level deep stack, and multiple internal and external interrupt sources. The separate instruction and data buses of the Harvard architecture allow a 14-bit wide instruction word with a separate 8-bit wide data bus. The two stage instruction pipeline allows all instructions to execute in a single cycle, except for program branches (which require two cycles). A total of 35 instructions (reduced instruction set) are available. Additionally, a large register set is used to achieve a very high performance level. The PIC16F84 has up to 68 bytes of RAM, 64 bytes of Data EEPROM memory, and 13 I/O pins.
PIC16F84 devices contain an 8-bit ALU and working register. The ALU is a general purpose arithmetic unit. It performs arithmetic and Boolean functions between data in the working register and any register file. The ALU is 8-bits wide and capable of addition, subtraction, shift and logical operations. Unless otherwise mentioned, arithmetic operations are two's complement in nature. In two-operand instructions, typically one operand is the working register (W register), and the other operand is a file register or an immediate constant. In single operand instructions, the operand is either the W register or a file register. The W register is an 8-bit working register used for ALU operations. It is not an addressable register .
ROM Instruction Memory:
In the 16F84 instructions are 14 bits wide and stored in EEPROM. There is a maximum of 1024 of these. It is impossible to modify these instructions except through external programming. You can't have self modifying code. When the chip is reset, a program counter is set to zero and instructions are executed from there. The program is retained when power is removed from the chip.
RAM Memory :
Besides the 14 bit program bus there is another 8 bit data bus in the PIC connected to registers, ports, timer etc. There are 80 RAM locations in the 16F84. RAM is where the variables are put. The only way to change these RAM locations is through instructions. The information in RAM disappears when power is removed. The first 12 RAM locations, (00h - 0Bh), have internal registers mapped to them. Changing these locations with instructions changes the corresponding registers. Microchip calls RAM locations 'files' or 'registers' and uses the symbol 'f' when referring to them. The remaining 68 locations can be used for your variables. Microchip calls the first 12 locations special function registers and the remaining 68 general purpose registers.
Banked RAM Memory :
The memory is split into two banks-bank 0 and bank 1. Bank 1 is used to control the actual operation of the PIC. Bank 0 is used to manipulate the data. A 'banking' bit has to be set in the byte at RAM location 3 to reach the se two banks . This location is called STATUS and the bit, (bit 5), is called RP0. If RP0 is zero, bank 0 is in access, if it is 1 bank 1 is available. For user defined variables it doesn't matter which bank is in use because they are mapped to both banks. For some of the first 12 locations it does matter. Seven of the 12 are mapped to both banks but five are not; so location 5 for example has two meanings depending on RP0. If RP0 is clear, (bank 0), location 5 refers to the data on PORT A. IF RP0 is set, (bank 1), location 5 refers to the direction register TRISA that tells which bits of PORTA are inputs and which are outputs.
The TRIS instruction can be used to set the port direction registers and OPTION can be used to set the OPTION register which deals mainly with timer operations. If code is ported to future Microchip processors that don't support these instructions, you will probably want to rewrite the code for some other reason anyway.
EEPROM Memory:
There is a third type of memory in the 16F84, 64 bytes of electrically reprogrammable memory, (8 bit). This is used to hold values would have to be stored when the power is turned off. There are a couple of difficulties. First, the memory is not directly addressable; it has to be worked indirectly through four of the special function registers. Secondly, it takes a few hundredths of a second to 'burn' the information in so it isn't fast memory like RAM. This memory can also be burned in when the program memory is burned in.
Instructions:
Each PIC16F84 instruction is a 14-bit word divided into an OPCODE which specifies the instruction type and one or more operands which further specify the operation of the instruction. The PIC16F84 instruction set can be summarized as byte-oriented, bit-oriented and literal and control operations. For byte-oriented instructions, ‘ f ' represents a file register designator and ‘ d ' represents a destination designator. The file register designator specifies which file register is to be used by the instruction. The destination designator specifies where the result of the operation is to be placed. If ‘ d ' is zero, the result is placed in the W register. If ‘ d ' is one, the result is placed in the file register specified in the instruction. For bit-oriented instructions, ‘b' represents a bit field designator which selects the number of the bit affected by the operation, while ‘ f ' represents the number of the file in which the bit is located. For literal and control operations, ‘ k ' represents an eight or eleven bit constant or literal value. The instruction set has been included in datasheet section.
Input/Output Ports:
The 16F84 has 13 pins that can be individually configured as either inputs or outputs. They are divided into PORTA, (5 bits), and PORTB, (8 bits). The direction of each bit is determined by the bits in the corresponding direction registers TRISA (Tristate A) and TRISB (Tristate B). A zero means the bit will be an output, a 1 means input.
Certain port pins are also hooked to other functions of the processor. The high 4 bits of PORTB can be used as interrupt pins when they are programmed as inputs. The high bit of PORTA is also used as an external clock input for the counter/timer. Bit 0 of PORTB (RB0/INT) can be used for an external interrupt.
Timing with delay loops
Each instruction takes four clock cycles or 1 microsecond for a 4 MHz crystal unless the program counter has to be changed, (flow control instruction). 2 microseconds are required for program branches. The control signal given to the servo motor is pulse modulated using delay loops. The delay loops are implemented by the following technique: a value is stored inside a register and decremented inside a loop until it becomes zero after which the output is toggled. By changing the contents of the register above said the pulse width of the output signal is varied.