digital counter

75

Click here to load reader

Upload: 080787

Post on 12-Mar-2015

543 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Digital Counter

DESIGN AND CONSTRUCTION OF

DIGITAL COUNTER

BY

OWOEYE AKINTAYO DAMILOLA030403079

PROJECT REPORT SUBMITTED TO THE DEPARTMENT OF ELECTRICAL/ELECTRONICS ENGINEERING IN PARTIAL FULFILLMENT

OF THE REQUIREMENTS FOR THE AWARD OF

BACHELOR OF SCIENCE (HONOURS) DEGREE

IN

ELECTRICAL/ELECTRONICS ENGINEERING

Project Supervisor: Engr. O.D OSUNDE

NOVEMBER 2009

Page 2: Digital Counter

CERTIFICATION

This is to certify that OWOEYE DAMILOLA AKINTAYO carried out the design and

construction of a DIGITAL COUNTER, as partial fulfillment of the requirements for the

award of the degree of Bachelor of Science (Honours) in Electrical/Electronic Engineering,

University of Lagos.

______________________________ ______________________________

OWOEYE DAMILOLA ENGINEER 0.D OSUNDE

030403079 PROJECT SUPERVISOR

_______________________________ ______________________________

Dr. A.A AYORINDE Dr. F.N OKAFOR

PROJECT COORDINATOR H.O.D ELECT /ELECT ENGR

ii

Page 3: Digital Counter

DEDICATION

I dedicate my soul, my life, my health, my education and finally this project work;

To the Lord of Creation - My creation and that of many others;

To the Owner of Judgment Day - My judgment and that of many others;

To the Master of Death - My death and that of many others;

To the Source of Peace, Love and Compassion - My peace and that of many others;

To you my Lord, the ALL-KNOWING, the ALL-SEEING

In addition, my dedication goes to my mum and family, for always giving me the best.

Finally, I dedicate this project to all those that always expect the best from me, to true

Christians and anyone who cares to read this page.

iii

Page 4: Digital Counter

ACKNOWLEDGEMENT

All appreciation goes to the Maker of all that is in the Heavens and on the Earth. My warmth and sincere acknowledgement goes to my dearest mum ‘Maâmi’; Mrs. Monisola Vicoria Awoyinfa, for her love, care and discipline since pregnancy until date. I know cannot pay you back, but I am sure GOD will bountifully, reward you graciously in this world and in the Hereafter.

My gratitude goes totally to all those that aroused my interest in the field electrical/electronic/computer engineering and information technology, especially Tunde Lawal, Joseph Areghan for accommodating me, and always putting me on the right track.

I also acknowledge my friends for always being friends. The list is countless, but I will always remember the following people; Ayodele Ajayi, Efe Omoruyi Steven, Israel Gbose, Komolafe Akinyemi, Kinsley Akwe, Stanley Opara, and to all those in EGRAD’ 09. I thank them for making my final year, a challenging and interesting year.

Finally, I say Thank you sir, ENGR O.D OSUNDE, for his high-level of knowledge and discipline and commitment towards the development of young minds.

iv

Page 5: Digital Counter

TABLE OF CONTENT

Certification ii

Dedication iii

Acknowledgement iiv

Table of Content vi

List of Figures vii

Abstract1

Chapter One: Introduction 21.1 Background 2

1.2 Objectives of Project 3

1.3 Relevance of Project 4

Chapter Two: Literature Review

2.1 Power Supply 6

2.2 Liquid Crystal Display (PCD 8544) 7

2.3 Microcontroller (PIC micro) 10

Chapter Three: Design Methodology

3.1 Design Specification 14

3.2 Block Diagram 15

3.3 Design Analysis 16

3.4 Circuit Diagram 17

3.5 Circuit Analysis 18

v

Page 6: Digital Counter

3.6 Program Code Analysis 20

Chapter Four: Design Construction 23

4.1 PCB Layout 24

4.2 Software Burning 25

4.3 Hardware Troubleshooting and debugging 26

4.4 Simulation/Testing 27

Chapter Five: Conclusion 29

5.1 Discussion of result 29

5.2 Conclusion 29

5.2 Recommendation 29

References 30

Appendix I 31

Appendix II 46

Appendix III 47

vi

Page 7: Digital Counter

LIST OF FIGURES

Figure 1.1 Typical door 3

Figure 2.1: Power Supply Units 6

Figure 2.2: Liquid Crystal Display (PCD 8544): 8

Figure 2.3: Mid-range PIC Memory (Harvard architecture) 10

Figure 2.4: USB PIC micro Programmer 11

Figure 2.5: PIC 16F874A and PIC16F877 pin diagrams 12

Figure 2.6: Program memory map and stack for high-performance microcontroller 13

Figure 3.1: Block Diagram of system 15

Figure 3.2: Circuit Diagram of a Digital Counter 16

Figure 4.3: PCB Layout of Main Circuit 17

Figure 3.3: A Typical PIC16f877A 18

Figure 4.1: Circuit Diagram of the Power Supply 24

Figure 4.2 : WinPic800 USB Programmer software 26

vii

Page 8: Digital Counter

viii

Page 9: Digital Counter

ABSTRACT

The clamor for the need to stop Nigeria as a dumping ground for all electronic devices, and

the development of the talented Nigerian engineers, has gingered this sincere research.

Bearing in mind that, the field of microelectronics and embedded systems is very large, only

control and security systems have gained interest, and has prompted the design and

construction of a Digital Counter.

A digital counter is a device used for counting the occurrence of an event. The main purpose

of designing and constructing this device is to provide an alternative to and perhaps a more

effective way of counting the occurrence of an event. The device is able to detect occurrence

of an event as long as the line sight between transmitter and receiver and is broken. Physical

counting is important because of the need to know how much in order to cater for how many.

This report presents a very simple, interactive, secured and low-cost design. The design uses

a PIC16f877A microcontroller chip. The chip is in a family of high performance

microcontrollers with ICSP (In-Circuit Serial Programming).

The microcontroller is interfaced with a LCD display PCD8544.Finally, the design and

construction was professionally done, with industry standard software and tools. The

microcontroller was programmed using Microchip’s MPLAB v8.30, while the design and

simulation of the project circuits were accomplished using Labcenter’s Proteus Professional

7.5. In addition, the final construction was done on a PCB (printed circuit board), with model

board packaging.

