Build A PIC Beacon Clock Pt1
Build A PIC Beacon Clock Pt1
Project
Flash Memory
Flash memory is electrically erasable and programmable nonvolatile memory. Storing the microcontrollers program in ash memory means that the same PIC can be reprogrammed many times over. In addition, ash PICs have a simple, two wire serial programming interface, making them easy to physically program. The devices themselves are relatively inexpensive and Microchip provides a free development environment,which runs on the Windows operating system. Theres a bewildering array of PICs to choose from, mainly because commercial users of PICs always want maximum performance at minimum cost. Having a large number of different PICs available helps them achieve that objective. Electronics enthusiasts on the other hand, dont have commercial pressures and neither do they have the buying power of big companies. So, constructional enthusiasts like us have settled on a relatively small number of PICs, which form a logical progression in terms of size and performance and are readily available. Thats not to say theres no progress! Many enthusiasts are moving on to the higher performance 8-bit PICs and even on to the 16-bit ranges. The original Flash PIC was the 18-pin PIC16F84. Now
24
clocked at their maximum speed, which in the case of the 16F871 is 20MHz. That equates to ve million instructions per second, or one instruction every 200nS. As can be see from Fig. 1, the Flash program memory is accessed independently of the internal 8-bit data bus. However, theres a back way into the program memory, which allows it to be read in a similar way to EEPROM data memory. Its also possible for the 16F871 (and some other PICs) to reprogram the Flash memory while actually running a program. By the way dont get too concerned about the small amount of program memory, the code for the IBP l.e.d. clock, the l.c.d. clock and the l.e.d. repeater, all ts quite easily in 2K words!* There are 128 bytes of RAM data memory. This is static memory, which retains its contents as long as theres power supplied to the PIC. Incidentally, one particularly nice thing about the PIC is the inclusion of EEPROM in many devices. The EEPROM is nonvolatile data memory and it allows the PIC to remember things even in the absence of power, thereby allowing customised settings and the like to be retained through power down. The 16F871 has three timer/counters. Timer 0 and the more versatile Timer 2 are both 8-bit timers, while Timer 1 is a 16-bit timer. Allied to the timers is the CCP Capture/Compare/PWM module, used for timing events and for generating a pulsewidth modulated output. This is very useful for engine management systems and the like. Most PICs also incorporate a Watchdog Timer. This timer, which has its own oscillator, runs independently of the rest of the PIC. If the Watchdog Timer ever times-out whilst enabled, it instructs the Reset circuitry to reset the PIC. To prevent a timeout, the PIC must periodically execute a special Clear Watchdog Timer instruction. The idea being that this instruction is placed where it should get repeatedly executed, such as in the main program loop. If the PIC ever gets stuck somewhere else in its program, the Watchdog will timeout, reset the PIC and restart the program! *Note: Readers who are not familiar with computer speak may be confused with the upper case K. Many of us understand that it refers to Kelvin, a term related to temperature used in science. The term is also used to represent the nearest computer equivalent to 1,000, which is 1024 (or 210). Editor
outputs (I/O). Rather than dedicate pins on the i.c. package to each peripheral (which would be wasteful if a peripheral wasnt used), all input/output pins on a PIC can be used as general purpose I/O. Theyre arranged in groups of up to eight I/O lines, and referred to as Ports A, B, C, D and E. However, pins that are also allocated to a peripheral cannot be used as general purpose I/O if that peripheral is in use. This must be borne in mind at the hardware design stage and is the reason why the beacon l.e.d.s in Figure 3 skip certain port pins. The diagram, Fig 2, shows the pin-outs of the 16F871 and Ive included the peripheral function of those pins, which are of interest in the IBP Clock project. For example, pin 6 RA4/T0CKI is I/O line 4 on Port A or alternatively it can be the external input to Timer 0.
Smaller Sibling
As I mentioned earlier, certain PICs have become very popular with electronics enthusiasts, the 16F871 being one of them. It has a smaller sibling in the 16F870, which is the same as the 16F871, except that it has no Parallel Slave Port. Its available in a 28-pin Skinny DIP (0.3 inch pin spacing) package which occupies signicantly less space than the usual 0.6 inch spaced package. Also in a 28-pin package is the 16F872, which is effectively a 16F870. However, this has an enhanced synchronous communications module instead of a USART. If more memory is needed, then the 16F874 has twice the memory of the 16F871 and the 16F877 four times the memory. The 16F873 and 16F876 are the corresponding larger memory versions of the 16F870. The nice thing about these PICs is that theyre pin compatible providing they have the same number of pins, of course! In fact, many PICs are pin compatible, which allows designers to change processors without necessarily having to redesign p.c.b.s.
Down In Size
Going down in size, the 16F628 is an 18-pin device with many of the peripherals of the 16F870 but with fewer I/O pins. The 16F627 is found in some PIC kits and thats just a 16F628 but has 1,024 words of program memory instead of 2,048 words. Owing to their low cost and pin compatibility with the 16F84, the 16F627 and 16F628 have become the new entry level PIC processors. Even though the 16F628 is quite small for a microcontroller, Microchip make even smaller - and cheaper devices. Four of these are of interest to us. First, on the choice list the 16F676, a 14-pin device with two timers, an analogue comparator and an 8-channel A/D converter. It has 1,024 words of program memory, 64 bytes of RAM and 128 bytes of EEPROM. For applications that dont need to handle analogue inputs, the 16F630 is the same as the 16F676 but without the A/D converter. Finally on this topic, the smallest pair of PICs Im going to mention are the 12F675 and its A/D-less sibling, the 12F629. These devices have just eight pins and yet contain the same peripherals as their 14-pin counterparts Ive already described. All necessary support circuitry is inside these little PICs - even a 1% accurate 4MHz oscillator so all but two of their pins (remember PICs still need power) can be used as inputs or outputs. Given their low cost, ease of programming and high integration, PICs open up the possibility of enthusiasts using microcontrollers in situations where previously they would not have been practicable. And I take the view that using PICs in Amateur Radio is little different to using a 741 op amp in place of a bunch of transistors! Were just trading hardware for software, thats all.
Useful USART
Another very useful peripheral is the universal synchronous/ asynchronous receiver/transmitter or USART. This supports synchronous serial communications (like those used between the PCs keyboard and motherboard), and the more familiar asynchronous serial communications used by modems and v.d.u.s. Even if an application doesnt use serial communications, the availability of a serial interface is very useful during program development and debugging. As you may appreciate, its awkward to see whats happening to the program ow within a PIC (although there are ways and means), so sending a byte out of the serial port at strategic places in your program whilst monitoring them on a v.d.u., can help keep track of whats happening. Given the low cost of PICs, its tempting to use them as intelligent peripherals in more powerful systems. To this end, some PICs have a Parallel Slave Port or PSP. This module allows parallel communications between the PIC and another processor to be controlled by the other processor without the need for additional logic. Finally, many PICs incorporate analogue to digital converters and in the case of the 16F871, the A/D is a 10-bit, 8-channel converter. Most of the peripherals Ive mentioned need inputs and/or
Now were ready to tackle the main project! - turn the page!
Practical Wireless, May 2007 25
Project
The buffered test points are essential because connecting anything to the oscillator circuit itself will cause a frequency shift due to the capacitance of the probe. Alternatively, simply set C9 to maximum and run the clock over a period of a few hours. See how it keeps time and adjust C9 accordingly. The values of C8 and C9 are suitable for a crystal requiring a load capacitance of 12pF (a common value for 32KHz (see PIC intro note) watch crystals, as used in the prototype). If you use a crystal that requires a different load capacitance, then alter the values appropriately. Both C8 and C9 should be around twice the specied load capacitance of the crystal, when adjusted. If C9 cant quite pull a 12pF crystal exactly on to frequency, then a 10pF ceramic capacitor connected in parallel with the trimmer should solve the problem. Many PIC-based designs that maintain real time use either the PICs own crystal (X1 in our case), or a 32KHz watch crystal connected to Timer 1s internal oscillator. While both methods are valid, I prefer a separate oscillator.
RISC Microprocessor
WT3219
WT3232
Port B
Port C
PSP
MCLR/Vpp RAO/ANO RA1/AN1 RA2/AN2 RA3/AN3 RA4/TOCK1 RA5/AN4 RE0/AN5 RE1/AN6 RE2/AN7 VDD VSS OSC1/CLKIN OSC2/CLKOUT RC0 RC1 RC2 RC3 RD0 RD1
40
20 WT3220
21
RB7/PGD RB6/PGC RB5 RB4 RB3 RB2 RB1 ABO/INT VDD VSS RD7 RD6 RD5 RD4 RC7/RX RC6/TX RC5 RC4 RD3 RD2
Timer 0
Port E
Timer 1
Fig. 2: The pin-out for the PIC used for this project.
Watchdog timer
Clock generator
the clock running fractionally (half a second or so) ahead of the correct time. It works well in practice and means the l.e.d. is illuminated just before the allotted time slot for the beacon the band you are monitoring. This technique helps the user recognise the high speed (22w.p.m.) Morse and alerts you what to expect (as the beacon l.e.d. is labelled with its callsign). G3XFD
VDD
Power CCP
VSS
keep them in circuit. Given that the IBP Clock may be used in places where theres a signicant amount of r.f. energy oating around, its not impossible for this energy to disturb a PIC. In particular, most modern v.h.f./u.h.f. handheld transceivers can run up to 5W output and that kind of power at close range can interfere with many electronic devices, particularly if theyre not adequately shielded as we know to our cost with TV and radio receivers! Resistors R19 to R23 are, of course, necessary, as switch S1 selects the required band by connecting the appropriate band input (normally pulled high) to 0V. The sixth position is used for turning all the beacon l.e.d.s off. Incidentally, if a two-pole switch is used here, the other half of the switch can be used with more l.e.d.s to provide illuminated band indications. Switch S2 is the reset switch, which sets the clock to the beginning of a three minute cycle. The software effectively de-bounces S2 by providing a one second hold off period during which time further resets are inhibited. Try to press SW2 as close as possible to the start of a three minute cycle or maybe a fraction of a second early, which some people prefer to do*. Editorial comment: Phils advice is very sound and I have had a prototype of the original wired logic beacon clock at home for some years and follow his tip by having
Practical Wireless, May 2007
PIC16F871
+5V
R29 10k R26 R25 R18 R17 R24 R16 R15 10k 10k
S2 Reset
C3 22p
X1 4MHz
C2 C4 22p 01
R1 R2 R3 R4 R5 R6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
10k
PIC16F871
10k
Fig. 3: The PIC clock and its external clock tick generator.
+5V R32 2k2 C8 22p (see text) X2 C9 22p (see text) 32,768Hz (32KHZ) R34 220k
C5 47 16V
Fig. 3: The PIC clock and its external clock tick generator.
8 to 30V
R33 10M
C7 047 16 15 14 13 12 11 10 9 IC3 32,768Hz test CD4060/HEF4060 point 1 2 3 4 5 6 7 8 R35 WM3213b 2Hz test point 2k2 C B
Pin Functions
The function of some of the PICs individual pins isnt obvious, so Ill run through them now. Pin 1 on the PIC is the microcontrollers external reset input (and high voltage programming input), which is normally held at +5V. We have to momentarily short pin 1 to 0V to reset the PICs c.p.u. and peripherals.
28
Pin 26 is the USARTs receive input, and because its not used in this design, its held inactive (high) by R28. Pins 39 and 40 are used when physically programming the PIC. So, to allow in circuit programming (more about that later), its useful to congure these pins as inputs if at all possible. At switch on, the IBP Clocks software checks the
Practical Wireless, May 2007
+5V
1N4148
10k
47k
47k
unconnected. During initialisation, the IBP Clock software congures them as outputs and sets them low. Anyone modifying the software can use these pins for their own purposes.
Fig. 4: The PIC can be programmed in-situ with these circuit modications.
voltages on pins 39 and 40 and these voltages are then used to determine the function of the PIC. The following table should make things clear. Pin 40 +5V +5V 0V 0V Pin 39 PIC Function +5V l.e.d. Clock 0V l.c.d. Clock +5V l.e.d. Repeater 0V(Undened - defaults to l.e.d. Repeater)
Now to pin 36. This pin is pulled low by R24, and serves two purposes; one Microchips and one mine. First, pin 36 can be used as a low voltage programming input. This function is active on brand new devices as supplied by the factory. However, most of the PIC kits on the market use the PICs high voltage programming mode, which involves applying +12V to the reset input pin 1. To prevent the low voltage programming mode being accidentally activated, its essential that pin 36 be held at 0V the very rst time a new PIC is programmed. Unless specically required, its advisable to disable the PICs low voltage programming option. (Please see the 16F871s data sheet for more information). In this design, pin 36 is used as a l.e.d. active high/low select pin. The circuit of Fig. 3 shows the l.e.d.s connected between the PIC pins and +5V. The l.e.d. drive is thus said to be active low, in that the PIC pin switches from +5V to 0V to illuminate the required l.e.d. However, if resistor R24 is taken to +5V instead of 0V (remember the warning above though), then the l.e.d. drive is reversed and the PIC pins will switch from 0V to +5V to illuminate the required l.e.d. Ive included this option, not to drive l.e.d.s but to directly drive ULN2804 and similar, open collector buffers. These devices (as used on the original hardware IBP Clock) can switch up to 500mA to ground from a 50V supply. So, if 15mA at 5V isnt enough, then ULN2804s should provide more than sufcient current. Of course, active high outputs can also drive discrete transistors and thats another option. Just remember to use appropriate base resistors! Finally, the three pins which belong to Port E (Port E only has three I/O pins) are unused and are left
Practical Wireless, May 2007
PIC16F871
Aprils issue of PW featured a review of the Velleman K8048 PIC WM3212a Programmer and Experimentation Board by G4JCP This kit is eminently . suitable for programming the 16F871. If using this kit, you have two options. Either solder a 40pin d.i.l. socket (preferably turned pin) onto a piece of stripboard, and wire it as described in the kits documentation. Or you can program the PIC while its actually in circuit, in the IBP Clock. To do the latter, youll need to make a few simple modications, as shown in Fig. 4. First, resistors R25 and 26 need to be increased to 47k. Next, wire a 1N4148 diode in series with R27. Four connections are then required between the IBP Clock and the K8048 board. These connections dont have to be permanent, and in fact, the K8048 p.c.b. is laid out for a 5-pin connector that carries the required programming signals. Only four pins are used, the fth is a +5V supply, which isnt needed as the IBP Clock has its own +5V supply. The actual programming procedure is described in a help le supplied with the K8048 kit. If you want to program your own PIC chip and are unfamiliar with the procedure, then I recommend you gain experience by rst programming the PIC supplied with the kit. There are two example programs and if both work as they should, then you can try the 16F871. (Please note my comments concerning computers made in the K8048 review, April PW).
Components
the l.e.d.s are mounted elsewhere, and pins are xed in the stripboard at places corresponding to the cathodes of the l.e.d.s on the circuit diagram. Also notice that the l.e.d.s run in sequence, anticlockwise from pin 15 on the PIC. When building the clock, please make sure you do double check everything! Its a relatively simple circuit, but its also very easy to make a mistake on stripboard. When testing, make sure you have a stable +5V rail before inserting the i.c.s. Also, ensure those pins that are connected to either 0V or +5V (via resistor or not), really are connected to the proper voltage rail. Assuming the PIC has been programmed correctly, the clock should work rst time. Ruling out wiring errors and faulty components, the only thing that can stop the clock working is the failure of one or both of the crystal oscillators. The 32KHz oscillator can be checked by wiring a l.e.d. (plus a series resistor) between IC1 pin 3 to 0V. It should ash at 2Hz. To check the 4MHz PIC oscillator, either use a logic probe or an oscilloscope. Or simply use a general coverage receiver tuned to 4MHz. You may have to put the clock quite close to the receivers antenna to get a good signal. As with all software projects, there may be bugs in the code. Always use the latest version, which will be made available at the web address mentioned above. There may also be more features added to the code over time, so its worth checking the website even if you buy a ready programmed PIC. One last point, please dont expect me to modify the code just because you want a particular feature included. The source code is on the Net, so have a go yourself Its great fun! Resistors (All resistors 0.25W) R1-18 300 minimum (see text) R19-29 10k R30,31 1k R32,35 2.2k R33 10M R34 220k All resistors 0.25W Capacitors C1,2 100nF 50V ceramic C3,4,8 22pF 50V ceramic C5 47F 16V electrolytic C6,7 470nF 50V ceramic C9 22pF lm dielectric trimmer X1 4MHz HC-49/U (or low prole) crystal X2 32KHz miniature watch crystal D1 1N4002 LED1-18 l.e.d.s to suit (see text) IC1 PIC16F871-I/P [*] IC2 7805 IC3 CD4060 or HEF4060 S1 Single-pole, 6-way rotary switch S2 Single-pole, non-latching push-to-make switch Stripboard, pins, wire, case to suit. [*] Programmed PIC16F871-I/P microcontrollers for the PIC IBP Clock are available from: KRC, Unit 11, Marlbrough Court, Westerham, Kent. TN16 1EU. Tel: (01959) 563023, E-mail: [email protected]. Price 7.99 all inclusive to UK addresses. (Overseas phone or E-mail for quote.)
In part 2, a version with a liquid crystal display will be featured, together with an associated l.e.d. repeater. (The latter allowing a l.e.d. display to be added to the l.c.d. unit.).
Phil Cadman G4JCP writes: The PW team and I are planning to present Part 2 of this project in the July issue of the magazine. In the meantime, as this is our rst PIC-based project, everyone involved in preparing it for you will be most interested to hear your comments and what you think of the idea. Please contact the PW ofces or myself via [email protected] or at 21 Scotts Green Close, Scotts Green, Dudley, West Midlands DY1 2DX.
30