Demo8

19
1 Demo 8 - Programmable Timer www.MightyMicons.com Introduction: This demo describes a complete project meant for industrial timing applications. This is a com- plete one with all the required features to manage various time-based operations in the industrial environments. This module is the Programmable Timer with the facility to activate its output based on the Real Time Clock. The timer has the facility to activate its output for eight different timings. To implement this project, a LCD module of 2 line x 16 characters, or key matrix of 16 keys are used for the user interaction. A real time clock is also included to get timing information for the project . A buzzer is connected with the micon to indicate the output condition. In real, this buzzer can be replaced by a relay or any other suitable output element. Hardware: This project, Programmable Timer, is based on the Real Time Clock device, PCF8583 along with user interaction elements; 2 line x 16 character LCD module, a keypad with 16 keys. A buzzer is also included to indicate the output status. The LCD module is interfaced with the micon using 4 bit data bus to save the port lines. Micon’s port lines are simulated for the IIC bus to interface the IIC bus based real time clock. During the operation, the micon keeps comparing the current time with the set time. When the time matches, the controller activates the output for the predefined time. A buzzer is connected with the micon to indicate the output status.

Transcript of Demo8

Page 1: Demo8

1

Demo 8 - Programmable Timer

www.MightyMicons.com

Introduction:

This demo describes a complete project meant for industrial timing applications. This is a com-

plete one with all the required features to manage various time-based operations in the industrial

environments. This module is the Programmable Timer with the facility to activate its output

based on the Real Time Clock. The timer has the facility to activate its output for eight different

timings.

To implement this project, a LCD module of 2 line x 16 characters, or key matrix of 16 keys are

used for the user interaction. A real time clock is also included to get timing information for the

project . A buzzer is connected with the micon to indicate the output condition. In real, this

buzzer can be replaced by a relay or any other suitable output element.

Hardware:

This project, Programmable Timer, is based on the Real Time Clock device, PCF8583 along with

user interaction elements; 2 line x 16 character LCD module, a keypad with 16 keys. A buzzer is

also included to indicate the output status.

The LCD module is interfaced with the micon using 4 bit data bus to save the port lines. Micon’s

port lines are simulated for the IIC bus to interface the IIC bus based real time clock.

During the operation, the micon keeps comparing the current time with the set time. When the

time matches, the controller activates the output for the predefined time.

A buzzer is connected with the micon to indicate the output status.

Page 2: Demo8

2

Demo 8 - Programmable Timer

www.MightyMicons.com

Circuit:+5

V

+5V

+5V

+5V

+5V

+5V

+12

V

+5V

+5V

Key

boar

d

CN

C

NC

C

PR

OG

RA

M

NO

RM

AL

+5V

EX

01

23

45

67

89

Nex

tB

ack

Esc

Set

IIC R

TC P

CF8

583

A0

1 0

1K

LCD

GND1

VCC2

B.ADJ3

RS4

R/W5

E6

D07

D18

D29

D411D310

D512

D613

D714

0.1u

FC

1

33P

FC

4

20M

HZ

XT

AL1

33P

FC

3

0.1u

FC

2

R2

4K7

R1

4K7

JP1 1

23 JP

2

123

JP4

1 2 3

IN41

48D

1

CD

4010

6

U2

12

+ 4.7u

FC

6

CD

4010

6U

2

34

10K

R1

JP3

1 2 3

RE

SE

T

SW

1

R76

10K

R78

100K

+ -

BZ

1

BU

ZZ

ER

T5

BC

547

D4

IN41

48D

5IN

4148

C19

0.1u

FC

1810

PF

B1

3.6VBATT

U6

PC

F85

83

VDD8VSS

4

OS

C1

1

OS

C0

2

A0

3S

DA

5

SC

L6

INT

7

R28

1KD

3IN

4148

X1

32.7

68K

HZ JP

2

1 2 3

R8C

11/1

3U

1

P37

/TX

D10

/RX

D1

1

CN

VS

S2

RE

SE

T3

XO

UT

/P47

4

VSS5

XIN

/P46

6

VCC7

11

P16

/CLK

0

10P

15/R

XD

0

9

P17

/INT

1/C

NT

R0

8

P10

/KI0

15

P11

/KI1

14

P12

/KI2

13

P13

/KI3

12

P45

/INT

016

