Top Banner
i
88
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 826_MOHDBASRIBINOTHMAN2010

i

Page 2: 826_MOHDBASRIBINOTHMAN2010

ii

Page 3: 826_MOHDBASRIBINOTHMAN2010

i

DIGITAL PRAYER CLOCK

MOHD BASRI BIN OTHMAN

A thesis submitted in fulfillment of the requirement for the award of degree of Bachelor

of Engineering (Electrical - Control and Instrumentation)

Faculty of Electrical Engineering

Universiti Teknologi Malaysia

APRIL 2010

Page 4: 826_MOHDBASRIBINOTHMAN2010

ii

Page 5: 826_MOHDBASRIBINOTHMAN2010

iii

DEDICATION

Dedicated, in thankful appreciation for support, encouragement and understandings to

my beloved mother and father.

Page 6: 826_MOHDBASRIBINOTHMAN2010

iv

ACKNOWLEDGEMENT

First and foremost, I would like to express my deepest gratitude to my

supervisor, Associate Professor Zamani bin Md. Zain for the guidance, enthusiasm and

motivation given throughout the progress of this project

I would also like to thank my parents for always be there to support me all the

time and give me the courage and strength that are necessary for me to carry on with this

project. Thanks for their encouragement, love and emotional supports that they had give

to me.

Page 7: 826_MOHDBASRIBINOTHMAN2010

v

ABSTRACT

At present, there are various types of electronics display devices used to display

information. For example, a device that can display the right prayer time for a place.

This project is designed to display the five Islamic prayer times, direction Qiblat, and

also the date and current time to be a reference to the user. The procedure of operation is

designed to be simple. That user have to set the date, time, longitude and latitude, and

then prayer times will be displayed on the LCD display, together with the Qiblat

displayed on the compass. The project is designed and developed using a

microcontroller of the PIC 16F877A and is programmed using C language.

Page 8: 826_MOHDBASRIBINOTHMAN2010

vi

ABSTRAK

Pada masa kini, terdapat pelbagai jenis alat paparan elektonik digunakan untuk

memaparkan pelbagai maklumat. Sebagai contoh, suatu alat yang boleh memaparkan

waktu solat yang tepat untuk sesuatu tempat. Projek ini direkabentuk untuk memaparkan

lima waktu solat, arah Qiblat, dan juga alat untuk memaparkan jam dan tarikh untuk

menjadi rujukan kepada pengguna. Cara penggunaannya adalah mudah sahaja, dengan

hanya memasukkan tarikh, masa, longitude dan latitude, dan kemudiannya waktu solat

akan dipaparkan pada LCD Display bersertakan arah Qiblat dipaparkan melalui

compass. Projek ini direkabentuk dan dibangunkan menggunakan mikropengawal jenis

PIC 16F877A dan diprogramkan menggunakan bahasa C.

Page 9: 826_MOHDBASRIBINOTHMAN2010

vii

TABLE OF CONTENTS

CHAPTER TITLE PAGE

TITLE PAGE i

DECLARATION ii

DEDICATION iii

ACKNOWLEDGMENTS iv

ABSTRACT (ENGLISH) v

ABTRACT (BAHASA MELAYU) vi

TABLE OF CONTENTS vii

LIST OF TABLES x

LIST OF FIGURES xi

LIST OF SYMBOLS xiii

I INTRODUCTION

1.1 Prayer (Solat) 1

1.1.1 Definition 1

1.1.2 Social aspect of Solat 3

1.1.3 Types of Solat 3

1.1.4 Times of Solat 4

1.1.5 Qiblat Direction 4

1.2 Objective 5

1.3 Work Scope 5

Page 10: 826_MOHDBASRIBINOTHMAN2010

viii

1.4 Problem Statements 6

II CONCEPT AND THEORY

2.1 Introduction 8

2.2 Dalil from A-Quran & Hadith on Prayer Time 9

2.3 Definition of prayer times 11

2.4 Calculation of Solat Times 12

2.4.1 Twilight 12

2.4.2 Astronomical Measures 14

2.4.3 Calculating Prayer Times 16

III METHODOLOGY

3.1 Introduction 19

3.2 Main Process 20

3.3 Hardware Development Process 21

3.4 Software Development Process 22

IV HARDWARE DESIGN AND DEVELOPMENT

4.1 Hardware components 24

4.1.1 PIC 16F877A Microcontroller 24

4.1.2 2x 16 Characters LCD 26

4.1.3 DS1307 Real Time Clock Chip (RTC) 29

4.1.4 SK40C PIC Start-Up Kit 30

4.2 Voltage regulator circuit 31

4.3 Circuit Design 32

V SOFTWARE DESIGN AND DEVELOPMENT

5.1 Programming Language 34

5.1.1 C programming 34

5.1.2 Assembly language 35

Page 11: 826_MOHDBASRIBINOTHMAN2010

ix

5.2 Software Development Environment 35

5.2.1 MPLAB IDE 36

5.2.2 HI-TECH PICC-Lite 37

5.2.3 Home Made Programmer 38

5.2.4 Isis 7 Professional 38

5.3 Coding writing 39

VI RESULTS AND DISCUSSION

6.1 Results 42

6.2 Programming 43

6.3 Hardware 43

6.3.1 Power regulator circuit 44

6.3.2 PIC Basic circuit 45

6.3.3 LCD Display circuit 46

6.3.4 RTC circuit 47

6.4 Problems and discussion 47

VII CONCLUSION AND FURTHER WORKS

7.1 Introduction 49

7.2 Strength and Weakness 50

7.3 Suggestion for Future Work 51

REFERENCES 53

APPENDIX 55

Page 12: 826_MOHDBASRIBINOTHMAN2010

x

LIST OF TABLES

TABLE NO. TITLE PAGE

4.1 LCD connection pins and function of each pin 27

4.2 Parallel LCD Protocol 28

4.3 LCD Command Bit function and value 29

Page 13: 826_MOHDBASRIBINOTHMAN2010

xi

LIST OF FIGURES

FIGURE NO. TITLE PAGE

2.1 Twilight 13

2.2 Graph of Twilight illumination versus Solar Depression 13

2.3 The Equation of Time 15

2.4 The Declination of the Sun 15

3.1 Flow chart Main Process 20

3.2 LCD Display component requirements 21

4.1 PIC 16F877A 25

4.2 PIC16F877A pin configuration 26

4.3 LCD Display 27

4.4 DS1307 30

4.5 SK40C PIC Start-Up Kit 31

4.6 Voltage regulator circuit 31

4.7 LCD Display circuit 32

4.8 RTC Display circuit 33

5.1 MPLAB IDE 36

5.2 HI-TECH PICC-Lite Compilers 37

5.3 Application windows Isis Professional 39

6.1 The overall Digital Praying circuit 44

6.2 Power Regulator circuit 45

Page 14: 826_MOHDBASRIBINOTHMAN2010

xii

6.3 PIC Basic circuit 46

6.4 LCD Display circuit 46

6.5 RTC circuit 47

Page 15: 826_MOHDBASRIBINOTHMAN2010

xiii

LIST OF SYMBOLS

B - Latitude of place

L - Longitude of place

R - Reference longitude (i.e. TIME BAND x 15)

H - Height above sea level in meters

D - Declination angle of sun from celestial equator

T - Equation of time

G - Twilight angle

Page 16: 826_MOHDBASRIBINOTHMAN2010

1

CHAPTER I

INTRODUCTION

This chapter discusses the structure of this project, the prerequisites and the

importance in developing the device. These questions will be discussed in details in this

chapter and the other chapters as we move on. The objectives of this thesis and a brief

on some devices and appliances related will also be presented in this chapter.

1.1 Prayer (Solat)

1.1.1 Definition

Salat (Arabic: ص�ة ; pl. ṣalawāt) is the name given to the formal prayer of Islam.

Its supreme importance for Muslims is indicated by its status as one of the obligations

Five Pillars of Sunni Islam and of the Ten Practices of the Religion of Shi'a Islam. Solat

Page 17: 826_MOHDBASRIBINOTHMAN2010

2

is a ritual prayer, having prescribed conditions, a prescribed procedure, and prescribed

times.

Performing solat is obligatory on all Muslim adults. To perform valid solat,

Muslims must be in a state of ritual purity, which is mainly achieved by ritual ablution

according to prescribed procedures. The place of prayer should be clean. In a few cases

where blood is leaving the body, solat is forbidden until a later time.

