Figure 15.9 A pull-up circuit
PCL (program counter low)
PC is the program counter that keeps track of the instruction being executed at the time. It is a 13-bit register divided into PCL for low byte and PCH for high byte. Its behaviour is common with other microcontrollers and microprocessors as described in Chapter 8.
Stack
This register stores the return address when interrupts occur. It stores up to eight 13-bit addresses. Again, there are more details in Chapter 8.
Register 6–1: PIC16F84A configuration word
This is a special register which is just memory location 2007H which can only be accessed during programming. Unprogrammed pins are read as ‘1’.
It is a 13-bit word with the following options:
Bit 13–4. This protects the program code from being read after the programming is complete. 1 = no protection 0 = all program memory is code protected.
Bit 3 Power-up timer enabling. This allows a nominal 72 ms delay when first switching the microcontroller on to allow power supplies to settle. The delay can be activated by loading bit 3 with ‘0’, no delay = ‘1’.
Bit 2 watchdog timer, disable with ‘0’, enable with ‘1’.
Bits 1–0 oscillator selection bits
00 = LP oscillator
01 = HS oscillator
10 = XT oscillator
11 = RC oscillator
Oscillators
The crystal or ceramic resonator options are shown in Figure 15.10 and the crystal oscillators are divided into the frequency ranges:
LP low power crystals 32 kHz–200 kHz.
XT crystal/ceramic resonator 100 kHz–4 MHz.
HS high speed crystal/ceramic resonator 4 MHz–20 MHz.
RC resistor/capacitor.
Figure 15.10 Oscillator options
A note on ceramic resonators
These offer an alternative to crystals for oscillators. They are generally lighter, more shock resistant and are usually somewhat smaller and cheaper to buy. The downside is that they do not have quite the frequency accuracy or stability and in many respects they are positioned somewhere between an RC network and the quartz crystal.
An interrupt summary
The PIC16F84A has four sources of interrupts:
(i) External interrupt on the RB0/INT pin.
(ii) TMR0 overflow.
(iii) PortB change detector pins RB7–RB4
(iv) EEPROM data write complete interrupt. This is used only when we are loading new programs into the EEPROM.
In each case, choose the best option.
1 An 8-bit microcontroller has:
(a) an 8-bit data bus.
(b) eight ports.
(c) an 8-bit address bus.
(d) eight internal registers.
2 Compared with the Pentium 4, a microcontroller:
(a) is faster.
(b) consumes less current.
(c) is more complex.
(d) is larger.
3 watchdog circuit prevents:
(a) damage due to excessively high supply voltages.
(b) an overrun in timer/counter circuits.
(c) burglars.
(d) wastage of power while the microcontroller is not being used.
4 It is NOT essential for a microcontroller circuit to include:
(a) registers.
(b) a reset pin.
(c) some memory.
(d) a crystal.
5 The term ‘ISR’ refers to an:
(a) immediate service register.
(b) internal system reset.
(c) interrupt service routine.
(d) instruction set register.
16. Using a PIC microcontroller for a real project
PIC microcontrollers are a very convenient choice to get started in this field although, as we have seen, there are enormous areas of overlap between microprocessors and other microcontroller designs.
One convenience is that Microchip Technology has taken the RISC concept seriously. There are only 35 instructions and, of these, only a few are required to write quite usable programs.
All data movements are based around just a single register called ‘W’ for ‘working’. This performs much the same function as the accumulator in the earlier processors like the 6502.
To program the PIC we need a copy of software instructions and more details of the register layout.
There is no getting away from the fact that when we first meet a microcontroller, the information appears overwhelming – not only in the quantity but in the apparent complexity. This is despite the efforts of the designers to make it as simple as possible. It is very much like seeing a new foreign language, which it is really – and tackle it in much the same way, a bit at a time.
There are four golden rules which may help:
1 Don’t get scared! From time to time, it will seem tempting just to throw it all away and find something easier to do.
2 Start really simple – one step at a time.
3 Be prepared for it to take a long time in the early stages.
4 When you are feeling low and despondent, remember that everyone else has felt the same. It will get better. Honest.
Let’s keep it really simple. We have to buy a microcontroller and connect it up and then program it.
The device that we are going to use is a PIC16F84A-04/P. The pinout is shown in Figure 16.1. The number PIC16F84A is the type of microcontroller. The 04 tells us that the maximum frequency is 4 MHz and the /P means that it is the standard plastic package. There is no minimum operating frequency and the slower it runs, the less power it consumes.
Figure 16.1
Figure 16.2 shows the most basic circuit. It includes only the chip supplies and a positive supply to prevent the MCLR (master clear) from for a real project accidentally resetting the PIC during the program. Unless they are tied to a definite value all disconnected inputs will float up and down and can cause random switching.
Figure 16.2
We need a clock signal and the simplest and cheapest is just an RC combination. The resistor should be between 5 kΩ and 100 kΩ and the capacitor should be greater than 20 pF. The values chosen for this circuit were not selected carefully and are not critical. In practice, it is difficult to predict the operating frequency of an RC combination. If we need a particular frequency it is better to use a variable resistor and adjust it to give the desired frequency.
Later on, when we have written the program we must tell the assembler what clock signal we are going to use. This programs the PIC to expect an RC oscillator. This information can, as an alternative, be included into the program. It’s our choice but it is slightly easier to do it at the assembly stage but we must remember to do it.
There are three layers of information that we need to use the registers. Firstly, we need the names and addresses of the files as we met in the register file map in Figure 15.8. The second step is the function of each bit in each register, and this is shown in Appendix A, and finally, an explanation of these functions as we see in Appendix B.