ZiLOG Worldwide Headquarters • 532 Race Street • San Jose, CA 95126
Telephone: 408.558.8500 • Fax: 408.558.8300 • www.ZiLOG.com
Application Note
Fail–Safe Clock Feature of
the Z8 Encore! XP™
Microcontroller
AN020101–0804
AN020101–0804
This publication is subject to replacement by a later edition. To determine whether a later edition
exists, or to request copies of publications, contact:
ZiLOG Worldwide Headquarters
532 Race Street
San Jose, CA 95126
Telephone: 408.558.8500
Fax: 408.558.8300
www.zilog.com
ZiLOG is a registered trademark of ZiLOG Inc. in the United States and in other countries. All other
products and/or service names mentioned herein may be trademarks of the companies with which they are
associated.
Information Integrity
The information contained within this document has been verified according to the general principles of
electrical and mechanical engineering. Any applicable source code illustrated in the document was either
written by an authorized ZiLOG employee or licensed consultant. Permission to use these codes in any
form, besides the intended application, must be approved through a license agreement between both
parties. ZiLOG will not be responsible for any code(s) used beyond the intended application. Contact the
local ZiLOG Sales Office to obtain necessary license agreements.
Document Disclaimer
©2004 by ZiLOG, Inc. All rights reserved. Information in this publication concerning the devices,
applications, or technology described is intended to suggest possible uses and may be supersed-
ed. ZiLOG, INC. DOES NOT ASSUME LIABILITY FOR OR PROVIDE A REPRESENTATION OF
ACCURACY OF THE INFORMATION, DEVICES, OR TECHNOLOGY DESCRIBED IN THIS
DOCUMENT. ZiLOG ALSO DOES NOT ASSUME LIABILITY FOR INTELLECTUAL PROPERTY
INFRINGEMENT RELATED IN ANY MANNER TO USE OF INFORMATION, DEVICES, OR
TECHNOLOGY DESCRIBED HEREIN OR OTHERWISE. Except with the express written approv-
al ZiLOG, use of information, devices, or technology as critical components of life support systems
is not authorized. No licenses or other rights are conveyed, implicitly or otherwise, by this docu-
ment under any intellectual property rights.
AN020101–0804 Table of Contents
iii
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
Table of Contents
List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .v
Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Z8 Encore! XP 4K Series Flash Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . 1
Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Developing the Application with Z8 Encore! XP MCU . . . . . . . . . . . . . . . . . . . 3
Software Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Demonstrating the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Appendix A—References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Appendix B—Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
AN020101–0804 List of Figures
iv
Application Note
Fail–Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
List of Figures
Figure 1. Preprocessor Definitions in Project Settings Dialog Box . . . . . . . . . . . 5
AN020101–0804 List of Tables
v
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
List of Tables
Table 1. List of References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
AN020101–0804 Abstract
1
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
Abstract
This Application Note discusses the fail-safe clock feature of the Z8 Encore! XP
4K Series microcontroller.
The source code file associated with this Application Note, AN0201-SC01.zip,
demonstrates the fail-safe clock feature, and is available on zilog.com.
Z8 Encore! XP 4K Series Flash Microcontrollers
ZiLOG’s Z8 Encore!® products are based on the new eZ8 CPU and introduce
Flash memory to ZiLOG’s extensive line of 8-bit microcontrollers. Flash memory
in-circuit programming capability allows for faster development time and program
changes in the field. The high-performance register-to-register based architecture
of the eZ8 core maintains backward compatibility with ZiLOG’s popular Z8 MCU.
Z8 Encore!® MCUs combine a 20 MHz core with Flash memory, linear-register
SRAM, and an extensive array of on-chip peripherals.
The Z8 Encore! XP 4K Series of devices support up to 4 KB of Flash program
memory and 1 KB register RAM. An on-chip temperature sensor allows tempera-
ture measurement over a range of –40ºC to +105 ºC. These devices include two
enhanced 16-bit timer blocks featuring PWM and Capture and Compare capabili-
ties. An on-chip Internal Precision Oscillator (5 MHz/32 kHz) can be used as a trim-
mable clock source requiring no external components. The Z8 Encore! XP
devices include 128 bytes of Non Volatile Data Storage (NVDS) memory where
individual bytes can be written or read. The full-duplex UART, besides providing
serial communications and IrDA encoding and decoding capability, also supports
multidrop address processing in hardware.
The rich set of on-chip peripherals make the Z8 Encore! XP MCUs suitable for a
variety of applications including motor control, security systems, home appliances,
personal electronic devices, and sensors.
AN020101–0804 Discussion
2
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
Discussion
The Z8 Encore! XP Series of devices use five possible user-selectable clocking
schemes, as listed below.
On-chip precision trimmed RC oscillator
On-chip oscillator using off-chip crystal or resonator
On-chip oscillator using external RC network
External clock drive
On-chip low precision Watch-Dog Timer oscillator
The user must select one of the above oscillators by writing appropriate hex val-
ues to the Oscillator Control Register (OSCCTL).
The Z8 Encore! XP™ Series of devices generate an interrupt when the primary
oscillator fails, provided the fail-safe clock feature is enabled. To maintain system
functionality in this situation, the clock failure recovery circuitry automatically
forces the Watch-Dog Timer (WDT) oscillator to drive the system clock. The
Watch-Dog Timer oscillator must be enabled to allow the recovery. Although this
oscillator runs at a much lower frequency than the original system clock, the CPU
continues to operate as before. The user can add the software routines to the
clock failure vector to provide either a remedy for the oscillator failure or issue a
clock failure alert.
This automatic switch-over is not available if the Watch-Dog Timer is the primary
oscillator. It is also unavailable if the Watch-Dog Timer oscillator is disabled. The
following section discusses the software implementation of the fail-safe clock
option.
For a detailed explanation of the OSCCTL and the WDT peripheral, refer
to the Z8 Encore! XP 4K Series Product Specification (PS0228).
Note:
AN020101–0804 Developing the Application with Z8 Encore! XP MCU
3
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
Developing the Application with Z8 Encore! XP MCU
This section discusses the software implementation and demonstration of the fail-
safe clock feature in detail.
Software Implementation
System clock selection, GPIO and timer initialization, and the oscillator failure
interrupt routine are discussed below.
Selecting the System Clock
The init_external_systemclock () function selects the type of oscillator used
with the Z8 Encore! XP 4K Series MCU. The Z8 Encore! XP 4K Series Devel-
opment Board uses an 8 MHz off-chip ceramic resonator. The Oscillator Control
Register must be unlocked before writing. Writing the two-step sequence E7h fol-
lowed by 18h unlocks the Oscillator Control Register. The register automatically
locks upon successful completion of a register write to the OSCCTL.
The following sequence writes to the Oscillator Control Register:
OSCCTL = 0xE7; // Unlock sequence for OSCCTL write
OSCCTL = 0x18;
OSCCTL = 0x72;
With the above setting, the internal precision oscillator is enabled, the WDT oscil-
lator is enabled, failure detection and recovery of the primary oscillator is enabled,
and the external RC oscillator is enabled as the system clock.
Initializing the GPIO
The Development Board used to implement the fail-safe clock option has three
LEDs --- D2, D3 and D4. In this example, the GPIO pins are configured as output
pins to switch the D2, D3, and D4 LEDs on or off.
The D4 LED is connected to Port C, pin 3 (PC3), the D3 LED is connected to Port
A, pin 7 (PA7), and the D2 LED is connected to Port A, pin 6 (PA6). For schematic
details of these connections, please refer to the Z8 Encore! XP 4K Series Devel-
opment Kit User Manual (UM0166).
After these connections are made, the init_led_gpio() function initializes the
GPIO ports. On the target board, PA6 and PA7 control the D2 and D3 LEDs,
respectively, and PC3 controls the D4 LED. As a result of this initialization, each of
these port pins are configured as output ports.
Port C pins provide a current sourced output capable of driving an LED without
requiring an external resistor. The current output is programmed to source 13 mA.
AN020101–0804 Developing the Application with Z8 Encore! XP MCU
4
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
This mode is enabled through the Alternate Function Subregisters. For details
about the Alternate Function Subregisters, please refer to the Z8 Encore! XP 4K
Series Product Specification (PS0228).
Initializing the Timer
Timer0 is initialized to generate an interrupt every 4.05 milliseconds with an exter-
nal oscillator frequency of 8 MHz. The init_timer0(void) function loads appro-
priate values to the T0H, T0L, T0RH and T0RL registers, loads the prescale
value, and selects the timer-operating mode.
The interrupt service routine (ISR) for the timer void isr_timer0(void) sets the
ProgStat variable to RUN mode.
The Oscillator Failure Interrupt Routine
The SET_VECTOR (POTRAP, Oscillator_Failure) function called in the main
routine initializes the vector location PORTAP (003Ah) with the address of the
oscillator_failure(void) interrupt routine.
When the primary oscillator fails, the program jumps to the oscillator_failure
(void) ISR which disables the external system clock and sets the internal preci-
sion clock either to 5.5 MHz or 32 kHz based on the #define statement in the
project setting. Then, the program execution continues as before. The details of
this operation is demonstrated in the next section.
Demonstrating the Application
This section lists the equipment used and the procedure followed to demonstrate
the fail-safe clock feature.
Equipment Used
Z8 Encore! XP Development Board
PC with HyperTerminal application
Procedure
The procedure to demonstrate the fail-safe clock feature is provided below.
1. Download and extract the AN0201-SC01.zip file.
2. Launch ZDS II–Z8 Encore!® and open the fail-safe clock project from the
extracted zip folder.
3. Click Project Settings. The Project Settings dialog box appears.
4. Select the Preprocessor category from the C tab.
AN020101–0804 Developing the Application with Z8 Encore! XP MCU
5
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
5. Add the _LOW_FREQ definition to the Preprocessor Definitions field. This
definition is required to select 5.5 MHz as the frequency of the internal
precision oscillator. Figure 1 depicts the Preprocessor Definitions field in the
Project Settings dialog box.
6. Compile and build the code.
7. Download the .lod output file to the target Z8 Encore! XP board, using
ZDS II.
8. Execute the code.
9. Observe that the D2, D3, and D4 LEDs flash sequentially.
10. Short pin 7 of the Z8 Encore! XP MCU (U5) to GND to create a clock failure
condition. As a result, an oscillator failure interrupt is immediately generated
and the internal precision oscillator begins to function.
11. Observe that the rate at which the three LEDs flash is reduced when
compared to the observation made in step 9.
Figure 1. Preprocessor Definitions in Project Settings Dialog Box
AN020101–0804 Summary
6
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
12. Remove the short between pin 7 of U5 and ground.
13. To test the fail-safe clock feature at the internal precision oscillator frequency
of 32 kHz, open the project and remove the _LOW_FREQ definition in the
Preprocessor Definitions field. As a result of this modification, 32 kHz is
selected as the frequency of the internal precision oscillator.
14. Recompile and build the project. Download the .lod output file to the target
board and execute the program.
15. Short pin 7 of U5 to ground, and observe the frequency at which the LED
flashes. The rate at which these LEDs flash should be greatly reduced
compared to the observations made in steps 9 and 11.
Summary
This Application Note discusses the fail-safe clock feature of the Z8 Encore! XP
4K Series microcontroller. With this fail-safe clock feature enabled, both program
execution and system operation continue even if the external oscillator fails. The
user can generate an audiovisual alarm that indicates the condition of the primary
clock failure. As a result, the user can decide whether to allow the system to oper-
ate at a lower frequency, or to switch the system off for maintenance or trouble-
shooting purposes.
AN020101–0804
Appendix A—
References
7
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP
Microcontroller
Appendix
A—References
Further details about the
Z8 Encore!
®
p
roducts can be found in the references
listed in
T
able 1
.
T
able 1.
List of References
T
opic
Docu
me
nt Name
eZ8 C
PU
eZ8 CPU U
ser Manual (UM0
128)
Z8 Encore! XP
4K Series
Microcontroller
s
Z8 Encore! XP
4K Series
Product Specification
(PS0228)
Z8 Encore! XP
4K Series
Development Kit User
Manual (UM0166)
ZDS
II
-IDE
ZiLOG Developer Studio ll-
Z8 Encore!
®
User Manual
(UM0130)
AN020101–0804 Appendix B—Source Code
8
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
Appendix B—Source Code
This appendix provides a listing of all source code associated with the application
described in this document.
A companion source code file, AN0201-SC01.zip, is available on zilog.com.
The following C file is listed in this section:
main.c
/*
**********************************************************************
* File: main.c
* Description: This program demonstrates how the system designed with
* Z8Encore XP can switch over to fail-safe clock mode when the primary
* oscillator fails. For testing purposes, the primary oscillator is
* made to fail by connecting the XP F042A clock input to GND (e.g.
* connect the J2 pin{2, 4, 6, 8, 10, 12, 14, 16} to pin 7 of the U5).
* When connected, a clock failure will occur and the system clock will
* switch from the external 8MHz clock (assembled on the evaluation
* board) to an internal 5.5MHz clock or 32KHz clock based on the
* selection. Observe the blinking rate of the D2, D3, and D4 LEDs on the
* evaluation board, which slows down very little when switched to 5.5
* MHz and operates much more slowly when switched to 32KHz.
*
* Copyright 2004 ZiLOG Inc. ALL RIGHTS RESERVED.
*
* The source code in this file was written by an
* authorized ZiLOG employee or a licensed consultant.
* The source code has been verified to the fullest
* extent possible.
*
* Permission to use this code is granted on a royalty-free
* basis. However users are cautioned to authenticate the
* code contained herein.
*
* ZiLOG DOES NOT GUARANTEE THE VERACITY OF THE SOFTWARE.
**********************************************************************
*/
#include <eZ8.h>
#define RUN 0
#define BUSY 1
#define OSC_UNLOCK_SEQ1 0xE7
#define OSC_UNLOCK_SEQ2 0x18
AN020101–0804 Appendix B—Source Code
9
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
int ProgStat = BUSY; // Program status variable
/********************************************************************/
void init_led_gpio(void)
{
PAADDR = 0x01; // PA Data Dir =
PACTL &= 0x3F; // PA6-PA7 as Output
PCADDR = 0x01; // PC Data Dir
PCCTL &= 0xF7; // PC3 as Output
LEDEN = 0x08; // PC3 uses LED drive
LEDLVLH = 0x08; // PC3 Led Driver current = 13ma
LEDLVLL = 0x00;
}
/********************************************************************/
// Turns off ALL LEDs
void turn_off_led(void)
{
PAOUT |= 0xC0; // Turn off PA6-PA7 leds
PCOUT |= 0x08; // Turn off PC3 Led
}
/********************************************************************/
// Timer 0 interrupt that is shifting LEDs
#pragma interrupt
void isr_timer0(void)
{
ProgStat = RUN;
}
//
**********************************************************************
// Primary clock oscillator fail trap interrupt
#pragma interrupt
void Oscillator_Failure(void)
{
AN020101–0804 Appendix B—Source Code
10
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
OSCCTL = OSC_UNLOCK_SEQ1; // Unlock sequence for OSCTL write
OSCCTL = OSC_UNLOCK_SEQ2;
#if defined(_LOW_FREQ)
OSCCTL = 0xB0; // Crystal oscillator is disabled
// Internal precision oscillator as
// system clock at 5.5MHz
#else
OSCCTL = 0xB1; // Internal precision oscillator is
// enabled.
#endif // WDT oscillator enabled
// Failure detection and recovery of
// primary oscillator is enabled
// Internal precision oscillator as
// system clock at 32kHz
// for demonstration purposes
}
//
**********************************************************************
//Initialize Timer0
void init_timer0(void)
{
SET_VECTOR(TIMER0, isr_timer0);
T0H = 0xFF; // Timer High
T0L = 0xFF; // Timer Low
T0RH = 0x7F; // Reload Compare High
T0RL = 0xFF; // Reload Compare Low
T0CTL1 = 0xE9; // Continuous/Prescale
IRQ0ENH |= 0x20; // Set Interrupt Priority Nominal
IRQ0ENL &= 0xDF; // Set Interrupt Priority Nominal
}
//
**********************************************************************
// Initialize system clock source to use external crystal
void init_external_systemclock()
AN020101–0804 Appendix B—Source Code
11
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
{
OSCCTL = OSC_UNLOCK_SEQ1; // Unlock sequence for OSCTL write
OSCCTL = OSC_UNLOCK_SEQ2;
OSCCTL =0x72; // Crystal oscillator is enabled
// Internal precision oscillator is
// disabled
// WDT oscillator enabled
// Failure detection and recovery of
// primary oscillator is enabled
// Crystal or external RC oscillator
// as system clock
}
//
**********************************************************************
// Main program begins here
// This program blinks LED-3 on the evaluation board
main ()
{
int ledstate = 1; // LED display state
SET_VECTOR(POTRAP, Oscillator_Failure);// Pass the vector number
//and the ISR address
init_external_systemclock(); // Initialize external
// system clock source
turn_off_led(); // Turns off ALL LED's
init_led_gpio(); // Initializes LED ports
// (Port A and Port C)
DI(); // Disable Interrupts
init_timer0(); // Initialize Timer-0
EI(); // Enable Interrupts
while(1) { // Infinite while loop
if(ProgStat != BUSY) // Wait for Timer Interrupt
{
switch(ledstate) // Shift LED state machine
{
case 1:
turn_off_led(); // Turns off ALL LED's
break;
case 2:
PCOUT &= 0xF7; // Turn On LED Red
break;
AN020101–0804 Appendix B—Source Code
12
Application Note
Fail-Safe Clock Feature of the Z8 Encore! XP™ Microcontroller
case 3:
PAOUT &= 0x7F; // Turn On LED Yellow
break;
case 4:
PAOUT &= 0xBF; // Turn On LED Green
break;
}
ProgStat = BUSY; // Change to wait state
ledstate++; // Shift LED
if(ledstate == 5)
ledstate = 1; // Start shifting LEDs all
// again
}
} // End of main program
// End of file