Top Banner
UNIVERSITY OF NAIROBI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING PROJECT 005: POWER QUALITY MONITORING UNIT AUTHOR: NJOGU SWALEH KAMUTHIERE REG NO: F17/21669/2007 SUPERVISOR: PROF. ELIJAH MWANGI EXAMINER: Mr. S.A. AHMED A PROJECT REPORT SUBMITTED TO THE DEPARTMENT OF ELECTRICAL AND INFORMATION ENGINEERING IN PARTIAL FULFILLMENT OF THE REQUIREMENTS OF BSc. ELECTRICAL AND ELECTRONIC ENG. OF THE UNIVERSITY OF NAIROBI. MAY, 2016
97

UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

Apr 09, 2018

Download

Documents

vomien
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: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

UNIVERSITY OF NAIROBI

DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING

PROJECT 005: POWER QUALITY MONITORING UNIT

AUTHOR: NJOGU SWALEH KAMUTHIERE

REG NO: F17/21669/2007

SUPERVISOR: PROF. ELIJAH MWANGI

EXAMINER: Mr. S.A. AHMED

A PROJECT REPORT SUBMITTED TO THE DEPARTMENT OF ELECTRICALAND INFORMATION ENGINEERING IN PARTIAL FULFILLMENT OF THEREQUIREMENTS OF BSc. ELECTRICAL AND ELECTRONIC ENG. OF THE

UNIVERSITY OF NAIROBI.

MAY, 2016

Page 2: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

DECLARATION OF ORIGINALITY

NAME OF STUDENT: Njogu Swaleh Kamuthiere

REGISTRATION NUMBER: F17/21669/2007

COLLEGE: Architecture and Engineering

FACULTY/SCHOOL/INSTITUTE: Engineering

DEPARTMENT: Electrical and Information Engineering

COURSE NAME: Bachelor of Science in Electrical and Electronic Engineering

TITLE OF WORK: Microcontroller based power quality measurement

1. I understand what plagiarism is and I am aware of the university policy in this regard.2. I declare that this project is my original work and has not been submitted elsewhere for

examination, award of a degree or publication. Where other peoples work or my ownwork has been used, this has properly been acknowledged and referenced inaccordance with the University Of Nairobi

3. I have not sought or used the services of any professional agencies to produce this work.4. I have not allowed, and shall not allow anyone to copy my work with the intention of

passing it off as his/her own work.5. I understand that any false claim in respect of this work shall result in disciplinary action, in

accordance with University anti-plagiarism policy.

Signature:……………………………………………………………………………………………………….

Date ……………………………………………………………………………………………………………….

This report has been submitted to the Department of Electrical and Information Engineering,The University of Nairobi with my approval as supervisor:

Signature:…………………………………………………………………………………..

Name: Prof. E. Mwangi

Page 3: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

DEDICATION

To my dear parents, family and friends for their continuous support and encouragement throughoutthis course.

Page 4: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

ACKNOWLEDGEMENT

First and foremost, I would like to thank God for giving me the strength and ability andguidance to carry out this project and complete the project successfully.

I would also like to thank my supervisor, Prof. E. Mwangi, for being a source of guidanceThroughout the duration of the project. The solutions to the cosine function in assembly languagewas a big help.

My sincere gratitude goes out to my classmates for their suggestions, opinions, and help inthis project.

I would like to thank my family for their support financially.

Page 5: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

LIST OF FIGURES

Fig 2-1 in phase sinusoidal waveforms ……………………………………………………………………………………..5

Fig 2-2 phase relations waveforms……………………………………………………………………………………………8

Fig 2-3 in phase sinusoidal waveforms………………………………………………………………………………………9

Fig 2-4 30 degrees out of phase waveforms……………………………………………………………………………..10

Fig 2-5 phase difference between a Sine wave and Cosine wave………………………………………………12

Fig 2-6 PIC16F690 Pin Diagram…………………………………………………………………………………………………..20

Fig 2-7 PIC16F690 PIN SUMMARY………………………………………………………………………………………………21

Fig 2-8 PIC16F690 pin diagram……………………………………………………………………………………………………22

Fig 2-9 memory organization of PIC16f690…………………………………………………………………………………24

Fig 2-10 above are the PIC16F690 Special Function Registers……………………………………………………26

Fig 2-11 ADC CONVERTER BLOCK DIAGRAM………………………………………………………………………………27

Fig 2-12 results formatting………………………………………………………………………………………………………..29

Fig 2-13 above is the capture mode…………………………………………………………………………………………..30

Fig 2-13 LCD Pin diagram and instructions…………………………………………………………………………………..32

Fig 2-14 initializing by internal reset circuit diagram…………………………………………………………………..35

Fig 2-15 8-bit initialization………………………………………………………………………………………………………….37

Fig 2-16 4-bit initialization………………………………………………………………………………………………………….38

Fig 2-17 LCD instruction set……………………………………………………………………………………………………….39

Fig 3-1 online LCD images………………………………………………………………………………………………………….40

Fig 3-2 simulation circuit op amp ……………………………………………………………………………………………….44

Fig 3-3 power supply circuit…………………………………………………………………………………………………………45

Fig 3-4current zero crossing detection circuit……………………………………………………………………………..46

Fig 3-5 voltage zero crossing detection circuit…………………………………………………………………………….46

Page 6: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

LIST OF TABLES

Table 4-1 simulated results ………………………………………………………………………………………………………..49

Page 7: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

LIST OF ABBREVIATIONS

A/D Analog to digital

AC Alternating Current

ALU Arithmetic Logic Unit

ASCII Arithmetic Standard Code for Information Interchange

CISC Complex Instruction Set Computer

CPU Control Processing Unit

DDRAM Double Data Random Access Memory

EPROM Erasable Programmable Read Only Memory

HZ Hertz

I/O Input/Output

KPLC Kenya Power and Lighting Company

LCD Liquid Crystal Display

PIC Peripheral Interface Controller

RAM Random Access Memory

RISC Reduced Instruction Set Computer

ROM Read Only Memory

TMR1 Timer One

TMR0 Timer Zero

Page 8: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

TABLE OF CONTENTS

DECLARATION OF ORIGINALITY……………………………………………………………………………………… i

DEDICATION………………………………………………………………………………………………………………….. ii

ACKNOWLEDGEMENT…………………………………………………………………………………………………… iii

LIST OF FIGURES……………………………………………………………………………………………………………. iv

LIST OF TABLES……………………………………………………………………………………………………………… v

LIST OF ABBREVIATIONS……………………………………………………………………………………………….. vi

CHAPTER 1: INTRODUCTION........................................................................................................ 1

1.1 Background study……………………………………………………………………………………………………..1

1.2 Problem definition…………………………………………………………………………………………………….1

1.3 Objectives .................................................................................................................. .2

1.4 Justification................................................................................................................3

1.5 Scope ......................................................................................................................... 3

CHAPTER 2: LITERATURE REVIEW ................................................................................................ 5

2.1 Introduction.............................................................................................................. 5

2.2 Waveforms ................................................................................................................ 5

2.2.1 Phase Difference……………………………………………………………………………………..6

2.2.2 Phase Relationship of a Sinusoidal Waveform…………………………………………8

2.4 MICROCONTROLLER………………………………………………………………………………………………..14

2.4.1 Microcontrollers and Microprocessors…………………………………………………….14

2.4.2 Microcontroller types……………………………………………………………………………...14

2.4.3 Microcontroller structure…………………………………………………………………………17

2.5 Pic architecture………………………………………………………………………………………………………..19

2.6 PIC16F690…………………………………………………………………………………………………………………19

Page 9: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.6.1 PIC16F690 pin diagram……………………………………………………………………………..20

2.6.2 PIC16F690 pin summary…………………………………………………………………………...21

2.6.3 PIC16F690 properties………………………………………………………………………………..23

2.6.4 Memory Organization……………………………………………………………………………….24

2.6.4.1 Program Memory Organization………………………………………………...24

2.6.4.2 Data Memory Organization……………………………………………………….25

2.6.5 Analog to Digital Converter………………………………………………………………………27

2.6.6 Interrupts………………………………………………………………………………………………….29

2.6.7 Capture mode…………………………………………………………………………………………..30

2.6.8 Timers……………………………………………………………………………………………………….31

2.7 Liquid Crystal Display………………………………………………………………………………………………..31

2.7.1 DDRAM - Display Data RAM……………………………………………………………………..33

2.7.2 CGROM - Character Generator ROM……………………………………………………….33

2.7.3 CGRAM - Character Generator RAM………………………………………………………..33

2.7.4 BF - Busy Flag……………………………………………………………………………………………33

2.7.5 Instruction Register (IR) and Data Register (DR)………………………………………34

2.7.6 Commands and Instruction set………………………………………………………………..34

2.7.7 LCD initialization……………………………………………………………………………………….34

2.7.7.1 Initializing by Internal Reset Circuit…………………………………………..35

2.7.7.2 Initialization by Instruction……………………………………………………….36

2.7.7.2.1 8-Bit Interface, Initialization by Instruction……………….37

2.7.7.2.2 4-Bit Interface, Initialization by Instruction……………….38

2.7.8 LCD instruction set……………………………………………………………………………………39

2.7.9 LCD connection…………………………………………………………………………………………39

CHAPTER 3: DESIGN AND IMPLEMENTATION…………………………………………………………………………….40

Page 10: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

3.1 Introduction…………………………………………………………………………………………………………….40

3.2 Hardware section…………………………………………………………………………………………………….42

3.2.1 LCD…………………………………………………………………………………………………………..42

3.2.2 Operational Amplifier………………………………………………………………………………44

3.2.3 Power Supply…………………………………………………………………………………………..44

3.2.4 Zero Crossing Detection………………………………………………………………………….46

3.3 Programming………………………………………………………………………………………………………….46

CHAPTER 4: Results and Analysis………………………………………………………………………………………………47

4.2 Simulation results…………………………………………………………………………………………………..48

4.3 Simulated circuit…………………………………………………………………………………………………….49

4.4 Practical results………………………………………………………………………………………………………50