MO

DE

28

IVCC23

P30

/CN

TR

022

AVSS21

P31

/TZ

OU

T20

AV

CC

/VR

EF

19

P32

/INT

2/C

NT

R1

18

P33

/INT

3/T

CIN

17

P00

/AN

7/T

XD

1132

P01

/AN

631

P02

/AN

530

P03

/AN

429

P04

/AN

327

P05

/AN

226

P06

/AN

125

P07

/AN

024

Row

0

Row

1

Row

2

Row

3

Column0

Column1

Column2

Column3

P14

/TX

D0

Not

e: D

otte

d lin

es in

dica

te th

e re

quire

dco

nnec

tions

mea

nt fo

r th

e ap

plic

atio

n.

Page 3: Demo8

3

Demo 8 - Programmable Timer

www.MightyMicons.com

Connections:

1. Connect port lines P04 to P07 to D4 to D7 line of LCD.

2. Connect port lines P01, P02 and P03 to RS, R/W and E lines of LCD.

3. Connect port lines P10 to P13 to Column0 to Column3 of 4X4 matrix keyboard.

4. Connect port lines P14 to P17 to Row0 to Row3 of 4X4 matrix keyboard.

5. Connect P45 to SCL and P37 to SDA lines of IIC bus for RTC.

6. Connect Port line P32 to either a point LED to get visual output or to buzzer to get sound

output.

Functional Description:

In this module, a complete project for the programmable timer is provided. The programmable

timer has one output indication and user can set upto 8 timings.Two line by 16 characters LCD

is used for displaying the RTC data, 4 X 4 matrix keyboard is used to enter the user setting, IIC

RTC is used to maintain the calender and one LED/Buzzer is used for the output.

Registers Used:

PD0 - Port 0 Direction Register

PD1 - Port 1 Direction Register

PD3 - Port 3 Direction Register

PD4 - Port 4 Direction Register

PUR0 - Pull-up control register 0

KIEN - Key input enable register

KUPIC - Key input Interrupt control register

Page 4: Demo8

4

Demo 8 - Programmable Timer

www.MightyMicons.com

Port Direction Registers:

PD0 is set to H’FE to select the port lines P00 to P07 as output lines to control the LCD. PD1 is

loaded with H’0F to select port lines P14 to P17 as output lines to connect matrix keyboard. The

port line P45 is selected as output line by setting bit PD45 in PD4 register. The port line P32 is

selected as output line by setting bit PD32 in PD3 register.

PUR0 - Pull-up Control Register:

The bit PU02 is set to 1 to enable internal pull ups for port lines P10 to P13.

Page 5: Demo8

5

Demo 8 - Programmable Timer

www.MightyMicons.com

KIEN - Key Input Enable Register:

The register KIEN is loaded with H’55 to enable KI0 to KI3 key input interrupts and select falling

edge detection. The priority level 2 is selected for key input interrupt by moving H’02 to KUPIC

register.

KUPIC - Key Input Interrupt Control Register:

Page 6: Demo8

6

Demo 8 - Programmable Timer

www.MightyMicons.com

Software Description:

After reset, the following initialization are taken place before entering into the main loop.

1. Initialize the port line P32 as output line.

2. Read the user setting stored in RTC RAM area and place the same in the local variables.

3. Initialize the port lines P14 to P17 as output lines and enable KI0 to KI3 interrupts.

4. Initialize Port lines P01 to P07 as output line and initialize the LCD in 4 bit mode.

Key input interrupt will be generated for any key press in the keyboard. In the key input (KI0 to

KI3) interrupt service routine, the pressed key value is indentified.

In the main loop, the keyboard status is checked and the control goes to setting function if the

“SET” key is pressed. Otherwise the calender data are read from RTC and they are displayed in

the first line of LCD and the last read timing is compared with user set timing, then a buzzer will

be switched on if they are equal.

Page 7: Demo8

7

Demo 8 - Programmable Timer

www.MightyMicons.com

The files used in this module are listed below:

Files Description

Demo8.C

Main file for this module, will read and display the RTC data from

RTC and compares the user set timmings with the current time and then activates the output if they are equal and checks the

keyboard for user setting.

R8C1113_FE_SimIICRoutines.C

Basic routines to access the IIC bus such as Giving start bit, Stop

bit, reading a byte serially, sending a byte serially etc.