Solat consists of the repetition of two or more units of a prescribed sequence of

actions and words. One complete sequence is known as a rak'ah. The number of

obligatory (fard) rak'at varies according to the time of day or other circumstances

requiring solat (such as Friday congregational prayers). Additions to the fard rak'at can

be made, again in different multiples according to the circumstances. These are not

required, but are considered meritorious. There are also dispensations from some or all

of the prescribed actions for those who are physically unable to complete them. The

prescribed words of the prayer remain obligatory.

Solat is prescribed at five periods of the day, which are measured according to

the movement of the sun. These are: near dawn (Subuh), after the sun's noon (Zuhur), in

the afternoon (Asar), just after sunset (Maghrib) and around nightfall (Isyak). (The

Islamic day begins at sundown.) Under some circumstances prayers can be shortened or

combined (according to prescribed procedures). Prayers can be missed in serious cases,

but they should be made up later [13].

Page 18: 826_MOHDBASRIBINOTHMAN2010

3

1.1.2 Social aspect of Solat

Solat has both individual and social aspects. Solat properly offered cleanses and

expiates sins. Solat is a direct communication between the worshipper and Allah. The

actions of qiyaam, sujuud, and rukuu’ are all direct actions of respect and reverence that

the worshipper performs in front of the Creator. Solat in congregation or in the mosque

has the social advantage of bringing people together in worship and to strengthen their

brotherhood. The spirit of brotherhood and social organization is reflected in the

requirement to straighten rows (saff), to stand shoulder- to-shoulder and to follow the

imam.

1.1.3 Types of Solat

Some prayers are obligatory like the 5 daily prayers, (i.e. Subuh, Zuhur, Asar,

Maghrib, and Isyak), Solat Jumaat, and Solat Jenazah. Many more are voluntary or

supererogatory for example Solat Tahajjud, Solat Tarawih, Solat Eid, and Solat Witr.

Some Solat is for special reasons for example the Solat of fear (Solat Khawf) and Solat

Istikharah. Obligatory prayers have fixed times and the best of ‘Ibadat’ is prayer in its

time.

Page 19: 826_MOHDBASRIBINOTHMAN2010

4

1.1.4 Times of Solat

Solat times are set in such a way that there is continuous communication with the

Creator. There are times in which solat cannot be offered. Solat is not allowed at sunrise

and sunset to avoid confusion with worshippers of the sun. The obligatory prayers need

to be performed during specific time. Currently, astronomical theories based Quran and

Hadith being used on deciding each time prayer [1].

“When ye pass (Congregational) prayers, celebrate Allah’s praises,

standing, sitting down, or lying down on your sides; but when ye are free

from danger, set up Regular Prayers: For such prayer are enjoined on

believers at stated times.”

(Surah al-Nisaa’; 103)

1.1.5 Qiblat Direction

Qiblat are the direction of the Muslim to pray which is the direction of Kaabah.

The direction of Qiblat moves which previously from Masjid al-Aqsa to Masjid Nabawi

in Mekah. Astronomical theories and calculations based on Quran and Hadith are also

being used to determine Qiblat direction for each place around the world [4].

Page 20: 826_MOHDBASRIBINOTHMAN2010

5

“We see the turning of thy face (for guidance) to the heavens: now Shall

We turn thee to a Qiblat that shall please thee. Turn then Thy face in the

direction of the sacred Mosque: Wherever ye are, turn your faces in that

direction. The people of the Book know well that is the truth from their

Lord. Nor is Allah unmindful of what they do.”

(Surah a-Baqarah: 144)

1.2 Objective

The objective of this project is to assist Muslim in doing their prayers by

developing a portable, real-time micro-controller based Prayer time and Qiblat finder

device.

1.3 Work Scope

Scope and guidelines are listed to ensure the project is conducted within its

intended boundary. This is to ensure the project is heading in the right direction to

achieve its objectives.

Page 21: 826_MOHDBASRIBINOTHMAN2010

6

The first scope of this project is to implement a device that can find time prayer

and Qiblat direction using key-in implementation or Global Positioning System as an

option.

Secondly, device is build based on a PIC16F877A microcontroller, a digital

compass, keypads, LED and LCD Display. The Qiblat and the Prayer Time algorithm

will be implemented on the EEPROM.

The development of the application is made using C programming, a high level

language to communicate the software part and the hardware part. MPLAB IDE and HI-

TECH C PRO, software from Microchip will be used in checking and compiling the

algorithm before it is ready to be the PIC microcontroller.

1.4 Problem Statements

It is hard to determine the Qiblat direction and know the prayer time of each

prayer. In the old days, people use stick and sees it shadow to know the prayer times and

determine the Qiblat direction. Nowadays, researches and findings in astronomical help

much in computing the prayer time and the direction of Qiblat automated.

There are certain applications in the websites that are available to the user to

know Qiblat direction and prayer times. They have to insert information on current

location, and the Qiblat and prayer information will be given. As the technologies grow,

Page 22: 826_MOHDBASRIBINOTHMAN2010

7

various mobiles company gives prayer time and Qiblat direction services. The problem

is that applications itself are not portable, self generated but rely on the service itself.

Services from websites need the user to have Internet connections making it not practical

for daily use.

People commute everyday, moving from one place to another and they need to

know the time and right Qiblat direction in the current place they go. There are devices

like ‘Travelers Watch’ and ‘Qiblat Finder’ that can help travelers by showing time and

direction according to their latitude and longitude. The problem is these applications do

not come from a single device. To determine the Qiblat and prayer time user needs two

different devices.

Some people use prayer list or calendar to know the prayer time, and manual

compass to know the direction of Qiblat. This method is quite cumbersome for travelers

since a person has to know the latitude and longitude of the current location.

A device that can combine this two main function to solve problems when

moving from a place to another is needed. This device is reliable, user friendly and easy

to bring to any location.

Page 23: 826_MOHDBASRIBINOTHMAN2010

8

CHAPTER II

CONCEPT AND THEORY

2.1 Introduction

The five Islamic prayers are Subuh, zuhur, Asar, Maghrib and Isyak. The timing

of these five prayers varies from place to place and from day to day. It is obligatory for

Muslims to perform these prayers at the correct time.

The prayer times for any given location on earth may be determined

mathematically if the latitude and longitude of the location are known. However, the

theoretical determination of prayer times is a lengthy process. Much of these difficulties

may be alleviated by using computer programs [6].

Page 24: 826_MOHDBASRIBINOTHMAN2010

9

2.2 Dalil from A-Quran & Hadith on Prayer Time

There are Quran is quotes [1] define the importance of performing prayer in the

specific time and to be compulsory to follow the prayer time, (which means):

“When ye have performed the act of worship, remembers Allah, standing,

sitting and reclining. And when ye are in safety, observe proper worship.

Worship at fixed times hath been enjoined on the believers.”

(Surah An-Nisaa’; ayat 103)

“Therefore be patient with what they say, and celebrate (constantly) the

praises of thy Lord, before the rising of the sun, and before its settling;

yea, celebrate them for part of the hours of the night, and at the sides of

the day: that thou mayest have (spiritual)”

(Surah Toha; ayat 130)

Hadith is quotes [14] related that verify the prayer time which means: Ibn Abbas

said, The Messenger of Allah, peace and blessings of Allah be on him, said:

“Gabriel acted as imam for me twice in the (Sacred) House; so he said the

Zuhur prayer with me when the sun had declined from the meridian and

(the shadow) was the measure of a thong, and he said the ‘Asar prayer

with me when the shadow of everything was the like of it, and he said

Page 25: 826_MOHDBASRIBINOTHMAN2010

10

Maghrib prayer with me when one who fasts breaks the fast, and he said

the ‘Isyak prayer with me when redness in the horizon had disappeared,

and he said the Subuh prayer with me when food and drink are prohibited

to one who fasts. When it was the next day, he said with me the Zuhur

prayer when the shadow (of a thing) was the like of it, and he said with me

the ‘Asar prayer when the shadow (of a thing) was its double, and he said

with me the Maghrib prayer when one who fasts breaks the fast, and he

said with me the ‘Isyak prayer when one-third of the night had passed, and

he said with me the Subuh prayer when the dawn was bright. Then he

turned to me and said, O Muhammad! This is the time of the Prophets

before thee, and the time is between these two times.”

(AD., Tr-Msh. 4:1.)

This is a comprehensive Hadith relating to the times of prayer. It further shows

