Upload
ngodiep
View
216
Download
0
Embed Size (px)
Citation preview
Projects
62 | January & February 2013 | www.elektor-magazine.com
This project all began out of frustration with my alarm-clock/radio, which despite being a big-name brand, and not cheap, is very awkward to use and is lacking most of the features I would prefer to have. Whenever I raise the issue among friends and work colleagues, it seems that all alarm-clocks (and clock-radios) fall short of user expectations in some way or other. Here’s a summary of com-plaints I’ve often heard from people about their alarm clocks...
Forgetting to switch on the alarm before going to bed (because it had to be switched off in the morning to silence it, of course).Clock-radio not tuned into a station properly, or the volume was turned way down low, so the alarm could not be heard when it activated.Tedious ritual to change the clock time or alarm time, typically using one button to set ‘hours’ and another for ‘minutes’. Holding down a button (in some cases two) causes the minutes or hours displayed to change
automatically, but in most cases either too slow or too fast.Alarm time is the same every day, whether it's a weekday, Saturday or Sunday. (Most people need to wake up earlier on weekdays than on weekends and some people work part-time or shifts varying each day.)The snooze time interval is fixed (typically 7 to 9 minutes) and can't be adjusted.It's a pain having to press the snooze but-ton to silence the alarm. It would be great to have the option for the alarm to sound for a few seconds, then go silent for a few min-utes, and repeat the process until cancelled somehow.The display is too bright at night or too dim in daylight.There's a switch to adjust the display bright-ness, but it's awkward to use. Why can't the clock adjust its brightness automatically to suit the ambient light level?
Part 1
By Michael J. Bauer (Australia)
The 7-uP Alarm Clock/ Time-Switch
Although the main objective of the 7-uP project was to create a digital alarm-clock with enhanced functionality, a key feature is the ability to automate the operation of appliances in the owner’s study, office or bedroom, controlling devices such as lamps, computer & peripherals, and audio-visual equipment. Some enthusiasts might also think of some ingenious ways to apply the device to wake themselves up on work-day mornings!
Alarm Clock/Time-Switch
www.elektor-magazine.com | January & February 2013 | 63
I don't like waking up to the radio in the morning, but isn't there an alarm-clock that makes a more interesting sound? (Or, better still, a choice of different sounds.)Toddlers like to play with the buttons on the alarm-clock. Often the time or alarm settings get changed accidentally.The snooze button is too small, or badly located, or otherwise difficult to operate in the dark.It doesn't make the coffee.
How many of the foregoing complaints apply to your alarm-clock?A trip to the local electronics store failed to find an alarm-clock meeting enough of the require-ments on my wish list. So, being an Electronics Engineer by profession, I decided to design and build my ‘ideal alarm-clock’.
An improved digital alarm-clock designFollowing is a concept for a digital alarm-clock meeting my objectives…My main focus was to combine enhanced func-tionality with convenience (ease of use), while attempting to address many of the deficiencies of commercial products. The design would not be compromised to compete on a cost basis with consumer mass-market products. Do-it-yourself construction cannot compete with mass-produc-tion, but there is great satisfaction derived from making something better.An intuitive local user interface (control panel) comprising display, a number of push-buttons and a rotary encoder switch (knob) should be provided for operation of the clock while not con-nected to a computer. The local controls would be designed primarily for alarm time setting and for selection of displayed information, rather than for advanced configuration. Programming of the 7-day time-switch ON/OFF schedule, if required, would be more conveniently done via a USB PC link using a Windows software application.The front panel should have a large 4-digit dis-play, normally showing the time-of-day, plus sev-eral dedicated back-lit enunciators, e.g. PM, 24 hr,
A1–A4, S1–S4, MON, TUE, WED, etc. The ‘alarm’ enunciators (A1, A2, A3, A4) show the ON/OFF status of up to four daily alarm events. Another set of four enunciators (S1, S2, S3, S4) indicates the ON/OFF status of the time-switch outputs. Display brightness must be variable, and prefer-ably controllable by a choice of user-configurable options, e.g. ambient light sensor (‘automatic’), or alarm event (‘power-save mode’), or manu-ally using the control panel.The alarm function should have a variety of con-figurable options, e.g. choice of alarm sounds, initial volume, maximum volume, volume auto-increasing (‘ramp-up’ option), number of repeat alerts, interval between repeat alerts, etc, etc.There should be a good selection of preset (built-in) alarm sounds, which can be custom-ized by the user. A different alarm sound could be programmed for each alarm ‘event’ occurring throughout the 7-day alarm schedule, if the user so desired. Ideally, alarm sounds would be digital audio clips (PCM/MP3 encoded), downloadable via the USB link, giving wide-range low-distortion audio quality. (This could be an optional extra, as the added cost might not be justifiable to many enthusiasts.)The ‘snooze’ function might have two modes of operation. In ‘classic’ snooze mode, the alarm sound is silenced temporarily by a single push of the button, but is reactivated after a preset time interval (e.g. 10 minutes). Alarm activa-tion repeats until cancelled (e.g. by pressing the snooze button three times rapidly) or until a pre-set timeout expires. In the alternative ‘auto-repeat’ snooze mode, however, alarm activation would recur automati-cally at preset intervals (e.g. 10 minutes), for a number of times, without operator interven-tion, before cancelling itself. A single press of the snooze button would cancel repeat activa-tions, regardless.In either snooze mode, it would not be necessary to switch off the alarm every morning and re-enable it again before going to bed every night!In normal operating mode, the knob would be a manual brightness control for the display. In time-setting and configuration modes, however,
DIY cannot compete with mass-production, but there is great satisfaction derived from making something better
Projects
64 | January & February 2013 | www.elektor-magazine.com
make assembly easier for hobbyists with lesser soldering skill and/or limited tool kit. The micro-controller chip (MCU) is socketed (52-pin PLCC) for the same reason.The main board incorporates a simple, low-cost sound generator. Audio level (attack/decay ampli-tude envelope and overall volume) is controlled digitally by the MCU using pulse-width modula-tion (PWM). With the help of some tricky firm-ware, which can add frequency-modulation and/or amplitude-modulation effects, this arrangement can produce a good variety of alarm sounds rang-ing from quite pleasant to unbearably horrible (to suit all tastes). A selection of 16 pre-defined sounds is provided with the generic firmware. Alternative sounds may be customized by the user, by modifying the ‘sound shape’ parameters stored in EEPROM.
The parts may be housed in a ready-made enclo-sure, or in a unique housing of your own design and construction. The ‘standard’ enclosure is PAC-TEC model CM6-225, but a Chinese-made replica is available at a lower price. The circuit boards were designed to fit in this box. (See Assembly Instruc-tions in download package.) Although the seven ‘control surface’ push-buttons are normally fitted to the top of the display board, so that they are accessible along the front edge of the top panel, they may be located elsewhere, e.g. on a piece of prototyping board, wired back to the display board. Such an arrangement would allow creative variations from the ‘standard’ enclosure design.Hardware expansion is possible via an optional ‘mezzanine’ board which connects to the main board via a ribbon-cable connector. This makes it possible to extend the functionality of the clock, or to use the hardware platform for a completely different purpose. A mezzanine board to generate hi-fidelity PCM/MP3 digital audio alarm sounds may be developed by the author in the future. The board will include an MP3 decoder chip and serial data-flash memory to hold alarm ‘sound clips’.The firmware is based on the author’s ‘ALERT’ (Low-End Real-Time) operating system. Project builders may choose to write (and share) their own firmware, or to download a generic version free-of-charge. The source code is available at [1] for readers who may wish to modify or extend the firmware for their own purposes.The hardware is based on Atmel’s AT89C5131 USB microcontroller. This MCU was chosen because it is available in a PLCC package (sock-
the knob could well be used for convenient adjust-ment of displayed data.A means to confirm the next pending alarm event, up to 48 hours in advance, must be provided. Perhaps pressing the snooze/cancel button (while there is no alarm active) could enter an ‘alarm confirmation’ mode in which the time and day of the next pending alarm is displayed. And if the button was held down for longer than (say) two seconds, the sound assigned to the pending alarm event would be played to check the loud-ness level. Taking this concept a step further, the knob could be used to adjust the loudness, while a pair of buttons might be used to change the sound effect.For technology enthusiasts, the clock should con-nect to a PC (via USB) and support a variety of accessories via rear-panel I/O connectors. These provide logic outputs which can be used to switch appliances on and off at preset times, and/or to switch on when the alarm activates. Examples of accessories are: alarm bell/chime, bed-side lamp, radio, TV, kettle, coffee-maker, computer, PC peripherals, etc. Each of the time-switch ‘chan-nels’ should be settable independently and may have ON/OFF times set differently for each day of the week.A backup battery must be provided to maintain the clock time and date in the event of temporary power failure. When the battery voltage drops below the minimum usable voltage, a warning should be indicated.All user-settable options, parameters, on/off/alarm weekly time schedule, etc., must be preserved in non-volatile memory. User configurations may be transferred to and from a PC using a Windows util-ity. While connected to a host PC, the clock should automatically synchronize its time and date to the PC’s internal real-time clock, which in turn may be synchronized to Internet time.In addition to supporting a Windows ‘graphical user interface’ (GUI) to configure clock options, and for programming the alarm schedule, etc., the USB port must allow downloading of firmware upgrades. This capability is essential to support feature enhancements and bug fixes.
Technical introductionEssential hardware consists of a main board and display board, plus encoder switch (knob), speaker and snooze/cancel button. Surface-mount (SMT) components have been eliminated from the 2 boards comprising the basic hardware, to
Alarm Clock/Time-Switch
www.elektor-magazine.com | January & February 2013 | 65
lar design, I would choose a different MCU, e.g. Freescale MC9S08-JM60, Microchip PIC18F66J50, or maybe even a 32-bit ARM core MCU (since the cost is comparable with many 8-bitters). All components are readily available from the major online suppliers such as Farnell / Avnet), Digi-Key, Mouser, etc. Most parts should also be stocked by local hobby electronics stores (probably at more inflated prices). Beware of high delivery fees when ordering online from overseas suppliers.
General operationPower supply requirementsThe clock is designed to be powered from a 9 V DC plug pack. The clock itself draws less than 100 mA (with the display set to normal bright-ness). A switch-mode regulated plug-pack is recommended to minimize AC power consump-tion. The clock has a switch-mode 5 V regulator IC for optimum efficiency.The clock may also be powered from the USB port (Vbus = 5 Vdc). The external DC supply (+VEX), normally available at the accessory sock-ets, comes directly from the 9 V power supply input, so it is not available when running on
etable), it has a generous amount of on-chip flash program memory (32 KB) with separate data EEPROM (1 KB), it has an on-chip USB peripheral module, it is popular and hence readily availa-ble at low cost (around US$7.00 from Digi-Key). Most importantly, the ‘C5131 has a bootloader for flash programming via the USB port, requir-ing no additional programming device.The ‘C5131’s only bad trait is that it is has an 8051 core, which is (in the author’s opinion) one of the worst MCU architectures ever devised! Not that it really matters if you’re programming in C and there’s enough flash PROM to compen-sate for the inefficient instruction set. (To be fair, the 8051 instruction set is quite efficient when working with the core ‘register file’, i.e. RAM below 0xFF, but horribly inefficient working with extended data RAM.)
For DIY firmware developers, there is a free C compiler available for 8051 code development. (Google with “SDCC 8051 compiler”.) Overall, I think the 89C5131 was a good choice for a DIY project, especially for enthusiasts not skilled in SMD soldering. For a commercial product of simi-
Features of the ‘7-uP’ Alarm-clock/Time-switchConvenience and ease of use Intuitive local controls and setup menuKnob (rotary encoder switch) for easy time setting and data entry One-touch confirmation of pending alarm time and loudnessMultiple alarm ‘events’ and weekly (7-day) scheduleProgram up to four alarm ‘events’ in a 24-hour day Different alarm times each day of the week, if desiredVersatile snooze/cancel options‘Classic mode’ — alarm sounds continuously until deferred by button hit ‘Auto-repeat mode’ — alarm recurs at periodic intervals, until cancelled Adjustable snooze time interval (in either mode)Variable brightness display, with choice of control modes, i.e.Ambient light sensor Alarm or timer activatedManual override — adjust brightness using knobHigh quality, programmable alarm soundsChoose from a variety of ‘interesting’ preset alarm sounds Customize the preset sounds or create your own soundsAssign a different sound to each alarm ‘event’
USB link to host PC #
Synchronize the clock to PC system time and date (i.e. Internet time)Save and restore option settings, alarm event and time-switch schedules ^
Download firmware upgrades (feature enhancements and bug fixes) Time-switch and Alarm-switch accessory connectionsFour independent time-switch channels with 7-day ON/OFF scheduleControl AC appliances (lamps, radio, TV/AV, computer & peripherals)*
Alarm-activated output for controlling external wake-up devices Auxiliary (countdown) timer with accessory control outputSwitch PC peripherals on automatically when USB bus power is detected
* Requires optional 4-outlet solid-state relay board for controlling AC line
powered equipment.
# PC link uses the USB-CDC Virtual COM port API, also accessible using
HyperTerminal.
^ Facilitated by Windows GUI application software, to be developed by
3rd-party developers.
Projects
66 | January & February 2013 | www.elektor-magazine.com
ble fuse. The alarm-switched control output is capable of sourcing up to 0.5 A to a DC-powered accessory (e.g. bell, lamp, radio, etc.).
Firmware downloadFirmware is installed into the device’s Flash pro-gram memory via the USB link using Atmel’s FLIP (Flexible In-system Programmer) PC util-ity. The latest version of FLIP software and user documentation can be downloaded from Atmel’s website.To start the device’s USB bootloader, press and hold the ISP (In-System Programming) button, then press and release the reset button, then release the ISP button. The display should be blank. Plug in the USB cable to your PC. Start the FLIP application. Follow FLIP instructions from Atmel.
Host Command Interface (HCI)The firmware incorporates a communications protocol intended primarily for passing data to and from a host PC via the USB link. The protocol is based on a simple command-line user interface format, hence the term ‘host command interface’ (HCI). Since the protocol uses printable ASCII characters, the HCI can be accessed using a terminal emulator (such as HyperTerminal) for human interaction. The HCI can be used interac-tively for setting clock options, parameters, etc. Advanced configuration options, not accessible via the ‘local user interface’ (control panel), can be set using the HCI with HyperTerminal, or a custom Windows GUI application (TBD).The device USB port appears to the host PC as a virtual COM port. Windows application soft-ware can communicate with the clock through the standard Windows COM port API function calls (open, read, write, etc.).A command string is composed of a 2-letter command mnemonic and a number of user-supplied arguments (parameters). Some com-mands have no arguments. A single space must be inserted between command line arguments, where there is more than one (including the two-letter command name). HCI input is gen-erally not case-sensitive. There is no provision for command-line editing, but Escape or Ctrl-X will cancel the command line.A brief command summary is given by the help command, ‘HE’. Debug commands are provided to assist programmers developing their own firmware code, and for those interested in the
USB power. Consequently, accessories which rely on the 9 V DC supply will obviously not function. The backup battery keeps the clock running while disconnected from both USB and the 9 V plug-pack.
Backup batteryThe clock is not intended to be battery powered in normal operation. The battery is intended only to prevent loss of time and date during the occasional brief AC line power failure; also to allow the clock to be disconnected momen-tarily while being moved from one location to another, e.g. for connection to a host PC. While running solely from the backup battery, only the bare minimum clock functions are kept alive to maintain the time and date.The backup battery comprises three ‘AA’ size cells, which may be 1.5 V alkaline types or 1.2 V rechargeable types. The microcontroller runs on any battery voltage in the range 3.3 V to 5 V. Battery voltage is monitored continuously by the microcontroller when running on the main 9 V DC supply. A switch is provided to discon-nect the backup battery in case the clock may be powered down for a prolonged time.
Accessory DC supplyThe Alarm and Time-Switch Accessory sockets provide a protected DC supply, fed from the 9 V
DC inlet (plug-pack). The external DC supply is current limited to about 0.5 A by a resetta-
Figure 1.Top panel button legend.
Figure 2. Front panel display legend.
Alarm Clock/Time-Switch
www.elektor-magazine.com | January & February 2013 | 67
internal workings of the micro-controller and firmware.For further details on the HCI, refer to the User Guide contained in the free archive download at [1].
Control panel operation — a brief overviewFigure 1 shows the standard layout of push-buttons along the top panel of the clock. The SNOOZE/CANCEL button is also located on the top panel. The buttons, LED display and encoder switch (knob) comprise the local user interface. Most clock operations and settings are acces-sible via the local user interface. Some of the more esoteric user options are adjustable only via the USB link.Unlike most alarm clocks, the display on this one shows the day-of-the-week. When setting an alarm, the user can choose which day(s) of the week the alarm will be activated. Other LED enunciators show the four alarm events’ on/off status (A1–A4) and the four time-switch out-puts’ on/off status (S1–S4). The control panel (Figure 2) allows manual override of the time-switch outputs.Most of the push-buttons have more than one function. The function performed by a button press is dependent on the context of the opera-tion. For example, in normal time-of-day display mode, pressing the [+] or [−] button will select a different item for display, e.g. seconds, year, date (month & day), etc. In a setting mode, however, the [+] and [−] buttons may be used to incre-ment or decrement a numeric value, or to scroll through a list of items, e.g. days of the week.In general, when in a setting mode, a flashing digit or LED enunciator indicates an item that can be changed by a button press or by turning the knob. The EXIT button quits a setting operation without making any changes.Table 1 presents a summary of ‘local user inter-face’ (control panel) operations. The left column contains operations that can be performed from normal time-of-day display mode. For full details on operation, see the User Guide in the down-load package.
Next month’s second and closing instalment will discuss the schematics and the construction of the clock
100149
Table 1. Button functions
Button press or other input
Resulting action
TIMER/CLOCK button hit
Countdown Timer (initial or remaining time, mm.ss) is displayed. From here, the Countdown Timer can be set and started.
ALARM CHECK button hit
Alarm Check mode is entered. The scheduled time of a selected alarm event (A1..A4) is shown. From here, the selected alarm (time and day-of-the-week) may be re-programmed, or enabled or disabled. The [+] and [−] buttons select the day-of-the-week for the alarm to be set. The ON/OFF button toggles the alarm status. Pressing the SNOOZE button while in Alarm Check mode allows the alarm sound effect and loudness to be changed.
TIME-SWITCH button hit
Time-Switch control mode is entered. A selected time-switch channel (S1–S4) may be switched on or off (temporarily over-riding the programmed time-switch schedule).
SET button held for >3 sec
Time-of-Day (TOD) setting mode is entered. The knob adjusts the hours, then if the SET button is pressed again, adjusts the minutes. Press SET again to commit to the change, or press EXIT to quit without any change.
MENU button held for >3 sec
Menu Setup mode is entered. From here, various user options and parameters can be adjusted. The [+] and [−] buttons scroll thru a list of menu items.
[+] or [−] button hit
The displayed item is changed from normal TOD to seconds or the date (year, month, day). Pressing [+] scrolls thru items in the sequence: Seconds (ss.cc), Year (20xx), Date (MM dd), then back to time-of-day. Pressing [−] reverses the sequence. The SET button can be pressed to set any of the displayed items.
SNOOZE/CANCEL button pressed
Alarm confirmation: The time of the next pending (enabled) alarm event, up to 48 hours in advance, is displayed. If the button is held down (> 2 sec), the alarm sound plays. Pressing SET while holding down the SNOOZE button allows the alarm sound type and loudness to be changed.
KNOB rotated (while normal time of day is displayed)
Display brightness is adjusted. Depending on selected options, the setting may be retained until next adjusted, or it may change again automatically.
h t t p : / / w w w . h a n d s o n t e c . c o m Page 1/of 1
8051‐Based System for Rapid Firmware Development
MODEL:HT‐USB5130
8051 is one of the most popular 8-bit µController architectures in use today, learn it & use it the practical and HandsOn® way.
Suitable for Industrial Embedded Systems Control & Applications
HT-USB5130 Development Board with Atmel 8051 AT89C5131 µController
Website: http://www.handsontec.com On-Line Ordering: http://handsontec.com/USB8051.php Detail Documentation: http://handsontec.com/USB8051.php Note: This development board is suitable to use for “The 7-Up Alarm Clock/Time Switch” Project.
HT-USB5130 USB 8051 Flash µC Development Board
HandsOn Technology HT‐USB5130
12‐12 Menara TJB, Jalan Syed Mohd
Mufti, 81100 Johor Bahru, Malaysia.
http://www.handsontec.com
Projects
24 | March 2013 | www.elektor-magazine.com
The main board schematic shown in Figure 3 gives the impression of a very simple industrial controller with minimal external I/O interface connections. A notable exception is the audio generator circuitry, which is more complex than you would expect on a general-purpose program-
mable controller, for example.The choice of micro-controller chip has already been explained in the technical introduction (above). The At89c5131 is a derivative of the Intel 8051 family and thus has many 8051 core characteristics, including the inexplicably bizarre
Part 2
By Michael J. Bauer (Australia)
The 7-uP Alarm Clock/ Time-Switch
Y1
24MHzC09
22p
C10
22p
TP4
R03
2k2
S2
ISP
R01
100R
C07
10n
C06
2n2
C08
1u
R04
1k5
R05
27R
R06
22R
J2
D–
GND
D+
VBUS
23
4
1
5 6
D1
1N5817
+5V
P1.5/CEX2/KIN5/MISO
P1.7/CEX4/KIN7/MOSIP1.6/CEX3/KIN6/SCK
P1.1/T2EX/KIN1/SS
AT89C5131A-S3SUM
P3.3/INT1/LED0
P1.3/CEX0/KIN3P1.4/CEX1/KIN4
P1.2/ECI/KIN2
P3.5/T1/LED1P3.6/WR/LED2P3.7/RD/LED3
P1.0/T2/KIN0
PLCC-52 SKT
P3.2/INT0P3.1/TXD
P0.1/AD1P0.2/AD2P0.3/AD3P0.4/AD4P0.5/AD5P0.6/AD6P0.7/AD7
P2.2/A10P2.3/A11P2.4/A12P2.5/A13
P2.7/A15
P0.0/AD0
P2.6/A14
P4.0/SCLP4.1/SDA
P3.0/RXD
P3.4/T0
P2.0/A8P2.1/A9
USB D+USB D–
XTAL2 XTAL1
U1
AVDD
AVSS
VREF
UVSS
UCAP
PLLFPSEN
VDD ALE
VSS
RST
45
29
12 1319
16 17
EA
26
303132333539
27
444240383736
24
2322
25
1011
15
47
52
48495051
14
18
20
2128
41
43NC34
NC 46
1239
456
78
R02
10k
TP5H1
"USB STATUS"
R16
10k
R17
10k
RXDTXDINT0DE/RECANCEL
XTAL
2
ENCAENCB
SSDISPSSEXT1SSEXT2FREQ0FREQ1FREQ2ALMOUTDOUTA
SCLSDA
CMPIN1CMPIN2CMPIN3DOUT1DOUT2DOUT3DOUT4
PWRONVBUSDMUTEPWM0PWM1MISOSCK
MOSI
RESET
+5V
USB-B
S1
RESET
R07
10k
+5V
C11
100nRESET
VBUS
C04
100n
+5V
CMPIN1CMPIN2CMPIN3DOUT1DOUT2DOUT3DOUT4
J1
3
2
1
9V DC
F1
500mA
+VEX
D2
1N5817 +5V
C12
10n
R08
4k7
P51234
R09
100R
+5V
J3123456
+VEX
SIL-4
C01
220u
D3
1N5817
+VIN
TP1
(+VIN)
L1
100uH
C02
100n
C03
220u25V 16V
R15
3k0C05
4n7
R14
1k0
TP3+5V
TP2GND 0V
+5V
Q1
BC547R11
10k
D5
4V7
R10
10kC13
100n
+VEX
Q2
BC547R13
10k
R12
47kC14
100n
P61234
SIL-4
S3ON/OFF D4
1N5817
+5V
BT1
3V6...4V5
(3x AA)
SN75176
U3
VCC
GND
RE
DE
5
43
7
6
8
21 R
D
A
B
LM2594N-ADJU2
OUT
GND
VIN
EN FB
NC NC NC
6
7 8
5 4
1 2 3
TLC7555
U7
DIS
THR
OUT
GND
VCC
TRCV
2
7
6
4R
3
5
8
1
+5V
C22
100n
C23
100n
R28
100k
C24
6n8
R27
300k
R29
200kU8.A
13
2
1
U8.B
5
3
4
C25
6n8
U8.D
12
1011
U8.C
6
9
8
D7
1N4148
R30
4k7
+5V
Q4
BC547
Q6
BD
Q5
BC557
R31
10k
R32
2k2
+5V
R33
330R
R34
2k2
D6
1N5817139
R36
10R
1W
P812
SPEAKERSIL-2
R35
330R
C27
220u25V
C26
100n
+VIN
U814
7U5
9
8
+5V +VEX
+VBATT
U414
7
C16
100n
RA1
8x 22k
123456789
FREQ1
PWRO
N
VBUS
PWRON
VBUSD
DE/RE
TXDRXD
FREQ0
FREQ2
PWM1
MUTE
(P2.3)
(P2.4)
(P2.5)
(CEX1)
(P1.2)
CMPIN0
CMPIN0
MOD-6P4C
RS422/485SERIAL BUS
SERIAL INTERFACE(INTERNAL)
ALE
Figure 3. Circuit diagram of the main board electronics.
Alarm Clock/Time-Switch
www.elektor-magazine.com | March 2013 | 25
I/O port configurations. Port 0, for example, is open-drain and therefore requires external pull-ups on pins to function as outputs. Other ports have internal weak pull-ups (~25 kΩ) which do not provide sufficient output drive capacity for most purposes.
Nevertheless, the 89c5131 has many redeeming features including a very fast processor core. With a 24 MHz crystal and ‘X2’ clocking mode, the MCU core runs at 48MHz! The device incorporates many extra peripheral modules, e.g. USB, Timer T2, PCA (used for PWM outputs), SPI, and TWI (IIC).The At89c5131 provides a separate flash program memory block (4 KB) for a bootloader. The device ships with a USB bootloader pre-programmed into this ‘boot block’, allowing user program code to be loaded into the main flash program area (32 KB) via the USB port, i.e. requiring no addi-tional flash programming hardware. If the pro-cessor is reset while the PSEN# pin is pulled low (using the ISP button), the bootloader is started instead of the user application code.The UART TX and RX signals are routed to the expansion header (P1), also to a 4-pin header (P5), in case the user wants to attach an RS232 serial interface adapter. The UART signals are also routed to an RS422/485 transceiver (U3 = SN75176) to support external accessories via a serial bus. The initial release of clock firm-ware does not include any feature which uses the RS422/485 serial bus, so the transceiver (U3) and bus port connector (J3) may be omitted until such time as might be needed.Circuitry is provided to monitor the external DC supply voltage (+VEX). If it drops below about 6 V, transistor Q1 will turn off and the logic sig-nal PWRON# will be in the High state due to a pull-up resistor in the MCU. The firmware moni-tors this signal and turns on an enunciator (PF) if the supply drops below minimum required volt-age. The signal PWRON# is also routed to the 5V switch-mode regulator IC (U2 = LM2594) EN# (enable) pin. If the external 9V power supply is removed, the 5 V regulator is shut down, allow-ing the +5 V rail to be energized either by USB power or battery power.The USB ‘Vbus’ line is sensed by the circuit of transistor Q2. If USB power is not present, Q2 turns off and signal VBUSD# will float High. The firmware reads this signal and adapts its operat-ing mode accordingly. The state of the USB Vbus signal can also be used by the firmware to control one (or more) of the accessory control outputs.
Description of the main board,
the display board, and assembly
P1
1011 1213 1415 1617 1819 20
1 23 45 67 89
DIL-20
XTAL2PWM1SCKSSEXT1FREQ0FREQ2TXDSCL
MISOMOSI
SSEXT2FREQ1
RXDINT0SDA
+5V
(P1.4)(P1.6)(P2.1)(P2.3)(P2.5)(P3.1)(P4.0)
RESET(P1.5)(P1.7)(P2.2)(P2.4)(P3.0)(P3.2)(P4.1)
+VIN
P212345678
DISPLAY BOARDSIL-8
EXPANSION BOARD
MISOSCK
PWM0
MOSISSDISPSENSOR
(CEX0)(P1.5)(P1.6)(P1.7)(P2.0)
J5 1
2
6
345
123
4 56
DIN6
ALARM
J4 6
7
12345
1245
6
3 7
DIN7
TIME-SWITCHACCESSORY
U4.A1
21
U5.A1
161
U4.B3
41
U5.B2
151
U4.C5
61
U5.C3
141
U4.D9
81
U5.D4
131
U4.E11
101
U5.E5
121
U4.F13
121
U5.F6
111R24
4k7
R25
22k
Q3
BD140
D8
1N58174
52 U6.A
6
71 U6.B
8
914 U6.C
10
1113 U6.D
R20
47k
R22
47k
C19
100n
R21
47k
C20
100n
R23
47k
R19
10k
R18
22k
+5V
C17
100n
C18
10u 16V
C21
10n
+VIN
U63
12
C15
100n
U6 = LM339
+VEX
DOUTA
DOUT1
DOUT2
DOUT3
DOUT4
ALMOUT
+VBATT
CMPIN1
SENSOR
R26100kPWM1
CMPIN3
CMPIN2
U5 = ULN2003U4 = 74HC04
ACCESSORY
(P2.7)
(P0.4)
(P0.5)
(P0.6)
(P0.7)
(P2.6)
(P0.1)
(P0.0)
(CEX1)
(P0.3)
(P0.2)(1V6)
P3123
SIL-3ENCODER SWITCH
ENCA
ENCB
P412
SNOOZE BUTTONSIL-2
CANCEL
CMPIN0
100149 - 11
AUXOUT
(TSW.OUT1)
(TSW.OUT2)
(TSW.OUT3)
(TSW.OUT4)
(GP.IN1)
(GP.IN2)
Projects
26 | March 2013 | www.elektor-magazine.com
logue inputs to monitor the ambient light sensor and backup battery voltage. Analogue-to-digital conversion is implemented by a firmware algorithm, making use of a pulse-width modulation (PWM) out-put from the MCU to provide a variable reference voltage. A low-pass filter (R26, C21) removes the AC component (47 kHz) from the PWM pulse train, so that the DC level is controlled by the PWM duty. The PWM reference voltage is fed into the (−) inputs of two comparators U6A & U6B. The ana-logue input voltages to be measured are fed into the (+) inputs of U6A & U6B. By changing the reference voltage at periodic intervals, the firm-ware can determine the input voltages. Conver-sion accuracy is limited by the resolution (and linearity and noise) of the PWM-generated ref-erence voltage. About 1% error is the best we could hope for. Due to the filter time-constant, it takes a few milliseconds for the reference voltage to stabilize after a change in PWM duty. Conse-quently, the conversion time is quite slow com-pared to a hardware ADC. Fortunately, in this application, speed is not an issue.
Accessory logic inputs and outputsSix logic outputs are provided on the accessory sockets J4 and J5. The four time-switch outputs are active low (current sinking) control signals which can sink up to 50 mA each. These are intended to drive optocouplers (isolated inputs) in a solid-state AC power board. The alarm-switched 9V DC output is active high (current sourcing) and can source up to 500mA (limited by the resetta-ble fuse F1). The corresponding MCU output port pins are buffered with a CMOS hex-inverter (U4) because the port pins cannot source enough cur-rent to drive the inputs of the peripheral driver IC (U5 = ULN2003A).Two logic inputs are implemented by comparators U6C and U6D. The logic threshold is set to +1.6 V. The external inputs are buffered and filtered by RC networks to protect the MCU input port pins from nasty external transients (e.g. ESD).
Analogue inputsThe AT89c5131 MCU does not provide analogue (ADC) inputs. The clock application needs two ana-
HDSP-523E
HD1
CC1 CC2
DP2
DP1
16
14 13
B1A1
15
C1 D1 E1 F1
18 17
G1
11
A2 B2
10
C2 D2 E2 F2
12
G2
93 2 1 4 8 6 5 7
HDSP-523E
HD2
CC1 CC2
DP2
DP1
16
14 13
B1A1
15
C1 D1 E1 F1
18 17
G1
11
A2 B2
10
C2 D2 E2 F2
12
G2
93 2 1 4 8 6 5 7
74HC595
U3
LCLKSCLK
RST
VCC
GND
SDI
SQHOE
QA
14
15
13
10
QDQC
QE
QH
QB
QFQG
1112
16
65
1
7
4
23
8
9
74HC595
U4
LCLKSCLK
RST
VCC
GND
SDI
SQHOE
QA
14
15
13
10
QDQC
QE
QH
QB
QFQG
1112
16
65
1
7
4
23
8
9
ULN2003
U5
GND
COM
10111213141516I1
I2I3I4I5I6I7
O1O2O3O4O5O6O7
9
123
67
45
8
+5V
+5V
H01"MO"
H02"TU"
H03"WE"
H04"TH"
H05"FR"
H06"SA"
H07"SU"
H08"PF"
H09"sec"
H10"24h"
H11"PM"
H12"OFF"
H13"ON"
H14"AL"
H15 colon
H16 colon
H17"A1"
H18"A2"
H19"A3"
H20"A4"
H21"S1"
H22"S2"
H23"S3"
H24"S4"
1
2
ASEG0
ASEG1
ASEG2
ASEG3
ASEG4
ASEG5
ASEG6
ASEG7
ASEG0
ASEG1
ASEG2
ASEG3
ASEG4
ASEG5
ASEG6
ASEG7
ASEG0
ASEG1
ASEG2
ASEG3
ASEG4
ASEG5
ASEG6
ASEG7
ASEG0ASEG1ASEG2ASEG3ASEG4ASEG5ASEG6ASEG7
C3
100n
C4
100n
74HC165
CLK INH
SH/LD
U1 CLK
SER
VCC
GND
16
11121314 QH
QH
1015
ABCDEFGH
8
1
3456
2
97
+5V
C1
100n
RA1
8x 22k
1
23456789
+5V
S5
MENUEXIT
S1
TIMERCLOCK
S6
SETOK
S2
ALARMCHECK
S7
(–)<<
S3
ON/OFF
S8
(+)>>
S4
TIME-SWITCH
12345678
P1
TP1 +5V
Q1
BPW85C
+5V
R1
100k
U2.A
EN
2 3
1R2
3k3
+5V
+5VU2.B
EN5 6
4
R3
3k3
U2.C
EN
98
10
U2.D
EN
12 11
13U214
7
C2
100n
+5V
+5VPWM0MISOSCKMOSISSDISPSENSOR
(CEX0)
SW8SW7SW6SW5SW4SW3SW2SW1
S9
P2123
A
B
SIL-3
SIL-8
U2 = 74HC125
AMBIENTLIGHTSENSOR ROTARY
ENCODERSWITCH
SCK
PWM0
SSDISPMOSI
100149 - 12
MAIN
BOAR
DTo
/ Fro
m
Figure 4. Circuit diagram of the display board.
Alarm Clock/Time-Switch
www.elektor-magazine.com | March 2013 | 27
author has a fond nostalgia). The hold-off time determines the interval of silence between suc-cessive plays of an alarm sound. (This is not the same as the ‘snooze interval’, which is indepen-dently settable.)In addition, the firmware provides a means to modulate the audio amplitude and/or frequency with (virtual) modulating signals, the period of which can be set to any integer multiple of two milliseconds (i.e. 2, 4, 6, 8 … ms), up to 500 ms.In summary, a simple low-cost audio genera-tor circuit in combination with a smart firmware driver routine delivers a diverse range of appeal-ing sound effects (and some not so appealing!).
Circuit description – display boardThe display uses a 4-digit 7-segment LED display because it is a low-cost solution giving adequate readability, and because the technology lends itself to variable-brightness capability. A white or multi-color backlit graphical LCD panel would have been more aesthetic, but the extra cost was considered unjustified, and continuity of supply of LCD panels can be an issue.Figure 4 shows that the 4-digit numeric display is realized using two 2-digit devices (Avago HDSP-523X), available in three colours: red, green or yellow. Pin-compatible parts are made by other optoelectronics manufacturers. Backlit enuncia-tors are realized by 24 discrete LEDs, 5 mm round diffuse lens, of various colours.The 7-segment display and enunciator LEDs are multiplexed in a common cathode 7 x 8 matrix. The cathodes are driven (active low) by a 7-ele-ment peripheral driver IC (U5 = ULN2003A), which is just an array of seven Darlington tran-sistors with TTL/CMOS logic compatible inputs.The anode (segment) lines are driven by an 8-bit CMOS shift-register. Looking at the display sche-matic, astute readers will notice there are no current-limiting resistors in series with the anode lines. The design relies on the source resistance of the CMOS outputs (approx. 60 Ω) to limit the LED current. The ULN2003 outputs have a minimum low-state voltage near 1 V (because of the Dar-lington configuration). The LEDs’ forward voltage drop is about 2 V. That accounts for 3 V, which when subtracted from the 5 V supply, leaves 2 V on the CMOS outputs’ equivalent resistance (60 Ω). The LED peak current is therefore limited to about 33 mA (= 2 V/60 Ω).Keep in mind the LEDs are multiplexed 8:1, and dimmed using PWM, so the peak current is
Audio generator and power amplifierA square-wave oscillator is realized with a CMOS timer IC (U7 = TLC555). The frequency of the timer audio output signal is controlled by switch-ing the resistance and capacitance of the RC tim-ing network. Eight different frequencies in the range 500Hz to 2kHz (approx.) are selectable by MCU logic output signals FREQ2, FREQ1 and FREQ0. A CMOS quad analogue switch (U8 = CD4066B) is used to switch the resistors (R27, R29) and capacitor (C25) in or out of circuit. The chosen frequencies are harmonically related.Audio amplitude is controlled by a PWM signal generated by the MCU (CEX1), fed to the control input of analogue switch (U8D) which ‘chops’ the square-wave audio signal. The duty cycle of the PWM signal determines the effective audio output level. The PWM frequency is about 47 kHz; well out of range of human hearing and out of range of the speaker too. (The speaker itself acts as a filter to remove the 47 kHz PWM signal.) The PWM pulse duty is variable over a 256:1 range using the 8-bit duty register in the MCU. This is an adequate dynamic range for the application, but a 10-bit (or higher) resolution would have been really nice.The audio signal remains a square-wave (chopped at 47 kHz), right up to the speaker driver transis-tor Q6. Hence, the power amplifier is a class-D design. Transistor Q5 is necessary to boost the base drive current required to turn on Q6. The PWM analogue switch (U8D) is not capable of passing currents higher than about 2 mA. Using a class-D power amplifier not only provides higher audio output power level than a linear amplifier, but it has another major advantage in this appli-cation. When the audio generator is “muted” (by firmware putting the MUTE signal High), the PWM switch (U8D) is off and hence the output transis-tor Q6 is off, so there can be no current whatso-ever flowing in the speaker, no matter how much noise is present on the supply rail (+VIN). This is a very important consideration for an appliance which may be sitting closer than 3 feet to your ears while you are trying to sleep.The audio level is updated by the firmware ‘sound synthesizer’ driver routine every two milliseconds. The firmware driver is capable of synthesizing an amplitude ‘envelope’ for the alarm sounds. User-settable parameters determine the attack (slope), sustain (time), release (slope) and hold-off (time), in similar manner to the analogue sound synthesizers of the 1970s (for which the
Projects
28 | March 2013 | www.elektor-magazine.com
The LED display requires two 8-bit output ports and the push-buttons need an 8-bit input port. MCU I/O port expansion is realized by 8-bit CMOS shift-registers with parallel data latches and serial data transfer logic. The shift-registers are (almost) directly compatible with the MCU ‘SPI’ (serial peripheral interface) bus. With a tiny sprin-kling of “glue” logic, implemented by a quad tri-state buffer (U2 = 74HC125), the display output registers (U3, U4 = 74HC595) and button input register (U1 = 74HC165) connect easily to the SPI bus. Note that buffers U2A and U2B are wired to function as inverters, with the aid of pull-down resistors on their (tri-state) outputs. Inverting the polarity of the SPI clock signal (SCK) to the display registers allows the same SPI clocking mode to be used for simultaneous read (slave input) and write (slave output).Using the SPI bus minimizes the number of sig-nals required to interface the display board to the main board. The display and button regis-ters share a common SPI ‘slave-select’ signal (SSDISP#). The display registers are written and the button register is read in the same 2-byte SPI data transfer cycle. Since the button input register is only one byte wide, the second byte of the received data is ignored by the firmware.The slave-select signal SSDISP# is inverted to obtain a shift/load signal SH/LD#. While the input port is de-selected (SSDISP# High), parallel data are accepted by the input latch. During an SPI transfer, SSDISP# is put Low, SH/LD# goes High, so the input latch is inhibited and the serial clock is enabled, i.e. the input data is held constant while being shifted out.Output registers (74HC595) are comprised inter-nally of an 8-bit serial-in/parallel-out SR with an 8-bit D-type latch. Output pins are driven from the latch bits. Whenever the latch clock signal (LCLK) is pulsed, the rising edge causes the 8 bits in the internal shift register to be transferred to the output latch. While LCLK remains High, the bits in the output latch remain unchanged. Bits in the internal shift register change whenever any SPI transfer cycle (read or write) is executed, regardless of whether the SPI slave select signal is asserted or not. In other words, the shift reg-ister data will change when other devices on the SPI bus, if any, are selected and written to. This doesn’t matter, so long as LCLK remains static while there is garbage data in the internal shift-register. Connecting LCLK to the display slave-select, SSDISP#, ensures the desired behaviour.
allowed to be near the LEDs’ maximum rating. The average LED current will be much lower than this — about 2 mA in normal viewing conditions. Variable brightness is achieved by applying a vari-able duty 47 kHz pulse waveform (PWM) to the Output Enable (OE#) input of the LED cathode driver register (U4). The range of PWM duty, and hence brightness, is 256:1. At minimum duty (1/256), the display is still bright enough to be readable in low lighting conditions.By the way, don’t be tempted to fit blue or white LEDs for any of the enunciators. Their forward voltage drop is too high (typically about 4 V) to be used in this circuit design.
28 | March 2013 | www.elektor-magazine.com
Figure 5. Top view of completed main board.
Figure 6. Top view of completed display board.
Alarm Clock/Time-Switch
www.elektor-magazine.com | March 2013 | 29
pendently. Examples: Four outlet solid-state relay (TRIAC) AC power board; Low-voltage dimmable LED bed-side lamp; IR remote-control interface (for TV/AV equipment); PCM/MP3 add-on sound-effects board (with SPI link to MCU). The last three of these examples would require firmware updates for use with the clock.Also, Elektor readers active at the ElektorLabs community website [2] are expressly invited to develop a Windows GUI software application for use with the clock, e.g. for the purpose of edit-ing, saving and restoring alarm and time-switch schedules, option parameters, etc. Windows GUI software developer, please step forward. Note: The clock operates stand-alone — it is not dependent on Windows software.Please feel free to suggest any changes you think might enhance the appeal or utility of the project. Express yourself at ElektorLabs and be heard.
(100149)
Internet Links
[1] www.elektor.com/100149
[2] www.elektor-labs.com
The display board has a phototransistor (Q1 = BPW85C) which senses the ambient light level. The current flowing in the emitter resistor, and hence the voltage across it, increases with increasing illumination of the transistor junction. The emitter voltage is monitored by a comparator on the main board using a primitive analogue-to-digital conversion technique, as noted earlier.Provision is made on the display board for mount-ing a rotary encoder switch (S9). Project build-ers who choose to deviate from the ‘standard’ enclosure arrangement might want to relocate the encoder switch. This is easily done by cutting the display board into two pieces, separating off the smaller piece with the encoder switch. For this reason, wiring terminations to the encoder switch are separate from the other display board connections (8-way pad strip).Internally, the encoder switch has two sets of contacts which make and break as the shaft is rotated. Using pull-up resistors to the +5 V sup-ply rail, the two switch outputs (A and B) gen-erate square pulse trains in a quadrature phase relationship (i.e. 90 degrees out of phase with each other). Each output produces 24 pulses per revolution of the shaft, but the relative phases of the two pulse outputs is different (by 180 degrees) depending on the direction of rotation. A firmware decoding routine removes noise from the signals and keeps track of changes in the encoder shaft position.
AssemblyThe assembly of the clock is described in detail in a document called Assembly Instructions which may be downloaded free of charge from the Elektor website [1]. The documentation package also contains BOMs. Due to space limitations, we are limited here to printing photographs of the assembled main board (Figure 5), display board (Figure 6), the connector line-up at the rear of the clock (Figure 7), and how the display board is mounted upright in the casing (Figure 8).
You’re invited!The firmware is richly annotated, and could serve as a useful example to Electronics Engineering, IT and Embedded Technologies students developing embedded real-time firmware projects, whether or not they build the 7-uP Alarm Clock.This project has the potential to spawn a number of spin-off projects, not only as accessories for the alarm-clock, but which could be used inde-
9V DC
INPUT
BATT.
ON/OFF
RESET LED USB ISPRS422 ALARM
ACCESSORY
TIME-SWITCH
ACCESSORY
Figure 7. There’s an impressive array of connectors at the back of the 7-uP Alarm Clock.
Figure 8. Showing how the display board is held vertically in the Pactec CM6-225 case.
HandsOn Technology support Open SourceHardware(OSHW) Development Platform.
HandsOn Tech provides a multimedia and interactive platform foreveryone interested in electronics. From beginner to diehard, fromstudent to lecturer… Information, education, inspiration andentertainment. Analog and digital; practical and theoretical; softwareand hardware…
Learn : Design : Sharewww.handsontec.com