CHAPTER 5: Conclusions And Recommendations…………………………………………………………………….52

5.1 Conclusions …………………………………….…………………………………………………………………….525.2 Recommendations………………………………………………………………………………………………..52

REFERENCES………………………………………………………………………………………………………………………………53

APPENDIX A: Assembly Language Code…………………………………………………………………………………….54

APPENDIX B: Cost Analysis…………………………………………………………………………………………………………84

Page 11: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin
Page 12: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

CHAPTER 1: INTRODUCTION

1. BACKGROUND STUDY

Power factor is basically due to lag and lead concept of current and voltage. When

current waveform lags behinds the voltage by some angle, power factor will be lagging and when

current wave form leads the voltage by some angle, power factor will be leading. Inductor causes

a lagging power factor and capacitors produces leading power factor. Hence capacitors improve

power factor by providing leading VARS or reactive power to system which cancels the lagging

power factor effect of inductors.

Power factor was measured using a pic microcontroller. To measure power factor it was

needed to measure phase angle difference between voltage and current wave form. The cosine of

this phase angle is power factor. Power factor is also represented by following formula:

Power factor = Cos (θ)

Where θ is basically phase angle difference between voltage and current wave form.

The phase angle is the lateral difference between two waveforms as per our case. That is

voltage and current along a common axis. Low power factor causes increase in current drawn by

load. This results in use of larger equipment on distribution systems and also an increase in the

amount of greenhouse gases released into the atmosphere.

2. PROBLEM DEFINITION

In order to measure the power factor it was required to obtain the phase angle in order to

obtain the power factor by calculating the cosine of the phase angle. It is required to measure the

power factor accurately because of its importance to the consumer and the supplier. In this

project a microcontroller was used to measure the phase angle and calculate the power factor.

Page 13: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

With the power factor low as mentioned earlier a lot of current is drawn by the consumers

or the load connected. The drawing of current leads to more losses being encountered. Which

affects the profits in distribution companies.

From the resistance equation it can be observed that resistance is inversely proportional

to the area of the conductor. Maintaining the voltage in ohms law V=IR ( where V=Voltage,

I=Current and R=Resistance) increase in current leads to reduction in the current. As the

resistance decreases the area of the conductor must increase according to the resistance equation

with the inverse relation between resistance and area of the conductor. Increase in the area of the

conductor assuming copper means that more copper will be used thus increasing the distribution

cost.

It should be noted that KPLC require their consumers to keep thepower factor close to

one as possible.

1.3 OBJECTIVES

The main objective was to design and implement a system that will measure the phase

difference in a single phase supply and compute the power factor. The power factor was to be

displayed on an LCD. That is a liquid crystal display. The circuit was supposed to also measure

the frequency and the amplitude.

Compare two sinusoidal waves ie voltage and current and obtain their

phase difference. That is check if they are in phase or not.

Calculate the power factor by finding the cos of the phase angle.

Compute the frequency of the single phase supply and record.

Obtain the amplitude and record.

Page 14: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

1.4 JUSTIFICATION

There are different methods to measure the phase angle. For instance the use of an

oscilloscope. But this has limitations like the cost of a modern oscilloscope is very high. And an

oscilloscope has errors too which are due to;

Present dc offset errors present

Reduced precision near certain angles

Incorrect solution possibility

By obtaining the power factor power factor correction measures can be implemented in

order to reduce the phase angle. Large phase angle leads to a lot of power being drawn leading to

very high costs which are unnecessary. But first the phase angle must be measured very

accurately so that the required corrections xan be made to reduce the costs. Large consumers

require very accurate data.

Microcontrollers are very efficient and fast. Being fast the power factor will obtained fast

and displayed instantly. Other methods of power factor measurement are full of errors and and

tiresome.

1.5 SCOPE OF THE PROJECT

Page 15: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

The project will deal with the problem of power factor measurement by measuring the

phase angle from a single phase supply and calculating the power factor by obtaining the the cos

of the phase angle. The power factor is to be displayed on an LCD. The power factor will not be

displayed as leading or lagging because in the project we wont know which wave form will be

leading or lagging. Timers will be employed, in this case we will use TMR1 for the phase

measurement. Where the rising edge of the first waveform will be recorded and the timer will be

activated. The timer will count until the second rising edge which will stop the timer and this will

be considered as sample one. To be accurate five readings will be taken and the average power

factor will be obtained, this improves the accuracy of our project.

The average phase angle will then be used to compute the power factor which will be

displayed on the LCD. For the frequency the rising edge at the zero crossing will be detected and

the falling edge will also be detected. This is a quarter of the period of the wave. The timer will

record the rising edge time and the falling edge time. Thus obtaining the time difference which

will be multiplied by four to obtain the entire period. The frequency will be the reciprocal of the

entire period. Which can also be displayed on the LCD.

Page 16: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

CHAPTER 2:LITERATURE REVIEW

2.1 INTRODUCTION

In this chapter, we will describe the various researches that has been carried out from

various sources such as the internet, textbooks, online articles. This information is essential to

the development of the project. The power quality monitoring unit is made up of the following :

PIC16F690 Microcontroller-

This is the heart of the whole monitoring unit. It is the main device in which the project

will be based on. Interfacing the PIC microcontroller with external devices is made

possible with the various I/O pins that the PIC microcontroller has.

Liquid Crystal Display(LCD)-

Facilitates the viewing of the power factor, the peak voltage and frequency that is

required of this project.

OP-AMP

2.2 WAVEFORMS

Page 17: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

A waveform is the result of plotting values of quantities which vary with time (t). It is in

the form of a graph. As for alternating waveforms like in the case of single phase the wave

changes direction that is it has positive and negative values as shown in the image above.

A period is defined as the time taken by an alternating signal to complete one cycle of

the waveform. Usually described by the symbol T.

Frequency on the other hand is described as the number of cycles completed in a

second. It can also be described as the the reciprocal of the period. In Kenya the frequency of

supplied electricity is 50 Hz.

2.2.1 Phase Difference

Sine Wave can be presented graphically in the time domain along an horizontal zero

axis, and that sine waves have a positive maximum value at time π/2, a negative maximum

value at time 3π/2, with zero values occurring along the baseline at 0, π and 2π. However, not

all sinusoidal waveforms will pass exactly through the zero axis point at the same time, but may

be “shifted” to the right or to the left of 0o by some value when compared to another sine

wave.

Page 18: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

Comparing a voltage waveform to that of a current waveform. This then produces an

angular shift or Phase Difference between the two sinusoidal waveforms. Any sine wave that

does not pass through zero at t = 0 has a phase shift.

The phase difference or phase shift as it is also called of a Sinusoidal Waveform is the

angle Φ (Greek letter Phi), in degrees or radians that the waveform has shifted from a certain

reference point along the horizontal zero axis. In other words phase shift is the lateral

difference between two or more waveforms along a common axis and sinusoidal waveforms of

the same frequency can have a phase difference.

The phase difference, Φ of an alternating waveform can vary from between 0 to its

maximum time period, T of the waveform during one complete cycle and this can be anywhere

along the horizontal axis between, Φ = 0 to 2π (radians) or Φ = 0 to 360o depending upon the

angular units used.

Phase Difference Equation

Where:

Am - is the amplitude of the waveform.

ωt - is the angular frequency of the waveform in radian/sec.

Φ (phi) - is the phase angle in degrees or radians that the waveform has shifted either

left or right from the reference point.

If the positive slope of the sinusoidal waveform passes through the horizontal axis “before” t = 0

then the waveform has shifted to the left so Φ >0, and the phase angle will be positive in nature,

+Φ giving a leading phase angle. In other words it appears earlier in time than 0o producing an

anticlockwise rotation of the vector.

Likewise, if the positive slope of the sinusoidal waveform passes through the horizontal x-axis

some time “after” t = 0 then the waveform has shifted to the right so Φ <0, and the phase angle

Page 19: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

will be negative in nature -Φ producing a lagging phase angle as it appears later in time than 0o

producing a clockwise rotation of the vector. Both cases are shown below.

2.2.2 Phase Relationship of a Sinusoidal Waveform

Considering that two alternating quantities such as a voltage, v and a current, i have the same

frequency ƒ in Hertz. As the frequency of the two quantities is the same the angular velocity, ω

must also be the same. So at any instant in time we can say that the phase of voltage, v will be

the same as the phase of the current, i.

Then the angle of rotation within a particular time period will always be the same and the phase

difference between the two quantities of v and i will therefore be zero and Φ = 0. As the

frequency of the voltage, v and the current, i are the same they must both reach their maximum

positive, negative and zero values during one complete cycle at the same time (although their

amplitudes may be different). Then the two alternating quantities, v and i are said to be “in-

phase”

Page 20: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

In phase sinusoidal waveforms

Considering that the voltage, v and the current, i have a phase difference between

themselves of 30o, so (Φ = 30o or π/6 radians). As both alternating quantities rotate at the same

speed, i.e. they have the same frequency, this phase difference will remain constant for all

instants in time, then the phase difference of 30o between the two quantities is represented by

phi, Φ as shown below.

Phase Difference of a Sinusoidal Waveform

Page 21: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

The voltage waveform above starts at zero along the horizontal reference axis, but at that same

instant of time the current waveform is still negative in value and does not cross this reference

axis until 30o later. Then there exists a Phase difference between the two waveforms as the

current cross the horizontal reference axis reaching its maximum peak and zero values after the

voltage waveform.

As the two waveforms are no longer “in-phase”, they must therefore be “out-of-phase” by an

amount determined by phi, Φ and in our example this is 30o. So we can say that the two

waveforms are now 30o out-of phase. The current waveform can also be said to be “lagging”

behind the voltage waveform by the phase angle, Φ. Then in our example above the two

waveforms have a Lagging Phase Difference so the expression for both the voltage and current

above will be given as.

Page 22: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

where, i lags v by angle Φ

Likewise, if the current, i has a positive value and crosses the reference axis reaching its

maximum peak and zero values at some time before the voltage, v then the current waveform