1

Page 10: Digital Counter

CHAPTER ONE

INTRODUCTION

Background

Counting has a very useful application in monitoring visitor’s circulation and in

security related issues. By visitor circulation we mean the number of INS and OUT of people

(visitors) in a place and over a particular period of time. In area of security the device can be

used as an object counter for example, if you have 10 items and one goes missing. Also has

application in parking lot where there are limited spaces for cars. With a counter it is easily

known where the allocated number of spaces for the cars in the parking lot is exceeded.

Previously this exercise has been done manually which leads to errors, and

sometimes disorganization but with a counter these errors and disorganization can be

avoided. This device can also be used in Airports, Stadium, Churches, Mosques, Parking lots

and even in lecture rooms where attendance taking is very important.

A digital counter has a variety of application but for the purpose of this project we

are going to concentrate on two areas which are the areas of visitor’s circulation and security

which most of the time go hand in hand.

In visitor’s circulation, if a place exists, it is imperative for the managers of the

place to know the exact INS and OUT so as to be able to know when to provide properly for

other services that would encourage more visitors to come to the place. It is not until people

start standing in a place they should be sitting that one should know when to provide extra

seats. This is applicable in airports, museums etc where there is constant flow of people.

Another application of digital object counters is stadium where visitor’s circulation and

security is important. Stadiums usually have existing capacities. Organizers of some events in

2

Page 11: Digital Counter

such places most times sell more tickets than the allotted capacity. With a digital counter, the

stadium manager can ensure that the stadium’s capacity is not exceeded during such an event.

This will ensure that manager has control over the crowd coming for such an event. In

museums, a digital counter can also be very useful because many people come into and out of

a museum with a counter when the average number of people increases, the managers of the

museum will be able to provide customers related services better.

Figure 1.1 Typical door

3

Page 12: Digital Counter

Objectives of the Project

Accurate counting is very important. As mentioned earlier to cater for how much you have to

know how many. It is not that this can’t be done manually but usually, it is very difficult

especially when the objects to be counted are much. Mistakes are often made or an object or

an object may be counted twice.

Method of design I have used can be later on is this report. I have used this mode of design

because of it is not too difficult to understand. It can be designed by anybody that has a good

background of digital electronics .The unique part of my design is the programming of the

micro controller chip to realize the necessary logic

The system is to implement a low power consuming, cost effective, and secured design. The

design is flexible, interactive and sophisticated to suit any application.

4

Page 13: Digital Counter

Relevance of the Project

This project finds application where added security measure is to be put in place to minimize

the entry of unauthorized personnel into any restricted area such as in entrances, safety

vestibules, corridors inmate housing and administrative areas.

The project is ideally suited for the following:

High Security Applications

Dormitories

Assisted Living Facilities

Hospitals

Retail Buildings

Meeting Rooms

Office complexes, Banks

Stairwell doors, et.c

The design is extensible to any security barrier or remote keyless entry system4.

5

Abdulsalam Abdullateef, 11/11/09,
Reference: Chuck Simmers, Microchip Technology Inc, Remote Keyless Entry and Convenience Center ReferenceDesign with LIN Bus Interface
Page 14: Digital Counter

CHAPTER TWO

LITERATURE REVIEW

The technology of a Digital Counter has been in existence, since the advent of digital

authorizing systems. Theoretically, the system is based on basic electrical and electronic

devices such as resistors, capacitors, diodes, transistors, motors, switches and

microcontrollers.

The building blocks of this project make use of these devices and many more, with effective

design considerations. The theories behind the building blocks of this project are as follows;

Power Supply:

Standard logic circuits require power supply of about ±5V DC. One possible source of the 5V

DC is the use of one or more batteries connected in series. But a reliable source is the

rectified AC voltage that will provide continuous power supply.

To obtain +5V DC from the usual 239.6V AC, 50Hz, requires scaling down of the voltage

using a power transformer and converting from alternating current to pulsating direct current,

using a bridge rectifier. A smoothing filter (usually a capacitor) is used to convert the

pulsating direct current to direct current. A voltage regulator is incorporated to ensure

constant DC voltage, in case of variation in load or input voltage.

The block diagram shows the stages involved in a power supply unit.

Figure 2.1: Power Supply Units

6

Transformatio

n

Rectification

Filteration

Regulation

ac input dc output

Page 15: Digital Counter

Liquid Crystal Display (PCD 8544):

As is the case with input devices, electronic systems must provide data output in a human-

readable form to increase interactivity. One of the numerous output technologies is the liquid

crystal display. A liquid crystal display (LCD) is a pixilated output device capable of

displaying ASCII characters and dot-based graphics. LCDs can be color or monochrome

according to their construction. One of the advantages of LCD display when compared to

other output device such as seven-segment LED is the very low power consumption, making

it suitable for battery-powered devices. Hitachi LCD displays also have a standard ASCII set

of characters plus Japanese, Greek and mathematical symbols. In operation, the liquid crystal

display consists of two pieces of polarized glass with perpendicular axes of polarity.

The PCD8544 is a low power CMOS LCD controller/driver, designed to drive a graphic display

of 48 rows and 84 columns. All necessary functions for the display are provided in a single

chip, including on-chip generation of LCD supply and bias voltages, resulting in a minimum

of external components and low power consumption. The PCD8544 interfaces to

microcontrollers through a serial bus interface. The PCD8544 is manufactured in n-well

CMOS technology.

7

Page 16: Digital Counter

Figure 2.2: Liquid Crystal Display (PCD 8544):

Features

Single chip LCD controller/driver

48 row, 84 column outputs

Display data RAM 48 x 84 bits

8

Page 17: Digital Counter

On-chip:

Generation of LCD supply voltage (external supply also possible)

Generation of intermediate LCD bias voltages

Oscillator requires no external components (external clock also possible).

External RES (reset) input pin

Serial interface maximum 4.0 Mbits/s

CMOS compatible inputs

Mux rate: 48

Logic supply voltage range VDD to VSS : 2.7 to 3.3 V

Display supply voltage range VLCD to VSS

6.0 to 8.5 V with LCD voltage internally generated (voltage generator enabled)

6.0 to 9.0 V with LCD voltage externally supplied (voltage generator switched-off).

Low power consumption, suitable for battery operated systems

Temperature compensation of VLCD

Temperature range: -25 to +70 °C.

9

