Time Switch V1.0

Wichit Sirichote, kswichit@kmitl.ac.th

New project for time controlling, 16-pin Nitron chip with LCD,
keypad and two channels optotriac.

My sister asked me to help make the device to control nigh light at
her new house in Korat. I thought, it 's time for Motorola MCU and
better to have LCD and keypad for program and time settings. I am so
fun using Nitron chip. It can run from +3V to +6V. The most important
for battery operated is low voltage reset module. Figure 1shows the
prototype of the Time Swicth V1.0. I made it with universal pcb and
placed Nitron chip at the bottom layer. The output has two channels
with optotriac. It can drive the resistive AC load directly.

Figure 1: The prototype of Time Switch V1.0.

Hardware Schematic

At first I tried with internal oscillator, since the OSC1 and OSC2
will be available for more I/O pins, however the time accuracy was not
correct. So I change to external 32,768 Hz circuit with 4093, to
provide 32768Hz for IRQ, but not success. The low power oscillator
circuit made with 4093 gave unstable frequency. Then I changed my
design to use external 4MHz X-tal. The bus frequency 1MHz, is used to
make timer0 counting. I have modulo register to compare providing
100Hz timer interrupt. See software for more detailed. The LCD is
simple four bits interfacing to PORTB, no busy bit checking, simple
delay was used instead. Four tact switch are tied to PORTA 0-3. All
keys use internal pull-up. The output is sink current driving to the
opto-triac MOC3040. The internal zero-circuit switching helps reducing
inrush current for AC load. I added bigger Triac, BT138F, 600V 8A for
bigger load.

Figure 2: Complete schematic of Time Switch V1.0.

As seen in the schematic, a simple diode switch provides battery
backup to the MCU and LCD. You may try with signal diode, 1N914 or
1N4148 if your battery is +3V. I tried with schottky diode and
tested with +3V lithium battery, it works fine. The +5V power
supply is made by zener diode.

Figure 3: Simple diode switch for battery backup.


The firmware is timer interrupt driven with fixed interval 10ms.
Every 10ms three tasks will be executed, update clock, tick display
and execute keypad.

// enter timer overflow every 1/100s or 10ms
#pragma interrupt_handler isr_TIM
void isr_TIM(void)
TSC &= ~0x80; // clear TOF
COPCTL = 0; // clear COP
tick_display(); // tick every 0.5 sec

For second and minute running tasks, I put them in function update
clock. The print time and print output functions are executed every
second and compare time every minute.

void update_clock()
if(++sec100 >=100)
sec100 = 0;
if(mode !=3 && mode !=4)
if(++sec > 59)
sec = 0;
if( min >59)
min = 0;
if(++hour >23)
hour = 0;
if(++day > 7)
day = 1;

Tick display is for indicating program running properly, I used
colon between HOUR and MIN to blink every second.
Blinking is done by toggle k every 0.5s (50x10ms) and swapping
between ':' and ' '.

void tick_display()
if(mode !=3 && mode !=4)
if(++timer1> 50)
timer1 = 0;
LCDWI(0x85); // swap display
if (k) LCDWD(' ');
else LCDWD(':');

The hardware has four tact switches. Each key when pressed, the
input port will be logic 0. The function that detects key pressed
will execute every 200ms (20x10ms).

// check key every 250ms
void exe_keypad()
if(++timer2 > 20)
timer2 = 0;
if(key0 == 0) servicekey0();
if(key1 == 0) servicekey1();
if(key2 == 0) servicekey2();
if(key3 == 0) servicekey3();

Functional keypad

Figure 4 shows keypad and LCD display. The summary of each key is as
MODE: to select MODE 0, 1, 2, 3 and 4

MODE 0: Set current time with HOUR and MIN keys.
MODE 1: Set current day from MON - SUN with HOUR key.
MODE 2: Manual set output ON/OFF with HOUR for OUTPUT1 and MIN for
MODE 3: program settings.
MODE 4: Display program from 0-4 by key HOUR.

AUX: to be used with MODE 3 for scheduler programming. In MODE 0 it
will reinitialize LCD.

MIN: to set minute in MODE 0 and MODE 3, to set output2 manually in

HOUR: to set hour in MODE 0 and MODE 3, set DAY in MODE 1, set output1
manually in MODE2, display scheduler in MODE 4.
How to set time and program the scheduler are detailed in Quick Start

Figure 4: Keypad and LCD display.


RTC.C : source code and RTC.S19 the Motorola s19 record.
timeswitch1.pdf : Quick Start Guide.


July 18, 2004