will be “leading” the voltage by some phase angle. Then the two waveforms are said to have a

Leading Phase Difference and the expression for both the voltage and the current will be.

where, i leads v by angle Φ

The phase angle of a sine wave can be used to describe the relationship of one sine wave to

another by using the terms “Leading” and “Lagging” to indicate the relationship between two

sinusoidal waveforms of the same frequency, plotted onto the same reference axis. In our

example above the two waveforms are out-of-phase by 30o so we can say that i lags v or v leads i

by 30o.

The relationship between the two waveforms and the resulting phase angle can be measured

anywhere along the horizontal zero axis through which each waveform passes with the “same

slope” direction either positive or negative.

In AC power circuits this ability to describe the relationship between a voltage and a current sine

wave within the same circuit is very important and forms the bases of AC circuit analysis.

Phase Difference between a Sine wave and a Cosine wave

Page 23: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

Alternatively, we can also say that a sine wave is a cosine wave that has been shifted in the other

direction by -90o. Either way when dealing with sine waves or cosine waves with an angle the

following rules will always apply.

Sine and Cosine Wave Relationships

When comparing two sinusoidal waveforms it more common to express their relationship as

either a sine or cosine with positive going amplitudes and this is achieved using the following

mathematical identities.

Page 24: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

By using these relationships above we can convert any sinusoidal waveform with or without an

angular or phase difference from either a sine wave into a cosine wave or vice versa.

In the next tutorial about Phasors we will use a graphical method of representing or comparing

the phase difference between two sinusoids by looking at the phasor representation of a single

phase AC quantity along with some phasor algebra relating to the mathematical addition of two

or more phasors.

Page 25: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.4 Microcontroller

A microcontroller is a computer on a chip. It is an electronic device containing:

A processor core

Memory

Input/Output peripherals

The input/output peripherals are programmable . They allow the microcontroller to

communicate with the outside world. They do not require external components for their applications.

They are useful in that they are cheap thus reducing prices and due to their small size they make the end

products smaller in size.

They are used in development of electronics for instance television sets, dvd machines, phones,

personal computers, laptops etc.

2.4.1 microcontrollers and microprocessors

Microcontrollers and microprocessors do differ in functionality. Microprocessor requires other

components for example memory or components for receiving and sending data. While the

microcontroller already has all that. Microprocessors are also designed to be general purpose. Therefore

comparing the two it can be concluded that microcontrollers are definitely smaller. Therefore saving on

space.

Page 26: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.4.2 Microcontroller types

Microcontrollers are divided into categories according to their memory architecture, bits, and

instruction sets.

1. Memory Architecture

Harvard Memory Architecture Microcontroller

In the Harvard Architecture, the program memory is completely separate from the data

memory. All PICs have Harvard Memory Architecture. The CPU fetches the next instruction

and loads or stores data simultaneously and independently. This physical separation of

instructions and data is the distinguishing feature of Harvard Architecture. The PIC

architecture has a two-stage instruction pipeline; however, since the fetch of the current

instruction and the execution of the previous one can overlap in time, one complete

instruction is fetched and executed at every machine cycle. This is known as instruction

pipelining.

Princeton Memory Architecture Microcontroller

In Princeton Architecture, the program memory is such that it stores both the instructions and

data in one memory pool. When the computer needs memory to hold a program, it goes to the

memory pool and requests that some memory is allocated for it. When it needs space to hold

data it goes to the same pool and asks for memory for data. The disadvantage of this is that if

a certain amount of memory is allocated for data, but the program tries to put more data than

will fit, it may place the extra data in the next memory location, which could actually contain the

program.

2. Bits

Page 27: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

8 bit microcontrollers-8 bit microcontrollers contain 8 bit internal bus with the ALU performing

logic and arithmetic operations. Examples of 8 bit microcontrollers include the Intel 8031/8051.

16 bit microcontrollers-This executes ALU operations with greater accuracy and performance

in contrast to the 8 bit microcontrollers. Example of the 16 bit microcontroller is the Intel

8096.

32 bit microcontrollers-This are employed mainly in automatically controlled appliances such

as office machines, implantable medical appliances etc. It requires 32 bit instructions to carry

out any logical or arithmetic operation.

3. Memory

Microcontrollers can be divided into two types in accordance with memory :

External Memory Microcontroller- When an embedded structure is built with

a microcontroller which does not comprise of all the functioning blocks

existing on a chip, then it is named an external memory microcontroller. For

illustration, 8031 microcontroller does not have program memory on the chip.

Embedded Memory Microcontroller- When an embedded structure is built

with a microcontroller which comprises of all the functioning blocks existing

on the chip, then it is named an Embedded Memory Microcontroller. For

Example the 8051 Microcontroller has all the program and data memory,

counters & timers, Interrupts, I/O ports etc.

4. Instruction set

This can be categorised into the following;

Page 28: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

CISC- CISC meaning Complex Instruction Set Computer. It allows

the user to apply 1 instruction as an alternative to many simple instructions. The CISC design is

based on each low-level instruction performing several operations. Examples of CISC instructions

include decrementing a counter register, determining the state of a processor flag, and executing a

jump instruction if the flag is set or cleared. The idea of CISC was to provide high level instructions

in order to implement the implementation of high level languages[4][6].

RISC- RISC means Reduced Instructions Set Computer. RISC reduces the operation time by

shortening the clock cycle per instruction. In contrast to CISC, RISC machine contains fewer instructions

and each instruction performs more elementary operations. Consequences of this are a smaller silicon

area, faster execution, and reduced program size with fewer accesses to main memory. The PIC

designers have followed the RISC route[4][6].

2.4.3 Microcontroller structure

The basic structure of a microcontroller comprises of:

I. Memory- There are two types of memory: Volatile and Non-volatile. Volatile

Memory loses its data when its switched off, but can be written by the CPU to store current data e.g.

RAM(Random Access Memory). ROM(Read Only Memory)is nonvolatile and retains its data when

switched off. The memory in the microcontroller stores all programs and data. The ideal memory

should be non-volatile, read and write, fast, large and cheap, but since it does not exist, then the Flash

ROM is closest to the ideal memory since its non-volatile and rewritable [1].

II. CPU- This is the brain of the microcontroller. The CPU facilitates the fetching of

data, decoding of the same and at the end completing the assigned task successfully. With the help of

the CPU all the components of the microcontroller are connected into a single system. A program

that is stored in the memory of the microcontroller is fetched by the CPU in sequence and executed.

In actual sense the instructions that are stored in the memory locations are copied to an instruction

Page 29: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

register in the CPU via a data bus. The instructions control the selection of the required operation in

the control unit of the CPU[4].

III. Serial Ports-These ports give serial interfaces amid microcontrollers and various

other peripherals. The serial communication involved in the PIC microcontroller for example include

the EUSART(Enhanced Universal Synchronous Asynchronous Receiver Transmitter), the SPI(Serial

peripheral Interface) and the I2C (Inter-Integrated circuit).

IV. Input/output ports- Without some means of getting information or signals in and

out, then the microcontroller would be of no help to the outside world. Thus the importance of

input/output ports. There are two main type of ports: serial and parallel. Parallel ports transfer data in

and out 8 bits at a time. While in serial ports it is transmitted 1 bit at a time on a single line.

Therefore the I/O ports are employed to interface or drive different external appliances e.g. LCD’s ,

LED’s etc.[2].

V. Timers and Counters-A microcontroller may be in-built with one or more timers

and counters.[4].They control all timing and counting operations within the microcontroller. The

main operations performed by timers are :

i. Pulse generations

ii. Clock functions

iii. Frequency measuring

iv. Modulation

v. Making oscillations

VI. Analog-to-Digital Converters- The peripheral signals that we connect to the

microcontroller(usually analogue) are quite different from the ones that the microcontroller

comprehends (i.e. one and zero).Thus they have to be converted into a pattern which can be

understood by the microcontroller. This task is performed by the Analog-to -Digital Converter[2].

VII. Bus - It represents a group of 8, 16, or more wires that have the function of

interconnecting the memory and the CPU. They are two types of buses.

Page 30: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

i. Address bus

ii. Data bus

The address bus consists of as many lines as the amount of memory we wish to

address. On the other hand a data bus will be as wide as the data.it can either be 8 bit or 16 bit. The

address is employed to transmit address from CPU memory while the data bus will connect all the

internal components of the microcontroller.

VIII. Watchdog Timer-It acts a free-run counter where our program needs to write a

zero in every time it executes a program correctly. If program counter skips to a memory

location where its not supposed to be, this will allow the chip to recover. This means that the zero

will not be written on the watchdog timer and it will reset the microcontroller.

2.5 Pic architecture

PIC microcontrollers are classified by Microchip into three groups :

a) Baseline-This group includes members of the PIC10, PIC12, and

PIC16 families.

b) Mid-range -The mid-range PIC family includes members of the PIC12

and PIC16 groups.

c) High-performance -The high-performance PICs belong to the PIC18

group.

2.6 PIC16F690

This is a 20 pin count microcontroller from microchip technology. It basically

contains:

Page 31: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

Program memory – 4096 Flash (words)

Data memory

SRAM – 256 bytes

EEPROM – 256 bytes

I/O – 18 pins

10 bit A/D – 12 ch

Comparators -2

Timers

8 bit – 2

16 bit – 1

SSP

ECCP+

EUSTART

2.6.1 PIC16F690 pin diagram

Page 32: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.6.2 PIC16F690 pin summary

Page 33: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

PIC16F690 Pin diagram (QNF)

The PIC16F690 consists of three ports namely :

a. PORTA- Pins 19,18,17,4,3,2

b. PORTB- Pins 13,12,11,10.

c. PORTC- pins 16,15,14,5,6,7,8,9.

A few key observations while looking at the pin out diagram of the above PIC 16F690 are:

i. Pins 1 and 20 act as the power supply and ground of the pic

microcontroller.

ii. Pins 2 and 3 acts as the external oscillator pins.

Page 34: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

iii. Pin 4 is the Master Clear pin

iv. Pins 10 and 12 can be configured as the transmitter and receiver pins