Page 18: Digital Counter

Microcontroller (PICmicro):

A microcontroller is a type of microprocessor furnished in a single integrated circuit to serve

as an intelligent core for specialized dedicated systems. Microcontrollers are embedded in

many control, monitoring, and processing systems. A microcontroller usually includes a

central processor, input and output ports, memory for program and data storage, an internal

clock, and one or more peripheral devices such as timers, counters, analog-to-digital

converters, serial communication facilities, and watchdog circuits1.

Among the various manufacturers of microcontrollers are Intel (such as the 8051 and the

Pentium), Zilog (derivatives of Z-80 microprocessor), Motorola (such as the 68HC05), Atmel

(the AVR), Parallax (the BASIC Stamp)1, and Microchip. PICmicro is a family of

microcontrollers made by Microchip Technology.

PIC (Programmable IC / Programmable Interface Controller)1 microcontrollers, do not use

the conventional Von Neumann architecture, instead they use the Harvard architecture1. That

is, the microcontroller can read and write instructions and data to and from memory at the

same time.

Figure 2.3: Mid-range PIC Memory (Harvard architecture)

PICs use an instruction set that varies in length from about 33 instructions for the low-end

PICs to more than 70 for the high-end devices. PIC does not implement the CISC (Complete

Instruction Set Computer) design, but uses the RISC1 (Reduced Instruction Set Computer)

design1 because fewer instructions are required to perform basic operations.

The PICs are programmable in their native Assembly Language. Programming the PIC

microcontroller requires the following tools and components:

10

Your User Name, 11/11/09,
Pg143 micro book
Your User Name, 11/11/09,
Pg 142…..micro book
Your User Name, 11/11/09,
Pg129….Microcontroller Programming book
Your User Name, 11/11/09,
Pg129………… Microcontroller Programming book
Page 19: Digital Counter

1. An Assembler or high-level language compiler. The software package usually

includes a debugger, simulator, and other support programs.

2. A computer (usually a PC) in which to run the development software such as

MPLAB.

3. A hardware device called a programmer that connects to the computer through the

serial, parallel, or USB line. The PIC is inserted in the programmer and “blown” by

downloading the executable code generated by the development system. The

hardware programmer usually includes the driver or support software.

4. A cable or connector for connecting the programmer to the computer.

5. A PIC microcontroller.

Figure 2.4: USB PICmicro Programmer

Microchip as a company has classified the PICmicro architecture into three groups; namely

the baseline, mid-range, and high-performance.

Baseline family: These are low-cost, low - power, 8-bit

flash/EEPROM/EPROM/ROM-based CMOS microcontrollers. They use RISC

architecture and have 33 single - word, single-cycle instructions. This group includes

members of the PIC10, PIC12, and PIC14 families. They have 12-bit program words

and have 6- to 28-pin packages. Popular features and peripherals common to this

family are 4-20MHz clock frequency, Timer module, Wake-up from SLEEP, 8-bit

A/D converter, In-Circuit Serial Programming, Interrupt, Internal Pull-Up and

EEPROM data memory.

Mid-range family: This is by far the most extensive family. These are

microcontrollers with 14-bit program words, with either flash or OTP program

memory. This group includes members of the PIC12, and PIC16 families. Some

members of the mid-range group have 4-40MHz clock frequency, USB1, I2C, LCD,

11

Your User Name, 11/11/09,
Pg 138 ……..programming microcontroller book
Page 20: Digital Counter

and USART in addition to features in the baseline family. Implementations range

from 8- to 64- pin packages.

High-Performance family: These PICs belong to the PIC18 group. They have 16-bit

program words, flash program memory, a linear memory space of up to 2MB, and

protocol-based communications facilities. They all support internal and external

interrupts and have a much larger instruction set than members of the baseline and

mid-range families have. They have 32-level of stacks, integrated A/D converter, 8-

by-8 hardware multiplier, up to 48MHz clock frequency, over 79 instructions set and

are shipped in 18 to 80 pin packages.

Figure 2.5: PIC 16F874A and PIC16F877 pin diagrams

12

Page 21: Digital Counter

The pin diagrams for some mid-range microcontrollers are as shown in figure 2.10 above. In

addition, figure 2.11 is a pin diagram of high-performance microcontroller PIC18F2620. The

instruction set and data sheet for the microcontroller used for this project is as shown in

appendix. Some commonly used commands in the instruction set are ‘MOVLW’, ‘ADDLW’,

’RETLW’, ’BCF’, ’CALL’, ’DECFSZ’, ’ANDLW, ’INCF’, ’CLRF’, ’BTFSS’, ’GOTO’,

‘NOP’, and ‘END’.

The program memory map and stack for a typical microcontroller is as shown in figure 2.11

below. The diagram shows the addressing scheme and memory space available to

microcontrollers.

Figure 2.6: Program memory map and stack for high-performance microcontroller

13

Page 22: Digital Counter

CHAPTER THREE

DESIGN METHODLOGY

The design generally requires basic understanding of the project, its objectives and mode of

operation of the units in the circuit to achieve the desired aim. In doing this, some

fundamental factors need to be taken into consideration for effective realization of the

objectives. This includes;

Accuracy and Reliability

Flexibility and sophistication

Cost

Size and weight

Availability of components

These factors if not properly taken care of may impair the operation of the circuit, taking note

that, there has to be a trade-off between complementary specifications.

Design Specification:

The general specifications for the project are included in the objective; which means, the use

of Switches (Sensors) to increment and decrement the counting register. Using the standard

door as the reference for this project, Switch 1 is placed at the entry door and Switch 2 is

placed at the exit door

Identifying that the general specifications are hardware based. It is noted that the level of

security, sophistication and interactivity is software based. Therefore, software specifications

include:

14

Page 23: Digital Counter

Ability to increment the counting register Ability to decrement the counting register Ability to display input and authentication processes

In implementation of these specifications, consideration of high level of accuracy and speed

is of utmost importance.

Block Diagram:

With the aid of the specifications, the integrated system is divided into Three (3) different

sub-systems. Figure 3.1 shows the block diagram of the subsystems contained in the

complete system.

Figure 3.1: Block Diagram of system

Design Analysis:

15

SENSOR MICRO controller

LCD DISPLAY

Page 24: Digital Counter

The power supply circuit provides power to the system at all time and the keypad circuit