that the mode of prayer, the rak’ahs and the time thereof were taught to the Holy Prophet

by Divine revelation, as it was Gabriel who led the prayers and the Holy Prophet

followed him. Gabriel pointed out the time-limits within which different prayers could

be said. The different times thus are: Zuhur--when the sun begins to decline till ‘Asar;

‘Asar--when the sun is about midway on its course to setting till it begins to set;

Maghrib--after the sun sets till the disappearance of the red glow in the west; ‘Isyak’—

after the disappearance of the red glow till midnight (as other Hadith show); Subuh—

after dawn till sunrise. Where the days are too short, the Zuhur and the ‘Isyak’ may be

combined (h. 6.). Where the days or nights extend over 24 hours, the times of prayer

may be fixed in accordance with the times of work and rest. Thus the Morning Prayer

may be said on rising from sleep, the Maghrib and the ‘Isyak when going to bed, and the

Zuhur and ‘Asar in the middle of the day’s work.

Page 26: 826_MOHDBASRIBINOTHMAN2010

11

2.3 Definition of prayer times

Each prayer has to be performed in its specified time. To determine the exact

time period for each prayer, we need to determine seven points of time per a day. The

definition of each time based in Al-Quran and Hadith are as follows [7]:

� SUBUH starts with the dawn or morning twilight. Subuh ends just before

sunrise.

� SUNRISE is the time at which the first part of the sun appears above the

horizon.

� ZUHUR begins after midday when the trailing limb of the sun has passed the

meridian. For convenience, many published prayer timetables add five minutes

to mid-day (zawal) to obtain the start of Zuhur. Zuhur ends at the start of Asar

time.

� The timing of ASAR depends on the length of the shadow cast by an object.

According to the Shafi school of jurisprudence, Asar begins when the length of

the shadow of an object exceeds the length of the object. According to the Hanafi

school of jurisprudence, Asar begins when the length of the shadow exceeds

TWICE the length of the object. In both cases, the minimum length of shadow

(which occurs when the sun passes the meridian) is subtracted from the length of

the shadow before comparing it with the length of the object.

Page 27: 826_MOHDBASRIBINOTHMAN2010

12

� SUNSET is the time at which the sun disappears below the horizon.

� MAGHRIB begins at sunset and ends at the start of Isyak.

� ISYAK starts after dusk when the evening twilight disappears.

2.4 Calculation of Solat Times

2.4.1 Twilight

If the earth did not have an atmosphere, the sky would become dark immediately

after sunset. The earth's atmosphere causes scattering of sunlight so that light reaches the

observer before sunrise and after sunset. This scattered light is called twilight. After

sunset, as the depression of the sun increases the sky gets darker and darker until no

scattered light reaches the observer. Conversely, in the morning light starts to appear in

the sky even before sunrise. The morning twilight is called dawn whilst the evening

twilight is known as dusk [6].

Page 28: 826_MOHDBASRIBINOTHMAN2010

13

Figure 2.1: Twilight [6]

In astronomy the twilight period is divided into civil, nautical and astronomical

twilight corresponding to solar depressions of 6, 12 and 18 degrees respectively.

Figure 2.2: Graph of Twilight illumination versus Solar Depression [6]

i. Civil twilight - roughly equivalent to lighting up time. The brightest stars are

visible and at sea the horizon is clearly visible.

Page 29: 826_MOHDBASRIBINOTHMAN2010

14

ii. Nautical twilight - the horizon at sea ceases to be clearly visible and it is

impossible to determine altitudes with reference to the horizon.

iii. Astronomical twilight- when it is truly dark and no perceptible twilight remains.

2.4.2 Astronomical Measures

There are two astronomical measures that are essential for computing prayer

times. These two measures are the equation of time and the declination of the Sun.

The equation of time is the difference between time as read from a sundial and a

clock. It results from an apparent irregular movement of the Sun caused by a

combination of the obliquity of the Earth's rotation axis and the eccentricity of its orbit.

The sundial can be ahead (fast) by as much as 16 min 33 s (around November 3) or fall

behind by as much as 14 min 6 s (around February 12) [7].

Page 30: 826_MOHDBASRIBINOTHMAN2010

15

Figure 2.3: The Equation of Time [7]

The declination of the Sun is the angle between the rays of the sun and the plane

of the earth equator. The declination of the Sun changes continuously throughout the

year. This is a consequence of the Earth's tilt, i.e. the difference in its rotational and

revolutionary axes.

Figure 2.4: The Declination of the Sun [7]

Page 31: 826_MOHDBASRIBINOTHMAN2010

16

2.4.3 Calculating Prayer Times

To calculate the prayer times for a given location, we need to know the latitude

(L) and the longitude (Lng) of the location, along with the local Time Zone for that

location. We also obtain the equation of time (EqT) and the declination of the Sun (D)

for a given date using the algorithm mentioned in the previous section.

Declination is the angular distance between a celestial object and the celestial

equator. The Declination and the Right Ascension are used together to give the position

of a star with reference to the celestial equator and the vernal equinox respectively.

The equation of time is a correction to be added to apparent solar time, as read on

a sundial, to obtain means solar time, as commonly used. This difference is a

consequence of the elasticity and tilt of the Earth's orbit, causing the irregular apparent

movement of the Sun across the sky.

D and T vary according to the time of year and can be obtained accurately from

The Star Almanac or calculated approximately.

The Equations to calculate the prayer times [6]:

…….1

Page 32: 826_MOHDBASRIBINOTHMAN2010

17

…….2

…….3

. …….4

…….5

Where:

B= latitude of place

L= longitude of place

R= reference longitude (i.e. TIME BAND x 15)

H= height above sea level in meters

D= declination angle of sun from celestial equator (-ve in southern hemisphere)

T= equation of time

G= twilight angle

� Fajr /Subuh= Z-V

� Sunrise = Z-U

� Zuhur = Z

� Asar1 (Shafi) = Z+W

� Asar2 (Hanafi) = Z+X

� Maghrib/Sunset = Z+U

� Isyak = Z+V

Page 33: 826_MOHDBASRIBINOTHMAN2010

18

The algorithms to calculate T and D are not shown here. Zuhur time is calculated

using equation 1. The time for sunrise and sunset/ Maghrib may be calculated by

subtracting or adding 'U'(obtained from equation 2) to the Zuhur time respectively.

Subuh and Isyak times may be calculated by subtracting or adding 'V' (obtained

from equation 3) to the Zuhur time respectively. The term G (twilight angle) in equation

3 is usually set to 18 degrees. For a location with extreme latitude, days in summer may

be so long that twilight persists between sunset and the next sunrise. Under these

circumstances, 'V' is undefined and Subuh and Isyak have to be determined using agreed

principles of Fiqh.

The start of Asar time (Shafi) may be obtained by adding 'W' (obtained from

equation 4) to Zuhur; Asar time (Hanafi) is calculated by adding 'X' (obtained from

equation 5) to Zuhur.

Zuhur, Asar (Shafi and Hanafi) and Maghrib times may be calculated

unambiguously. Subuh and Isyak times depend on twilight and require the adoption of a

suitable twilight angle. Generally, a twilight angle of 18 degrees may represent a safe

upper limit although opinions and practices vary. In locations at higher latitude, twilight

may persist throughout the night during some months of the year - here alternative

methods such as 1/7th night rule must be used to calculate Subuh/Isyak times [6].

Page 34: 826_MOHDBASRIBINOTHMAN2010

19

CHAPTER III

METHODOLOGY

3.1 Introduction

Methodology is an organized, documented set of procedures and guidelines for

one or more phases of the project that is analysis or design. This methodology include a

flow chart notation for documenting the results of the procedure; a step-by-step

approach for carrying out the procedure; and an objective (ideally quantified) set of

criteria for determining whether the results of the procedure are of acceptable quality.

Firstly, I studies the specification for Digital Prayer Clock. This project is to

show Prayer time, Qiblat direction, and the clock and date. The prayer time and Qiblat

direction for any given location on earth will determine mathematically with insert the

value of latitude and longitude of the location and the date. After that, the prayer time

and Qiblat direction will appear on the LCD screen. In addition a device to display clock

and date is also designed as a guide and reference to the user. For this project I using

Page 35: 826_MOHDBASRIBINOTHMAN2010

20

controller PIC 16F877A to controller the whole of the system these Digital Prayer

Clock.

3.2 Main Process

Figure 3.1: Flow chart Main Process

Literature Review

Select device

Design and Implement Hardware