respectively.

v. Pins 19 and 17 can be configured as the comparator1 input and output

respectively

vi. Pins 16 and 6 can be configured as the comparator2 input and output

respectively

vii. Pin 18 can be used as a Voltage reference when using the Analog to

Digital Converter.

2.6.3 PIC16F690 properties

4 x 14 flash memory

256 bytes RAM

256 bytes EEPROM

13 – bit 8 – level stack

Internal oscillator – software selectable 8MHz – 32MHz

2.0 – 5.5V operating voltage

12 channel of 10 – bit A/D converter

Two channel analog comparator

Programmable on chip voltage reference

Auto shut down and restart option

Maximum current source/sunk in all ports combined in 200 mA

Maximum output current sourced/sunk by any input/output pin is 200Ma

Temperature range of -40 to 125 degrees celcius

Page 35: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.6.4 Memory Organization

2.6.4.1 Program Memory Organization

The PIC16F6690 has a 13-bit program counter capable of addressing an 8K x 14 program

memory space. Only the first 4K x 14 (0000h-0FFFh) for the PIC16F690. Accessing a location

above these boundaries will cause a wrap around. The Reset vector is at 0000h and the

interrupt vector is at 0004h

2.6.4.2 Data Memory Organization

Page 36: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

The data memory is partitioned into four banks which contain the General Purpose Registers

(GPR) and the Special Function Registers (SFR). The Special Function Registers are located in the

first 32 locations of each bank. The General Purpose Registers, implemented as static RAM, are

located in the last 96 locations of each Bank. Register locations F0h-FFh in Bank 1, 170h-17Fh

inBank 2 and 1F0h-1FFh in Bank 3 point to addresses 70h-7Fh in Bank 0. The actual number of

General Purpose Resisters (GPR) in each Bank depends on the device. All other RAM is

unimplemented and returns ‘0’ when read. RP<1:0> of the STATUS register are the bankselect

bits:

RP1 RP0

0 0 → Bank 0 is selected

0 1 → Bank 1 is selected

1 0 → Bank 2 is selected

1 1 → Bank 3 is selected

GENERAL PURPOSE REGISTER FILE

The register file is organized as 256 x 8 in the PIC16F690. Each register is

accessed, either directly or indirectly, through the File Select Register (FSR)

SPECIAL FUNCTION REGISTERS

The Special Function Registers are registers used by the CPU and peripheral functions

for controlling the desired operation of the device. These registers are static RAM. The special

registers can be classified into two sets:

o Core

Page 37: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

o Peripheral

Page 38: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

Above are the PIC16F690 Special Function Registers

Page 39: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.6.5 Analog to Digital Converter

The Analog-to-Digital Converter (ADC) allows conversion of an analog input signal to a

10-bit binary representation of that signal. This device uses analog inputs, which are

multiplexed into a single sample and hold circuit. The output of the sample and hold is

connected to the input of the converter. The converter generates a 10-bit binary result via

successive approximation and stores the conversion result into the ADC result registers (ADRESL

and ADRESH).

Page 40: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

ADC CONVERTER BLOCK DIAGRAM

ADC CONFIGURATION

The Analogue to Digital Converter need to be configured before being able to operate. The

following are the steps that need to be understood first before anything else. These are:

i. PORT CONFIGURATION

When converting analog signals, the I/O pins should be configured for analog by setting the

associated TRIS and ANSEL bits.

Page 41: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

ii. CHANNEL SELECTION

This determines which channel is connected to the sample and hold circuit .

iii. ADC VOLTAGE REFERENCE

The VCFG bit of the ADCON0 register provides control of the positive voltage reference.

The positive voltage reference can be either Vdd or an external voltage source.

iv. CONVERSION CLOCK

The source of the conversion clock can be selected via the ADCS bit of the ADCON1 register

There are seven possible clock options :

Fosc/2

Fosc/4

Fosc/8

Fosc/16

Fosc/32

Fosc/64

Frc(dedicated internal oscillator).

v. INTERRUPT CONTROL

Upon completion of the analog-to-digital converter the ADC module allows for generation of

an Interrupt. This interrupt can be generated while the ADC is in operation or while in Sleep. Upon

waking from Sleep, the next instruction following the SLEEP instruction is always

executed

vi. RESULTS FORMATTING

The 10-bit A/D conversion result can be supplied in two formats, left justified or right

justified. The ADFM bit of the ADCON0 register controls the output format.

Page 42: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.6.6 Interrupts

The ADC module allows for the ability to generate an interrupt upon completion of an

Analog-to-Digital conversion. The ADC interrupt flag is the ADIF bit in the PIR1 register. The

ADC interrupt enable is the ADIE bit in the PIE1 register. The ADIF bit must be cleared in

software.

This interrupt can be generated while the device is operating or while in Sleep. If the

device is in Sleep, the interrupt will wake-up the device. Upon waking from Sleep, the next

instruction following the SLEEP instruction is always executed. If the user is attempting to

wake-up from Sleep and resume in-line code execution, the global interrupt must be disabled. If

the global interrupt is enabled, execution will switch to the interrupt service routine.

Page 43: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.6.7 Capture mode

In Capture mode, CCPR1H:CCPR1L captures the 16-bit value of the TMR1 register when an

event occurs on pin CCP1. An event is defined as one of the following and is configured by the

CCP1M<3:0> bits of the CCP1CON register:

• Every falling edge

• Every rising edge

• Every 4th rising edge

• Every 16th rising edge

When a capture is made, the Interrupt Request Flag bit CCP1IF of the PIR1 register is set. The

interrupt flag must be cleared in software. If another capture occurs before the value in the

CCPR1H, CCPR1L register pair is read, the old captured value is overwritten by the new

captured value.

Page 44: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

Above is the capture mode.

2.6.8 Timers

Timers are either 8-bit or 16-bit counters with postscalers and prescalers which divide

down the input or output of the counter to extend its range. They are used in counter or timer

mode and also capture, compare or pulse width modulation. Which was useful in this case.

PIC16F690 has 3 hardware timers:

o Timer 0 (8-bit timer 0)

o Timer 2 (16-bit timer 2)

o Timer 1 (8-bit timer 1)

Page 45: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin
Page 46: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.7 Liquid Crystal Display

HD44780 has 8 bit wide data bus represented by D0-D7. It also has three control lines

RS, RW and E. And three power lines VSS, VDD and VEE. It is a small low cost display easy to

interface with a micro-controller because of an embedded controller. It can connected to the

microcontroller via 11 ports or 7 ports depending on the configuration method chosen.

Below is a diagram of the LCD and a table representing the pins.

Page 47: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

An LCD is an electronic visual display. It uses the light modulating properties of liquid crystals

which do not emit light directly. Using electronically modulated optical device made up of any

number of segments filled with liquid crystals and arrayed in front of a light source or reflector to

produce images in monochrome or color.

Data is displayed when the microcontroller sends data to the LCD via the data lines when pin

E is enabled.

2.7.1 DDRAM - Display Data RAM

Display data RAM (DDRAM) stores display data represented in 8-bit character codes. Its

extended capacity is 80 X 8 bits, or 80 characters. The area in display data RAM (DDRAM) that is not

used for display can be used as general data RAM. So whatever you send on the DDRAM is actually

displayed on the LCD. For LCDs like 1x16, only 16 characters are visible, so whatever you write after 16

chars is written in DDRAM but is not visible to the user.

2.7.2 CGROM - Character Generator ROM

The character generator ROM generates 5 x 8 dot or 5 x 10 dot character patterns from 8-bit

character codes (see Figure 5 and Figure 6 for more details). It can generate 208 5 x 8 dot character

patterns and 32 5 x 10 dot character patterns. Userdefined character patterns are also available by

mask-programmed ROM.

2.7.3CGRAM - Character Generator RAM

CGRAM area is used to create custom characters in LCD. In the character generator RAM, the

user can rewrite character patterns by program. For 5 x 8 dots, eight character patterns can be written,

and for 5 x 10 dots, four character patterns can be written.

2.7.4 BF - Busy Flag

Busy Flag is an status indicator flag for LCD. When we send a command or data to the LCD for

processing, this flag is set (i.e BF =1) and as soon as the instruction is executed successfully this flag is

cleared (BF = 0). This is helpful in producing and exact ammount of delay. for the LCD processing.

To read Busy Flag, the condition RS = 0 and R/W = 1 must be met and The MSB of the LCD data

Page 48: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

bus (D7) act as busy flag. When BF = 1 means LCD is busy and will not accept next command or data and

BF = 0 means LCD is ready for the next command or data to process.

2.7.5 Instruction Register (IR) and Data Register (DR)

There are two 8-bit registers in HD44780 controller Instruction and Data register. Instruction

register corresponds to the register where you send commands to LCD e.g LCD shift command, LCD

clear, LCD address etc. and Data register is used for storing data which is to be displayed on LCD. when

send the enable signal of the LCD is asserted, the data on the pins is latched in to the data register and

data is then moved automatically to the DDRAM and hence is displayed on the LCD.

Data Register is not only used for sending data to DDRAM but also for CGRAM, the address

where you want to send the data, is decided by the instruction you send to LCD. We will discuss more on

LCD instuction set further in this tutorial.

2.7.6 Commands and Instruction set

Only the instruction register (IR) and the data register (DR) of the LCD can be controlled

by the MCU. Before starting the internal operation of the LCD, control information is

temporarily stored into these registers to allow interfacing with various MCUs, which operate at

different speeds, or various peripheral control devices. The internal operation of the LCD is

determined by signals sent from the MCU. These signals, which include register selection signal

(RS), read/write signal (R/W), and the data bus (DB0 to DB7), make up the LCD instructions

(Table 3). There are four categories of instructions that:

Designate LCD functions, such as display format, data length, etc.

Set internal RAM addresses

Perform data transfer with internal RAM

Perform miscellaneous functions

2.7.7 LCD initialization

There are two methods for LCD initialization:

Page 49: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

Initializing by Internal Reset Circuit Initialization by Instruction

2.7.7.1 Initializing by Internal Reset Circuit