scans for input, while the display of all the operations will be on the LCD display

simultaneously.

The microcontroller coordinates all the operations of the system. Therefore, the program on

the microcontroller must take input from the sensors (switches), while displaying it on the

LCD, processes such as incrementing and decrementing and storage are been done smoothly

without break in power. The power section must also smoothly switch between the a.c mains

and the circuit without any distortion in operation of the main system.

Circuit diagram

16

Page 25: Digital Counter

Figure 3.2 shows the complete circuit diagram excluding the power section. The circuit was

designed and simulated using Proteus 7.5 professional software

PIC16F877

OSC1OSC2

GNDVCCMCLR

RB0RB1RB2RB3RB4RB5RB6RB7

RC0RC1RC2RC3RC4RC5

RD0RD1RD2RD3RD4RD5RD6RD7

RE0RE1

RE2

U1

+

C14.7uF

3V

LCD3310

1 2 3 4 5 6 7 8

U2

4MHZ

S1

S2

+V5V

R10R9R8

R7

R6

R5

R4R3R2R1

R11

1k

R121k

R131k

Figure 3.2: Circuit Diagram of a Digital Counter

Circuit Analysis:

17

Page 26: Digital Counter

The four (3) sub-systems; microcontroller, Sensor, and LCD display make up the main

circuit.

Microcontroller: The heart of the digital counter is a microcontroller PIC16f877A; it is

interfaced with a LCD display PCD8544.The microcontroller was selected because of its

memory size, low power consumption and flexibility. PIC16F877A is a High performance

RISC CPU with Only 35 single word instructions to learn which operates at a speed between

DC - 20 MHz clock input with single cycle instructions except for program branches which

are two cycle Operating at DC - 200ns instruction cycle time, it has 8K x 14 words of

FLASH Program Memory,368 x 8 bytes of Data Memory (RAM) and 256 x 8 bytes of

EEPROM Data Memory, has a Pin out compatible to the PIC16C73B/74B/76/77 with the

following features

Figure 3.3: A Typical PIC16f877A

• Interrupt capability (up to 14 sources)

• Eight level deep hardware stack

• Direct, indirect and relative addressing modes

• Power-on Reset (POR)

• Power-up Timer (PWRT) and Oscillator Start-up Timer (OST)

18

Page 27: Digital Counter

• Watchdog Timer (WDT) with its own on-chip RC oscillator for reliable operation

• Programmable code protection

• Power saving SLEEP mode

• Selectable oscillator options

• Low power, high speed CMOS FLASH/EEPROM technology

• Fully static design

• In-Circuit Serial Programming (ICSP) via two pins

• Single 5V In-Circuit Serial Programming capability

• In-Circuit Debugging via two pins

• Processor read/write access to program memory

• Wide operating voltage range: 2.0V to 5.5V

• High Sink/Source Current: 25mA

• Commercial, Industrial and Extended temperature ranges

• Low-power consumption:

- < 0.6mA typical @ 3V, 4 MHz

- 20μA typical @ 3V, 32 kHz- < 1μA typical standby current.

Program Code Analysis:

19

Page 28: Digital Counter

As earlier stated, that the core of this integrated system, is based on the program in the

microcontroller. Appendix I show over a thousand (1000) lines of program code written in

Assembler Language with the MPLAB IDE v8.30 software. The major divisions in the

program are macros, functions, subroutines, configurations, and variable and constant

declarations. The program is modularized, to implement the blocks in the flowchart,

following the sequence of programming in Assembler Language.

Description of Program

The program starts with the declaration of the processor used with the ‘list’ command,

followed by the ‘#include’ command indicating the processor file imported (Lines 234 and

235 also imported ‘lcd_routine’ and ‘keypad_routine’ files). Lines 6-10 define the

configuration bits; most bits are OFF, except PWRT (Power-up Timer), and OSC (oscillator)

is set INTIO67, to set up internal oscillator of the microcontroller chip. The ‘cblock’ from

lines 14-75 is used to declare the variables used. Lines 80-106 define all the registers used by

the application. Lines 108-113 and lines 115-119 are two macros, one to set address of LCD

and the other to send characters to LCD.

The main program starts at line 121 with the ‘org 0xf00000’ statement, indicating the RAM

pointer locator for accessing the EEPROM memory, and lines 123-169 are declarations of

EEPROM data addresses using the ‘de’ command. Noting that the idea behind this system is

the fast detection of interruption of normal operations, many timers and interrupt handlers are

set. Lines 172-180 set up interrupt vector address and flag test. Lines 188-222 sets timer

interrupt to handle first time use (that is, key not yet pressed) by displaying initial message on

screen.

20

Page 29: Digital Counter

The ‘org0x0000’ and ‘goto’ statements are the actual start point for the program, setting the

pointer address to 0x0000. The ‘goto’ statement branches to lines 226-230 the ‘takeoff’

labeled point, by initializing internal oscillator to 4MHz and enabling PLL mode.

Lines 240-274 set both timers and delay subroutines for different time durations that is need

for the system. While lines 275-346, does quick processing on the processor such as; clearing

of registers, pointing to EEPROM data memory, delaying time for key debounce of the

keypad, turning the backlight of LCD display ON, turning OFF of interrupt timer, and

resetting all other timers. Lines 348-374 initialize the pins of the microcontroller as inputs

and output. Lines 378-398 initialize the LCD display with the ‘lcd_init’ call command, and

enable a global interrupt for any kind of interruption in the system.

With the processor setup and ready for operation, the processor will change from one state to

another; the idle state, PIN-change state, unlock state, error state, process state, and save

states. The idle state is also the reset state. This state is always waiting key press. Any process

state will also return to the idle state when the ‘escape’ key is pressed. Lines 401-465 is the

code section for the idle state. While the ‘clear’ is not pressed, lines 467-538, are at PIN-

change state, with a branch to lines 717-855 to save the changes. To unlock, after entering the

PIN, the ‘#’ button is key pressed, so lines 540-558 handles unlocking state.

Before unlocking, authentication is required, and this is at the process state. In order for the

microcontroller to authenticate effectively, all interrupt sources are disabled. The program

searches for existing/old PINs in the EEPROM. During the authentication process, there is

transfer of error or success to related states to handle. The authentication process state is at

lines 560-593. The actual unlocking and display of access message is at lines 595-650. This

line of code is a branch from the process state. The line displays access message, turns the