End Product

(Digital Praying Clock)

Software programming

Verification

Testing

Troubleshoot and repair bugs

Error

Page 36: 826_MOHDBASRIBINOTHMAN2010

21

Figure 3.1 shows the main process of the Digital Praying Clock device.

Literature reviews are done to study all devices related followed by selecting device

needed. The components that are selected were based on its features and cost.

Prototype Board is used in designing and implementing the hardware before

programming the codes in software. Testing and verifications need to be carried out to

ensure the results are reliable and achieves the objective of the project.

3.3 Hardware Development Process

The development process of the hardware was done designed using PIC16F877A

Microcontroller as the controller to control all the system Digital Praying Clock. Then

the circuit of 2x16 Character LCD is use to display prayer time, clock and date. The

compass is use to show the direction of Qiblat.

Figure 3.2: LCD Display component requirements [10]

Page 37: 826_MOHDBASRIBINOTHMAN2010

22

3.4 Software Development Process

The development process of the software was done using The Spiral Model. The

model originally proposed by Boehm [BOE88] is an evolutionary software process

model that couples the iterative nature of prototyping with the controlled and systematic

aspects of the linear sequential model. A Spiral Model is divided into a number of

framework activities also called as task regions. In this project, the model is divided to

four main phase and consist of 6 task regions. The developing phases are as follows:

� Phase 1: Conceptualization and Planning Phase

This phase is where the planning of the project is done. The specifications that

are being taken are the main objective, the project scope, time frame of the

development process and sources needed in accomplishing the projects.

� Phase 2: Development process

After deciding the concept and prerequisites of the projects, this implementation

phase is done. The software design was carried out by coding the program and

this phase is very time consuming.

Phase 3: Troubleshooting

� The software and the hardware are implemented together. Debugging is carried

out to overcome any logical flaws in algorithm implementation.

Page 38: 826_MOHDBASRIBINOTHMAN2010

23

� Phase 4: Testing and verification

Testing and verification have to be made to ensure the software is reliable. Data

comparisons with the standard output from other sources are applied.

Page 39: 826_MOHDBASRIBINOTHMAN2010

24

CHAPTER IV

HARDWARE DESIGN AND DEVELOPMENT

4.1 Hardware components

4.1.1 PIC 16F877A Microcontroller

PIC 16F877A microcontroller produced by Microhip was chosen as the

processor unit to control operations and the operations of the project. This powerful (200

nanosecond instruction execution) yet easy-to-program (only 35 single word

instructions) CMOS FLASH-based 8-bit microcontroller packs Microchip's powerful

PIC® architecture into an 40- or 44-pin package and is upwards compatible with the

PIC16C5X, PIC12CXXX and PIC16C7X devices. Features of the device [10]:

• 256 bytes of EEPROM data memory

• Self programming

• 2 Comparators

Page 40: 826_MOHDBASRIBINOTHMAN2010

25

• 8 channels of 10-bit Analog-to-Digital (A/D) converter

• Capture/compare/PWM functions

• Synchronous serial port can be configured as either 3-wire Serial Peripheral

Interface (SPI™) or the 2-wire Inter-Integrated Circuit (I²C™) bus

• Universal Asynchronous Receiver Transmitter (UART).

All of these features make it ideal for more advanced level A/D applications in

automotive, industrial, appliances and consumer applications.

Figure 4.1: PIC 16F877A

Figure 4.1 shows the real picture PIC 16F877A microcontroller. It has a 40 pin output.

Page 41: 826_MOHDBASRIBINOTHMAN2010

26

Figure 4.2: PIC16F877A pin configuration

Figure 4.2 show pin configurations produce PIC 16F877A microcontroller.

4.1.2 2x 16 Characters LCD

2x16 Character LCD is the most common message display device to display any

alphabet and numerical characters. By using the SK40C, user is able to complete the

circuit for this project easily.

Page 42: 826_MOHDBASRIBINOTHMAN2010

27

Figure 4.3: LCD Display [10]

Table 4.1: LCD connection pin and function of each pin [10]

Page 43: 826_MOHDBASRIBINOTHMAN2010

28

User can control the LCD using either with 8 pins (8- bit interface) or 4 pins (4-

bit interface), depending on the I/O pins users have. For 16F877A which has 33 I/O

pins, there should be no problem in using the 8-bit interface which is much easier. The

pin 15 and 16 is for LCD backlight which is optional for you whether you want the light

to be switched on. FYI, switching on the backlight of LCD will cause your battery to dry

up rapidly. The 3rd pin (VEE) needs to be connected to a variable resistor to control the

LCD contrast, while the R/W (read or write) pin is connected to the ground. R/S and E

are connected to RD2 and RD3 to your PIC respectively. For data communication pins,

D0-D7 (8- bit) is connected directly to RB0-RB7 of your PIC [10].

Table 4.2: Parallel LCD Protocol [10]

Before display any numeric or alphabet, some initialize of LCD is needed. Table

4.1 is LCD protocol/functions which is need to initiate before use LCD and table 2 is

LCD command bit function and the value. Users need both tables to initiate LCD

display.

Page 44: 826_MOHDBASRIBINOTHMAN2010

29

Table 4.3: LCD Command Bit function and value [10]

To initiate LCD, R/S must be set to 0 as a command. R/S is set to 1 to write a

character to LCD display. User will initiate LCD display in program with refer to Table

4.1 and Table 4.2.

4.1.3 DS1307 Real Time Clock Chip (RTC)

RTC is to develop a clock/calendar system. DS1307 will provide the information

of seconds, minutes, hours, day, date, month and year. This information will be read by

the PIC 16F877A and displayed on LCD.

Page 45: 826_MOHDBASRIBINOTHMAN2010

30

Figure 4.4: DS1307

The DS1307 serial real-time clock (RTC) is a low power, full binary-coded

decimal (BCD) clock/calendar plus 56 bytes of NV SRAM chip. Address and data are

transferred serially through an I2C, bidirectional bus. The clock/calendar provides

seconds, minutes, hours, day, date, month, and year information. The end of the month

date is automatically adjusted for months with fewer than 31 days, including corrections

for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM

indicator. The DS1307 has a built-in power-sense circuit that detects power failures and

automatically switches to the backup supply. Timekeeping operation continues while the

part operates from the backup supply [11].

4.1.4 SK40C PIC Start-Up Kit

SK40C is enhanced version of SK40B. It is designed to offer an easy to start

board for PIC MCU user. This board comes with basic element for user to begin project

development. It offer plug and use features. Users are able to utilize the function of PIC

by directly plugging in the I/O components in whatever way that is convenient to user.

Page 46: 826_MOHDBASRIBINOTHMAN2010

31

This kit comes without PIC microcontroller to provide the freedom for user to choose

PIC type.

Figure 4.5: SK40C PIC Start-Up Kit [11]

4.2 Voltage regulator circuit

Figure 4.6: Voltage regulator circuit

Page 47: 826_MOHDBASRIBINOTHMAN2010

32

User can choose either to use the AC to DC adaptor or 9V-12V battery to power

up the circuit. Higher input voltage will produce more heat at LM7805 voltage regulator.

For PIC16F877A, it requires a voltage of 5V to enable it to enter the work; if the input

voltage exceeds 5V PIC will be destroyed. Thus, a 5V voltage regulator circuit is

required.

4.3 Circuit Design

I use Proteus 7 Professional software to design the circuit LDC display, power

regulator and RTC. Figure 4.8 and 4.9 shows the circuit designed by using Proteus 7

Professional.

Figure 4.7: LCD Display circuit

Page 48: 826_MOHDBASRIBINOTHMAN2010

33

Figure 4.8: RTC Display circuit

Page 49: 826_MOHDBASRIBINOTHMAN2010

34

CHAPTER V

SOFTWARE DESIGN AND DEVELOPMENT

5.1 Programming Language

5.1.1 C programming

The software part presented will be in C high-level language. C programming

actually provides more convenience for new users by minimizing the need to understand

the detail architecture of the microcontroller itself (data maps, file registers) and stresses

more on the programming algorithms. This mean that C programming can help the user

to focus on thinking how to make a functional routine to run the code smoothly rather

then spend time thinking of where to put the data and code.

With C high-level programming, users will only need to know how to write the

codes rather than to knowing how the microcontroller executes codes to desired result.

Though it is inevitable that it is still a need to understand the architectural structures of

Page 50: 826_MOHDBASRIBINOTHMAN2010

35

