10minf84.asm - a PIC based 10 minute ID timer / beacon 2/8/2000 Chuck Olson, WB9KZY Jackson Harbor Press http://jacksonharbor.home.att.net jacksonharbor@att.net Files in this zip: 10MINF84.ASM source code, assemble with MPASM 2.2 10MINF84.GIF schematic P16F84.INC include file used by source code 10MINREA.DME this file Discussion: 10minf84.asm is my attempt at a 10 minute timer for ham radio. I thought this would be a really quick and easy project for a PIC but then as I got into the coding, it started to grow and get ugly. Anyhow, here it is: The idea is to use a PIC to provide a 10 minute timer to assist an operator to comply with the identification regulations for ham radio. Instead of using a visual display, Morse code is used to "display" timer and message settings. This timer is fairly accurate and can be set for any duration from 0 seconds to 99 minutes 59 seconds. It can also be set to retrigger the delay automatically after the initial delay. It also will send a message in Morse code which could be used to send an ID sequence to a transmitter like: DE WB9KZY K. The timer will output a once a second "wink" to an LED and/or a once a second "click" to the piezo speaker. If the delay time is set to zero, this design could also be used as an id'er which is triggered by the switch input. If the retrigger input is connected to +5V and the delay is set to a few seconds, this design could also be used as a Morse code beacon. Finally, if one of the outputs (pin 13) is connected a 2n2222 transistor through a 4.7k resistor (similar to the keying transistor), that transistor can be used as an interval timer to control loads such as an ultraviolet EPROM eraser. Here, (from the source code file, 10minf84.asm) is how the timer works: This program uses a 16f84 to wait for 10 minutes after the switch input is pressed - it will wink an LED once a second - then when 10 minutes have elapsed, the PIC will send a Morse code message through both the piezo speaker and an output transistor. The Morse message can be up to 60 characters long. When the timer is not in use the PIC is put to sleep to save power and reduce interference. There is also an LED output - the LED is "winked" once per second while the delay proceeds to let the user know that the PIC is active. There is another output pin used to control a relay - this might be useful for non-ham apps of the timer such as an EPROM eraser timer? The timer can be automatically retriggered after the initial delay by connecting the retrig pin to +5V. The timer delay OR the message send can be interrupted by a press and release of the switch input. The timer can output a once a second click to the piezo if the click input is connected to +5V The message, the delay time and the code speed are stored in eeprom memory and can be changed using a PIC programmer or using a nasty little recording routine stored in the PIC. This routine uses the click and retrig inputs to set the various parameters. The eeprom change routine (MENU) is entered with a press and hold of the switch input for 2 seconds - after this initial hold, the PIC will send: MIN? x (where x is the current minute delay) The user can then use the retrig input to decrease the delay by connecting it to +5V OR use the click input to increase the delay by connecting it to +5V. The PIC will send the changed minute delay and then check the inputs again. To exit the routine and save the changed minute value, press and release the switch input. The PIC will then send: SEC? y (where y is the current second delay) Again, the retrig switch decreases the delay when connected to +5v, the click switch increases the delay when connected to +5V. Pressing and releasing the switch input will save the changed value and then the PIC will send: DL? z (where z is the current dit length in ms) Connecting the retrig switch to +5V will decrease the dit length, connecting the click switch to +5V will increase the dit length. The maximum dit length is 80 ms (15 WPM). The minimum dit length is 30 ms (40 WPM). A press and release of the switch input will save the dit length in eeprom and then the PIC will send: MSG? If the switch input is pressed and released, the message record routine will be skipped. If either the retrig or click switches are set to +5V, the first character in the message will be played. The user can then changed this character (retrig to +5v goes up the character table, click to +5V goes down the character table). Proceed to the next character with a press and release of the switch input. Exit the message record routine with a 2 second press of the switch input. The PIC will then send: DONE Note that there are two special characters used in the recording process: a word space is denoted with a run together SP (....--.). Then end of the message is denoted with a run together END (.-.-..). The size of the message is limited to 60 characters - the recording routine should automatically bail out when the 60th character is entered. The PIC 16F84 has 64 bytes of eeprom - 1 is used for the minutes, another for the seconds, a third for the dit length and the last for the end of message byte. Note that all of the routines will "wraparound" either from high to low OR low to high when the user reaches a limit (ex, increase the delay from 99 minutes and the delay will wrap around to 0). The easier way to change any of these parameters is to simply edit the EEPROM data at the end of this file and then use the programmer to burn it into the 16F84 EEPROM. The message can be entered in ASCII (upper case letters only). Some of the special characters should be entered in numerical format. An easy way to reset the timer is to power off, then press and hold the switch input while powering on the timer. This will reset the timer to a 10 minute, 0 second delay at 15 WPM with a single period (.-.-.-) as the only message. Hardware: The parts used in this project are fairly common, the PIC 16F84 along with a 4 MHz crystal, an LED, some resistors, an NPN keying transistor and 3 switches. Nothing in the layout is critical - it should work well on perf board. There is a special LDO 5V regulator, the LM2936, specified but the user can either replace it with a 78L05 OR use batteries connected directly such as two or three AA cells or one or two lithium 3V cells. The LM2936 was used because I like to use old 9V batteries to power PIC projects and the LM2936 works well with them - it uses little current itself (7 ua or so) and also has low dropout (the battery needs only to be a few tenths of a volt above 5 volts to allow the regulator to perform it's function). Why are the retrig and click inputs made as direct connects to ground or +5V? If a pullup resistor was used (such as those on PORTB), the PIC would continue to draw current through the pullup even during sleep mode - since there are no pullups on the retrig and click inputs, no current is used in sleep mode even if the pins are tied low to ground. Two 22 pf capacitors are specified for use with the crystal - their exact value really will depend on the crystal used. Depending on the circuit stray capacitance, these caps can even be eliminated. One alternate to the use of a crystal is a 4 MHz, 3 pin ceramic resonator. These are small, rugged units with decent accuracy (0.5%) and as a bonus, the capacitors are built in. The series resistor of the LED was set at 750 ohms arbitrarily. The user may want to decrease the resistor value to increase the brightness of the LED. Avoid going too low, the PIC outputs are robust but shouldn't be allowed to sink more than say 10 ma - if a brighter wink is desired, the wink delay might be extended in software or one of the high brighness LED could be used. Software: This program is my old favorite, the timed loop. The PIC enters an initialization routine at powerup, setting up the I/O and also the code speed. At the end of the init, the PIC will send an FB through the piezo to assure the user that the powerup has been sucessful. The PIC then enters the main loop of the program - if there are no inputs, the PIC enters sleep mode, which turns off the clock and thus allows the PIC to use very little current. At the press of the switch input, the PIC will awake and then enter either the timing countdown OR if the switch input is held for 2 seconds, the PIC will enter the menu system for setting the timing parameters and message. The timing countdown is based on a one second main timing loop. At a PIC clock crystal frequency of 4 MHz, this one second loop must execute at a consistent 1 million PIC instruction cycles. Part of the loop checks the switch input, other parts downcount the second and minute bytes. Each of these check routines is setup so that alternate paths through the loop results in the same number of PIC instruction cycles executed. Note that the initial call to the countdown can be up to 20 ms or more long, due to the debounce of the switch input and due to some setup instructions. But subsequent executions of the loop should be consistently 1 second long. The accuracy is sufficient for this application. At the start of the loop, the LED and conditionally (depending on the state of the click input pin), the piezo are pulsed briefly to give the user the assurance that the timer is counting down the delay. After the timing countdown is complete, the playcal routine will send a Morse code message of up to 60 characters to both the piezo speaker and to a keying transistor - this keying transistor can be used to key a transmitter, possibly even as a Morse code beacon. The keying speed is set by the ditlen byte. This is just the integer number of 1 ms intervals in the length of a dit. The dit interval can be computed from the desired wpm value with this formula: ditlen = 1200 / wpm The 1 ms interval is also used to set the frequency (1 KHz) of the sidetone signal sent to the piezo speaker. After the playcal routine is complete, the PIC will loop back to the main loop and unless the retrigger input is set high, the timer will go to sleep. The menu routines are used to set the minute, second, ditlen and the message bytes. These routines could stand improvement or at the least, customization to the users preference. They all basically use the switch input to advance and save, the click input is used to increase the value of a byte, the retrig input is used to decrease the value of a byte. When the minimum or maximum value of a byte is exceeded, the routine will "wraparound" to the maximum or minimum as appropriate. The menu routines are really just included for field changes of the parameters or for those whose PIC programmer will not program the eeprom of the 16F84. Normally, in the 10 minute application, once the paramters are set, there shouldn't be much of a need to change them.