21

Your User Name, 11/11/09,
Branch statement..check appendix
Page 30: Digital Counter

motor, delay for some seconds, and then returns the motor to the original position. These

lines also return the system back to the idle state.

To accommodate for mistakes and error from either the user or system instability, lines 653-

715 handles the error state. The error state is usually a branch from anywhere in the program,

so the error handling lines has display error messages, set-up of the alarm (intruder alert),

delay in time, turn-off of the LCD display, and restore back to the idle state.

Key scanning is through lines 856- 1088, by checking if any of the number 0-9 is pressed.

The save state is required to save new PIN. After a new PIN is entered, and the ‘save button’

is pressed, lines 1091-1128, handles the saving process, by saving to EEPROM, at the correct

address and displaying the action as ‘PIN SAVED’ on the LCD..

22

Page 31: Digital Counter

CHAPTER FOUR

DESIGN CONSTRUCTION

In this chapter, we shall take a look at the design and analysis of the circuit for constructing

the frequency counter. The project is constructed in three modules – the power supply, the

control system (Micro-controller) and lastly the counter/display system (LCD display

PCD8544).

The sensor is used to detect what to be counted, when used as a visitor counter the sensor will

be connected to a foot mat or to an infrared which sends a clock to the microcontroller when

someone steps on the foot mat, the microcontroller counts the number of clocks the sensor

sends then converts it and send it to the LCD display

The sensor is a switch, when pressed sends a high logic to the microcontroller and when

released it sends a low logic, the transition from low to high and back to low logic is called a

clock so for every clock the microcontroller increase its count register by 1 and the counts are

converted from binary to decimal before being displayed on the LCD.

MICROCONTROLLER

The microcontroller operates on 5v DC supply and the LCD operate on a maximum 3.3v

DC so in other to interface the 5v output of the microcontroller to the 3.3v input of the

LCD a voltage divider is used, R1-R10 are used to divide the 5v output of the

microcontroller to 2.5v which is within the operating range of the LCD

S1 and S2 are push button switches which are used as sensors, S1 is used to count visitor

coming in while S2 people going out, when S1 is open a low logic flows from ground

through resistor R12 to RB1 of the microcontroller, the diode blocks the flow of the low

logic to RB0, the logic at RB0 is low logic because it is pulled to ground by R11. When

23

Page 32: Digital Counter

S1 is pressed a high logic flows to the RB1 pin, it also flow to RB0 pin through the diode.

The RB0 pin is the interrupt input of the microcontroller so when the high logic is

received, an interrupt occurs and the microcontroller goes to the counting routine, it

checks if its S1 or S2 that send the logic by checking the logic state of RB1 and RB2

since S1 was pressed and the high logic also flowed to RB1 therefore the microcontroller

increases the COUNT IN register. If S2 was pressed the high logic will flow to RB0 and

RB2 so the microcontroller will increase the COUNT OUT register

POWER ANALYSIS

3V

R19

+9V

5V

IN

COM

OUT

U4LM317

IN

COM

OUT

U378L05

+C21000uF

D3

AC PLUG 220V - 12V

+

C31uF

+

C41uFR18

180

Figure 4.1: Circuit Diagram of the Power Supply

To power the circuit, the 220v input is stepped down to 12v AC and converted to 12v DC

using a bridge rectifier D3, C2 filters the voltage and U3 is used to regulate the voltage to 5v,

U4 regulates the 5v to 3.2v which is used to power the LCD, capacitor C3,C4 filters out the

ripples while R19 andR18 are used to preset the 3.2v of U4.

24

Page 33: Digital Counter

CALCULATION FOR VOLTAGE RECTIFICATION

Vin(min) is the minimum input voltage – let it be = 10v

Vp is the peak input voltage = 12v

Vr is the ripple voltage =?

Vin(min) = Vp – 2*0.7 – Vr

Vr = Vp – 2*0.7 – Vin(min)

Vr = 12v-1.4-10

= 0.6v

Filter capacitor

C = Vp/(2*f*Vr*R).

Vp = 12 f= 50Hz R= 200 Vr =0.6v from above

C = 12/(100*0.6*200)

= 0.001F

= 1000uf

25

Page 34: Digital Counter

Software Burning:

After compilation, into hex file, the machine code is burnt into the chip, using a WinPic800

USB programmer. The PIC is first erased, and the code is downloaded into it. Verification of

the downloaded content and memory allocation is ensured to provent improper execution.

Figure 4.2 : WinPic800 USB Programmer software

After initial burning, subsequent burning of modifications to the code was achieved using

the ICSP (In-Circuit Serial Programming) feature of the microcontroller.

Hardware Troubleshooting and debugging:

While programming the code, a lot of errors and problems were encountered, as result of

ingeniuity. Also, during construction, a lot of troubleshooting and trial and error activities

was so much involved. But all problems were successfully resolved.

Simulation/Testing:

The program after compilation, was simulated using both MPLAB v8.30, and Proteus

Professional 7.5. The program worked perfectly, but some software parameters were not

physical, limiting the real situation. Such parameters include board resistance, environmental

temperature changes, component tolerance. The final system was tested, after construction

and also worked well, Figure 4.6 shows the real keypad outline as simulated.

26

Page 35: Digital Counter

PCB Layout:

The PCB layout was generated by Proteus Professional 7.5 package, after the circuit design.

The 3D visualization, made construction easier. The PCB layouts for the main circuit are

shown in figure 4 below.

Figure 4.3: PCB Layout of Main Circuit

Hardware Troubleshooting and debugging:

While programming the code, a lot of errors and problems were encountered, as result of

ingeniuity. Also, during construction, a lot of troubleshooting and trial and error activities

was so much involved. But all problems were successfully resolved.

27

Page 36: Digital Counter

Simulation/Testing:

The program after compilation, was simulated using both MPLAB v8.30, and Proteus

Professional 7.5. The program worked perfectly, but some software parameters were not

physical, limiting the real situation. Such parameters include board resistance,

environmental temperature changes, component tolerance. The final system was tested,

after construction and also worked well

28

Page 37: Digital Counter

CHAPTER FIVE

CONCLUSION/RECOMMENDATION

Discussion of Result

The result observed so far indicate the accuracy of the program code and hardware

construction. A fair pre-assessment to the final system (that is, after final packaging) also

indicates that the system will work perfectly, if maintained and handled with care.