R8C1113_FE_SimIICRoutines.H Declarations of functions in R8C1113_FE_SimIICRoutines.c

R8C1113_FE_LCD_4Bit.CLCD routines to initialize LCD,

cursor on/off, display a message etc.

R8C1113_FE_LCD_4Bit.H Declarations of functions in R8C1113_FE_LCD_4Bit.C

R8C1113_FE_Keyboard.C

Keybaord routines to initialize Key input interrupts, I/O lines

used by keyboard and key input interrupt service routine.

R8C1113_FE_Keyboard.HDeclarations of functions in R8C1113_FE_Keyboard.C

Page 8: Demo8

8

Demo 8 - Programmable Timer

www.MightyMicons.com

The functions in the file “Demo8.C” and short descriptions are listed below:

Functions Description

main

Reads RTC data and display it on LCD. Compares the current time with the user set

timing and activates one output if they are equal. Checks the keyboard for "SET" key press.

Input: None. Output : None.

InitializeProgrammbleTimer

Initialize I/O lines, Keyboard, LCD and read user time setting in RTC

Input: None. Output : None.

ReadSetting

Reads the user time setting stored in RTC and place it in local variables.

Input: None. Output : None.

ReadRTCDataReads the current time from RTC device.

Input: None. Output : None.

DisplayRTCDataDisplays RTC data read from RTC on LCD.

Input : None. Output : None.

CompareTiming

Compares current time read from RTC with the user set data and activates the output if they are

equal. Input: None.

Output : None.

SwitchOnOutput

Switches on the output LED connected at port line P32 for a predefined time and displays on

status of output at 2nd line of LCD. Input: None.

Output : None.

ConvertBCDConverts a digit Hex number into BCD.

Input: 2 digit Hex data. Output : 2 digit BCD data.

Page 9: Demo8

9

Demo 8 - Programmable Timer

www.MightyMicons.com

Functions Description

DisplayLCD2DigitBCDDisplays the 2 digit Hex variable in BCD format

Input: 2 digit Hex data. Output : None.

GetSettingGets the user time setting through keyboard.

Input: None. Output : None.

StoreSettingStores the user setting to RTC RAM area.

Input: None. Output : None.

DisplayLCD4Digit

Displays a 4 digit data (Hour:Minute) on LCD. Input: Line number, Character Position and data

to be displayed in 4 digits. Output : None.

Get4DigitDataGets 4 digit data through keyboard from user.

Input: None. Output : Last pressed key code.

ReadByteRTCReads a byte from RTC.

Input: Address of the location. Output : Read data from RTC.

WriteByteRTCWrites a byte to RTC.

Input: Address and data. Output : None.

Page 10: Demo8

1 0

Demo 8 - Programmable Timer

www.MightyMicons.com

The functions in the file “R8C1113_FE_SimIICRoutines.C” and short descriptions are listed below:

Functions Description

PollAck

Sends the SlaveAck command and wait until slave acknowledges. This routine

can be used to sense the completion of a write cycle. Input: None.

Output : None.

SendOneByteIIC

Sends a byte (command or address) to the slave IIC device serially.

Input: Data to be send. Output : None.

StartBitGenerates a start condition for IIC bus.

Input: None. Output : None.

StopBitGenerates a stop condition for IIC bus.

Input: None. Output : None.

SlaveAckGets acknowledge from the slave.

Input: None. Output : None.

ReadOneByteIIC

Rends a byte from the slave IIC device serially.

Input: Data to be send. Output : None.

Page 11: Demo8

1 1

Demo 8 - Programmable Timer

www.MightyMicons.com

The functions in the file “R8C1113_FE_LCD_4Bit.C” and short descriptions are listed below:

Functions Description

DisplayLCD

Displays a message (16 Characters) on LCD on the given line number.

Input: Line number and message string. Output : None.

DisplayLCD2Digit

Displays the given 2 digit number on LCD at given location.

Input: Line number, character position and data.

Output : None.

CursorONMakes the cursor visible on LCD.

Input: None. Output : None.

CursorOFFHides the cursor.

Input: None. Output : None.

InitializeLCD

Initializes the I/O lines used by LCD and LCD in 4 bit mode. Clears the LCD.

Input: None. Output : None.

WriteDataLCDWrite a data byte to LCD.

