Demo 8 - Programmable Timer
Demo 8 - Programmable Timer
Introduction: This demo describes a complete project meant for industrial timing applications. This is a complete 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. Micons 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.
1
www.MightyMicons.com
Circuit:
LCD
+12V C1 0.1uF
+
1K BUZZER +5V
-
VCC
IVCC
23
1 2 3
Colum n0
Colum n1
+5V
1 2 3
JP2 P13/KI3 P12/KI2 M ODE P10/KI0 CNVSS P17/INT1/CNTR0 P16/CLK0 P15/RXD0 P14/TXD0 18 17 11 10 9 8 15 P11/KI1 14 13 12
Row3
Next
Colum n2
Back
PROGRAM
R2 4K7
28
JP4
1 2 3
Row 2
7
R28 1K
Colum n3
P45/INT0
16 7 6 5 INT SCL
SW 1
CD40106 U2
Keyboard
3.6VBATT
VDD
VSS
21
www.MightyMicons.com
P00/AN7/TXD11 P01/AN6 R76 30 29 27 26 100K R78 10K P02/AN5 XIN/P46 P03/AN4 P04/AN3 P05/AN2 XO UT/P47 P07/AN0 24 P06/AN1 25 31 32 T5 BC 547
JP1
NC
C3 33PF
XTAL1
20M HZ
Esc
C4 33PF
NC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
C2 0.1uF
+5V +5V
BZ1
Set
+5V
+5V
NORMAL
R1
4K7
D3 IN4148
R1 10K
D1 IN4148
Row 1
3
D5 IN4148 D4 IN4148 B1 C19 0.1uF C18 10PF
CD40106
Row 0
+5V
RESET
C6 4.7uF
19
JP3
1 2 3
EX
U1
R8C11/13
Note: Dotted lines indicate the required connections m eant for the application.
PCF8583
1
1 2 3
JP2
A0
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 PD1 PD3 PD4 KIEN - Port 0 Direction Register - Port 1 Direction Register - Port 3 Direction Register - Port 4 Direction Register - Key input enable register
PUR0 - Pull-up control register 0 KUPIC - Key input Interrupt control register
3
www.MightyMicons.com
PD0 is set to HFE to select the port lines P00 to P07 as output lines to control the LCD. PD1 is loaded with H0F 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.
4
www.MightyMicons.com
The register KIEN is loaded with H55 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 H02 to KUPIC register. KUPIC - Key Input Interrupt Control Register:
5
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.
6
www.MightyMicons.com
Files
Description 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.
Demo8.C
Basic routines to access the IIC bus such as Giving start bit, Stop R8C1113_FE_SimIICRoutines.C bit, reading a byte serially, sending a byte serially etc. Declarations of functions in R8C1113_FE_SimIICRoutines.c LCD routines to initialize LCD, cursor on/off, display a message etc. Declarations of functions in R8C1113_FE_LCD_4Bit.C Keybaord routines to initialize Key input interrupts, I/O lines used by keyboard and key input interrupt service routine. Declarations of functions in R8C1113_FE_Keyboard.C
R8C1113_FE_SimIICRoutines.H
R8C1113_FE_LCD_4Bit.C
R8C1113_FE_LCD_4Bit.H
R8C1113_FE_Keyboard.C
R8C1113_FE_Keyboard.H
7
www.MightyMicons.com
The functions in the file Demo8.C and short descriptions are listed below:
Functions Description 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. Initialize I/O lines, Keyboard, LCD and read user time setting in RTC Input: None. Output : None. Reads the user time setting stored in RTC and place it in local variables. Input: None. Output : None. Reads the current time from RTC device. Input: None. Output : None.
main
InitializeProgrammbleTimer
ReadSetting
ReadRTCData
DisplayRTCData
Displays RTC data read from RTC on LCD. Input : None. Output : None. Compares current time read from RTC with the user set data and activates the output if they are equal. Input: None. Output : None. 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. Converts a digit Hex number into BCD. Input: 2 digit Hex data. Output : 2 digit BCD data.
CompareTiming
SwitchOnOutput
ConvertBCD
8
www.MightyMicons.com
Functions
Description
DisplayLCD2DigitBCD
Displays the 2 digit Hex variable in BCD format Input: 2 digit Hex data. Output : None.
GetSetting
Gets the user time setting through keyboard. Input: None. Output : None.
StoreSetting
Stores the user setting to RTC RAM area. Input: None. Output : None. Displays a 4 digit data (Hour:Minute) on LCD. Input: Line number, Character Position and data to be displayed in 4 digits. Output : None. Gets 4 digit data through keyboard from user. Input: None. Output : Last pressed key code.
DisplayLCD4Digit
Get4DigitData
ReadByteRTC
Reads a byte from RTC. Input: Address of the location. Output : Read data from RTC.
WriteByteRTC
9
www.MightyMicons.com
The functions in the file R8C1113_FE_SimIICRoutines.C and short descriptions are listed below:
Functions Description 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.
PollAck
SendOneByteIIC
Sends a byte (command or address) to the slave IIC device serially. Input: Data to be send. Output : None.
StartBit
Generates a start condition for IIC bus. Input: None. Output : None.
StopBit
Generates a stop condition for IIC bus. Input: None. Output : None.
SlaveAck
Gets acknowledge from the slave. Input: None. Output : None. Rends a byte from the slave IIC device serially. Input: Data to be send. Output : None.
ReadOneByteIIC
10
www.MightyMicons.com
The functions in the file R8C1113_FE_LCD_4Bit.C and short descriptions are listed below:
Functions Description Displays a message (16 Characters) on LCD on the given line number. Input: Line number and message string. Output : None. Displays the given 2 digit number on LCD at given location. Input: Line number, character position and data. Output : None. Makes the cursor visible on LCD. Input: None. Output : None. Hides the cursor. Input: None. Output : None. Initializes the I/O lines used by LCD and LCD in 4 bit mode. Clears the LCD. Input: None. Output : None. Write a data byte to LCD. Input: Data Byte. Output : None. Write a command byte to LCD. Input: Command Byte. Output : None.
DisplayLCD
DisplayLCD2Digit
CursorON
CursorOFF
InitializeLCD
WriteDataLCD
WriteCommandLCD
11
www.MightyMicons.com
The functions in the file R8C1113_FE_Keyboard.c and short descriptions are listed below:
Functions
ReadKeyboardStatus
WaitForKeyPress
Waits for a key press and returns the key code of the pressed key. Input: None. Output : Key Code. Reads and returns the last pressed key code without waiting for a key press. Input: None. Output : Key Code. Initializes the I/O lines used by keyboard and enables Key input (KI0 to KI3) interrupts. Input: None. Output : None. Interrupt service routine for key input interrupt. Input: None. Output : None.
ReadKeyCode
InitializeKeyboard
ProcessKey_Int
12
www.MightyMicons.com
Start
1 . In itia lize Po rt lin e P32 as output line. 2. Initialize LC D in 4 bit m ode. 3. Initialize key input inte rru p t a n d I/O lines used for keyboard.
Yes
No
Com pare current tim e read from RTC with user set values and activate the output if they are equal.
13
www.MightyMicons.com
Start
1. Display the message " ** " SETTING ** on 1st line of LCD. 2. Display the m essage " T I M I N G 1 -> 2222" on 1st line of LCD.
No
1. Switch on the cursor on LCD. 2. Initialize Loop Flag to 1. Is Last pressed key = Esc key? Display setting count and corresponding o ld s e t ho u r a n d m inute in 4 digits. No
If Loop Flag = 1? Is Last pressed key = Next key? No No Yes Store the new setting and increm ent setting count
Yes
Return
14
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.
15
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 simulators memory for simulation.
Do the settings to the LCD modules as shown. Connect LCD control and data lines to port lines P01 to P07.
16
www.MightyMicons.com
17
www.MightyMicons.com
Connect the port lines P45 and P37 to IIC lines SCL and SDA using IIC module setting and enable 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.
18
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.
19
www.MightyMicons.com