Conclusion

Indeed, securing restricted locations can become more secured and flexible without the use of

complete mechanical locking devices. This project has designed and constructed a portable

electronically controlled lock, with features that are expansible. The project has also

accounted for power failure in many premises that utilize electronic locks.

The design is also adaptable to other input mechanisms such as iris, retinal, and fingerprint,

by just designing adapter hardware capable of generating 4-digit code OR adjusting the code

for longer digit codes and using the same microcontroller design.

Recommendations

A more elaborate system or access control system can be built that can store several PIN

codes of different devices at different locations in a protected zone, to a central server for

coordination, surveillance, and crime detection purposes.

Advancement in power usage and back-up system, can implement piezo power supply.

Research in RFID (Radio Frequency ID) and SMS (Short message service) as other means of

activating the lock should also be favoured.

29

Page 38: Digital Counter

REFERENCES

Chuck Simmers, Microchip Technology Inc, Remote Keyless Entry and Convenience Center Reference Design with LIN Bus Interface L298 datasheet by Sgs. Thomson Microelectronics, 1998, Pg 1-4

CSC Cherry Semi - conductor. “ Power Conversion IC Data Book ”. 1992

Damle, P and Dubey, G.K. (1976). “ Software Burning on Micro Chip ”. IEEE Trans. VOL. 31. NO. 8. Pp.1724-1735.

Dewan S.B. (1981). Optimum input and output filters for single phase rectifiers power supply .IEEE Trans Industry Applications, vol. IA17, No3, 282-288.

Dixon J.W, Venegas G. and Moran L. (1997). A Series Active Power Filter based on a Sinusodial Current – Controlled Voltage- Source Inverter. IEEE Transactions on Industrial Electronics, Vol. 44, NO . 5. 612-620

Gbadebo S(2002) “project report on digital frequency counter” Electrical/Electronic Engineering Department University of Lagos . Pg 23-40

Jacob Millman, (1979) “Microelectronic Digital and Analog Circuits and Systems”, McGraw-Hill Book Company, New York, ISBN 1979 0-07- 02327-X. Pg 53, 145-148

James Buchanan (1970) “CMOS/TTL digital systems” John Wiley and Sons LA 1970.Pg 29-32.

Julio Sanchez and Maria Canton (2002) Microcontroller programming….The Microchip PIC by Canton pg 120, 129, 140, 142, 143

PIC18F2525/2620/4525/4620 Datasheet by Microchip Technology Inc. 2004

PIC 16F877A datasheet by Microchip Technology Inc. pg3-8

Paul Horowitz and Winfield Hill, (1989) “The Art of Electronics 2nd Ed“ Cambridge University Press, Cambridge, 1989 ISBN 0-521-37095-7. Pg 76.

William Klietz (1975) “Digital Electronics a practical application” 5 th Edition. Cambridge University press. Cambridge, 1975 ISBN 0-521-37095-7. Pg 100-109

30

Page 39: Digital Counter

APPENDIX I

Main Program Code

;************************************************************************** ; Filename: access.asm * ; Author: OWOEYE DAMILOLA AKINTAYO * ; Date: July 19, 2009 * ; Processor:PIC18F2620 * ; Reference Circuit: Figure 3.2 *; Description: Software program of a Digital counter *; ;**************************************************************************list p=16F877, f=INHX8M include "P16f877.inc"include "set1A.inc"

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_OFF & _LVP_OFF

;Set up general purpose registerscblock d'32'

WTEMPWTEMP1 ;General use variablesWTEMP2 ;WTEMP3 ;WTEMP4 ;General use variablesWTEMP5 ;WTEMP6WTEMP7WTEMP8WTEMP9WTEMP10MODEXTEST

INT_W_STORESTATUS_TEMPPCLATH_TEMPWTEMP_ISR_STOREWTEMP_BUF

COUNT_IN_LCOUNT_IN_H

31

Page 40: Digital Counter

COUNT_OUT_LCOUNT_OUT_HCOUNTX_IN_LCOUNTX_IN_H

CAR_LCAR_H

SETXSETY

NUMBERNUMNUMHINUMLOD4D3D2D1D0

COUNTxCOUNT0xCOUNT1x

COUNTCOUNT1COUNT2COUNT3

ADDRADDR1NUM_LEN

endc

;-------------------------------------------------------------------------------------------------------------; Origin code;-------------------------------------------------------------------------------------------------------------

ORG 0x000 ;Start here. Programmer puts bootloader code before it.

nopnopgoto INIT ;Go to your main application start point

32

Page 41: Digital Counter

ORG 5 ;Interrupt Vectorgoto ISR

org 0x01FF ;-------------------------------------------------------------------------------------------------------------;Initialise ports;-------------------------------------------------------------------------------------------------------------;-------------------------------------------------------------------------------------------------------------;Initialise ports;-------------------------------------------------------------------------------------------------------------;---------------------------------------------------------------------------------------------------;-------------------------------------------------------------------------------------------------------------

;----------------------------------------------------------------------------------------------

START

;-----------------------------------------------------------------------------------------------movlw D'100'

goto START

;----------------------------------------------------------------------------------------------

;----------------------------------------------------------------------------------------------

SEND_DATA

bcf STATUS,6bcf STATUS,5movwf SSPBUF ; put in SSPBUFBANKSEL SSPSTAT ; BANK 1

Char1 btfss SSPSTAT,BF ; Data transfer complete? (Buffer Full?)goto Char1 ; if not, check againBANKSEL SSPBUF ; BANK0movf SSPBUF,W ; Get Data from SSPBUF; ; Throw it awayreturn

;----------------------------------------------------------------------------------------------

33

Page 42: Digital Counter

;----------------------------------------------------------------------------------------------

DELAYmovlw D'200'movwf WTEMP

LOOP decfsz WTEMPgoto LOOPreturn

;-----------------------------------------------------------------------------------------------

;This routine will delay program operation from 0.7 to 200mSMSDELAY

movlw d'1'DELAY_0 movwf WTEMP1DELAY_1 movlw d'150'

movwf WTEMP2

DELAY_2 movlw d'255' movwf WTEMP3

DELAY_3 decfsz WTEMP3,Fgoto DELAY_3

decfsz WTEMP2,Fgoto DELAY_2decfsz WTEMP1,Fgoto DELAY_1return