Input: Data Byte. Output : None.

WriteCommandLCDWrite a command byte to LCD.

Input: Command Byte. Output : None.

Page 12: Demo8

1 2

Demo 8 - Programmable Timer

www.MightyMicons.com

The functions in the file “R8C1113_FE_Keyboard.c” and short descriptions are listed below:

Functions Description

ReadKeyboardStatusReturns the keyboard

Input: None. Output : Keyboard Status.

WaitForKeyPress

Waits for a key press and returns the key code of the pressed key.

Input: None. Output : Key Code.

ReadKeyCode

Reads and returns the last pressed key code without waiting for a key press.

Input: None. Output : Key Code.

InitializeKeyboard

Initializes the I/O lines used by keyboard and enables Key input (KI0 to KI3)

interrupts. Input: None.

Output : None.

ProcessKey_Int

Interrupt service routine for key input interrupt.

Input: None. Output : None.

Page 13: Demo8

1 3

Demo 8 - Programmable Timer

www.MightyMicons.com

Program Flow:For Main Loop

Start

1. In itia lize Port lineP32 as output line.

2. Initialize LCD in 4bit mode.

3. Initialize key inputin te r ru p t a n d I /Ol i n e s u s e d f o rkeyboard.

Check for "SET"key press.

Get new setting fromuser.

Yes

No

Read calender datafrom RTC

Display RTC data onLCD

Compare current timeread from RTC withuser set values andactivate the output if

they are equal.

Page 14: Demo8

1 4

Demo 8 - Programmable Timer

www.MightyMicons.com

For Setting Routine

Start

1 . D i s p l a y t h em e s s a g e " * *SETTING ** "on 1st line of LCD.

2 . D i s p l a y t h em essage "TIMING1 - > 2 2 2 2 " o n1st line of LCD.

1 . S w i t c h o n t h ecursor on LCD.

2. Initialize Loop Flagto 1.

Display setting countand correspondingo ld s e t h o u r a n dminute in 4 digits.

G e t the new 4 d ig itdata from user.

Is Last pressedkey = Next key?

Store the new setting andincrement setting count

No

Yes

Is Last pressedkey = Back key?

Decrement setting count

No

Yes

Is Last pressedkey = Esc key?

Clear Loop Flag

No

Yes

If Loop Flag = 1?Yes

No

Return

Page 15: Demo8

1 5

Demo 8 - Programmable Timer

www.MightyMicons.com

Execute Demo:

After reset, the program will display the date, month, year, hour, minute and seconds on first line

of LCD and the second line is kept blank as shown below:

“12JAN06 12:23:37

The program will check the current time with the user set time and activates the output if they

are equal. During output activation a message will be displayed in the second line of LCD as

shown below:

“12JAN06 12:23:37

RELAY IS ON ->xx “

Where xx is the predefined on time of the output and gets decremented for every second. when

it reaches zero the output will be deactivated and the second line of LCD will be cleared.

Page 16: Demo8

1 6

Demo 8 - Programmable Timer

www.MightyMicons.com

Use Topview Simulator to Verify the Design.

Open the project Demo8 in the R8C/Tiny System Simulator using Open Project option from

Project menu. The project window opens up along with the Demo8.c file. Use Build option from

Build menu to compile the project. An output window captures the compiler ouput.

Use Project -> Download Project from main menu to download the .mot file into the simulator’s

memory for simulation.

Do the settings to the LCD modules as shown. Connect LCD control and data lines to port

lines P01 to P07.

Page 17: Demo8

1 7

Demo 8 - Programmable Timer

www.MightyMicons.com

Make the setting for keyboard module as shown below:

Page 18: Demo8

1 8

Demo 8 - Programmable Timer

www.MightyMicons.com

Connect the port lines P45 and P37 to IIC lines SCL and SDA using IIC module setting andenable RTC function as shown below:

Connect one point LED to the port line P32 using LED module setting.

Then open the LCD, Keyboard and LED windows and arranage them as shown below.

Page 19: Demo8

1 9

Demo 8 - Programmable Timer

www.MightyMicons.com

Down load the program using Download Project command in Project menu.

Run the program using Go command in Run menu. The message,

The program continuously watches the time from RTC with the setting and activate the output

LED for 10 seconds if both timing are equal. After 10 seconds the output LED will be switched

off.