This is the datasheet information regarding Initialization of the LCD controller.

The 'Internal Reset' technique described above is relied upon by many programmers but, in my

opinion, this is not a wise choice. Perhaps they are seduced by the first sentence with the promise of

'automatic' initialization. If you look at the result of this automatic initialization you will see that the

controller is configured for a 1-line display when in fact the majority of LCD modules should be

configured for a 2-line display. It also leaves the display off. This means that two of the four steps that

were automatically performed are going to have to be redone.

But that is NOT the main reason that this technique should be avoided. The note at the bottom

clearly states that this technique will fail if the power supply does not meet certain specifications,

specifications that are buried elsewhere in the datasheet. In cases where the power supply cannot be

guaranteed to meet those specification the datasheet recommends using "Initializing by Instruction".

Page 50: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.7.7.2 Initialization by Instruction

It really isn't that hard to use this technique once you decipher the flowcharts that

describe the procedure. All of the flowcharts in the various datasheets seem to derive from the

same source and they are all equally ambiguous in certain areas. Any differences are probably

due to typographical or editing errors and those are easily spotted, providing you compare

several different datasheets.

There are separate initialization flowcharts for the 8-bit interface and the 4-bit interface, but the

actual sequence of instructions sent to the LCD controller is essentially the same in each

case. First there are a series of what are technically Function Set instructions whose purpose is

to effectively 'reset' the LCD controller. Next, if the 4-bit interface is desired, there is an

additional Function Set instruction to change the interface from the default 8-bit

configuration. Finally there are four more instructions, the 'real' Function Set, the Display

on/off Control, the Clear Display, and the Entry Mode Set.

When power is applied to the LCD module the LCD controller always comes up in the 8-bit

interface mode. This means that the LCD controller reads all eight of its data pins each time the

Enable pin is pulsed. This is fine if an 8-bit data interface is actually being used, but what about

the other possibility, where a 4-bit data interface is connected? In this second case there may be

indeterminate data on the lower four bits, especially if those pins have not been grounded as

recommended. The answer is that the controller has been set up to ignore those lower four bits

throughout the early part of the initialization process, until the actual interface has been

established by what I called the 'real' Function Set instruction in the previous paragraph.

It is important to make sure that the LCD controller has finished executing an instruction before

sending it another one, otherwise the second instruction will be ignored. The datasheets give

specific times for the delays during the beginning what I call the 'reset' sequence. The

datasheets are ambiquious when it comes to the time delay associated with the end of this

sequence and with the mode change in the 4-bit initialization. For the final sequence of

instructions the datasheets all specify that you must either check the busy flag to see if the LCD

Page 51: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

controller is finished or wait a sufficient amount of time, a time that is longer than the instruction

execution time.

Below you will find a detailed explanation of the 8-bit initialization sequence followed by a

detailed explanation of the 4-bit initialization sequence.

2.7.7.2.1 8-Bit Interface, Initialization by Instruction

Here's the flowchart as it appears in the Hitachi datasheet.

Page 52: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.7.7.2.2 4-Bit Interface, Initialization by Instruction

Here's the flowchart as it appears in the Hitachi datasheet.

Page 53: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.7.8 LCD instruction set

Page 54: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

2.7.9 LCD connection

This depends on number of lines used which is usually four or eight. There is 4-bit LCD

mode and 8-bit LCD mode. 4 bit LCD mode uses D4-D7 while 8 bit LCD mode uses data pins D0-

D7.

Page 55: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

CHAPTER 3: DESIGN AND IMPLEMENTATION

3.1 Introduction

This chapter seeks to explain in depth the process used in the implementation and

operation of the power quality monitoring unit which is PIC-microcontroller based. In this

chapter the various project segments or breakdowns shall be discussed. The segments are:

Hardware section

Software section

The following flow chart below depicts the sequence of events used in the implementation of

the power quality monitoring unit and power factor measuring system.

Page 56: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

3.2 Hardware section

3.2.1 LCD

The LCD used uses alphanumeric dot matrix. It is used to display a wide range of

characters and Numeric values. The display is on LM016L as seen on the circuit diagram. The

LM016L displays sixteen characters on two lines thus the 16 x 2 display. Each character is 8 x 6

pixels making 80 x 16 pixels. The LCD pins are divided into Command pins (RS, E and RW)

and data pins (D0-D7).

For this project the 4 bit initialization was used instead of the 8 bit initialization. Data

pins D4 – D7 used and E was enabled. A variable resistor is used to control the LCD brightness.

RW is set at 0 thus setting the LCD at write mode.

Page 57: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

3.2.2 Operational Amplifier

The LM358N was used to amplify the generated signal. This is because the signal

produced is small and in order to use falling edge as an interrupt detectable. LM358 is a type of

operational amplifier that consists of two independent, high gain, frequency compensated

operational amplifier designed to operate from a single supply over a wide range of voltages.

Operation from split power supplies is also possible and low power supply current drain

is independent of the magnitude of the power supply.

Application areas include transducer amplifiers, dc gain blocks and all the conventional

op-amp circuits which can be easily implemented in single power supply systems.

It features:

Low power drain.

A common mode input voltage range extending to ground/VEE

Single supply or split supply operation

Compatible pinouts

They operate at at supply voltages as low as 3.0V or as high as 32V. the common mode

input range includes the negative supply thereby eliminating the necessity for external biasing

components in many applications. The output voltage range also includes the negative power

supply voltage.

Page 58: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

3.2.4 Power Supply

The figure below shows the circuit used to implement the stepping down of 240v(AC) to 5V

(DC).The circuit used in supplying the 5 volts was not used due to the cumulative expensive

cost of all the components. Instead alternative and cheaper means of providing a constant

supply of 5v(dc) was used. A mobile phone charger was used in this case.

Page 59: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

3.2.5 Zero Crossing Detection

To measure the time difference between the voltage and current signals it was needed

to get the zero-crossing detection of the two signals. From there the time difference between

these two signals was obtained. There are many ways to detect the zero point of the signals,

but as we are using PIC micro-controllers we can use the advantage of the micro-controller

itself. The micro-controller has a input voltage protection itself, but we can ensure that limit

using a Zener diode across the pins those are used to take the signals.

The sine wave was rectified by diode (1N4007) and then the signal is fed to the interrupt