registers and data file of the microcontroller, users can make use the structure of C

programming language to spare more time by studying the codes for the LCD, compass,

microcontroller and way to integrate them together.

5.1.2 Assembly language

Apart from the complexity of assembly code, assembly language is important in

microcontroller programming where it enable users to monitor every aspect of the

program. Another advantage of assembly language is the available resource on the

Internet where one can just download most of demo and example code in this language

format. The downside of C language code is that the size compiled is usually longer than

assembly language.

5.2 Software Development Environment

The software developing process needs to be run by using certain software. List

of software such as Microchip MPLAB IDE, HI-TECH PICC-Lite, Home Made

Programmer and ISIS 7 Professional are implemented in this project.

Page 51: 826_MOHDBASRIBINOTHMAN2010

36

5.2.1 MPLAB IDE

MPLAB Integrated Development Environment (IDE) is a comprehensive editor,

project manager and design desktop for application development of embedded designs

using Microchip PICmicro and dsPIC microcontrollers. It is a free product of Microchip

Inc. and is an effort to make source code development as smooth and comprehensive as

possible. It is called an Integrated Development Environment, or IDE, because it

provides a single integrated “environment” to develop code for embedded

microcontrollers. MPLAB IDE provides a good platform for other compiler language

tools to be integrated. MPLAB C17, MPLAB C18 and MPLAB C30 from microchip

provide fully integrated, optimized code [15].

Figure 5.1: MPLAB IDE

Page 52: 826_MOHDBASRIBINOTHMAN2010

37

5.2.2 HI-TECH PICC-Lite

HI-TECH Software is the third party compilers for PIC10/12/16 and PIC18,

dsPIC, PIC24 and PIC32 devices. It is the only compiler vendor with comprehensive

support for all PIC10/12/14/16/17/18/24/32 MCUs, plus the dsPIC Digital Signal

Controllers. HI-TECH Software has teamed with Microchip Technology Inc. for over a

decade. It is a world class provider of ANSI C compilers: many featuring Omniscient

Code GenerationTM (OCG) –a whole-program compilation technology. The Lite mode

has OCG optimizations disengaged, and is ideal for entry level students and hobbyists.

The HI-TECH PICC-Lite version 9.60PL1 compiler is comes together with the installer

of MPLAB V8.10 [16].

Figure 5.2: HI-TECH PICC-Lite Compilers

5.2.3 Home Made Programmer

Page 53: 826_MOHDBASRIBINOTHMAN2010

38

Home Made Programmer is used to introduce (burn) a program that was written

in MPLAB IDE software into the PIC microcontroller. PIC microcontroller with a 18,

28, 40 pin products can burn with this circuit. However, it requires an interface to allow

programs written in PIC microcontroller. Interface or software required is PIC Start-up

Kit SK40C.

5.2.4 Isis 7 Professional

Isis 7 Professional provides an environment or atmosphere for Proteus 7

Professional, a revolutionary interactive system level simulator. This software combines

simulation of mixed mode circuits, microcontroller models and interactive components

model allows simulation flag design a complete microcontroller-based.

Isis provides a space for entering a circuit design on a location, the architecture

for real-time interactive simulation and management of resources and the object code

associated with each project. In addition, some of the graphic objects can be placed on

the schematic to simulate the conventional and variable frequency oscillation

implemented. Figure 5.3 shows the application window Isis Professional

Page 54: 826_MOHDBASRIBINOTHMAN2010

39

Figure 5.3: Application windows Isis Professional

5.3 Coding writing

I use the C programming language to write the codes for my project. This coding

is written using MPLAB IDE, written after coding is complete, it will compile using HI-

TECH PICC-Lite with the according number of steps have been given. Among the steps

are as follows:

Page 55: 826_MOHDBASRIBINOTHMAN2010

40

Part I: Using PICC Lite

1. To use the compiler and write our code in C, we will still be using MPLAB IDE.

Open the MPLAB Integrated Development Environment.

2. We will start a new project using the project wizard, just as in the past.

3. Specify the device you will be using, making sure it is one of the devices

supported by PICC Lite Compiler.

4. This time, when you must select a tool-suite, choose Hi-Tech PICC Toolsuite

from the drop down menu. Then set up the directories and project name. If the

assembler, linker and compiler are not selected, browse to location

C:\PICCLite\Bin\PICL.exe.

5. Then create a Project and Directory just as you’ve done with the Assembly

Language version.

6. Click “NEXT” brings up this window, but there is nothing to add.

7. After that select “Finish” in Summary window and verify project parameters. Go

to File and select NEW; open a window “untitled”.

Page 56: 826_MOHDBASRIBINOTHMAN2010

41

8. Then create a new blank file and save it in your project directory with a “.c”

extension.

9. Then add this file to your project Source Files. By adding the “.c” extension,

MPLAB automatically uses C syntax for editing in the IDE. Right click on

source file and select “Add Files” as shown in the following window.

Part 2: Setting up the Microcontroller Some Code

1. Open the blank file you’ve created, cprog.c, and the first line of code to add to

your blank page is:

2. Navigate to the Hi-Tech directory and look at this include file. It basically sets up

all the definitions for the particular chip you are using, similar to the 16F877 Inc

file we used in assembly.

3. Next, we will add our main program body. Everything in PICC is just like ANSI

C. Just like in our assembly programs, we must set up all of the registers

associated with our device - only in C, it is much easier. To set up the

configuration bits, just select the CONFIGURATION Pull-Down Menu and set

the configuration you want.

4. To verify that the device you’ve selected is supported by the compiler, go to the

PROJECTS pull-down menu and select BUILD ALL.

Page 57: 826_MOHDBASRIBINOTHMAN2010

42

CHAPTER VI

RESULT AND DISCUSSION

6.1 Results

After implementing this project, the results of this project is divided into two

parts, namely the:

i. Programming

ii. Hardware

The part of programming was taken to long time to write, there have many

problem and error to solve. This programming is very long and complicated. After the

completion of writing the program, it needs to compile by using software that support

the PIC 16F877A.

Page 58: 826_MOHDBASRIBINOTHMAN2010

43

6.2 Programming

Program written in C language and designed using MPLAB IDE to program the

PIC 16F877A microcontroller to display the prayer time and Qiblat direction based on

longitude and latitude that have been included. Five hours of prayer times that is Subuh,

Zuhur, Asar, Maghrib and Isyak will be displayed using an LCD Display and the Qiblat

will appear with the number of degrees from the North.

In addition, a circuit to show clock and date was created using the Real Time

Clock (RTC), the language used is C language. This circuit was designed by using

Proteus software.

6.3 Hardware

The time taken to make the hardware is not too long, the circuit which is LCD

Display circuit, Power regulator circuit, PIC Basic circuit, RTC circuit, Compass circuit,

and key-pad.

Page 59: 826_MOHDBASRIBINOTHMAN2010

44

Figure 6.1 shows the overall hardware of this project.

Figure 6.1: The overall Digital Praying circuit

6.3.1 Power Regulator circuit

This circuit serves to divide the supply voltage of the modifier (adaptor) to the

circuit up PIC and other circuits.

Page 60: 826_MOHDBASRIBINOTHMAN2010

45

Figure 6.2: Power Regulator circuit

6.3.2 PIC Basic circuit

This circuit functions to switch the PIC microcontroller. The circuit is built

separately from the LCD Display circuit. Terminal (port) output circuit is connected to

the LCD Display using a cable.

Page 61: 826_MOHDBASRIBINOTHMAN2010

46

Figure 6.3: PIC Basic circuit

6.3.3 LCD Display circuit

This circuit functions to display the prayer times, clock and the date. This circuit

will be connected to the PIC 16F877A circuit.

Figure 6.4 LCD Display circuit

Page 62: 826_MOHDBASRIBINOTHMAN2010

47

6.3.4 RTC circuit

This circuit functions as a tool that calculates the hours and dates, then it will be

connected to the PIC and the LCD display to show the clock and date.

Figure 6.5: RTC circuit

6.4 Problems and discussion

While developing the application design, several difficulties and mistakes were

countered. The project required research and troubleshooting at hardware and software

part. These are:

Page 63: 826_MOHDBASRIBINOTHMAN2010

48

i. Algorithm

Qiblat finding algorithm and prayer time algorithm are the main key in creating

the device. Selection have to be made in selecting the most suitable algorithm as

there are many theories in finding the Qiblat and Time Prayer. The algorithms