;-------------------------------------------------------------------------------------------------BIN_TO_BCD ;10b binaey to BCDBin2DecFast movf NUMHI,w iorlw 0xF0 ;w=H2-16 movwf D1 ;D1=H2-16 addwf D1,f ;D1=H2*2-32 addwf D1,f ;D1=H2*3-48 movwf D2 ;D2=H2-16 addlw -D'5' ;w=H2-21 addwf D2,f ;D2=H2*2-37 Done! addlw D'41' ;w=H2+20 movwf D0 ;D0=H2+20

swapf NUMLO,w iorlw 0xF0 ;w=H1-16 addwf D1,f ;D1=H2*3+H1-64 addwf D0,f ;D0=H2+H1+4, C=1

34

Page 43: Digital Counter

rlf D0,f ;D0=(H2+H1)*2+9, C=0 comf D0,f ;D0=-(H2+H1)*2-10 rlf D0,f ;D0=-(H2+H1)*4-20 movf NUMLO,w andlw 0x0F ;w=H0 addwf D0,f ;D0=H0-(H2+H1)*4-20 Done! rlf D1,f ;C=0, D1=H2*6+H1*2-128 Done!

movlw D'5' movwf D3

movlw D'10' mod0 addwf D0,f ;D(X)=D(X)mod10 decf D1,f ;D(X+1)=D(X+1)+D(X)div10 skpc goto mod0 mod1 addwf D1,f decf D2,f skpc goto mod1 mod2 addwf D2,f decf D3,f skpc goto mod2

return

ISR

;Interrupt Service Routine;This should be called every 1/100th of a second

movwf INT_W_STORE ;Backup the W registerswapf STATUS,W ;Backup STATUSclrf STATUS ;Forces correct page and bank IRP,RP0

and RP1movwf STATUS_TEMP ;movf PCLATH, W ;Only required if using pages 1, 2 and/or 3movwf PCLATH_TEMP ;Save PCLATH into Wclrf PCLATH ;Page zero, regardless of current pagemovf WTEMP,Wmovwf WTEMP_ISR_STOREmovf SSPBUF,0movwf WTEMP_BUF

btfss INTCON,INTFgoto INT_EXIT

35

Page 44: Digital Counter

btfsc PORTB,2goto TESX

btfsc MODE,0goto CARPARK_FULL

call MSDELAYbtfss PORTB,1goto INT_EXITcall CAR_INgoto INT_EXIT

TESXcall CAR_OUTclrf MODEgoto INT_EXIT

INT_EXIT;---------------------------------------------------------------------------------------------; SET LCD DATA FOR IN

bcf PORTC,0 ;WRITE COMMAND TO LCD

movlw H'42' ; move to line 2 of display Y=2call SEND_DATAmovlw H'92' ; move to COLUNM 0 of display X=0call SEND_DATA

bsf PORTC,0 ;WRITE DATA TO LCD;----------------------------------------------------------------------------------------------

;CONVERT COUNT IN TO DIGITS

movf COUNT_IN_H,0movwf NUMHImovf COUNT_IN_L,0movwf NUMLOcall BIN_TO_BCD

IN_TENSmovf D2,0 ;SELECTED_TENS FOR COUNT INcall TABLE_LOOKUPmovwf WTEMP7

IN1movf WTEMP7,0call TABLE2movwf WTEMP8btfsc WTEMP8,7goto IN_ONES ;SELECTED_ONES FOR COUNT IN

36

Page 45: Digital Counter

call SEND_DATA ; SEND W TO LCDincf WTEMP7goto IN1

IN_ONESmovf D1,0 ;SELECTED_ONES FOR COUNT INcall TABLE_LOOKUPmovwf WTEMP7

IN2movf WTEMP7,0call TABLE2movwf WTEMP8btfsc WTEMP8,7goto IN_DECI ;SELECTED_DECIMAL FOR COUNT INcall SEND_DATA ; SEND W TO LCDincf WTEMP7goto IN2

IN_DECImovf D0,0 ;SELECTED_DECIMAL FOR COUNT INcall TABLE_LOOKUPmovwf WTEMP7

IN3movf WTEMP7,0call TABLE2movwf WTEMP8btfsc WTEMP8,7goto OUT_CONVERT1 ;COUNT INcall SEND_DATA ; SEND W TO LCDincf WTEMP7goto IN3

OUT_CONVERT1movlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATA

37

Page 46: Digital Counter

movlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATA

movlw D'30'movwf SETX

N_Amovlw H'00' ; clear displaycall SEND_DATA

incf SETXmovlw D'75'movwf WTEMPmovf SETX,0subwf WTEMP,0btfss STATUS,Zgoto N_A

;--------------------------------------------------------------------------------------------

;--------------------------------------------------------------------------------------------

OUT_CONVERT;---------------------------------------------------------------------------------------------

bcf PORTC,0 ;WRITE COMMAND TO LCD

movlw H'43' ; move to line 3 of display Y=3call SEND_DATAmovlw H'80' ; move to COLUNM 0 of display X=0call SEND_DATA

bsf PORTC,0 ;WRITE DATA TO LCD

38

Page 47: Digital Counter

;OUTmovlw H'3E' ; letter Ocall SEND_DATAmovlw H'41' ; letter Ocall SEND_DATAmovlw H'41' ; letter Ocall SEND_DATAmovlw H'3E' ; letter Ocall SEND_DATAmovlw H'00' ; letter Ocall SEND_DATA

movlw H'3F' ; letter Ucall SEND_DATAmovlw H'40' ; letter Ucall SEND_DATAmovlw H'40' ; letter Ucall SEND_DATAmovlw H'3F' ; letter Ucall SEND_DATAmovlw H'00' ; letter Ucall SEND_DATA

movlw H'03' ; letter Tcall SEND_DATAmovlw H'03' ; letter Tcall SEND_DATAmovlw H'7F' ; letter Tcall SEND_DATAmovlw H'03' ; letter Tcall SEND_DATAmovlw H'03' ; letter Tcall SEND_DATAmovlw H'00' ; letter Tcall SEND_DATA

movlw H'08' ; letter -call SEND_DATAmovlw H'08' ; letter -call SEND_DATAmovlw H'08' ; letter -call SEND_DATAmovlw H'08' ; letter -call SEND_DATAmovlw H'00' ; letter -call SEND_DATA