pin (RB0/INT0; pin # 33) of the micro-controller (PIC16F690). The supply voltage is much more

than 5V, but as that voltage is not directly but through a series resistor (R1-100K) and a

protection Zener diode so the MCU will get just a clipped 5V signal at its pin.

The voltage signal is clipped by the Zener diode and being feed to the micro-controller

interrupt pin so that the signal can be used as a falling edge interrupt.

The current signal is also converter in to this type of falling edge signal by adding some extra

circuitry. As the CT(Current Transformer) generates only a small signal so we need to amplify

this signal to a level so that we can use the falling edge as the interrupt detectable.

current zero crossing detection circuit

Page 60: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

voltage zero crossing detection circuit

Using the hardware interrupt facilities of the micro-controller and the two signals

(voltage and current) were fed and ran. A timer interrupt was used in between these two signals.

A timer was started while MCU gets an interrupt signal [hardware interrupt, falling edge]in INT0

[PIN#33] and stop the timer while MCU gets an interrupt signal [hardware interrupt, falling

edge] in INT1 [PIN# 34]. In between Timer1 module is used to count a variable. Timer1

interrupt of 10uS timing was used. And counting a variable each time the timer overflows.

The variable value is obtained. And from that value the time difference was obtained. At

least 200 data was taken and average of these 200 data calculated for accuracy.

Multiplying the time difference by ∏ (pie) gives the phase difference between current

and voltage signals.

The cosine inverse of the phase angle gave the power factor.

3.3 Programming

Programmer PICKIT2 was used to load the code PIC16F690. The code was written using

MPLABX IDE and MIKROC PRO.

Page 61: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

CHAPTER 4: Results and Analysis

The simulation and practical results are displayed in this chapter.

4.2 Simulation results

The following table shows the simulation results after the circuit was schemated in the proteus

software and loaded with the hex file of the written code.

With no supply no power factor was displayed. Supplying the power and adjusting the phase

angle gave several results that were saved and displayed in the table below

Page 62: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin
Page 63: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

NO PHASE

ANGLE

SETTING

EXPECTED

POWER

FACTOR

SIMULATED RESULTS

1 83 0.12186

2 76 0.24192

3 47 0.68199

4 66 0.40673

Page 64: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

CHAPTER 5: CONCLUSIONS AND RECOMMENDATIONS

5.1 Conclusion

The project was to develop a power quality monitoring unit using PIC16F690 that

monitored the power factor and also recorded the frequency and amplitude of a single phase

outlet.

The experiment objectives were met a seen from the results both simulated and practical.

The resulting circuit was constructed and found to be lighter and portable due to the use of the

PIC.

It wasn’t possible to tell whether the power factor found was leading or lagging therefore

no comment could be made on the type of load present.

There were challenges when it came to the programming but thanks to the supervisor

Prof. E Mwangi the required hex code required was generated.

Page 65: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

5.2 RECOMMENDATIONS

The system designed and implemented monitors power quality. In the research it was

shown how important maintaining the power factor at one is in order to avoid losses. It would be

important to develop a power factor correction circuit

A PIC with more pins should have been used because for instance to load the current

waveform and voltage waveform separately to reduce the number of equipments that had to be

added.

Page 66: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

REFERENCES

[1] Nathan Maclnnes, Electrical Tutorials, “Phase Difference and Phase Shift”. Online

http://www.electronics-tutorials.ws/accircuits/phase-difference.html

http://www.electronics-tutorials.ws/accircuits/acp29.gif

[2] M. Verle, “PIC microcontrollers: your personal introductory course” Elsevia Newnes, 2005

[3] M. Bates, Interfacing PIC Microcontrollers-Embedded Design by Interactive

Simulation, London: Elsevier, 2006

[4] “Electronics Hub,” [Online]. Available: www.ElectronicsHub.com/Types &

Applications of Microcontrollers.

[5] N. Matic, PIC Microcontrollers for beginners.

[6] PIC16F631/677/685/687/689/690Datasheet, Microchip Technology Inc., 2007.

[7] Hitachi HD44780 LCD Display, Page 4 of 4, 17 Nov 1998, Bob Lineberry

[8] LCD Tutorial for interfacing with Microcontrollers Basics of Alphanumeric LCD atRickeysWorldofMicrocontrollers&Microprocessors.html

[9] Technical Information collected by Donald Weiman

H: LCDInitialization.html

[10] Online research for electrical engineers forum

https://mlabsbd.wordpress.com

Page 67: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

APENDIX A

Assembly language code

;************************************************************

; Written by Njogu Swaleh Kamuthiere

; University Of Nairobi

; Electrical and Electronics Department

; Reg No F17/21669/2007

; Date: 09/MAY/2016

; For PIC16F690

; Clock frequency: 4MHz

;**************************************************************

; Project Title: Microcontroller Based Power Quality Monitor

_Interrupt:

MOVWF R15+0

SWAPF STATUS+0, 0

CLRF STATUS+0

MOVWF ___saveSTATUS+0

MOVF PCLATH+0, 0

MOVWF ___savePCLATH+0

CLRF PCLATH+0

; void Interrupt ()

; (TMR1)

MOVF _TMR1+0, 0

IORWF _TMR1+1, 0

BTFSC STATUS+0, 2

GOTO L_Interrupt0

;TMR1 = 0;

CLRF _TMR1+0

CLRF _TMR1+1

Page 68: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

; TMR1L = 0xCD;

MOVLW 205

MOVWF TMR1L+0

; cnt_pf++;

INCF _cnt_pf+0, 1

BTFSC STATUS+0, 2

INCF _cnt_pf+1, 1

; }// Timer0

L_Interrupt0:

; TMR1IE_bit = 1;// enables timer0

BSF TMR1IE_bit+0, BitPos(TMR1IE_bit+0)

; TMR1IF_bit = 0;// clear flag

BCF TMR1IF_bit+0, BitPos(TMR1IF_bit+0)

; cnt_pf = 0;

CLRF _cnt_pf+0

CLRF _cnt_pf+1

; INTF_bit = 0;// clear flag for INT1

BCF INTF_bit+0, BitPos(INTF_bit+0)

L_Interrupt2:

; TMR1IE_bit = 0; //disable timer0

BCF TMR1IE_bit+0, BitPos(TMR1IE_bit+0)

; TMR1IF_bit = 0; // clear flag

BCF TMR1IF_bit+0, BitPos(TMR1IF_bit+0)

; INTF_bit = 0;//clear flag for INT0

BCF INTF_bit+0, BitPos(INTF_bit+0)

; i++;

INCF _i+0, 1

; sample[i] = cnt_pf;

MOVF _i+0, 0

MOVWF R0+0

RLF R0+0, 1

BCF R0+0, 0

Page 69: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVF R0+0, 0

ADDLW _sample+0

MOVWF FSR

MOVF _cnt_pf+0, 0

MOVWF INDF+0

MOVF _cnt_pf+1, 0

INCF FSR, 1

MOVWF INDF+0

; avg_pf+=sample[i];

MOVF _i+0, 0

MOVWF R0+0

RLF R0+0, 1

BCF R0+0, 0

MOVF R0+0, 0

ADDLW _sample+0

MOVWF FSR

MOVF INDF+0, 0

ADDWF _avg_pf+0, 1

INCF FSR, 1

MOVF INDF+0, 0

BTFSC STATUS+0, 0

ADDLW 1

ADDWF _avg_pf+1, 1

; if(i==5)

MOVF _i+0, 0

XORLW 5

BTFSS STATUS+0, 2

GOTO L_Interrupt5

; i = 0;

CLRF _i+0

; avg_pf/=5;

MOVLW 5

Page 70: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVF _avg_pf+0, 0

MOVWF R0+0

MOVF _avg_pf+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVF R0+0, 0

MOVWF _avg_pf+0

MOVF R0+1, 0

MOVWF _avg_pf+1

; final_pf_cnt = avg_pf;

MOVF R0+0, 0

MOVWF _final_pf_cnt+0

MOVF R0+1, 0

MOVWF _final_pf_cnt+1

; }

L_Interrupt5:

; }

L_Interrupt4:

; }

L_end_Interrupt:

L__Interrupt10:

MOVF ___savePCLATH+0, 0

MOVWF PCLATH+0

SWAPF ___saveSTATUS+0, 0

MOVWF STATUS+0

SWAPF R15+0, 1

SWAPF R15+0, 0

RETFIE

; end of _Interrupt

Page 71: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

_main:

; void main()

; TRISA = 0XFF;// All input

MOVLW 255

MOVWF TRISA+0

; TRISB = 0XFF;//set as input

MOVLW 255

MOVWF TRISB+0

; TRISB = 0XFF;//set as input

MOVLW 255

MOVWF TRISB+0

; ADCON0 = 0x00;// all digital inputs

CLRF ADCON0+0

; ADCON1 = 0xFF;

MOVLW 255

MOVWF ADCON1+0

; LCD_Init();

CALL _Lcd_Init+0

; (_LCD_CLEAR); // Clear display

MOVLW 1

MOVWF FARG_Lcd_Cmd_out_char+0

CALL _Lcd_Cmd+0

; LCD_Cmd(_LCD_CURSOR_OFF); // Cursor off

MOVLW 12

MOVWF FARG_Lcd_Cmd_out_char+0

CALL _Lcd_Cmd+0

; T1CON = 0xC8;

MOVLW 200

MOVWF T1CON+0

; TMR1L = 0xCD;

Page 72: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVLW 205

MOVWF TMR1L+0

; GIE_bit = 1;//global interrupt enable

BSF GIE_bit+0, BitPos(GIE_bit+0)

; TMR1IE_bit = 1;//enable timer1

BSF TMR1IE_bit+0, BitPos(TMR1IE_bit+0)

; TMR1IF_bit = 0;//clear flag

BCF TMR1IF_bit+0, BitPos(TMR1IF_bit+0)

; INTCON = 1;//enable INT1

MOVLW 1

MOVWF INTCON+0

; INTE_bit = 0;//interrupt on falling edge

BCF INTE_bit+0, BitPos(INTE_bit+0)

; INTF_bit = 0;//clear flag

BCF INTF_bit+0, BitPos(INTF_bit+0)

; INTCON = 1;//enable INT1

MOVLW 1

MOVWF INTCON+0

; INTE_bit = 0;//interrupt on falling edge

BCF INTE_bit+0, BitPos(INTE_bit+0)

; INTF_bit = 0;//clear flag

BCF INTF_bit+0, BitPos(INTF_bit+0)

; while(1)

L_main6:

; LCD_Out(1,1,"PFC Measurement...");

MOVLW 1

MOVWF FARG_Lcd_Out_row+0

MOVLW 1

MOVWF FARG_Lcd_Out_column+0

MOVLW ?lstr1_pf+0

MOVWF FARG_Lcd_Out_text+0

CALL _Lcd_Out+0

Page 73: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

; message1[3] = final_pf_cnt/1000+48;

MOVLW 232

MOVWF R4+0

MOVLW 3

MOVWF R4+1

MOVF _final_pf_cnt+0, 0

MOVWF R0+0

MOVF _final_pf_cnt+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVLW 48

ADDWF R0+0, 0

MOVWF _message1+3

; message1[4] = (final_pf_cnt/100)%10 +48;

MOVLW 100

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVF _final_pf_cnt+0, 0

MOVWF R0+0

MOVF _final_pf_cnt+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVLW 10

MOVWF R4+0

MOVLW 0

MOVWF R4+1

CALL _Div_16X16_U+0

MOVF R8+0, 0

MOVWF R0+0

MOVF R8+1, 0

MOVWF R0+1

Page 74: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVLW 48

ADDWF R0+0, 0

MOVWF _message1+4

; message1[5] = (final_pf_cnt/10)%10 +48;

MOVLW 10

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVF _final_pf_cnt+0, 0

MOVWF R0+0

MOVF _final_pf_cnt+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVLW 10

MOVWF R4+0

MOVLW 0

MOVWF R4+1

CALL _Div_16X16_U+0

MOVF R8+0, 0

MOVWF R0+0

MOVF R8+1, 0

MOVWF R0+1

MOVLW 48

ADDWF R0+0, 0

MOVWF _message1+5

; message1[6] = final_pf_cnt%10 +48;

MOVLW 10

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVF _final_pf_cnt+0, 0

MOVWF R0+0

Page 75: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVF _final_pf_cnt+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVF R8+0, 0

MOVWF R0+0

MOVF R8+1, 0

MOVWF R0+1

MOVLW 48

ADDWF R0+0, 0

MOVWF _message1+6

; LCD_Out(2,1,message1);

MOVLW 2

MOVWF FARG_Lcd_Out_row+0

MOVLW 1

MOVWF FARG_Lcd_Out_column+0

MOVLW _message1+0

MOVWF FARG_Lcd_Out_text+0

CALL _Lcd_Out+0

; count1 = (float)(final_pf_cnt*2*3.1416)/1000;

MOVF _final_pf_cnt+0, 0

MOVWF R0+0

MOVF _final_pf_cnt+1, 0

MOVWF R0+1

RLF R0+0, 1

RLF R0+1, 1

BCF R0+0, 0

CALL _word2double+0

MOVLW 249

MOVWF R4+0

MOVLW 15

MOVWF R4+1

MOVLW 73

Page 76: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVWF R4+2

MOVLW 128

MOVWF R4+3

CALL _Mul_32x32_FP+0

MOVLW 0

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVLW 122

MOVWF R4+2

MOVLW 136

MOVWF R4+3

CALL _Div_32x32_FP+0

MOVF R0+0, 0

MOVWF _count1+0

MOVF R0+1, 0

MOVWF _count1+1

MOVF R0+2, 0

MOVWF _count1+2

MOVF R0+3, 0

MOVWF _count1+3

; angle = (float)cos(count1);