need to be verified by comparing it with the current data using C programming.

This is to ensure its reliability and consistency.

ii. LCD

Studying the LCD takes most of the time. The display protocols, initial port

setting and programming to control the display need to be learned and practice

before it can implemented into the project.

Page 64: 826_MOHDBASRIBINOTHMAN2010

49

CHAPTER VII

CONCLUSION AND FURTHER WORK

7.1 Introduction

This thesis discusses the development of Digital Praying Clock implemented

using microcontroller PIC 16F877A and C programming.

The Digital Praying Clock device was developed. The objective of this project to create

an integrated portable device which is reliable, user friendly and can assist people in

doing their prayer has been achieved.

This chapter discusses about the problems faced during implementation of this

project. Conclusions are made to state out the strength and weakness of the project.

Lastly, recommendation for future works and are proposed.

Page 65: 826_MOHDBASRIBINOTHMAN2010

50

7.2 Strength and Weakness

This project offers simple and efficient way to find the prayer time and knowing

the direction of Qiblat. The portable and automated design make it as a device that be

used in any places. Most of other devices have only one of these functions and it is not

portable, bulky and some needs Internet connection to get this service.

The user friendly interface and simple instruction will help people in getting fast

result in any place at any time without having using other resources. This device not

only can show the Qiblat direction but it also can determine the North direction and can

be used in camping or orienteering activities. This Digital Praying Clock uses only 9V

battery will make it as a daily use device that can assist people in doing Prayer and

finding direction.

The weakness of this device is the date, time and current location have to be

inserted manually by the user every time to operate the system. The device is also

sensitive to any other electrical devices at it will affect the compass reading in

determining the Qiblat and North direction. The recommendation part will discuss on

how to improve the weakness and ways to make the device better.

Page 66: 826_MOHDBASRIBINOTHMAN2010

51

7.3 Suggestion for Future Work

The development of Digital Praying Clock can be improved to a more advanced

and better application in the future. For future improvement, several suggestions are

proposed:

i. Every day, at a certain time, the Qiblat can be determined from the sun, either

by facing it or by facing the shadow from it, or else there is a time when

Qiblat would be 90 degrees left or right of the sun. This time can be

calculated for everyday for any desired location. This is the most accurate

method. The compass, cannot tell the True North, because its needle points to

Magnetic North, and there is no way to know exactly how many degrees it is

away from True North. Estimated values of the difference between True

North and Magnetic North can be used but they can have errors, because

magnetic field is not only changing continuously, but also can be erratic due

to other physical changes present inside the earth. Moreover, the presence of

other magnetic fields or metallic objects can deflect compass needle pointing

in the wrong direction.

ii. By using the Global Positioning System (GPS), date, time, longitude and

latitude can be set automatically by connecting GPS with the DB9 connector

and interface with RS232to the PIC Microcontroller.

iii. Better Hardware design, the device can be made smaller by using printed

board. It is smaller (pocket size) and easier to bring the device anywhere.

Page 67: 826_MOHDBASRIBINOTHMAN2010

52

iv. Add function, this device can be multipurpose device, not only determining

the Qiblat and prayer time, but it can also shows Hijriah Calendar and

launched the azar.

Page 68: 826_MOHDBASRIBINOTHMAN2010

53

REFERENCES

1. Azlina Bte. Lot, “Perhitungan waktu solat secara berkomputer”, Thesis UTM,

2001.

2. Robert L.Floyd, “Digital Fundamental”, seventh Edition, 2000.

3. Robert L.Boylestad dan Louis Nashelsky, “Electronic Devices and Circuit

Theory”, Eight Edition, 2002.

4. Hayatuddin B. Zokry, “Paparan waktu solat menggunakan MCH68HC11”, Thesis

UTM, 2003.

5. Jum’Adil bin Ambo, “Jam Solat Digital”, Thesis UTM, Mei 2007.

6. Dr. Monzur Ahmed, “The Determination of Salat Times”,

http://www.ummah.com/astronomy/saltime/#7.%20Calculation, 1996/1999.

7. Hamid Zarrabi-Zadeh, “Prayer Times Calculation”,

http://tanzil.info/praytime/doc/calculation/, 2007-2010.

8. Dr. Mohammad Ilyas; “A Modern Guide to Astronomical Calculations of Islamic

Calendar, Times & Qibla”; Berita Publishing Sdn Bhd., Kuala Lumpur, Malaysia,

1984.

9. Dr. Mohammad Ilyas; “Astronomy of Islamic Times for the Twenty-first Century”,

Mansell, London 1989.

10. Cytron Technologies Sdn. Bhd., “PR3 LCD Display”,

http://www.cytron.com.my/viewProduct.php?pid=MiQZHjoYCjk5BTIkKA0fKbF

6NIsl2e9bhrE6Lo8PNcw=&store, May 2009.

Page 69: 826_MOHDBASRIBINOTHMAN2010

54

11. Cytron Technologies Sdn. Bhd., “Display Real Time Clock (RTC) On LCD”,

http://www.cytron.com.my/viewProduct.php?pid=EAo4Mh0xEQsqEzAxLi8yKqD

/6h4wOeifVdvOuCrCnwY=&store, Aug 2008.

12. http://picnote.blogspot.com/search/label/PIC16F877a

13. http://www.answers.com/topic/salah

14. http://aaiil.org/text/had/manhad/ch9had.shtml

15. Microchip Technology Inc, “MPLAB IDE v6.xx Quick Start Guide”,

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1

406&dDocName=en019469&part=SW007002, 2003.

16. Hi-Tech’s PICC , “Tutorial on C Compiler”,

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1

406&dDocName=en542849, 16 April 2008.

,

Page 70: 826_MOHDBASRIBINOTHMAN2010

55

APPENDIX

//SOURCE CODE DS1307 RTC Ineterfacing with PIC16F877A

//Coded by Mohd basri Othman

//20/02/2010

unsigned short read_ds1307(unsigned short address );

unsigned short sec;

unsigned short minute;

unsigned short hour;

unsigned short day;

unsigned short date;

unsigned short month;

unsigned short year;

unsigned short data;

char time[9];

char ddate[11];

unsigned char BCD2UpperCh(unsigned char bcd);

unsigned char BCD2LowerCh(unsigned char bcd);

void main(){

I2C_Init(100000); //DS1307 I2C is running at 100KHz

PORTB = 0;

TRISB = 0; // Configure PORTB as output

Page 71: 826_MOHDBASRIBINOTHMAN2010

56

TRISC = 0xFF;

Lcd_Init(&PORTB); // Initialize LCD connected to PORTB

Lcd_Cmd(Lcd_CLEAR); // Clear display

Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off

Lcd_Out(1, 1, "TIME:");

Lcd_Out(2, 1, "DATE:");

while(1)

{

sec=read_ds1307(0); // read second

minute=read_ds1307(1); // read minute

hour=read_ds1307(2); // read hour

day=read_ds1307(3); // read day

date=read_ds1307(4); // read date

month=read_ds1307(5); // read month

year=read_ds1307(6); // read year

time[0] = BCD2UpperCh(hour);

time[1] = BCD2LowerCh(hour);

time[2] = ':';

time[3] = BCD2UpperCh(minute);

time[4] = BCD2LowerCh(minute);

time[5] = ':';

time[6] = BCD2UpperCh(sec);

time[7] = BCD2LowerCh(sec);

time[8] = '\0';

ddate[0] = BCD2UpperCh(date);

ddate[1] = BCD2LowerCh(date);

ddate[2] ='/';

ddate[3] = BCD2UpperCh(month);

ddate[4] = BCD2LowerCh(month);

ddate[5] ='/';

ddate[6] = '2';

Page 72: 826_MOHDBASRIBINOTHMAN2010

57

ddate[7] = '0';

ddate[8] = BCD2UpperCh(year);

ddate[9] = BCD2LowerCh(year);

ddate[10] = '\0';

Lcd_Out(1,6,time);

Lcd_Out(2,6,ddate);

Delay_ms(500);

}

}

unsigned short read_ds1307(unsigned short address)

{

I2C_Start();

I2C_Wr(0xd0); //address 0x68 followed by direction bit (0 for

write, 1 for read) 0x68 followed by 0 --> 0xD0

I2C_Wr(address);

I2C_Repeated_Start();

I2C_Wr(0xd1); //0x68 followed by 1 --> 0xD1

data=I2C_Rd(0);

I2C_Stop();

return(data);

}