;-----------------------------------------------------------------------------------------------

movf COUNT_OUT_H,0

39

Page 48: Digital Counter

movwf NUMHImovf COUNT_OUT_L,0movwf NUMLOcall BIN_TO_BCD

OUT_TENSmovf D2,0 ;SELECTED_TENS FOR OUTcall TABLE_LOOKUPmovwf WTEMP7

OUT1movf WTEMP7,0call TABLE2movwf WTEMP8btfsc WTEMP8,7goto OUT_ONES ;SELECTED_ONES FOR OUTcall SEND_DATA ;SEND W TO LCDincf WTEMP7goto OUT1

OUT_ONESmovf D1,0 ;SELECTED_ONES FOR OUTcall TABLE_LOOKUPmovwf WTEMP7

OUT2movf WTEMP7,0call TABLE2movwf WTEMP8btfsc WTEMP8,7goto OUT_DECI ;SELECTED_DECIMAL FOR OUTcall SEND_DATA ; SEND W TO LCDincf WTEMP7goto OUT2

OUT_DECImovf D0,0 ;SELECTED_DECIMAL FOR OUTcall TABLE_LOOKUPmovwf WTEMP7

OUT3movf WTEMP7,0call TABLE2movwf WTEMP8btfsc WTEMP8,7goto STAFF1C ;call SEND_DATA ; SEND W TO LCDincf WTEMP7goto OUT3

STAFF1Cmovlw H'00' ; letter SPACEcall SEND_DATA

40

Page 49: Digital Counter

movlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATA

CAR_LEFT;---------------------------------------------------------------------------------------------; SET LCD DATA FOR CAR_LEFT

bcf PORTC,0 ;WRITE COMMAND TO LCD

movlw H'44' ; move to line 4 of display Y=4call SEND_DATAmovlw H'8B' ; move to COLUNM 0 of display X=0call SEND_DATA

bsf PORTC,0 ;WRITE DATA TO LCD

;-----------------------------------------------------------------------------------------------

41

Page 50: Digital Counter

movlw D'0'movwf NUMHImovf COUNTX_IN_L,0movwf NUMLOcall BIN_TO_BCD

CAR_TENSmovf D2,0 ;call TABLE_LOOKUPmovwf WTEMP7

CAR1movf WTEMP7,0call TABLE2movwf WTEMP8btfsc WTEMP8,7goto CAR_ONES ;SELECTED_ONES FOR CARcall SEND_DATA ;SEND W TO LCDincf WTEMP7goto CAR1

CAR_ONESmovf D1,0 ;SELECTED_ONES FOR CARcall TABLE_LOOKUPmovwf WTEMP7

CAR2movf WTEMP7,0call TABLE2movwf WTEMP8btfsc WTEMP8,7goto CAR_DECI ;SELECTED_DECIMAL FOR CARcall SEND_DATA ; SEND W TO LCDincf WTEMP7goto CAR2

CAR_DECImovf D0,0 ;SELECTED_DECIMAL FOR INcall TABLE_LOOKUPmovwf WTEMP7

CAR3movf WTEMP7,0call TABLE2movwf WTEMP8btfsc WTEMP8,7goto STA1 ;call SEND_DATA ; SEND W TO LCDincf WTEMP7goto CAR3

STA1movlw H'00' ; letter SPACE

42

Page 51: Digital Counter

call SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATAmovlw H'00' ; letter SPACEcall SEND_DATA

movlw D'30'movwf SETX

N_AXmovlw H'00' ; clear displaycall SEND_DATA

incf SETXmovlw D'75'movwf WTEMPmovf SETX,0subwf WTEMP,0btfss STATUS,Zgoto N_AX

43

Page 52: Digital Counter

;;INT_EXITmovf WTEMP_BUF,0movwf SSPBUFmovf WTEMP_ISR_STORE,Wmovwf WTEMPmovf PCLATH_TEMP, W ;Restore PCLATHmovwf PCLATH ;Move W into PCLATHswapf STATUS_TEMP,W ;Recall STATUSmovwf STATUSswapf INT_W_STORE,F ;Recall the W register without affecting

STATUSswapf INT_W_STORE,Wbcf INTCON,INTFretfie

CARPARK_FULLcall FULLbsf MODE,0

goto INT_EXIT

CAR_IN call WELCOMEcall DELAYXcall CLEARmovf COUNTX_IN_L,0subwf XTEST,0btfsc STATUS,Zgoto CARPARK_FULLincf COUNTX_IN_L

incf COUNT_IN_Lbtfss STATUS,Zreturnclrf COUNT_IN_Lincf COUNT_IN_Hcall MSDELAYreturn

CAR_OUTcall BYEcall DELAYXcall CLEARmovf COUNTX_IN_Lbtfss STATUS,Zdecf COUNTX_IN_Lincf COUNT_OUT_Lbtfss STATUS,Zreturnclrf COUNT_OUT_Lincf COUNT_OUT_H

44

Page 53: Digital Counter

call MSDELAYreturn

;---------------------------------------------------------------------------------------------------------------- DELAYX ; DELAYN for about 100 usec

movlw D'2' ;DELAYN loopmovwf COUNTx ;DELAYN loop

udelayxy movlw D'255' ;DELAYN loopmovwf COUNT0x ;DELAYN loop

udelayx movlw D'255' ;delay loopmovwf COUNT1x ;delay loop

udelay1x decfsz COUNT1x, F ;delay loopgoto udelay1x ;delay loop

decfsz COUNT0x, F ;delay loopgoto udelayx ;delay loop

decfsz COUNTx, F ;delay loopgoto udelayxy

return ; go back to program;----------------------------------------------------------------------------------------------------------------DELAYN ; DELAYN for about 100 usec

movlw D'255' ;DELAYN loopmovwf COUNT ;DELAYN loop

udelay0 movlw D'200' ;delay loopmovwf COUNT1 ;delay loop

udelay1 decfsz COUNT1, F ;delay loopgoto udelay1 ;delay loop

decfsz COUNT, F ;delay loopgoto udelay0 ;delay loopreturn

END

45

Page 54: Digital Counter

APPENDIX II

PIC16F877A Datasheet

46

Page 55: Digital Counter

APPENDIX III: PICF8774 INSTRUCTION SET

47

Page 56: Digital Counter

48

Page 57: Digital Counter

49

Page 58: Digital Counter

50