MOVF R0+0, 0

MOVWF FARG_cos_f+0

MOVF R0+1, 0

MOVWF FARG_cos_f+1

MOVF R0+2, 0

MOVWF FARG_cos_f+2

MOVF R0+3, 0

MOVWF FARG_cos_f+3

CALL _cos+0

MOVF R0+0, 0

Page 77: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVWF _angle+0

MOVF R0+1, 0

MOVWF _angle+1

MOVF R0+2, 0

MOVWF _angle+2

MOVF R0+3, 0

MOVWF _angle+3

; tlong = abs (ceil(angle*1000));

MOVLW 0

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVLW 122

MOVWF R4+2

MOVLW 136

MOVWF R4+3

CALL _Mul_32x32_FP+0

MOVF R0+0, 0

MOVWF FARG_ceil_x+0

MOVF R0+1, 0

MOVWF FARG_ceil_x+1

MOVF R0+2, 0

MOVWF FARG_ceil_x+2

MOVF R0+3, 0

MOVWF FARG_ceil_x+3

CALL _ceil+0

CALL _double2int+0

MOVF R0+0, 0

MOVWF FARG_abs_a+0

MOVF R0+1, 0

MOVWF FARG_abs_a+1

CALL _abs+0

Page 78: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVF R0+0, 0

MOVWF _tlong+0

MOVF R0+1, 0

MOVWF _tlong+1

MOVLW 0

BTFSC _tlong+1, 7

MOVLW 255

MOVWF _tlong+2

MOVWF _tlong+3

; message2[13] = (tlong/1000)+48;

MOVLW 232

MOVWF R4+0

MOVLW 3

MOVWF R4+1

CLRF R4+2

CLRF R4+3

MOVF _tlong+0, 0

MOVWF R0+0

MOVF _tlong+1, 0

MOVWF R0+1

MOVF _tlong+2, 0

MOVWF R0+2

MOVF _tlong+3, 0

MOVWF R0+3

CALL _Div_32x32_S+0

MOVLW 48

ADDWF R0+0, 0

MOVWF _message2+13

; message2[14] = '.';

MOVLW 46

MOVWF _message2+14

; message2[15] = (tlong/100)%10+48;

Page 79: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVLW 100

MOVWF R4+0

CLRF R4+1

CLRF R4+2

CLRF R4+3

MOVF _tlong+0, 0

MOVWF R0+0

MOVF _tlong+1, 0

MOVWF R0+1

MOVF _tlong+2, 0

MOVWF R0+2

MOVF _tlong+3, 0

MOVWF R0+3

CALL _Div_32x32_S+0

MOVLW 10

MOVWF R4+0

CLRF R4+1

CLRF R4+2

CLRF R4+3

CALL _Div_32x32_S+0

MOVF R8+0, 0

MOVWF R0+0

MOVF R8+1, 0

MOVWF R0+1

MOVF R8+2, 0

MOVWF R0+2

MOVF R8+3, 0

MOVWF R0+3

MOVLW 48

ADDWF R0+0, 0

MOVWF _message2+15

; message2[16] = (tlong/10)%10 + 48;

Page 80: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVLW 10

MOVWF R4+0

CLRF R4+1

CLRF R4+2

CLRF R4+3

MOVF _tlong+0, 0

MOVWF R0+0

MOVF _tlong+1, 0

MOVWF R0+1

MOVF _tlong+2, 0

MOVWF R0+2

MOVF _tlong+3, 0

MOVWF R0+3

CALL _Div_32x32_S+0

MOVLW 10

MOVWF R4+0

CLRF R4+1

CLRF R4+2

CLRF R4+3

CALL _Div_32x32_S+0

MOVF R8+0, 0

MOVWF R0+0

MOVF R8+1, 0

MOVWF R0+1

MOVF R8+2, 0

MOVWF R0+2

MOVF R8+3, 0

MOVWF R0+3

MOVLW 48

ADDWF R0+0, 0

MOVWF _message2+16

; message2[17] = (tlong)%10 + 48;

Page 81: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVLW 10

MOVWF R4+0

CLRF R4+1

CLRF R4+2

CLRF R4+3

MOVF _tlong+0, 0

MOVWF R0+0

MOVF _tlong+1, 0

MOVWF R0+1

MOVF _tlong+2, 0

MOVWF R0+2

MOVF _tlong+3, 0

MOVWF R0+3

CALL _Div_32x32_S+0

MOVF R8+0, 0

MOVWF R0+0

MOVF R8+1, 0

MOVWF R0+1

MOVF R8+2, 0

MOVWF R0+2

MOVF R8+3, 0

MOVWF R0+3

MOVLW 48

ADDWF R0+0, 0

MOVWF _message2+17

; LCD_Out(3,1,message2);

MOVLW 3

MOVWF FARG_Lcd_Out_row+0

MOVLW 1

MOVWF FARG_Lcd_Out_column+0

MOVLW _message2+0

MOVWF FARG_Lcd_Out_text+0

Page 82: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

CALL _Lcd_Out+0

; Delay_ms(100);

MOVLW 2

MOVWF R11+0

MOVLW 4

MOVWF R12+0

MOVLW 186

MOVWF R13+0

L_main8:

DECFSZ R13+0, 1

GOTO L_main8

DECFSZ R12+0, 1

GOTO L_main8

DECFSZ R11+0, 1

GOTO L_main8

NOP

; }// while

GOTO L_main6

; }// void main

L_end_main:

GOTO $+0

; end of _main

Page 83: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

APENDIX B

Assembly language code

;************************************************************

; Written by Njogu Swaleh Kamuthiere

; University Of Nairobi

; Electrical and Electronics Department

; Reg No F17/21669/2007

; Date: 09/MAY/2016

; For PIC16F690

; Clock frequency: 4MHz

;**************************************************************

; Project Title: Microcontroller Based Power Quality Monitor Frequency and Amplitude measurement

_Display_Freq:

;ACPARAMETERS.c,27 :: void Display_Freq(unsigned int freq2write)

;ACPARAMETERS.c,29 :: freq[1] = (freq2write/10) + 48; // Extract tens digit

INCF _freq+0, 0

MOVWF FLOC__Display_Freq+0

MOVLW 10

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVF FARG_Display_Freq_freq2write+0, 0

MOVWF R0+0

MOVF FARG_Display_Freq_freq2write+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVLW 48

Page 84: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

ADDWF R0+0, 1

MOVF FLOC__Display_Freq+0, 0

MOVWF FSR

MOVF R0+0, 0

MOVWF INDF+0

;ACPARAMETERS.c,30 :: freq[2] = (freq2write/1)%10 + 48; // Extract ones digit

MOVLW 2

ADDWF _freq+0, 0

MOVWF FLOC__Display_Freq+0

MOVLW 10

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVF FARG_Display_Freq_freq2write+0, 0

MOVWF R0+0

MOVF FARG_Display_Freq_freq2write+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVF R8+0, 0

MOVWF R0+0

MOVF R8+1, 0

MOVWF R0+1

MOVLW 48

ADDWF R0+0, 1

MOVF FLOC__Display_Freq+0, 0

MOVWF FSR

MOVF R0+0, 0

MOVWF INDF+0

;ACPARAMETERS.c,32 :: Lcd_Out(2, 11, freq);

Page 85: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVLW 2

MOVWF FARG_Lcd_Out_row+0

MOVLW 11

MOVWF FARG_Lcd_Out_column+0

MOVF _freq+0, 0

MOVWF FARG_Lcd_Out_text+0

CALL _Lcd_Out+0

;ACPARAMETERS.c,33 :: Lcd_Out(2,14,"Hz");

MOVLW 2

MOVWF FARG_Lcd_Out_row+0

MOVLW 14

MOVWF FARG_Lcd_Out_column+0

MOVLW ?lstr2_ACPARAMETERS+0

MOVWF FARG_Lcd_Out_text+0

CALL _Lcd_Out+0

;ACPARAMETERS.c,34 :: }

L_end_Display_Freq:

RETURN

; end of _Display_Freq

_main:

;ACPARAMETERS.c,35 :: void main()

;ACPARAMETERS.c,37 :: TRISA = 0xFF; // set all pins of PORT A as input

MOVLW 255

MOVWF TRISA+0

;ACPARAMETERS.c,38 :: TRISB = 0x00; // set all pins as output

CLRF TRISB+0

;ACPARAMETERS.c,39 :: PORTC = 0x00; // clear port C

Page 86: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

CLRF PORTC+0

;ACPARAMETERS.c,40 :: PORTB = 0x00; // clear port B

CLRF PORTB+0

;ACPARAMETERS.c,41 :: ADCON1=0x00; // set all Analog

CLRF ADCON1+0

;ACPARAMETERS.c,42 :: Delay_ms(1000);

MOVLW 11

MOVWF R11+0

MOVLW 38

MOVWF R12+0

MOVLW 93

MOVWF R13+0

L_main0:

DECFSZ R13+0, 1

GOTO L_main0

DECFSZ R12+0, 1

GOTO L_main0

DECFSZ R11+0, 1

GOTO L_main0

NOP

NOP

;ACPARAMETERS.c,44 :: OPTION_REG = 0b00101000; // set TOCKI as clock counter

MOVLW 40

MOVWF OPTION_REG+0

;ACPARAMETERS.c,46 :: Lcd_Init();

CALL _Lcd_Init+0

;ACPARAMETERS.c,47 :: Lcd_Cmd(_LCD_CLEAR);

MOVLW 1

MOVWF FARG_Lcd_Cmd_out_char+0

Page 87: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

CALL _Lcd_Cmd+0

;ACPARAMETERS.c,48 :: Lcd_Cmd(_LCD_CURSOR_OFF);

MOVLW 12

MOVWF FARG_Lcd_Cmd_out_char+0

CALL _Lcd_Cmd+0

;ACPARAMETERS.c,50 :: Lcd_Out(1,1,"Measurement of");

MOVLW 1

MOVWF FARG_Lcd_Out_row+0

MOVLW 1

MOVWF FARG_Lcd_Out_column+0