unsigned char BCD2UpperCh(unsigned char bcd)

{

return ((bcd >> 4) + '0');

}

unsigned char BCD2LowerCh(unsigned char bcd)

{

return ((bcd & 0x0F) + '0');

}

Page 73: 826_MOHDBASRIBINOTHMAN2010

58

//SOURCE CODE: DIGITAL PRAYER CLOCK

//BY MOHD BASRI BIN OTHMAN

#include <pic.h>

#include <string.h>

#include <time.h>

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

//Configuration Fuses_CONFIG(WDTDIS & PWRTDIS & BORDIS & LVPDIS & DUNPROT &

UNPROTECT & WRTEN & XT);

//define

#define XTAL 4000000 /*Crystal frequency (Hz). */

#define RATE 4000 /*Interrupt

rate. */

#define TIMER_VALUE XTAL/ (4*RATE)

#define _P12 6.283185307179590

#define degreesToRadians 0.01745329251994329577

#define radiansToDegrees 57.29577951308230876799

#define STAT_BF BF

//Global Variables

static bit E @ (unsigned) &PORTB*8+0;

static bit rw @ (unsigned) &PORTB*8+1;

static bit RS @ (unsigned) &PORTB*8+2;

static bit LED @ (unsigned) &PORTB*8+7;

static bit TRIE @ (unsigned) &TRISB*8+0;

static bit TRIRW @ (unsigned) &PORTB*8+1;

static bit TRIRS @ (unsigned) &PORTB*8+2;

static bit TRILED @ (unsigned) &PORTB*8+7;

static bit SW1 @ (unsigned) &PORTA*8+0;

static bit SW2 @ (unsigned) &PORTA*8+1;

static bit SW3 @ (unsigned) &PORTA*8+2;

static bit SW4 @ (unsigned) &PORTA*8+3;

static bit DIRSW1 @ (unsigned) &TRISA*8+0;

static bit DIRSW2 @ (unsigned) &TRISA*8+1;

static bit DIRSW3 @ (unsigned) &TRISA*8+2;

static bit DIRSW4 @ (unsigned) &TRISA*8+3;

volatile unsigned DATAD @ &PORTD;

volatile unsigned DIRD @ &TRISD;

Page 74: 826_MOHDBASRIBINOTHMAN2010

59

unsigned int msec = 4000;

unsigned char g_sec = 0;

bit min_flag;

const unsigned char Mon[] = { 0, 31, 28, 30, 31, 30, 31, 31, 30, 31, 30,31 };

const char*const SolatStr[] = {"Subuh", "Shuruk", "Zuhur", "Asar", "Maghrib", "Isyak" };

const char* const tDate[] = { " Adjust Date","Adjust Time","Adjust Latitude","Adjust Longitude"

};

const char*const tQib[] = {"Qiblat = ", " Found ", "North "};

persistent bank1 int year, month, day;

bank1 unsigned int g_min;

persistent bank1 double _signedLatitude,_signedLongitude;

bank1 int soltime, pos, savesoltime;

bank1 int solat, rsolat;

bank1 char splash[] = "DIGITAL PRAYER TIME "

"BY 'MOHD BASRI";

bank1 unsigned char compass[4], version, bearingB;

bank1 unsigned int bearingW;

void Delay500Us() //500 Usec Delay Routine

{

unsigned char cnt500Us = 165;

while(--cnt500Us != 0)

continue;

}

void Delay100Ms() //100 msec Delay Routine

{

unsigned char cnt100Ms = 200; //200*500 Usec = 100 msec

do

{

Delay500Us();

}while(--cnt100Ms != 0);

}

static void interrupt isr(void)

{

TMR0 += 3 - TIMER_VALUE;

msec--;

if(msec == 0)

{

g_sec++;

if(g_sec > 59)

Page 75: 826_MOHDBASRIBINOTHMAN2010

60

{

g_sec = 0;

min_flag = 1;

}

msec = 4000;

}

T0IF = 0;

}

void timer_init()

{

OPTION = 0b00001000;

TMR0 = 3 - TIMER_VALUE;

T0IE = 1;

GIE = 1;

}

void InitPort()

{

ADCON1 = 6;

TRIE = 0;

TRIRS = 0;

TRIRW = 0;

TRILED = 0;

rw = 0;

E = 0;

RS = 0;

DIRD = 0x00;

DATAD = 0x00;

DIRSW1 = 1;

DIRSW2 = 1;

DIRSW3 = 1;

DIRSW4 = 1;

}

void Trig()

{

E = 1;

Delay500Us();

E = 0;

}

void InitLCD()

{

Delay100Ms();

Delay100Ms();

Page 76: 826_MOHDBASRIBINOTHMAN2010

61

Delay100Ms();

DATAD = 0x38; //blink n active n open

Trig();

Delay100Ms();

DATAD = 0x0C; // 0f

Trig();

Delay100Ms();

}

void ClearScreen()

{

DATAD = 0x01;

Trig();

}

void GoCursor(char row, char column)

{

switch(row)

{

case 1 : DATAD = 0x80 + (column - 1); break;

case 2 : DATAD = 0xc0 + (column - 1); break;

}

Trig();

}

void DisplayChar(char huruf)

{

RS = 1;

DATAD = huruf;

Trig();

RS = 0;

}

void DisplayInt(int value)

{

static bank1 int digit;

if(value > 99)digit = 1000; else digit = 10;

for(;digit > 0; digit /=10)

{

DisplayChar('0'+ (value%(digit*10))/(digit));

}

}

void DisplayInt2(int value)

{

static bank1 int digit;

Page 77: 826_MOHDBASRIBINOTHMAN2010

62

if(value >= 0 && value < 10)

{

digit = 1;

}

else if(value >= 10 && value < 100)

{

digit = 10;

}

else if(value >= 100 && value < 1000)

{

digit = 100;

}

for(;digit > 0; digit /= 10)

{

DisplayChar('0'+ (value%(digit*10))/(digit));

}

}

void DisplayRow(char row, char*ayat)

{

char i;

GoCursor(row,1);

for(i=0;1<16;i++);

DisplayChar(*ayat++);

}

void DisplayScreen(char*ayat)

{

DisplayRow(1,ayat+0);

DisplayRow(2,ayat+16);

}

// -----------------------------------------------------------------------------------

//Compass Algoritm

void SetupCompass()

{

char x;

TRISC = 0x18;

SSPSTAT = 0x80;

SSPCON = 0x38;

SSPCON2 = 0x00;

SSPADD = 9;

x = SSPBUF;

}

Page 78: 826_MOHDBASRIBINOTHMAN2010

63

void read_compass(unsigned char*buffer)

{

SEN = 1;

while(SEN);

ACKDT = 0;

SSPIF = 0;

SSPBUF =0xC0;

while(!SSPIF);

SSPIF = 0;

SSPBUF = 0;

while(!SSPIF);

SSPIF = 0;

RSEN = 1;

while(RSEN);

SSPIF = 0;

SSPBUF = 0xC1;

while(!SSPIF);

SSPIF = 0;

RCEN = 1;

while(!STAT_BF);

buffer[0] = SSPBUF;

ACKEN = 1;

while(ACKEN);

RCEN = 1;

while(!STAT_BF);

buffer[1] = SSPBUF;

ACKEN = 1;

while(ACKEN);

RCEN = 1;

while(!STAT_BF);

buffer[2] = SSPBUF;

ACKEN = 1;

while(ACKEN);

RCEN = 1;

while(!STAT_BF);

buffer[3] = SSPBUF;

ACKDT = 1;

ACKEN = 1;

Page 79: 826_MOHDBASRIBINOTHMAN2010

64

while(ACKEN);

PEN = 1;

while(PEN);

}

//---------------------------------------------------------------------------------

//Start Qiblat Algorithm

bit isLeap(int yr)

{

int m1, m4;

m4 = yr%400;

m1 = yr%100;

return(yr%4 == 0 && m1 != 0) || m4 == 0;

}

double calc_qibla()

{

double latitude, longitude, t2;

latitude=_signedLatitude*degreesToRadians;

longitude=_signedLongitude*degreesToRadians;

t2 = cos(latitude)*tan(0.3739077)-sin(latitude)*cos(longitude - 0.69504828);

return (radiansToDegrees*atan(sin(longitude - 0.69504828)/ t2));

}

void calc_solat()

{

double Q,_declination;

int noon;

soltime = 0;

for(noon = 0; noon < month; noon++)

{

soltime = soltime + Mon[noon];

}

soltime = soltime + day;

if(isLeap(year) && month > 2)soltime++;

Q =_P12/365*(soltime - 81);

Q = 9.87*sin(2*Q) - 7.53*cos(Q) - 1.5*sin(Q);

noon = Q = 720 - (4*(_signedLongitude - (8*15)))-Q;

if(Q - noon >= 0.5) noon++;

Page 80: 826_MOHDBASRIBINOTHMAN2010

65

_declination = radiansToDegrees*-

_P12/360*23.45*cos(_P12/365*(soltime+10));

switch (solat)

{

case 1: Q = 200; break;

case 2: Q = -0.9433; break;

case 3:

case 4:

if(_signedLatitude>_declination)

Q =_signedLatitude-_declination;

else

Q =_declination-_signedLatitude;

Q

=radiansToDegrees*atan(1/(1+tan(Q*degreesToRadians)));

break;

case 5: Q = -0.9433; break;

case 6: Q = 198; break;

}

Q = sin(Q *degreesToRadians)-

(sin(_signedLatitude*degreesToRadians)*sin(_declination*degreesToRadians));

Q =

Q/cos(_signedLatitude*degreesToRadians)/cos(_declination*degreesToRadians);

soltime = Q = 4*radiansToDegrees*acos(Q);

if(Q - soltime >= 0.5)soltime++;

switch (solat)

{

case 1:

case 2: soltime = noon - soltime; break;

case 3: soltime = noon + 1;

break;

case 4:

case 5:

case 6: soltime = noon + soltime; break;

}

}

//---------------------------------------------------------------------------------------------------------

//Timer

static bit dflag;

void Timer_Run()

Page 81: 826_MOHDBASRIBINOTHMAN2010

66

{

static char temp;

if(min_flag)

{

min_flag = 0;

g_min++;

if(g_min > 1439)

{

g_min = 0;

day++;

if(day > Mon[month])

{

day =

1;

month++;

if(month > 12)

{

month = 1;

year++;

}

}

}

for(solat=1; solat <=6; solat++)

{

calc_solat();

if(g_min < soltime)

{

break;

}

}

}

GoCursor(1,2);

DisplayInt(day);

DisplayChar('-');

DisplayInt(month);

DisplayChar('-');

DisplayInt(year%2000);

GoCursor(1,11);

Page 82: 826_MOHDBASRIBINOTHMAN2010

67

DisplayInt(g_min/60);

DisplayChar(':');

DisplayInt(g_min%60);

//DisplayChar(':');

//DisplayInt(g_sec);

calc_solat();

GoCursor(2,2);

for (pos = 0; SolatStr[solat-1][pos];

pos++)

DisplayChar(SolatStr[solat-1][pos]);

GoCursor(2,10);

DisplayInt(soltime/60);

DisplayChar(':');

DisplayInt(soltime%60);

if(!SW2)

{

solat++;

// Delay100Ms();

if(solat == 7)

solat = 1;

}

}

void AdjustDate()

{

GoCursor(1,3);

for(pos = 0; tDate[0][pos]; pos++)

DisplayChar(tDate[0][pos]);

GoCursor(2,4);

DisplayInt(day);

DisplayChar('-');

DisplayInt(month);

DisplayChar('-');

DisplayInt(year);

if(!SW2)

{

day++;

if(day > Mon[month])

day = 1;

Delay100Ms();

Page 83: 826_MOHDBASRIBINOTHMAN2010

68

}

else if(!SW3)

{

month++;

if(month > 12)

month = 1;

Delay100Ms();

}

else if(!SW4)

{

year++;

if(year > 2010)

year = 2000;

Delay100Ms();

}

}

void AdjustTime()

{

GoCursor(1,3);

for(pos = 0; tDate[1][pos]; pos++)

DisplayChar(tDate[1][pos]);

GoCursor(2,5);

DisplayInt(g_min/60);

DisplayChar(':');

DisplayInt(g_min%60);

DisplayChar(':');

DisplayInt(g_sec);

if(!SW2)

{

g_min = g_min+60;

if(g_min > 1439)

{

g_min = 0;

}

Delay100Ms();

}

else if(!SW3)

{

g_min++;

Delay100Ms();

}

else if(SW4)

{

g_sec++;

Page 84: 826_MOHDBASRIBINOTHMAN2010

69

Delay100Ms();

}

}

void AdjustLatitude()

{

int temp;

temp = _signedLatitude;

GoCursor(1,1);

for(pos = 0; tDate[2][pos]; pos++)

DisplayChar(tDate[2][pos]);

GoCursor(2,7);

DisplayInt2(_signedLatitude);

DisplayChar('.');

DisplayInt2((_signedLatitude-temp)*10);

DisplayChar(' ');

DisplayChar(' ');

if(!SW2)

{

_signedLatitude++;

Delay100Ms();

}

else if(!SW3)

{

_signedLatitude--;

Delay100Ms();

}

else if(!SW4)

{

_signedLatitude = _signedLatitude + 0.1;

Delay100Ms();

}

}

void AdjustLongitude()

{

int temp;

temp = _signedLongitude;

GoCursor(1,1);

for(pos = 0; tDate[3][pos]; pos++);

DisplayChar(tDate[3][pos]);

GoCursor(2,6);

DisplayInt2(_signedLongitude);

DisplayChar('.');

Page 85: 826_MOHDBASRIBINOTHMAN2010

70

DisplayInt2((_signedLongitude-temp)*10);

DisplayChar(' ');

DisplayChar(' ');

if(!SW2)

{

_signedLongitude++;

Delay100Ms();

}

else if(!SW3)

{

_signedLongitude--;

Delay100Ms();

}

else if(!SW4)

{

_signedLongitude = _signedLongitude + 0.1;

Delay100Ms();

}

}

//main program

void main()

{

static int mode;

double arahqib, qbearing, cbearing;

int temp,i;

InitPort(); //Init

output ports and ser initial value

InitLCD();

SetupCompass();

_signedLatitude = 3.1;

_signedLongitude = 101.5;

mode = 1;

year = 2010;

month = 3;

day = 3;

g_min = 8*60 + 56;

min_flag = 1;

solat = 1;

timer_init();

Page 86: 826_MOHDBASRIBINOTHMAN2010

71

DisplayScreen(splash);

for(i=0; i <10; i++)

Delay100Ms();

ClearScreen();

dflag = 0;

while(1)

{

if(!SW1)

{

mode++;

if(mode == 7)

mode = 1;

Delay100Ms();

ClearScreen();

}

if(mode == 1)

{

Timer_Run();

}

if(mode == 2);

{

GoCursor(1,1);

for(pos = 0; tQib[0][pos]; pos++)

DisplayChar(tQib[0][pos]);

GoCursor(1,10);

arahqib = 360 - calc_qibla();

qbearing = arahqib;

DisplayInt(arahqib);

DisplayChar('.');

temp = arahqib;

arahqib = (arahqib - temp)*10;

DisplayInt(arahqib);

//read_compass(compass);

version = compass[0];

bearingB = compass[1];

bearingW = ((compass[2]<<8)+compass[3]);

cbearing = bearingW/10;

bearingW /=10;

// GoCursor(2,10);

Page 87: 826_MOHDBASRIBINOTHMAN2010

72

// DisplayInt(version);

// DisplayChar(' ');

// DisplayInt(bearingB);

// DisplayChar(' ');

DisplayInt(bearingW);

// DisplayChar(' ');

// DisplayChar(' ');

// DisplayChar(' ');

if(abs(cbearing - qbearing) <1.0)

{

LED = 1;

GoCursor(2,1);

for(pos = 0; tQib[1][pos];

pos++)

DisplayChar(tQib[1][pos]);

}

else

{

LED = 0;

GoCursor(2,1);

DisplayChar(' ');

DisplayChar(' ');

GoCursor(2,3);

DisplayInt(fabs(cbearing -

qbearing));

DisplayChar(' ');

DisplayChar(' ');

}

if(abs(cbearing < 1))

{

GoCursor(2,1);

for(pos = 0; tQib[1][pos];

pos++)

DisplayChar(tQib[2][pos]);

}

}

if(mode==3)

{

LED = 0;

AdjustDate();

}

if(mode==4)

{

AdjustTime();

}

Page 88: 826_MOHDBASRIBINOTHMAN2010

73

if(mode==5)

{

AdjustLatitude();

}

if(mode==6)

{

AdjustLongitude();

}

}

}