MOVLW ?lstr3_ACPARAMETERS+0

MOVWF FARG_Lcd_Out_text+0

CALL _Lcd_Out+0

;ACPARAMETERS.c,51 :: Lcd_Out(2,1,"AC parameter ");

MOVLW 2

MOVWF FARG_Lcd_Out_row+0

MOVLW 1

MOVWF FARG_Lcd_Out_column+0

MOVLW ?lstr4_ACPARAMETERS+0

MOVWF FARG_Lcd_Out_text+0

CALL _Lcd_Out+0

;ACPARAMETERS.c,52 :: Delay_ms(2000);

MOVLW 21

MOVWF R11+0

MOVLW 75

MOVWF R12+0

MOVLW 190

MOVWF R13+0

L_main1:

Page 88: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

DECFSZ R13+0, 1

GOTO L_main1

DECFSZ R12+0, 1

GOTO L_main1

DECFSZ R11+0, 1

GOTO L_main1

NOP

;ACPARAMETERS.c,54 :: Lcd_Cmd(_LCD_CLEAR);

MOVLW 1

MOVWF FARG_Lcd_Cmd_out_char+0

CALL _Lcd_Cmd+0

;ACPARAMETERS.c,56 :: while(1)

L_main2:

;ACPARAMETERS.c,59 :: max_point0 = 0;

CLRF _max_point0+0

CLRF _max_point0+1

;ACPARAMETERS.c,60 :: for(i=0;i<5000;i++)

CLRF _i+0

CLRF _i+1

L_main4:

MOVLW 19

SUBWF _i+1, 0

BTFSS STATUS+0, 2

GOTO L__main12

MOVLW 136

SUBWF _i+0, 0

L__main12:

BTFSC STATUS+0, 0

GOTO L_main5

Page 89: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

;ACPARAMETERS.c,62 :: ADCON0 = 0b00000001;

MOVLW 1

MOVWF ADCON0+0

;ACPARAMETERS.c,63 :: if(temp0 = ADC_Read(0),temp0>max_point0)

CLRF FARG_ADC_Read_channel+0

CALL _ADC_Read+0

MOVF R0+0, 0

MOVWF _temp0+0

MOVF R0+1, 0

MOVWF _temp0+1

MOVF R0+1, 0

SUBWF _max_point0+1, 0

BTFSS STATUS+0, 2

GOTO L__main13

MOVF R0+0, 0

SUBWF _max_point0+0, 0

L__main13:

BTFSC STATUS+0, 0

GOTO L_main7

;ACPARAMETERS.c,65 :: max_point0 = temp0;

MOVF _temp0+0, 0

MOVWF _max_point0+0

MOVF _temp0+1, 0

MOVWF _max_point0+1

;ACPARAMETERS.c,66 :: }

L_main7:

;ACPARAMETERS.c,67 :: Delay_us(5);

MOVLW 3

MOVWF R13+0

Page 90: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

L_main8:

DECFSZ R13+0, 1

GOTO L_main8

;ACPARAMETERS.c,60 :: for(i=0;i<5000;i++)

INCF _i+0, 1

BTFSC STATUS+0, 2

INCF _i+1, 1

;ACPARAMETERS.c,68 :: }

GOTO L_main4

L_main5:

;ACPARAMETERS.c,70 :: max_point0 = abs(ceil((long)max_point0));

MOVF _max_point0+0, 0

MOVWF R0+0

MOVF _max_point0+1, 0

MOVWF R0+1

CLRF R0+2

CLRF R0+3

CALL _longint2double+0

MOVF R0+0, 0

MOVWF FARG_ceil_x+0

MOVF R0+1, 0

MOVWF FARG_ceil_x+1

MOVF R0+2, 0

MOVWF FARG_ceil_x+2

MOVF R0+3, 0

MOVWF FARG_ceil_x+3

CALL _ceil+0

CALL _double2int+0

MOVF R0+0, 0

Page 91: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVWF FARG_abs_a+0

MOVF R0+1, 0

MOVWF FARG_abs_a+1

CALL _abs+0

MOVF R0+0, 0

MOVWF _max_point0+0

MOVF R0+1, 0

MOVWF _max_point0+1

;ACPARAMETERS.c,72 :: tlong0 = (long)max_point0*(5/255)*11*220/12; // find the peak amplitude of the line

CLRF _tlong0+0

CLRF _tlong0+1

;ACPARAMETERS.c,73 :: tlong1 = (long)max_point0*((5/255)*11*220/12)*0.707; // find the rms value of the linevoltage

CLRF R0+0

CLRF R0+1

CLRF R0+2

CLRF R0+3

CALL _longint2double+0

MOVLW 244

MOVWF R4+0

MOVLW 253

MOVWF R4+1

MOVLW 52

MOVWF R4+2

MOVLW 126

MOVWF R4+3

CALL _Mul_32x32_FP+0

CALL _double2word+0

MOVF R0+0, 0

Page 92: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVWF _tlong1+0

MOVF R0+1, 0

MOVWF _tlong1+1

;ACPARAMETERS.c,75 :: message1[3] = tlong0/100 + 48; // convert into ASCII

MOVLW 48

MOVWF _message1+3

;ACPARAMETERS.c,76 :: message1[4] = (tlong0/10)%10 +48; // convert into ASCII

MOVLW 48

MOVWF _message1+4

;ACPARAMETERS.c,77 :: message1[5] = (tlong0)%10 +48; // convert into ASCII

MOVLW 48

MOVWF _message1+5

;ACPARAMETERS.c,78 :: Lcd_Out(1,1,message1); // Display message on LCD

MOVLW 1

MOVWF FARG_Lcd_Out_row+0

MOVLW 1

MOVWF FARG_Lcd_Out_column+0

MOVLW _message1+0

MOVWF FARG_Lcd_Out_text+0

CALL _Lcd_Out+0

;ACPARAMETERS.c,80 :: RC0_bit = ~RC0_bit;

MOVLW

XORWF RC0_bit+0, 1

;ACPARAMETERS.c,82 :: message2[4] = tlong1/100 + 48; // convert into ASCII

MOVLW 100

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVF _tlong1+0, 0

Page 93: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVWF R0+0

MOVF _tlong1+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVLW 48

ADDWF R0+0, 0

MOVWF _message2+4

;ACPARAMETERS.c,83 :: message2[5] = (tlong1/10)%10 +48; // convert into ASCII

MOVLW 10

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVF _tlong1+0, 0

MOVWF R0+0

MOVF _tlong1+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVLW 10

MOVWF R4+0

MOVLW 0

MOVWF R4+1

CALL _Div_16X16_U+0

MOVF R8+0, 0

MOVWF R0+0

MOVF R8+1, 0

MOVWF R0+1

MOVLW 48

ADDWF R0+0, 0

MOVWF _message2+5

Page 94: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

;ACPARAMETERS.c,84 :: message2[6] = (tlong1)%10 +48; // convert into ASCII

MOVLW 10

MOVWF R4+0

MOVLW 0

MOVWF R4+1

MOVF _tlong1+0, 0

MOVWF R0+0

MOVF _tlong1+1, 0

MOVWF R0+1

CALL _Div_16X16_U+0

MOVF R8+0, 0

MOVWF R0+0

MOVF R8+1, 0

MOVWF R0+1

MOVLW 48

ADDWF R0+0, 0

MOVWF _message2+6

;ACPARAMETERS.c,85 :: Lcd_Out(1,9,message2); // Display message on LCD

MOVLW 1

MOVWF FARG_Lcd_Out_row+0

MOVLW 9

MOVWF FARG_Lcd_Out_column+0

MOVLW _message2+0

MOVWF FARG_Lcd_Out_text+0

CALL _Lcd_Out+0

;ACPARAMETERS.c,87 :: TMR0=0; // clear TMR0

CLRF TMR0+0

;ACPARAMETERS.c,88 :: Delay_ms(1000); // Delay 1 Sec

MOVLW 11

Page 95: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVWF R11+0

MOVLW 38

MOVWF R12+0

MOVLW 93

MOVWF R13+0

L_main9:

DECFSZ R13+0, 1

GOTO L_main9

DECFSZ R12+0, 1

GOTO L_main9

DECFSZ R11+0, 1

GOTO L_main9

NOP

NOP

;ACPARAMETERS.c,89 :: Lcd_Out(2,1,"FREQUENCY:");

MOVLW 2

MOVWF FARG_Lcd_Out_row+0

MOVLW 1

MOVWF FARG_Lcd_Out_column+0

MOVLW ?lstr5_ACPARAMETERS+0

MOVWF FARG_Lcd_Out_text+0

CALL _Lcd_Out+0

;ACPARAMETERS.c,90 :: Display_Freq(TMR0/2); // divide by 2

MOVF TMR0+0, 0

MOVWF FARG_Display_Freq_freq2write+0

CLRF FARG_Display_Freq_freq2write+1

RRF FARG_Display_Freq_freq2write+0, 1

BCF FARG_Display_Freq_freq2write+0, 7

MOVLW 0

Page 96: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

MOVWF FARG_Display_Freq_freq2write+1

CALL _Display_Freq+0

;ACPARAMETERS.c,91 :: TMR0=0;

CLRF TMR0+0

;ACPARAMETERS.c,93 :: }// while

GOTO L_main2

;ACPARAMETERS.c,94 :: }// void main

L_end_main:

GOTO $+0

; end of _main

Page 97: UNIVERSITY OF NAIROBI DEPARTMENT OF …eie.uonbi.ac.ke/sites/default/files/cae/engineering/eie/POWER...DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING ... Fig 2-6 PIC16F690 Pin

START

PROJECT PLAN

PROJECT DESIGN

HARDWARE DESIGN SOFTWARE DESIGN

HARDWARESELECTION

PROGRAMMER SELECTION

CIRCUIT SIMULATION

CIRCUIT CONSTRUCTION

IS THE CIRCUIT WORKING

WRITE CODE

IS THE CODE WORKING

NO

NO

LINK CIRCUIT WITH PROGRAM

FINAL TEST

FINISH

YES

YES