PSZ f9: 16 (Pind. 1r9l) T]NTVERSITI TEKNOLOGI MALAYSIA BORf,NG PENGESf,ITf,N STf,TUS TESISO ENHANCEMENT OF MASTERAI\aDSLAVE VO JIIDUL: CONTROLLER MODULES. SESIPENGAIIAN: Sava: 2WS2,fr06 MOHD SINA ZAKI BIN MORSIN G{URUF BESAR) Mengaku memberurkantesis (PStvl/SejaaelDe*ter4ehafoh)* ini di simpan di Perpustakaan Universiti Teknologi Malaysia dengansyarat-syaratkegunaanseperti berikut: l. 2, 3. 4. Tesis adalahhak milik Universiti Teknologi Malaysia. PerpustakaanUniveniti Teknologi Malaysia dibenarkanmembuatsalinan unnk tujuan pengajiansahaja Perpustakaandibenarkanmembuatsalinantesis ini sebagaibahanpertukaranantarainstitusi pengajiantinggi. **Silatandakan(y' ) n SULIT t:] (Mengandungimaklumat yang b€rdadahkeselamatanatau kepentinganMalaysia sepertiyangtermaktub di dalam AKTA RAHSIARASMI 1972) TERHAD tI (Mengandungimaklumat TERHAD yang t€leh ditentukan oleh organisasi/badandi mana penyelidik{randijalankan) TIDAK TERHAD Disahkanoleh (TANDATANGANPEhrrELlA) AlamatTetap: NO I.JALAI{ BT'KIT. BUKIT BATU.8lOzO. KULAI. JOHOR DARUL TA'AZIM. Tariktr :g-!&L!@6 CATATAN: . t' EN MOHAMAD SI{IJKRI BIN ZAINAL AEIDIN (NamaPenyelia) Tarikh:E MEI2006 Potmgyangtidalcbedccnaan. Jika tesis ini SULIT atau TERHAD, sila larpirkan surat dari@a pihak sekali sebabdan t€mpohtesis ini h*usa/organisasi bertenam dengsnmen1lataker polu dikelaskmsebagaiSI/LIT atauTERHAD. Tesis dimaksudkansebageitesis b6gi ljazah Doktor Falsafahdan Sarjanasecara penyelidikm,ataudisertasibagi p€ngajiansecra kerja lfiusus dm peoyelidikan,atau I-qoru gojek SarjanaMuda(PSM). " I herebyc€rtifythqtI haverdthistksis ad it is suffici€ntandsuitableinermsof scopcandqualityfor thepurposeof awardinga Bachelor'sDegroein El€ctricat (Computer)." Engineering Sigoanr€ Supcrvisor ; EN MOHAMAD SIilUKRI BIN ZAINAL ABIIXN Daf,e : t MAY2lllX ENHANCEMENT OF MASTER AND SLAVE I/O CONTROLLER MODULES MOHD SINA ZAKI BIN MORSIN This report is submitted for the partial fulfillment of the requirement for achieving the Bachelor’s Degree in Electrical Engineering (Computer) Faculty of Electrical Engineering Universiti Teknologi Malaysia MAY 2006 u 'I hcr*y dcclarctbd tr€ whob &erdsis tb resultof my ownwort rnd ttc mmrhls u,hicharcno[fu rc$lts of my onn wslc havcboenclcrly rcknowlodged"" Signrnre: Au66 : M0tI) SINA ZAKI BIN MO*llIN : 0EMAY 2006 iii Thankful to ALLAH swt, God Rabbil Alamin, lead the whole world, Ayah and Mak, Atuk and Maktuk, All my siblings, sisters and brothers, Lectures and teachers, Friends and relatives, Not forgotten to some one who gives completely shore up from my support. iv ACKNOWLEDGEMENT Praise to Allah S.W.T the Most Gracious, the Most Merciful, whose blessing and guidance have helped me through my thesis well. There is no power any strength save then Allah, the Highest and the Greatest. Peace and blessing of Allah be upon our Prophet Muhammad S.A.W who has given brightness to mankind. My parent, Hj Morsin Bin Parman and Hjh Mahani binti Hj Hussain, who gives me full support in my whole life especially in this project, from materials to advices to make me strength in intellectual and ethical. I would like to express my highest appreciation to En Mohammad Shukri bin Zainal Abidin, my supervisor for his patience and encouragement throughout my project here at UTM. His guidance and advice have been invaluable throughout the course of this work. It has been a tremendous wisdom understanding and I am grateful for it. I sincere thanks to all my friends and all those whoever has help me either directly or indirectly in the completion of my final year project and thesis. Thank you very much and Allah blesses you….. v ABSTRACT The topic of this project is Enhancement of Master and Slave I/O Controller Modules. This project is enhancement from the previous project, which is ‘Design and Development of The Embedded Master Controller and Remote I/O Modules’. The main idea of this project is constructing two levels of micro controllers using PIC microcontroller. The first level consist of Master Controller and the second level consist of Slave Controller (Humble).The constraints from previous project are timing in serial communication; using MAX485, which is collided when transmitting and receiving the data, where as MAX485 is a half duplex interface. It is because; many slaves will communicate between the master and the slave modules. This will increase the risk to collide when the number of the slave is big. To debug the previous project is very complicated compared to rebuild the new project. This project started by choosing the ideal components such as micro controllers, interface, converter using RS485 (MAX490 chips), cables, voltage regulator circuit and the PCB board. The software development includes the subroutine for keypad, LCD, USART application (Universal Synchronous Asynchronous Receiver Transmitter) in serial communications, hardware for USART, EEPROM, input, output and main program. Some adjustment for this project was implemented in this circuit to make sure that it can run smoothly. The learning for a new technique to connect the new peripheral to micro controller, develop some protocol and design the subroutine for the new peripheral. There are implemented some algorithms to manage the traffic in the data bus. Overall, this prototype project will enhance and can be implementing in “smart building”. vi ABSTRAK Tajuk bagi projek ini ialah Penambahbaikan Masukan Keluaran Pengawal Utama dan Pengawal Luaran. Projek ini adalah penambahbaikan yang lepas oleh iaitu Merekabentuk dan Membangunkan Masukan Keluaran Pengawal Utama dan Pengawal Luaran Terbenam. Idea utama bagi pojek ini ialah membina dua aras mikropengawal mengunakan pengawal PIC. Aras pertama berkenaan dengan pengawal utama dan aras kedua berkenaan dengan pengawal luaran. Kekangan yang pada projek yang lepas ialah berhubung dengan kaedah pemasaan pada komunikasi sesiri mengunakan MAX485, dimana berlaku perlanggaran data ketika penghantaran dan penerimaan data dengan mengunakan MAX485 iaitu pengantaramuka setengah duplek. Ini kerana terdapat beberapa pengawal luaran akan berkomuikasi dengan pengawal utama dan risiko berlakunya perlanggaran data akan berlaku apabila jumlah pengawal luaran semakin besar. Bagi memperbaiki projek yang lepas adalah lebih sukar berbanding membina semula projek yang baru. Projek bermula dengan merekabentuk perkakasan, termasuklah pemilihan komponen, mikropengawal, antaramuka mikropengawal dengan perkakasan, membina aras penhantaran data mengunakan MAX 490, merekabentuk talian penghantaran data, litar pengawal atur voltan dan papan PCB. Fasa kedua pula berkaitan dengan pembangunan perisian. Bahagian ini termasuk merekabentuk subrutin untuk papan kekunci, LCD, komunikasi serial mengunakan USART, peralatan untuk USART, EEPROM, masukan / keluaran dan program utama. Terdapat sedikit pengubahsuaian pada rekabentuk projek pada litar bagi memastikan ia dapat berfungsi dengan baik. Proses membelajaran teknik baru dilakukan untuk menghubungkan peralatan baru kepada mikro pengawal dan membangunkan sedikit protokol untuk peralatan tersebut. Pengunaan algorithma yang sesuai digunakan dalam pengurusan trafik pada bas data. Secara keseluruhannya projek ini boleh dinaiktaraf dan diimplimentasikan pada “smart building”. vii TABLE OF CONTENTS CHAPTER 1 2 TITLE PAGE TITLE PAGE i DECLARATION ii DEDICATION iii ACKNOWLEDGEMENT iv ABSTRACT v ABSTRAK vi TABLE OF CONTENTS vii LIST OF FIGURES x LIST OF TABLES xiii LIST OF ABBREVIATIONS xiv LIST OF APPENDICES xv INTRODUCTION 1.1 Background Project 1 1.2 Project Overview 2 1.3 Project Objective 3 1.4 Scope of Work 4 1.5 Thesis Layout 5 LITERATURE REVIEWS 2.1 Introduction 6 2.2 Communications Implementing 7 viii 2.2.1 2.3 3 Micro Controllers. 9 2.3.1 Microchip PIC18F452 Microcontroller 9 2.3.2 Microchip PIC 16F877A Microcontroller 10 MAX 490 Chips. 15 2.5 LCD 16 2.6 Keypad 17 2.7 ULN 2003 18 2.8 Eagle software 19 2.9 Daisy Chaining 20 DESIGN ARCHITECTURE 3.1 Master Controller 22 3.2 Slaves Controller 25 3.2.1 Slave A 26 3.2.2 Slave B 27 Communications 29 HARDWARE DESIGN 4.1 Controllers Circuit 31 4.2 Circuit for Master Controller 34 4.3 Circuit for Slave Controller A 36 4.4 Circuit for Slave Controller B 38 4.5 Home Modeling 40 4.5.1 40 4.6 5 7 2.4 3.3 4 Serial Data Transmission. Relay Circuit. Cable Design 42 SOFTWARE AND PROGRAMMING 5.1 Microcode Studio Plus Software 44 5.2 Programming Flow Chart 45 5.2.1 Master programming 46 ix 6 7 5.2.2 Scanning Keypad 57 5.2.3 Slave A programming 60 5.2.4 Slave B programming 64 EXPERIMENTS AND RESULTS 6.0 System Setup. 71 6.1 Experiment 1 72 6.2 Experiment II. 74 6.3 Experiment III 75 6.4 Experiment IV 77 CONCLUSION 7.1 Problem Encountered 80 7.1.1 Solve the problem in RS485 Half Duplex. 7.1.2 80 Complexity programming in Master Controller 82 7.2 Future Work 82 7.3 Conclusion 83 REFERENCES 84 APPENDIXES 85 x LIST OF FIGURES FIGURE TITLE PAGE 1.1. Last year PSM by LEE HENG SENG 2 2.1 Pin Configurations 10 2.2 Pin Configurations 11 2.3 MAX-490 16 2.4 LCD 17 2.5 Keypad 18 2.6 Eagle Layout GUI 19 2.7 New Slide Project 20 2.8: Overview of the project 21 3.1 Keypad 22 4.1 Oscillator circuit 32 4.2 Voltage regulator circuit 32 4.3 Reset circuit 33 4.4 LED circuit (Active High) 33 4.5 Master Controller circuit 25 4.6 Slave Controller A circuit 37 4.7: Slave Controller B circuit 39 4.8 Relay Circuit 41 4.9 Fan 42 4.10 Alarm System 42 4.11 Lamp 42 4.12: Magnetic Door 42 5.1 Micro Code Studio GUI 45 5.2 Master main programming 47 5.3 Master main programming (PART2) 48 5.4 Master main programming (PART3) 49 xi 5.5 Master main programming (PART4) 50 5.6 Master main programming (PART5) 51 5.7 Master main programming (PART6) 52 5.8 Master main programming (PART7) 53 5.9 Master main programming (PART8) 54 5.10 Master main programming (PART9) 55 5.11 Master main programming (PART10) 56 5.12 Master main programming (PART11) 57 5.13 Scanning Keypad 58 5.14 Serial Communication in master controller 59 5.15 Slave A main programming 61 5.16 Slave A main programming (PART 2) 62 5.17 Slave A main programming (PART 3) 63 5.18 Slave A main programming (PART 4) 64 5.19 Slave B main programming 65 5.20 Slave B main programming (PART2) 66 5.21 Slave B main programming (PART3) 67 5.22 Slave B main programming (PART4) 68 5.23 Slave B main programming (PART5) 69 5.24 Slave B main programming (PART6 70 6.1 Beginning output 71 6.2 Slave A (before) 72 6.3 Slave A (after) 73 6.4 Slave B (before) 73 6.5 Slave B (open door is pressed) 74 6.6 Slave B (Press lock door) 74 6.7 First Result (A) 75 6.8 First Result (B) 75 6.9a Slave A (before) 76 6.9b Slave A (after) 76 xii 6.10a Slave B (before) 76 6.10b Slave B (‘*’ button is pressed) 77 6.10c Slave B; and (‘#’ button is pressed) 77 6.11 The state the corresponding door is breached 78 6.12 The state the corresponding door is breached 78 6.13 This LED shows the door is breached at door 4 slaves 78 6.14 Master try to control before the breached door still open 79 6.15 Final result, the door that was breached is closed and the Master 79 turn off the alarm system 7.1 MAX 485 81 7.2 MAX 490 81 xiii LIST OF TABLES TABLE TITLE PAGE 3.1 Port A for Master Controller 23 3.2 Port B for Master Controller 24 3.3 Port C for Master Controller 24 3.4 Port D for Master Controller 25 3.5 Port E for Master Controller 25 36 Port A for Slave Controller A 26 3.7 Port B for Slave Controller A 26 3.8 Port C for Slave Controller A 26 3.9 Port D for Slave Controller A 27 3.10 Port E for Slave Controller A 27 3.11 Port A for Slave Controller B 27 3.12 Port B for Slave Controller B 28 3.13 Port C for Slave Controller B 28 3.14 Port D for Slave Controller B 28 3.15 Port E for Slave Controller B 29 4.1 Component and Description for Master Controller 36 4.2 Component and Description for Slave Controller A 38 4.3 Component and Description for Slave Controller B 40 xiv LIST OF ABBREVIATIONS LCD - Liquid Crystal Display GUI - General User Interface RAM - Random Access Memory ROM - Read only memory MHz - Mega Hertz LED - Light Emitting Diode USART - Universal Synchronous Asynchronous Receiver Transmitter PCB - Printed Circuit Board xiv LIST OF ABBREVIATIONS LCD - Liquid Crystal Display GUI - General User Interface RAM - Random Access Memory ROM - Read only memory MHz - Mega Hertz LED - Light Emitting Diode USART - Universal Synchronous Asynchronous Receiver Transmitter PCB - Printed Circuit Board 1 CHAPTER 1 INTRODUCTION 1.1 Background Project Smart buildings in this 21st Century will integrate the current control technology with other building systems to create a "Smart Building". The Smart Building will anticipate the needs of the building users to provide improved comfort, greater user control, and better energy efficiency. Here are a few examples of how this can work; microcontroller based control of building systems: Building occupants can adjust room temperature set points, change lighting schemes, and security system. However, this project tries to use master and slaves function to be implemented as controller for the smart building. The specific function was controlled in every slave controllers and the master controller is used to monitor and make the system efficient. 2 1.2 Project Overview MASTER CONTROLLER DATA BUS SLAVE CONTROLLER #A #B Figure 1.1: Last year PSM by LEE HENG SENG. Figure 1.1 above shows the previous project. The title of this project is “Development of Master and Slave controller Modules”. From this project, there are some problems that are founded. The main problem is the quality of the communication. It is because; the communicator was limited to one way communication at the same time using a half 3 duplex communications. The system is familiar to handshaking process where the slave will not send the data when the master is using data bus. This problem will become critical when the numbers of slave increases and the usage of data bus at the same time. Overall, this project is to find out solution of this problem, minimizing the errors when the transferring data, especially involves in a long distance communication and to make it user-friendly. 1.3 Project Objective The main objective of this project is to enchantment, build and develop of Master and Slave I/O Controller Modules which are: • To improve the communication process between master controller and slave modules. • To make sure that the master controller can control and monitor the input and output from the slave microcontrollers. • To make sure all the addition of the new peripheral can be run on the system. • To enhance the system from the past PSM project by LEE HENG SENG (2004/2005) especially in the communication problem. For the ending of the project is to make sure that the error and problem in the last year project will be done. The some addition of the new peripharal can be implement and run smoothly. 4 1.4 Scope of Work Basicly, this project involves in software and hardware development. For the entire system, PIC microcontroller is used as the controllers. This project are divided into two phase. The first phase of this project is to understand how to control and coding the PIC microcontrollers which act as the controller and brain of this project. This is the crucial part of this project where the desired input and output, controlling the slave and operation depend on how is really understand the microcontroller. The second phase of this project is focused on developing the interface between the Master controller and Slave controllers. The network set up is using RS485 full duplex to connect communication between master to slave controller and vice versa. This will be done by hardware connection using microcontroller as a main component, MAX490 transceiver. All the modeling will be adapted to the pattern of on/off light and fan, sensor and alarm system. MASTER CONTROLLER RS-485 (MAX490) SLAVE CONTRLLER DATA BUS As a slave controller unit, some simple input and output will de available to receive command from the master controller, perform its job assigned by master controller and arrangement basis by critical job first. Master will perform to monitor and control the entire job at the Slaves part that we need. This means that some data can be sent to master controller and the data will be show in the LCD on the master controller and will control using the keypad. 5 1.5 Thesis Layout Chapter 1 explains the background study, project objectives, scope of work and overview for this project. Chapter 2 explains the literature reviews on some of important sources, communication implementing and explanation of devices that was used in this project and chapter 3 describes the design architectures of project, project methodology and network topology used in project. Chapter 4 presents the hardware design for programmer, master controller, slaves’ controllers and cable design. Chapter 5 is the main focus which explained the software and programming in developing the embedded controller. Chapter 6 showed the result that was funded and the last chapter, chapter 7 is the conclusion of the project and recommendations for future works. 6 CHAPTER 2 LITERATURE REVIEWS 2.1 Introduction In recent years, there are a lot of microprocessor generation that can be found in the market such as microcontroller; single-chip programmable possessor. Nowadays, it is very familiar and will be found in all kind of equipment – microwave ovens, washing machines, hand phones, digital cameras and many more. Microcontrollers are developed from microprocessors. However, microcontroller is diverse from microprocessor in many ways. First and foremost is the functionality. In order for a microprocessor to be used, other components such as memory, or components for receiving and sending data must be added into it. On the other hand, microcontroller is designed to be all in one. No other external components required for its application because all the necessary peripherals are already built into it. Thus, saves. cost and space needed to construct devices. 7 There are various types of microcontroller namely ATMEL, MOTOROLA or INTEL can be used. However, the PIC microcontroller has been chosen as the controller due to familiarity, availability, and cost of this microcontroller. There are two types of PIC microcontrollers that are used in this project. For Master controller, PIC18F452 is selected and for Slave controller using 16F877A. 2.2 Communications Implementing There is various type of serial network today. However, the project is selected the RS-485 using MAX490 link to connected between the controllers. This network has several protocols and formatting that must be followed to ensure that every transmission reaches its destination and each node can understand the messages that want send to it. 2.2.1 Serial Data Transmission. There are two types of data transmissions: asynchronous transmission and synchronous transmission. In asynchronous transmission, clock line are not used because each line of the link is provides its own clock and each agree on the clock’s frequency. However, to make sure the communication is successful; every node that was communicated must match within a few percent. In serial data transmission the data is transmitted in serial format with the LSB of the byte to be transmitted, shifted out first among the data bits. The general format for serial transmission is Start Bit + Data Bits + Parity Bit (Optional) + Stop Bit. The Parity bit is becomes optional. It is used for error checking in communication. Once the start bit has been sent, the transmitter sends the actual data 8 bits. There may be 5, 6, 7, 8, or 9 data bits, depending on the number have selected. Both receiver and the transmitter must agree on the number of data bits, as well as the baud rate. Almost all devices transmit data using either eight or nine data bits. This project is implementing eight bits data transmission. However, software implementing addresses bytes for a slave to make sure the master controller well known that the specific slave that user want to control or monitor. Communication Good network are the main idea to make this project successful. In this project, it will be divided into two parts of network, internal and external communications. Internal communication is between micro controller and peripheral while the other one is communication between Master controller and Slave controller. • Internal communication This internal communication is communication in the same level. To make sure that the communication is efficient, so the data and the peripheral are controlling by the internal microcontroller. For master controller, it connected to keypad and LCD that are used to control and monitor the slave part. In the slave part, it connected to input and output which is lamp, fan, sensor alarm and etc. • External communication External communication is communication between master and slave controller. If there have request at slave controller to master controller, it must be signal to the master and the master will appear the job on LCD. When the communication is finished, the slave sends back a signal to the master reporting that the task had been done. 9 2.3 Micro Controllers. 2.3.1 Microchip PIC18F452 Microcontroller Microcontroller is a highly integrated chip which includes, on a single chip, all or most of the parts needed for a controller. The microcontroller could be called a “one-chip solution”. It requires very little external support hardware. It typically includes: • CPU (Central Processing Unit) • RAM (Random Access Memory) • EPROM/PROM/ROM (Erasable Programmable Read Only Memory) • I/O (input/output) – serial and parallel • Timers • Interrupt controller Microcontroller is a key of any embedded control applications. Suitable or best fit criteria in microcontroller must be chosen in order to achieve the objectives. The PIC 18F452 microcontroller used in this project is a low-power, high performance 16-bit RISC CPU with 10 bit Analog-to-Digital Converter module. The device is manufactured using Microchip’s high-density nonvolatile memory technology, compatible with the PIC 16 and PIC17 instruction sets. By combining an enhanced 16-bit CPU with high speed FLASH/EEPROM technology on a monolithic chip, the Microchip PIC 18F452 is a powerful computer which provides a highly flexible and cost effective solution to many embedded control applications. Microchip PIC is quit robust; it can 1,000,000 erase/write cycles Data EEPROM memory typical. Besides that, the data will retain more that 40 years. It just using 5 volt power supply, but can work in range 2.0-5.5 volt. 10 Figure 2.1 Pin Configurations PIC18F452 PIC 18F452 provides the following standard features: 32K bytes of Flash memory, 256 bytes of RAM, 34 I/O lines, four 8/16 timer/counter, addressable USART module which supports RS-490, 8 input channels of Analog to Digital Module. Figure 2.1 shows the interface peripheral available in Microchip PIC 18F452 microcontroller. Basically internal interface has an input-output peripheral which is used to interface with other devices such as LED, LCD, keypad and switches. 2.3.2 Microchip PIC 16F877A Microcontroller Slave’s circuit provided to use PIC 16F877A, there are some of there using for external support hardware: • CPU (Central Processing Unit) • RAM (Random Access Memory) 11 • EPROM/PROM/ROM (Erasable Programmable Read Only Memory) • I/O (input/output) – serial and parallel • Timers • Interrupt controller Microcontroller PIC 16F877A is the most popular and usually use for design. The PIC 16F877A microcontroller used in this project is a low-power, high performance 8-bit RISC CPU. 8K Flash program memory is quit enough to medium scale design. The Microchip PIC 16F877A is a quit powerful computer which provides a highly flexible designing and cost effective solution too many of embedded control applications. Figure 2.2 Pin Configurations PIC 16F877A PIC 16F877A provides the following standard features: 8K bytes of Flash memory, 256 bytes of RAM, 33 I/O lines, four 8/16 timer/counter, addressable UART module which supports RS-490 network, 8 input channels of Analog to Digital Module. 12 Figure 2.2 shows the interface peripheral available in Microchip PIC 16F877A microcontroller. Basically internal interface has an input-output peripheral which is used to interface with other devices such as LED and switches. Port Structure All five ports in the PIC 16F877A are bidirectional. Some pins of the I/O ports are multiplexed with an alternate function from the peripheral features on the device. In general, when a peripheral is enabled, that pin may not be used as a general purpose I/O pin. Each port has three registers for its operation. These registers are: • TRIS register (data direction register) • PORT register (reads the levels on the pins of the device) • LAT register (output latch) The data latch (LAT register) is useful for read-modify write operations on the value that the I/O pins are driving. PORTA is a 6-bit wide, bi-directional port. The corresponding Data Direction register is TRISA. Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input (i.e., put the corresponding output driver in a Hi-Impedance mode). Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output (i.e., put the contents of the output latch on the selected pin). PORTB is an 8-bit wide, bi-directional port. The corresponding Data Direction register is TRISB. Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input (i.e., put the corresponding output driver in a Hi-Impedance 13 mode). Clearing a TRISB bit (= 0) will make the corresponding PORTB pin an output (i.e., put the contents of the output latch on the selected pin). PORTC is an 8-bit wide, bi-directional port. The corresponding Data Direction register is TRISC. Setting a TRISC bit (= 1) will make the corresponding PORTC pin an input (i.e., put the corresponding output driver in a Hi-Impedance mode). Clearing a TRISC bit (= 0) will make the corresponding PORTC pin an output (i.e., put the contents of the output latch on the selected pin). PORTD is an 8-bit wide, bi-directional port. The corresponding Data Direction register is TRISD. Setting a TRISD bit (= 1) will make the corresponding PORTD pin an input (i.e., put the corresponding output driver in a Hi-Impedance mode). Clearing a TRISD bit (= 0) will make the corresponding PORTD pin an output (i.e., put the contents of the output latch on the selected pin). PORTE is a 3-bit wide, bi-directional port. The corresponding Data Direction register is TRISE. Setting a TRISE bit (= 1) will make the corresponding PORTE pin an input (i.e., put the corresponding output driver in a Hi-Impedance mode). Clearing a TRISE bit (= 0) will make the corresponding PORTE pin an output (i.e., put the contents of the output latch on the selected pin). USART (Universal Synchronous Asynchronous Receiver/Transmitter) Many PIC microcontroller devices have a built-in USART and it is one of the most commonly used serial interface peripherals. It is also known as the Serial Communications Interface, or SCI. 14 USART stands for Universal Synchronous Asynchronous Receiver Transmitter and its main function is to transmit or receive serial data. For implement USART, microcontroller must be able to generate accurate clock pulse for its own time cycles. For easy implementation, most microcontrollers today used the crystal as an oscillator. In this mode, the USART uses standard non-return-to zero (NRZ) format (one START bit, eight or nine data bits and one STOP bit). The most common data format is 8-bits. An on-chip dedicated 8-bit baud rate generator can be used to derive standard baud rate frequencies from the oscillator. The UART transmits and receives the LSB (less significant bit) first. The USART’s transmitter and receiver are functionally independent, but use the same data format and baud rate. USART can go up to more 115200 bps if higher oscillator are implement, but its will make program become more and more difficult. USART are half duplex communication. There are having 2 hardware pins will needed by this 16F877A, that one pin to transmit and one pin to receive To implement USART software; the code drives a pin low, waiting for a bit time then drives the pin low or high depending on bit zeros of the character to be transmit. Waiting the bit time and repeating this process until finish all eight bit are output followed by outputting a high for one bit period allows generation of RS490, the process is easily to allow parity or extra stop bits to be output. The timing is very important in USART implementation. USART can be implementing in full duplex to, but its must using task switching method. As mentioned before, the USART can transmit and receive data serially. It can transfer a frame of data with eight or nine data bits per transmission and detect 15 errors when data is overwritten or incorrectly framed. It can generate interrupts when a reception occurs (or a transmission completes) and it contains data buffers that simplify the timing requirements of the software controlling the USART. In order to configure pins RC6/TX/CK and RC7/RX/DT as the Universal Synchronous Asynchronous Receiver Transmitter: • Bit SPEN (RCSTA<7>) must be set (= 1), • Bit TRISC<6> must be cleared (= 0), and • Bit TRISC<7> must be set (=1). 2.4 MAX 490 Chips. This standard for RS 485 using MAX490 chip provide 4 wire full duplex and 2 wires in both receiver and transmission. Full duplex serial communication use 0 to 5 volt differential signals. The correct input and output voltage is around 4.5 to 5.5 volt for “mark 1” and -0.5 to 0.5 volt for ‘mark 0”. Voltage supply (VCC) for this chip is 12 Volt. The bit rate for transmission is up to 2.5 Mbps per transmitting. In the data bus, the logic 1 is represented in 12 volt and logic 0 is represented in -12 volt. This chip can be use in using two half duplex. There are two enable pins available for the transceiver to be using in receive mode or transmit mode. Driver output, receiver input, driver input, transmitting output voltage (A, B, Z and Y) provided -8V to +12.5V to work. Control input voltage (DI) provide 0.5V to (VCC + 0.5V) and receiver output voltage (RO) provide -0.5V to (VCC +0.5V). 16 Figure 2.3: MAX-490 2.5 LCD Recently, a number of projects using intelligence liquid crystal display (LCD) have increased. Their ability to display not just numbers but also letters, words, and all manner of symbols make them a good deal more versatile than 7- segment (LED display). In this project, the type of LCD display that was used is The HD44780U. The HD44780U is a dot-matrix liquid crystal display controller and the driver LSI displays alphanumeric, Japanese kana characters, and symbols. It can be configured to drive a dot-matrix liquid crystal display under the control of a 4- or 8-bit microprocessor. Since all the functions, such as displaying RAM, character generator, and liquid crystal driver, required for driving a dot-matrix liquid crystal display are internally provided on one chip, a minimal system can be interfaced with this controller/driver. 17 A single HD44780U can display up to one 8-character line or two 8-character lines. The HD44780U has a pin function compatibility with the HD44780S which allows the user to easily replace an LCD-II with an HD44780U. The HD44780U character generator ROM is extended to generate 208 5 × 8 dot character fonts and 32 5 × 10 dot character fonts for a total of 240 different character fonts. The low power supply (2.7V to 5.5V) of the HD44780U is suitable for any portable batterydriven product requiring low power dissipation. Figure 2.4: LCD 2.6 Keypad To make a Master controller control the external controllers (slave controllers), a keypad was chosen to make it user-friendly. There are various types of them in the market. The keypad that is discussed here is the 4x4 keypad type. The keypad is a simple type comprising of sixteen buttons. The figure2.5 below shows the keypad that was used in this project. However, the keypad also can be built personally using sixteen normally closed switches. The 18 keypad will be directly connected to the microcontroller because it is made by sixteen switches internal and build in resistors. The active low or active high are dependent on the design in the circuit. For other alternatives, a keypad encoder can reduce the complexity of software programming using assembly language. One of the encoder for 4 x 4 keypad is IDE1144 keypad encoder IC. Figure 2.5: Keypad 2.7 ULN 2003 ULN 2003 is used for relay circuit in house model system. The Relay transistor chip provides seven relay circuits into a single chip. ULN 2003 has seven NPN Darlington connected transistors. The relay allows high current application that is used as a switch. The chip provides easy connection to the microcontroller. There are various combinations (poles and throws), various activation voltage (5V, 6V, 9V, 12V, 24V), and various switching capacity. The chip can be applied on a logic circuit (without 19 microcontroller, manual controller box). In this project, it’s used to control the alarm and motor direction, but not speed. 2.8 Eagle software To design the PCB board for this project, Eagle Layout software editor is chosen. The figure 2.6 above shows the GUI form the Eagle software. It is simple software because the design is directed to the board (printed circuit). Design for the actual circuit will be done but it is optional. The new project is created with go to <File> option, select <New> and select <Board>.the figure 2.7 will be showing the GUI for a new project slide. Figure 2.6: Eagle Layout GUI 20 Figure 2.7: New Slide Project 2.9 Daisy Chaining Software skill implemented in this project comes out from the idea of Daisy Chaining. Daisy Chaining is the algorithm for arrangements to the slave to manage the traffic in data bus when/if two or more slaves request to master controller at the same time. It is based on polling algorithm and depends on speed, critical function, and size of data. There are two types of application of the Daisy Chaining, which is; 1) Hardware connecting 2) Software implementation 21 Hardware connecting is implemented if there is crucial traffic in the system but it can increase the cost. This project implements software only and the critical level of communications. MASTER CONTROLLER LCD Daisy Chaining Keypad DATA BUS #2 #1 SLAVE CONTROLLER INPUT & OUTPUT INPUT & OUTPUT RS232 RS485 Figure 2.8: Overview of the project The arrangement is that the slave controller chooses the first slave(#1) because it has the critical job, highest speed, and smallest size of data, it is followed by the second controller (#2) which is the less critical job, lower speed, and larger size of data. This will cause a starvation on data bus, when two or more slave want to request at the same time and the critical job will not be done in real time. Finally, the system will be not efficient. When the master controller receives a request, it will check the slave controller (#1) first, and if the first slave has work, the job must be settled. Master controller will send the enable signal to slave and link and data to/ from the Master controller until the work has been done. It is followed by the second slave and the process will continue indefinitely. 22 CHAPTER 3 DESIGN ARCHITECTURE 3.1 Master Controller This project is based on communication between micro controllers. It is constructed in two levels consisting the master level and slave level. There are one master and two slaves. The number of slaves is linear to the number of jobs that the master wants to monitor. In this project, the number of slaves that were designed is up to four slaves. 1 2 3 A 4 5 6 B 7 8 9 C * 0 # D Figure 3.1 Keypad 23 In the Master controller unit, one LCD and one keypad are attached to it. The LCD is used to monitor the slaves and the keypad is used to select the specific slave and control the specific port. The Alphanumeric numberings is designed to select the type of slaves. When the button ‘A’ is pressed, it considers that Slave A is selected to be monitored, When button ‘B’ is pressed, it considers Slave B is selected, and the other button ‘C’ and ‘D’ is not used (spared for new addition of slaves). The numbers ‘1’ to ‘9’ are used to select a specific port in a specific slave. The ‘*” button design is to control a variety of functions such as on/off light, fan, and open/close door. The ‘#’ switch is designed to lock or unlock the door only. PORT Design For Master Controller PORT A Table 3.1: Port A for Master Controller Bit 0 Bit 2 and 3 Switch to stop the Alarm Bit controller for LCD. 24 Bit 3 and 5 To show there has been communication between Master and Slave. In the initial state, the LED is always on. If the bit 3 is blinking, there has been communication between Master and Slave C. It is same if there have blinking in bit 5, it is showing you that there are communications between Master and Slave D. However, slave C and D are spared for any new additions, so the LEDs are always on. PORT B Table 3.2: Port B for Master Controller Bit 0 to 7 Keypad Scanning PORT C Table 3.3: Port C for Master Controller Bit 1 to 4 Alarm LED; this LED will show you that the alarm status is on or off. Bit 6 and 7 Designed for communications. Bit 6 is for transmit data using USART and another 1 for receiving data for USART 25 PORT D Table 3.4: Port D for Master Controller Bit 0 to 7 LCD data port. PORT E Table 3.5: Port E for Master Controller Bit 0 and 1 To show that they have been communications between Master and Slave. In the initial state, the LED is always on. If the bit 0 is blinking, there have been communications between Master and Slave A. It is same if there have blinking in bit 1, its shows that there are communications between master and slave B. 3.2 Slaves Controller Every slave is designed to control a specific function or job. This project is designed in two number of slaves only, Slave A and B. There are some of input output modules available for the slaves controller to control its activities For slave’s controller unit, the microcontroller is designed to revive and transmit command from or to master controller using serial communication RS 485 full duplex (MAX 490). 26 3.2.1 Slave A Slave A as a controller to control the lamp and fan. The slave A is designed to control 4 lamps another 4 fans. PORT Design For Slave Controller A PORT A Table 3.6: Port A for Slave Controller A Bit 0 to 7 Not used PORT B Port B uses LEDs for representing lamps and fans. Table 3.7: Port B for Slave Controller A Bit 0 to 3 Lamp 1 to lamp 4. Bit 4 to 7 Fan 1 to fan 4. PORT C Table 3.8: Port C for Slave Controller A Bit 5 For LED to show the system in slave A is ready and is supplied by power Bit 6 and 7 Designed for communications. Bit 6 is designed to transmit data using USART and another 1 for receiving data for USART. 27 PORT D Port D is designed for switch circuit. The entire bit is corresponding to lamps and fans for Port B. Table 3.9: Port D for Slave Controller A Bit 0 to 3 Lamp 1 to lamp 4. Bit 4 to 7 Fan 1 to fan 4. PORT E Table 3.10: Port E for Slave Controller A Bit 0 to 7 3.2.2 Not used Slave B Slaves B is to control the security system. 4 pins are designed to represent an open or closed door. Another 4 pins are designed to lock the corresponding door. For this application, the system has designed another 4 pins to represent that the door is opened or brake opened. PORT Design For Slave B PORT A Table 3.11: Port A for Slave Controller B Bit 0 to 3 Supply power (Vdd) for pin 11 (Vdd) in PIC controller. It is because; the pin 11 in 28 PCB is difficult to achieve Vdd point. The selected 4 pin is to make sure it will stabilize the power supply and the 4 pin must always be on (logic 1). PORT B LED to represent whether the door is close, open, lock and unlock. Table 3.12: Port B for Slave Controller B Bit 0 to 3 Shows the door is open or close Bit 4 to 7 Shows the door is lock or unlock. PORT C Table 3.13: Port C for Slave Controller B Bit 0 to 3 Represent the alarm in corresponding door. If bit 0 is on, its shows the door 1 is brake open, if bit 1 is on, its shows the door 2 is brake open and so on. Bit 6 and 7 Designed for communications. Bit 6 is design to transmit data using USART and another 1 for receiving data for USART. PORT D Table 3.14: Port D for Slave Controller B Bit 0 to 7 Designed as a switch with corresponding PORT B. Bit 0 is switch LED for PORT B.0, Bit 1 is switch LED for PORT B.1, and 29 so on. PORT E Table 3.15: Port E for Slave Controller B Not used. 3.3 Communications The communication phase is divided into two categories. It consists of master to slave and internal communication in every controller. In master to slaves’ communication, the master will send the address of the slaves that is selected to communicate. For communicating to Slave A, master will send character “A”, and the slave must receive the data and send back their address to master showing it is ready for start the communication. For the Daisy Chaining algorithm, Slave A is selected to be first slave because of simpler instruction, higher speed, and smaller data sizing. Because of larger instruction and data sizing at the Slave B, it selected to be a second slave to reduce delay for the whole system. The data that will be sent is in “logic 0”. So, when the slaves receive “logic 0”, it shows that there are instructions from master to slaves. The second phase is the communication from slave to master. There are some differences between master to slave communication. Firstly, it must wait for its turn 30 which was selected from the master. Secondly, the instructions will be sent in “logic 1’ that’s designed to differentiate between the 2 ways communications. In serial data transmission the data is transmitted in serial format with the LSB of the byte to be transmitted, shifted out first among the data bits. The eight bit is selected to provide one byte of data that want to deliver. Communication between master and slave A is in two byte of data. Firstly both must send it address to master, for example “A” character for slave A. The second byte is the data information that will execute. For communications between slave B, there is designed to send three byte of data. Firstly is there address, second byte for controlling the main input output and the last one is for the alarm routine. For this project, there have not been any communications between the slaves. It will show that the master have the biggest “voice” in this system implementation. 32 CHAPTER 4 HARDWARE DESIGN 4.1 Controllers Circuit The main component in the circuit is the microcontroller. There are two types of controllers used to construct the circuit, that is PIC 18F452 for Master controller and PIC 16F877A for slaves controller. To build the microcontroller circuit, there are three important parts that must be alerted. First is about the oscillator circuit, reset circuit, and power supply circuit (Vdd and ground). For master controller, the selected oscillator is 10 MHz, and the other oscillator for the two slaves’ controller is the same, 10 MHz to generate clock pulse for microcontrollers. The same oscillator frequency between Master and Salves oscillator is to make the system run clearly because of the rate for transmission and receiving the data must be same at least for a few second. 32 Figure 4.1: Oscillator circuit (C1 and C2 is 22pF) For power supply circuit, voltage regulator is used to reduce and stabilize the power to suit the function. Input for power supply is acceptable in range 12 – 15 volt. For PIC microcontroller, it is working in 4.5 - 5 Volt power supply. The voltage regulator used is 78L05 to reduce 12 Volt to 5 Volt. However, there is also another 2 voltage regulator in Master Controller board. First one (78L05) is used for LCD power supply and the other one (78L12) is to generate 12 Volt for serial communication. The separated voltage regulators are to make sure it will reduce the heat from voltage regulator. The capacitor in voltage regulator circuit is to stabilize the power when the power is oscillate. For this circuit 10uF capacitor is selected. Figure 4.2: Voltage regulator circuit (C1 and C2 is 10uF) There are many types and functions of switch. In this project, the normally open (NO) switch is used. The switches are used as a reset button switch, open/off 33 lamp and etc. The entire switch is build together with a 10k Ohm resistor to limit the current flow to the microcontroller. MLCR pin (Microcontroller) Figure 4.3: Reset circuit For LCD circuit, it uses one adjustable resistor to adjust the contrast of LCD. Another component that must be added is diode IN4007. This component is used to make sure that there is no current flow in the wrong way into the circuit when the power supply is wrongly connected. The design is optional because the function is to protect the component especially the microcontroller. Figure 4.4: LED circuit (Active High) 34 The function of MAX490 is to convert TTL signal to RS485 format or viceversa and one connector provides to be link up each other. 4.2 Circuit for Master Controller Figure below shows the circuit for master controller. 35 keypad Figure 4.5: Master Controller circuit LCD 36 Table 4.1: Component and Description for Master Controller Part R1-R7 Description Resistance is used for a switch normally open and reset circuit. C1-C11 R1, R2 =10kΩ R3 (Rheostat) =10kΩ R4 –R7 =10kΩ To be use in voltage regulator circuit, MCLR circuit, and oscillator circuit. Q1 LED C1 - C2 = 0.1uF C3 – C4 = 22pF C5 – C11 = 10uF Crystal oscillator = 10MHz LED1- LED 8 =5mm LED9 (power LED) =5mm IC1 Microchip 16F877A IC2 MAX 490 IC3, IC4 Voltage Regulator 78L05 IC5 Voltage Regulator 7812 S1, S2 Switch Normally Open. LCD Keypad HD44780U EDE1144 4.3 Circuit for Slave Controller A Figure below shows the circuit for slave controller A. 37 Figure 4.6: Slave Controller A circuit 38 Table 4.2: Component and Description for Slave Controller A Part R1-R9 Description Resistance is use for a switch normally open and reset circuit. R1 R2 – R9 C1-C5 To be use in voltage regulator circuit, MCLR circuit, and oscillator circuit. C1 C2 – C3 C4 – C5 Q2 = 330Ω = 10kΩ = 0.1uF = 22PF = 10uF Crystal oscillator = 10MHz LED LED1- LED 8 LED9 (power LED) IC1 MAX 490 IC2 Microchip 16F877A IC3 Voltage Regulator 78L05 S1- S9 =5mm =5mm Switch Normally Open. 4.4 Circuit for Slave Controller B Figure below shows the circuit for slave controller B. 39 Figure 4.7: Slave Controller B circuit Figure 4.7: Slave Controller B circuit 40 Table 4.3: Component and Description for Slave Controller B Part R1-R13 Description Resistance is used for a switch normally open and reset circuit. R1 –R4 R5 R6 –R13 C1-C5 To be used in voltage regulator circuit, MCLR circuit, and oscillator circuit. C1 C2 – C3 C4 – C5 Q1 = 0.1uF = 22pF = 10uF Crystal oscillator = 10MHz LED LED1- LED 12 LED13 (power LED) IC3 MAX 490 IC2 Voltage Regulator 78L05 IC1 Microchip 16F877A S1- S9 S10 4.5 House Model 4.5.1 =1kΩ = 100Ω =100Ω Relay Circuit Switch Normally Open. Switch ON/ OFF 4 input =5mm =5mm 41 Relay works as a switch and can be used for a different stage of power. The high power can’t be connected directly to the microcontroller because it will damage the system when it is connected. Relay have two states which are normally open or normally closed. In this project, single pole double throw relay was used for designing the relay circuit. To test the circuit, two storey home models is used for the system. In the design; it used two circuits for relay function, firstly to represent the fan, and another one is to represent the alarm. The fan used 12 volt and higher current than controller circuit. So it cannot connect directly to the microcontroller because the microcontroller only uses around 25 mA of current. The difference between the current and voltage will damage the microcontroller. Another relay circuit is used for alarm system. Alarm system needs quite a smaller current flow, but higher voltage than microcontrollers. The alarm used nine Volts direct current. The figure 4.7 below shows the relay circuit for house model. EXTERNAL RELAY ULN 2003 Figure 4.8: Relay Circuit The relay circuit include ULN 2003 chip. It acts like a relay circuit but developed using transistors into a single chip. One ULN 2003 can control up to 7 relay circuits (one transistor can control one relay circuit). It provides simple 42 interface to the input/output. For this system, it was developed in two level with the first level consist the connection between microcontroller to ULN2003 chip, and the second level consist ULN 2003 to external relay (Fan relay and Alarm relay). Figure 4.9: Fan Figure 4.11: Lamp Figure 4.10: Alarm System Figure 4.12: Magnetic Door Sensor 4.6 Cable Design. For this project, there is one type of device built to setup the RS485 network. The device is used to make sure that the length of communication is expandable. The 43 devices will perform up to 4000 feet in length. This prototype project used ribbon wire but in a real project the recommended wire is twisted pair wire because of their cost, reliability and length. The cable is twisted 2-12 twist per feet to reduce the damage in transmitting. The cable is usually known used as telephone wire. The frequency rate is compatible to send the data and voice. It provides 100Hz to 5 MHz of data frequency. So, it is quite enough to be implemented in this circuit that sends the simple data and instruction. CHAPTER 5 SOFTWARE AND PROGRAMMING 5.1 Microcode Studio Plus Software. Two common languages are used to program the PIC microcontroller which is the assembly language and the compiler language. Nowadays, there are many compiler language are available to make programming easier, reduce the complexity and faster development compared to assembler programming language and it was practical in industry. However, there are disadvantages when using compiler languages, which is it use larger memory and not suitable for real time functioning. But, it can be adjusted or edited because some of the compiler will creates .asm file and the editing will be there. In this project, all the functions are written using Microcode Studio Plus compiler. The main function is communication and it’s not critical for real time. This compiler use basic language and it gives to some of the library that we can use for some function that was already there. Microcode Studio Plus was created specifically for Microchip PIC codes only. The file will be saved in .pbp file. After the coding was 45 compiled, it can create many files and the main file is .hex (machine codes) and .asm file (lower level languages). Figure 5.1: Micro Code Studio GUI 5.2 Programming Flow Chart. In this project, Compiler Microcode Studio Plus is used in microcontroller programming. This Flow chart will explain in specifically that the microcontroller working. There are some of the routine that the master and slave are used like This project was implements keypad and LCD, so it have to own routine to scanning the keypad and showing the output in LCD. 46 5.2.1 Master programming Master programming will start with initialize the port which the input, output, baud rate, define buffer space for receive and transmit data, define bit, setting function and delay time for LCD. All the output input will be testing to make sure all the output will be function. Main function will be start with scan the keypad using keypad routine and the result of scanning will be used to select the specific slave, port and job. Besides that, all the information will be saved to declare memory and will be send using serial communication routine. Below will show the master controller programming flow chart; 47 Figure 5.2: Master main programming 48 Yes No Yes No Yes No Yes No Yes No Yes No Yes No Figure 5.3: Master main programming (PART2) 49 Yes No Yes No Yes No Yes No Yes No Yes No Yes No Figure 5.4: Master main programming (PART3) 50 C Yes If SLAVE = “A” No If SLAVE = “B” Yes CALL SELB No Yes If SLAVE = “C” CALL SELC No Yes If SLAVE = “D” CALL SELD CALL SELA Figure 5.5: Master main programming (PART4) 51 Yes No Yes No Yes No Yes No Yes No Yes No Yes No Figure 5.6: Master main programming (PART5) 52 Yes No Figure 5.7: Master main programming (PART6) SELA is a same with another 3 which is SELB, SELC, and SELD in the flow chart. But the different between SELA, SELB, SELC, and SELD is the memory, MDATA. If MDATA1 is used for SELA, MDATA2 is provided for SELB and so on. 53 Yes No Yes No Yes No Yes No Figure 5.8: Master main programming (PART7) 54 SELAD Clear LCD Check PORT bit and Data Receiving If PORT = (n) && Data Receiving.(n-1) = 1 Yes Display LAMP(n) = ON No If PORT = (n) && Data Receiving.(n-1) = 0 Yes Display LAMP1(n) = OFF No If PORT = (p) && Data Receiving.(p-1) = 1 Yes Display FAN(p) = ON No If PORT = (p) && Data Receiving.(p-1) = 0 Yes Display FAN(p) = OFF No Goto DISPLAYEND Figure 5.9: Master main programming (PART8) The figure above show the flow chart to control the input from port A. the character n is representing the no 0 to 3 and the character p is to representing no 4 to 7. 55 Yes No Yes No Yes No Figure 5.10: Master main programming (PART9) Figure above show the flow chart for control the input from port A. The character ‘n’ represent the no 0 to 3. 56 No Yes No Yes Figure 5.11: Master main programming (PART10) 57 Yes No Figure 5.12: Master main programming (PART11) 5.2.2 Scanning Keypad The figure below shows the keypad scanning. In that flow chart, it shows the scanning for alphanumeric number in keypad. To make it scan the keypad, it must send “logic 1” to the X-axis and scan the Y-axis. The Y-axis is 0 (input low), it showed the button was pressed. 58 Yes No Yes No Yes No Yes No Figure 5.13: Scanning Keypad 59 Yes No Yes No Yes No Yes No Figure 5.14: Serial Communication in master controller 60 5.2.3 Slave A programming Slave A programming will start with initializing the port which the input, output, baud rate, define buffer space for receive and transmit the data. Port B is set to be clear to make it default. All the output will test to make sure all the output will be function. Main function will be start with routine serial communication to check whether have any instruction from master and send the new scenario in Slave controller A. Figure 5.15 below will show the Slave A controller programming flow chart; 61 Figure 5.15: Slave A main programming 62 Yes No Yes No Yes No Yes No Figure 5.16: Slave A main programming (PART 2) 63 Second Test Testing All Bits in PORT B Yes If bit = 0 && PORTD = 0 Bit = 1 No If bit = 0 && PORTD = 1 Yes Bit = 0 No Yes If bit = 1 && PORTD = 0 Bit = 0 No Yes If bit = 1 && PORTD = 1 Bit = 1 No J Figure 5.17: Slave A main programming (PART 3) 64 Yes No Figure 5.18: Slave A main programming (PART 4) 5.2.4 Slave B Programming Slave B programming will start with initialize the port which the input, output, baud rate, define buffer space for receive and transmit the data. Port B is set to be clear to make it default. All the output will be testing to make sure all the output will be function. Main function will be start with routine serial communication to check whether it has any instruction from master and send the new situation in Slave controller B. Figure 5.19 below shows the Slave B controller programming flow chart; 65 START Initialize PORT Clear PORTB Testing all Output PORTA = 255 PORTD = 255 PORTB = 0 ALARMR = 0 ALARM = 0 M L PORTC = 0 Test1 Figure 5.19: Slave B main programming 66 Yes No Yes No Yes No Yes No Yes No Figure 5.20: Slave B main programming (PART2) 67 Yes No Yes No Yes No Yes No Yes No Figure 5.21: Slave B main programming (PART3) 68 CLEARBIT Yes PORTD = 255 ALARMTEST No Figure 5.22: Slave B main programming (PART4) 69 ALARMTEST Yes If PORTB.4 = 1 && PORTA.0 = 1 ALARM.0 = 1 No If PORTB.5 = 1 && PORTA.1 = 1 Yes ALARM.1 = 1 No Yes If PORTB.6 = 1 && PORTA.2 = 1 ALARM.2 = 1 No Yes If PORTB.8 = 1 && PORTA.3 = 1 ALARM.3 = 1 No Yes If ALARM = 0 L No ALARM_OUT Figure 5.23: Slave B main programming (PART5) 70 No Yes Figure 5.24: Slave B main programming (PART6) 72 CHAPTER 6 EXPERIMENTS AND RESULTS 6.1 System Setup When 12 to 15 volt is supplied to the Master Controller board, all the outputs for both the master and slave ports such as the LED, lamp, fan and alarm will be activated for a short period of time then will deactivate automatically. The reason it was designed that way is to initialize and make sure that every port is functioning. For the Slave Controllers, the power is supplied through the cable from master controller circuit. In the design, the baud rate is set to 9600 Mbps for the serial communication. At the beginning, when the power is turned on, the LCD will show the output in figure 6.1. Figure 6.1: Beginning output 72 This chapter will explain on some of the experiment that was conducted to show the function for the system. The result will be shown in the experiment result. 6.2 Experiment 1 The objective for experiment one is to test the function for every slave controller unit. The procedure is 1) Pressing any switch button between fans or lamps in Slave A 2) Pressing any switch button for input between open /close door and lock for Slave B. 3) Observe the result. Experiment result : The result for slave A, figure 6.2 shows the scenario of slave A before any switch is pressed. The figure 6.3 will show the result when switch for lamp one is pressed. Figure 6.2: Slave A (before) 73 Figure 6.3: Slave A (after) The result for slave B, figure 6.4 shows the scenario of slave B before any switch is pressed. The figure 6.5 will show the result when switch for open door is pressed. The result shows that door four is opened. Figure 6.6 shows the result when switch for lock door is pressed Figure 6.4: Slave B (before) 74 Figure 6.5: Slave B (open door is pressed) Figure 6.6: Slave B (Press lock door) 6.3 Experiment II. The objective for this experiment is to show the master will monitor the output of the Slave Modules. The procedures are, 1) Using alphanumeric keypad button in the master controller, select the slaves that needs to be monitored. 2) The specific port using button number in the keypad is selected. 75 Experiment result : Figure 6.7 shows the scenario of slave A when slave A is selected and the figure 6.8 will shows the result when slave B is selected. Figure 6.7: First Result (A) Figure 6.8: First Result (B) 6.4 Experiment III. The objective for this experiment is to show that the master can control the output of the slave modules. The procedures are, 1) Alphanumeric keypad button in the Master controller is used; the slave that needs to be controlled is selected. 2) The specific port using button number in keypad is selected. 76 3) ‘*’ button, control to on\of the lamp in the slave A is used, and open\close the door for slave B. 4) “#’ button in keypad is used to locked or unlock the door. Experiment result : Figure 6.9a and 6.9b shows the scenario of slave A when slave A, port one and ‘*’ button is pressed. Figure 6.10a, 6.10b and 6.10c shows the result when slave B is selected, ‘*” button and ‘#” button are pressed. Figure 6.9 a: Slave A (before) Figure 6.9 b: Slave A (after) Figure 6.10a: Slave B (before) 77 Figure 6.10b: Slave B (‘*’ button is pressed) Figure 6.10c: Slave B; and (‘#’ button is pressed) 6.5 Experiment IV The objective for this experiment is to show that the master will monitor and control the alarm security system. The procedures are; 1) Alphanumeric keypad button in the Master controller is used; the slave that needs to be controlled is selected. 2) The specific port is selected using the button number on the keypad. 3) Button‘*’ is used to control on/of the lamp in slave A, and open/close the door in slave B. 4) Button “#’ is used on keypad; control whether to lock or unlock the door. 5) When the door is closed and locked, try to breach the corresponding door. 6) Before closing the breached door, try to turn off the alarm. 7) Close the door, and try to turn off the alarm system. Experiment result : 78 Figure 6.11 shows the situation when door four is closed. Figure 6.12 will show the result in LCD when the door is breached. Figure 6.11: The state showing the corresponding door before being breached Figure 6.12: The state showing the corresponding door is breached Figure 6.13: This LED shows the door is breached at door four, slave B 79 Figure 6.14: Master trying to turn off the alarm while the door is still open after being breached. Figure 6.15: Final result, the door that was breached is closed and the Master turns off the alarm system. CHAPTER 7 CONCLUSION 7.1 Problem Encountered 7.1.1 Solve the problem in RS485 Half Duplex The previous project used the MAX 485 which is a half duplex transceiver. The design will be more complex if the number of slave increases. Half duplex communication is fixed to a one way transmission at one time. If there transmission and receiving are happening at the same time, the data will collapse, and the system will shut down and the circuit will be reset. So the system will start from the initialized state. If there are a big amount of slaves in the system, only a single slave will send the request at one time. The master will communicate with the slave and the other slaves must wait for request. The problem is how to reduce the execution time and how the master will manage the slaves as some of the slaves will control certain critical jobs in the system? Figure below shows the circuit of the MAX485. 81 Figure 7.1: MAX 485 The problem in two way communication would be settled by using a full duplex communicator. MAX 490 separates the data bus into two lines; first for receiving the data and the other one is to transmit the data. At the same time, the amount of connections will increase too. Figure 7.2 shows the internal circuit of the MAX490 chip. Figure 7.2: MAX 490 7.1.2 Complexity programming in Master Controller 82 Nowadays, there are many types of compiler that are developed to reduce the complexity of coding in assembly language. Many companies and industrial sectors today use the compiler to build their system. Simply because, they want to reduce construction time. If there is a critical job that must done in real time, the adjusting will done at the .asm file that will be created. The result is same, but execution time is reduced. 7.2 Future Work This is just a prototype project and there are a lot of additions that can be made to maximize the number of task monitored by the Master Controller. In conclusion, four slaves will be used, that means two addition slaves will be added into the system. So the enhancements and addition of a new slave into the next project is done just by linking up to the system that has been done. So it’s will save cost, and also time. Other recommendation is to use a remote control, where the system can be controlled from a distance. Besides that, this addition would make the system more user-friendly and compatible. This project is recommended to be implemented in medium size Smart buildings. The length will be up to 4000 feet using MAX 490 transmissions. The technique in MAX490 provides the error checking in transmission line. So the error will be minimized and the data is very accurate between the maximum lengths. 83 7.3 Conclusion There are various types of microcontrollers in the market like Atmel, Motorola and many more. The Microchip microcontroller is recommended to be used because it is cheaper and provide user up to 100,000 time’s more rewriteable flash memory. The other microcontroller is much better than Microchip in providing fast controllers and high accuracy. Atmel is generally used in Military systems as real time, speed and accuracy is needed. The RS-485 network is a powerful transmission method because it has a fast rate transmission and a large range (4000 feet). All the data can be received and transmitted in short time. The architecture of the circuit provides error checking technique. One of the data will be sent and the other will inverted. The receiving end also has an inverter to invert the received data back to the original data. So, it minimizes error especially in the long distance communication where invalid data will be ignored. REFERENCES 1. www.microchip.com. 2. 123 PIC® Microcontroller Experiments for the Evil Genius, Myke Predko, McGraw- Hill, 2005. 3. Thesis PSM, Design and Development of Embedded Master Controller and Remote I/O Modules, LEE HENG SENG 2004/2005. 4. Chris@greenengineer.com/ /Contents of this site are ©1999 Christopher R. Schaffner/ 5. www.coggan.com 6. RS-422 and RS-485 Application Note. USA: B&B Electronics International Ltd. 7. SPACE Microcontroller PIC UTM 2005_Day1[1] & Day2[2] 8. Microchip PIC 16F877A Datasheet & application notes 9. Selecting and Using RS-232, RS-422, and RS-485 Serial Data Standards Maxim-Dallas.htm 10. Serial Data Transmission by The Ganssle Group 11. MAX 485 & MAX 490 Data sheet and Application. 85 APPENDICES 86 APPENDIX A Printed Circuit Board of Master Controller 87 APPENDIX B Printed Circuit Board of Slave Controller A 88 APPENDIX C Printed Circuit Board of Slave Controller B 89 APPENDIX D Source Codes for Master Controller DEFINE OSC 40 MDATAT3 VAR BYTE Define HSER_BAUD 9600 ‘Variable transmit data from slave C DEFINE HSER_RCSTA 90h MDATAR4 VAR BYTE DEFINE HSER_TXSTA 20h ‘Variable for receive data slave D DEFINE CHAR_PACING 10 MDATAT4 VAR BYTE ‘Variable transmit data from slave D ' Set LCD Data port MPC1 VAR BYTE DEFINE LCD_DREG PORTD MPC2 VAR BYTE ' Set starting Data bit (0 or 4) if 4-bit bus MPC3 VAR BYTE DEFINE LCD_DBIT 0 MPC4 VAR BYTE ' Set LCD Register Select port BUTTONPRESS VAR BYTE DEFINE LCD_RSREG PORTA SLAVE VAR BYTE ' Set LCD Register Select bit SEL VAR BYTE DEFINE LCD_RSBIT 2 SWITCH VAR BYTE ' Set LCD Enable port ALARM VAR BYTE DEFINE LCD_EREG PORTA ‘Variable alarm status ' Set LCD Enable bit ALARM2 VAR BYTE DEFINE LCD_EBIT 1 ‘Variable alarm status slave B ' Set LCD bus size (4 or 8 bits) ALARM4 VAR BYTE DEFINE LCD_BITS 8 ‘Variable alarm status port D ' Set number of lines on LCD ALARMCUT VAR BYTE DEFINE LCD_LINES 2 ‘Variable data alarm cut ' Set command delay time in us CUTCOUNT VAR BYTE DEFINE LCD_COMMANDUS 2000 ' Set data delay time in us TRISA = %00000001 DEFINE LCD_DATAUS 100 TRISC = %10000000 TRISD = 0 ADCON1 = 7 TRISE = 0 DATA0 VAR BYTE TRISB = 15 '(0~3)=INPUT MDATAR1 VAR BYTE ‘Variable for receive data slave A PORTA = 0 MDATAT1 VAR BYTE PORTB = 0 ‘Variable transmit data from slave A PORTD = 0 MDATAR2 VAR BYTE PORTC = 0 ‘Variable for receive data slave B PORTE = 0 MDATAT2 VAR BYTE MDATAT1 = 255 ‘Variable transmit data from slave B MDATAR1 = 255 MDATAR3 VAR BYTE MDATAT2 = 255 ‘Variable for receive data slave C MDATAR2 = 255 (4~7)=OUTPUT 90 MDATAT3 = 255 '################################################# MDATAR3 = 255 ### MDATAT4 = 255 START: MDATAR4 = 255 PORTA = 0 MPC1 = 255 BUTTONPRESS = 255 MPC2 = 255 SWITCH = 255 MPC3 = 255 ALARM = 0 MPC4 = 255 ALARM2 = 0 SLAVE = "A" ALARM4 = 0 ALARMCUT = 0 '#################### KEYPAD PORTE.0 = 1 ###################### PAUSE 500 BUTTONTEST: PORTE = 0 PORTB = 16 PORTE.1 = 1 IF PORTB.0 = 1 THEN PRESSD PAUSE 500 IF PORTB.1 = 1 THEN PRESSC PORTE = 0 IF PORTB.2 = 1 THEN PRESSB PORTA.3 = 1 IF PORTB.3 = 1 THEN PRESSA PAUSE 500 PORTA = 0 PORTB = 32 PORTA.5 = 1 IF PORTB.0 = 1 THEN PRESSH PAUSE 500 IF PORTB.1 = 1 THEN PRESS9 PORTA = 0 IF PORTB.2 = 1 THEN PRESS6 PORTC.0 = 1 IF PORTB.3 = 1 THEN PRESS3 PAUSE 500 PORTC = 0 PORTB = 64 PORTC.1 = 1 IF PORTB.0 = 1 THEN PRESS0 PAUSE 500 IF PORTB.1 = 1 THEN PRESS8 PORTC = 0 IF PORTB.2 = 1 THEN PRESS5 PORTC.2 = 1 IF PORTB.3 = 1 THEN PRESS2 PAUSE 500 PORTC = 0 PORTB = 128 PORTC.3 = 1 IF PORTB.0 = 1 THEN PRESSS PAUSE 500 IF PORTB.1 = 1 THEN PRESS7 IF PORTB.2 = 1 THEN PRESS4 PORTA = 0 IF PORTB.3 = 1 THEN PRESS1 PORTB = 0 PORTD = 0 GOTO KEYPADEND PORTC = 0 PORTE = 0 PRESS1: BUTTONPRESS = 1 GOTO KEYPADEND128 '################################################# PRESS2: ### BUTTONPRESS = 2 '################################################# GOTO KEYPADEND64 ### PRESS3: BUTTONPRESS = 3 91 GOTO KEYPADEND32 ENDIF PRESS4: BUTTONPRESS = 4 KEYPADEND32: GOTO KEYPADEND128 IF PORTB = 32 THEN PRESS5: BUTTONPRESS = 5 GOTO KEYPADEND64 PRESS6: GOTO KEYPADEND ELSE GOTO KEYPADEND32 ENDIF BUTTONPRESS = 6 GOTO KEYPADEND32 KEYPADEND64: PRESS7: IF PORTB = 64 THEN BUTTONPRESS = 7 GOTO KEYPADEND128 PRESS8: BUTTONPRESS = 8 GOTO KEYPADEND ELSE GOTO KEYPADEND64 ENDIF GOTO KEYPADEND64 PRESS9: KEYPADEND128: BUTTONPRESS = 9 IF PORTB = 128 THEN GOTO KEYPADEND32 PRESS0: BUTTONPRESS = 0 GOTO KEYPADEND64 GOTO KEYPADEND ELSE GOTO KEYPADEND128 ENDIF PRESSS: BUTTONPRESS = "S" KEYPADEND: GOTO KEYPADEND128 '################################################# PRESSH: ### BUTTONPRESS = "H" '############### SERIAL COM. SLAVE A GOTO KEYPADEND32 ################ '################################################# PRESSA: ### BUTTONPRESS = "A" SERIALCOM1: GOTO KEYPADEND16 hserout ["A",MDATAT1] PRESSB: hserin 10, CHECKA, [WAIT("A"),STR MDATAR1\1] BUTTONPRESS = "B" PORTE.0 = 0 GOTO KEYPADEND16 GOTO SERIALCOM2 PRESSC: BUTTONPRESS = "C" CHECKA: GOTO KEYPADEND16 PORTE.0 = 1 PRESSD: '################################################# BUTTONPRESS = "D" ### GOTO KEYPADEND16 '############### SERIAL COM. SLAVE B ################ KEYPADEND16: '################################################# IF PORTB = 16 THEN ### GOTO KEYPADEND ELSE GOTO KEYPADEND16 SERIALCOM2: hserout ["B",MDATAT2,ALARMCUT] 92 hserin 10, CHECKB, [WAIT("B"),STR MDATAR2\1,STR MDATAT3 = 255 ALARM2\1] MDATAT4 = 255 PORTE.1 = 0 '################################################# ### CHECKB: '################################################# ALARM = 0 ### PORTE.1 = 1 '################################################# MAIN: ### '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '############### SERIAL COM. SLAVE A ~~~~~~~ ################ SLAVESELECT: '################################################# IF BUTTONPRESS = "A" THEN SLAVE = "A" ### IF BUTTONPRESS = "B" THEN SLAVE = "B" SERIALCOM3: IF BUTTONPRESS = "C" THEN SLAVE = "C" hserout ["C",MDATAT3] IF BUTTONPRESS = "D" THEN SLAVE = "D" hserin 10, CHECKC, [WAIT("C"),STR MDATAR3\1] PORTA.3 = 0 '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GOTO SERIALCOM4 ~~~~~~~ PORTSELECT: CHECKC: IF BUTTONPRESS = 1 THEN SEL = 1 PORTA.3 = 1 IF BUTTONPRESS = 2 THEN SEL = 2 '################################################# IF BUTTONPRESS = 3 THEN SEL = 3 ### IF BUTTONPRESS = 4 THEN SEL = 4 '############### SERIAL COM. SLAVE D IF BUTTONPRESS = 5 THEN SEL = 5 ################ IF BUTTONPRESS = 6 THEN SEL = 6 '################################################# IF BUTTONPRESS = 7 THEN SEL = 7 ### IF BUTTONPRESS = 8 THEN SEL = 8 SERIALCOM4: IF BUTTONPRESS = "S" THEN SWITCH = "S" hserout ["D",MDATAT4,ALARMCUT] IF BUTTONPRESS = "H" THEN SWITCH = "H" hserin 10, CHECKD, [WAIT("D"),STR MDATAR4\1,STR ALARM4\1] SELECTED: PORTA.5 = 0 IF SLAVE = "A" THEN SELA GOTO SERIALEND IF SLAVE = "B" THEN SELB IF SLAVE = "C" THEN SELC CHECKD: IF SLAVE = "D" THEN SELD ALARM = 0 GOTO PORTSELEND PORTA.5 = 1 '################################################# SELA: ### IF SEL = 1 AND SWITCH = "S" THEN MDATAT1.0 = 0 '################# SERIAL COM. PC IF SEL = 2 AND SWITCH = "S" THEN MDATAT1.1 = 0 ################### IF SEL = 3 AND SWITCH = "S" THEN MDATAT1.2 = 0 '################################################# IF SEL = 4 AND SWITCH = "S" THEN MDATAT1.3 = 0 ### IF SEL = 5 AND SWITCH = "S" THEN MDATAT1.4 = 0 SERIALEND: IF SEL = 6 AND SWITCH = "S" THEN MDATAT1.5 = 0 IF SEL = 7 AND SWITCH = "S" THEN MDATAT1.6 = 0 MDATAT1 = 255 IF SEL = 8 AND SWITCH = "S" THEN MDATAT1.7 = 0 MDATAT2 = 255 GOTO PORTSELEND 93 SELB: SELAD: IF SEL = 1 AND SWITCH = "S" THEN MDATAT2.0 = 0 LCDOUT $FE, 1, "A:" IF SEL = 2 AND SWITCH = "S" THEN MDATAT2.1 = 0 IF SEL = 1 AND MDATAR1.0 = 1 THEN LCDOUT IF SEL = 3 AND SWITCH = "S" THEN MDATAT2.2 = 0 "LAMP1=ON" IF SEL = 4 AND SWITCH = "S" THEN MDATAT2.3 = 0 IF SEL = 1 AND MDATAR1.0 = 0 THEN LCDOUT IF SEL = 1 AND SWITCH = "H" THEN MDATAT2.4 = 0 "LAMP1=OFF" IF SEL = 2 AND SWITCH = "H" THEN MDATAT2.5 = 0 IF SEL = 3 AND SWITCH = "H" THEN MDATAT2.6 = 0 IF SEL = 2 AND MDATAR1.1 = 1 THEN LCDOUT IF SEL = 4 AND SWITCH = "H" THEN MDATAT2.7 = 0 "LAMP2=ON" GOTO PORTSELEND IF SEL = 2 AND MDATAR1.1 = 0 THEN LCDOUT "LAMP2=OFF" SELC: IF SEL = 1 AND SWITCH = "S" THEN MDATAT3.0 = 0 IF SEL = 3 AND MDATAR1.2 = 1 THEN LCDOUT IF SEL = 2 AND SWITCH = "S" THEN MDATAT3.1 = 0 "LAMP3=ON" IF SEL = 3 AND SWITCH = "S" THEN MDATAT3.2 = 0 IF SEL = 3 AND MDATAR1.2 = 0 THEN LCDOUT IF SEL = 4 AND SWITCH = "S" THEN MDATAT3.3 = 0 "LAMP3=OFF" IF SEL = 5 AND SWITCH = "S" THEN MDATAT3.4 = 0 IF SEL = 6 AND SWITCH = "S" THEN MDATAT3.5 = 0 IF SEL = 4 AND MDATAR1.3 = 1 THEN LCDOUT IF SEL = 7 AND SWITCH = "S" THEN MDATAT3.6 = 0 "LAMP4=ON" IF SEL = 8 AND SWITCH = "S" THEN MDATAT3.7 = 0 IF SEL = 4 AND MDATAR1.3 = 0 THEN LCDOUT GOTO PORTSELEND "LAMP4=OFF" SELD: IF SEL = 5 AND MDATAR1.4 = 1 THEN LCDOUT IF SEL = 1 AND SWITCH = "S" THEN MDATAT4.0 = 0 "FAN1=ON" IF SEL = 2 AND SWITCH = "S" THEN MDATAT4.1 = 0 IF SEL = 5 AND MDATAR1.4 = 0 THEN LCDOUT IF SEL = 3 AND SWITCH = "S" THEN MDATAT4.2 = 0 "FAN1=OFF" IF SEL = 4 AND SWITCH = "S" THEN MDATAT4.3 = 0 IF SEL = 5 AND SWITCH = "H" THEN MDATAT4.4 = 0 IF SEL = 6 AND MDATAR1.5 = 1 THEN LCDOUT IF SEL = 6 AND SWITCH = "H" THEN MDATAT4.5 = 0 "FAN2=ON" IF SEL = 7 AND SWITCH = "H" THEN MDATAT4.6 = 0 IF SEL = 6 AND MDATAR1.5 = 0 THEN LCDOUT IF SEL = 8 AND SWITCH = "H" THEN MDATAT4.7 = 0 "FAN2=OFF" GOTO PORTSELEND IF SEL = 7 AND MDATAR1.6 = 1 THEN LCDOUT PORTSELEND: "FAN3=ON" IF SEL = 7 AND MDATAR1.6 = 0 THEN LCDOUT "FAN3=OFF" '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$ IF SEL = 8 AND MDATAR1.7 = 1 THEN LCDOUT "FAN4=ON" IF SEL = 8 AND MDATAR1.7 = 0 THEN LCDOUT DISPLAY: "FAN4=OFF" IF SLAVE = "A" THEN SELAD GOTO DISPLAYEND IF SLAVE = "B" THEN SELBD IF SLAVE = "C" THEN SELCD SELBD: IF SLAVE = "D" THEN SELDD LCDOUT $FE, 1, "B:" GOTO DISPLAYEND 94 IF SEL = 1 AND MDATAR2.0 = 0 AND MDATAR2.4 = 0 GOTO DISPLAYEND THEN DOOR1NLOCK IF SEL = 1 AND MDATAR2.0 = 0 AND MDATAR2.4 = 1 DOOR3NLOCK: THEN DOOR1LOCK LCDOUT "DOOR3=CLOSED" IF SEL = 1 AND MDATAR2.0 = 1 THEN DOOR1OPEN LCDOUT $FE, $C0, "UNLOCKED" GOTO DISPLAYEND IF SEL = 2 AND MDATAR2.1 = 0 AND MDATAR2.5 = 0 DOOR3LOCK: THEN DOOR2NLOCK LCDOUT "DOOR3=CLOSED" IF SEL = 2 AND MDATAR2.1 = 0 AND MDATAR2.5 = 1 LCDOUT $FE, $C0, "LOCKED" THEN DOOR2LOCK GOTO DISPLAYEND IF SEL = 2 AND MDATAR2.1 = 1 THEN DOOR2OPEN DOOR3OPEN: LCDOUT "DOOR3=OPENED" IF SEL = 3 AND MDATAR2.2 = 0 AND MDATAR2.6 = 0 GOTO DISPLAYEND THEN DOOR3NLOCK IF SEL = 3 AND MDATAR2.2 = 0 AND MDATAR2.6 = 1 DOOR4NLOCK: THEN DOOR3LOCK LCDOUT "DOOR4=CLOSED" IF SEL = 3 AND MDATAR2.2 = 1 THEN DOOR3OPEN LCDOUT $FE, $C0, "UNLOCKED" GOTO DISPLAYEND IF SEL = 4 AND MDATAR2.3 = 0 AND MDATAR2.7 = 0 DOOR4LOCK: THEN DOOR4NLOCK LCDOUT "DOOR4=CLOSED" IF SEL = 4 AND MDATAR2.3 = 0 AND MDATAR2.7 = 1 LCDOUT $FE, $C0, "LOCKED" THEN DOOR4LOCK GOTO DISPLAYEND IF SEL = 4 AND MDATAR2.3 = 1 THEN DOOR4OPEN DOOR4OPEN: GOTO DISPLAYEND LCDOUT "DOOR4=OPENED" GOTO DISPLAYEND DOOR1NLOCK: LCDOUT "DOOR1=CLOSED" LCDOUT $FE, $C0, "UNLOCKED" SELCD: GOTO DISPLAYEND LCDOUT $FE, 1, "C:" DOOR1LOCK: IF SEL = 1 AND MDATAR3.0 = 1 THEN LCDOUT LCDOUT "DOOR1=CLOSED" "LAMP1=ON" LCDOUT $FE, $C0, "LOCKED" IF SEL = 1 AND MDATAR3.0 = 0 THEN LCDOUT GOTO DISPLAYEND "LAMP1=OFF" DOOR1OPEN: LCDOUT "DOOR1=OPENED" IF SEL = 2 AND MDATAR3.1 = 1 THEN LCDOUT GOTO DISPLAYEND "LAMP2=ON" IF SEL = 2 AND MDATAR3.1 = 0 THEN LCDOUT DOOR2NLOCK: "LAMP2=OFF" LCDOUT "DOOR2=CLOSED" LCDOUT $FE, $C0, "UNLOCKED" IF SEL = 3 AND MDATAR3.2 = 1 THEN LCDOUT GOTO DISPLAYEND "LAMP3=ON" DOOR2LOCK: IF SEL = 3 AND MDATAR3.2 = 0 THEN LCDOUT LCDOUT "DOOR2=CLOSED" "LAMP3=OFF" LCDOUT $FE, $C0, "LOCKED" GOTO DISPLAYEND IF SEL = 4 AND MDATAR3.3 = 1 THEN LCDOUT DOOR2OPEN: "LAMP4=ON" LCDOUT "DOOR2=OPENED" 95 IF SEL = 4 AND MDATAR3.3 = 0 THEN LCDOUT IF SEL = 4 AND MDATAR4.3 = 0 AND MDATAR4.7 = 1 "LAMP4=OFF" THEN DOOR4LOCK2 IF SEL = 4 AND MDATAR4.3 = 1 THEN DOOR4OPEN2 IF SEL = 5 AND MDATAR3.4 = 1 THEN LCDOUT GOTO DISPLAYEND "FAN1=ON" IF SEL = 5 AND MDATAR3.4 = 0 THEN LCDOUT DOOR1NLOCK2: "FAN1=OFF" LCDOUT "DOOR1=CLOSED" LCDOUT $FE, $C0, "UNLOCKED" IF SEL = 6 AND MDATAR3.5 = 1 THEN LCDOUT GOTO DISPLAYEND "FAN2=ON" DOOR1LOCK2: IF SEL = 6 AND MDATAR3.5 = 0 THEN LCDOUT LCDOUT "DOOR1=CLOSED" "FAN2=OFF" LCDOUT $FE, $C0, "LOCKED" GOTO DISPLAYEND IF SEL = 7 AND MDATAR3.6 = 1 THEN LCDOUT DOOR1OPEN2: "FAN3=ON" LCDOUT "DOOR1=OPENED" IF SEL = 7 AND MDATAR3.6 = 0 THEN LCDOUT GOTO DISPLAYEND "FAN3=OFF" DOOR2NLOCK2: IF SEL = 8 AND MDATAR3.7 = 1 THEN LCDOUT LCDOUT "DOOR2=CLOSED" "FAN4=ON" LCDOUT $FE, $C0, "UNLOCKED" IF SEL = 8 AND MDATAR3.7 = 0 THEN LCDOUT GOTO DISPLAYEND "FAN4=OFF" DOOR2LOCK2: GOTO DISPLAYEND LCDOUT "DOOR2=CLOSED" LCDOUT $FE, $C0, "LOCKED" SELDD: GOTO DISPLAYEND LCDOUT $FE, 1, "D:" DOOR2OPEN2: IF SEL = 1 AND MDATAR4.0 = 0 AND MDATAR4.4 = 0 LCDOUT "DOOR2=OPENED" THEN DOOR1NLOCK2 GOTO DISPLAYEND IF SEL = 1 AND MDATAR4.0 = 0 AND MDATAR4.4 = 1 THEN DOOR1LOCK2 DOOR3NLOCK2: IF SEL = 1 AND MDATAR4.0 = 1 THEN DOOR1OPEN2 LCDOUT "DOOR3=CLOSED" LCDOUT $FE, $C0, "UNLOCKED" IF SEL = 2 AND MDATAR4.1 = 0 AND MDATAR4.5 = 0 GOTO DISPLAYEND THEN DOOR2NLOCK2 DOOR3LOCK2: IF SEL = 2 AND MDATAR4.1 = 0 AND MDATAR4.5 = 1 LCDOUT "DOOR3=CLOSED" THEN DOOR2LOCK2 LCDOUT $FE, $C0, "LOCKED" IF SEL = 2 AND MDATAR4.1 = 1 THEN DOOR2OPEN2 GOTO DISPLAYEND DOOR3OPEN2: IF SEL = 3 AND MDATAR4.2 = 0 AND MDATAR4.6 = 0 LCDOUT "DOOR3=OPENED" THEN DOOR3NLOCK2 GOTO DISPLAYEND IF SEL = 3 AND MDATAR4.2 = 0 AND MDATAR4.6 = 1 THEN DOOR3LOCK2 DOOR4NLOCK2: IF SEL = 3 AND MDATAR4.2 = 1 THEN DOOR3OPEN2 LCDOUT "DOOR4=CLOSED" LCDOUT $FE, $C0, "UNLOCKED" IF SEL = 4 AND MDATAR4.3 = 0 AND MDATAR4.7 = 0 GOTO DISPLAYEND THEN DOOR4NLOCK2 DOOR4LOCK2: LCDOUT "DOOR4=CLOSED" 96 LCDOUT $FE, $C0, "LOCKED" GOTO DISPLAYEND ALARM_OUT: DOOR4OPEN2: PORTC = ALARM LCDOUT "DOOR4=OPENED" LCDOUT $FE, 1, "ALARM" GOTO DISPLAYEND PAUSE 500 PORTC = ALARM DISPLAYEND: ALARMCUT = 255 hserout ["B",MDATAT2,ALARMCUT] ALARM_TEST: PAUSE 20 IF ALARM2.0 = 1 OR ALARM4.0 = 1 THEN ALARM.0 = hserout ["D",MDATAT4,ALARMCUT] 1 LCDOUT $FE, 1 IF ALARM2.1 = 1 OR ALARM4.1 = 1 THEN ALARM.1 = PAUSE 500 1 IF ALARM2.2 = 1 OR ALARM4.2 = 1 THEN ALARM.2 = IF PORTA.0 = 0 THEN 1 GOTO START IF ALARM2.3 = 1 OR ALARM4.3 = 1 THEN ALARM.3 = ELSE 1 GOTO ALARM_OUT ENDIF PORTC = ALARM END IF ALARM = 0 THEN GOTO START 97 APPENDIX E Sources Code of Slave Controller A DEFINE OSC 10 PAUSE 500 Define HSER_BAUD 9600 PORTB = 0 DEFINE HSER_CLROERR 1 PORTB.7 = 1 DEFINE HSER_RCSTA 90h PAUSE 500 DEFINE HSER_TXSTA 20h PORTB = 0 NAME VAR BYTE DATAT VAR BYTE PORTB = 0 ‘Variable transmit data to Master PORTD = 255 DATAR VAR BYTE DATAR = 255 ‘Variable Receive data from Master '************************************************* DATAS VAR BYTE *************** DATAD VAR BYTE START: DATAT = PORTB ADCON1 = 7 DATAR = 255 TRISB = 0 PORTC.5 = 0 TRISD = 255 PAUSE 1 TRISC = %10000000 hserin [WAIT("A"),STR DATAR\1] hserout ["A",DATAT] PORTB = 0 ''################################################ #### PORTB.0 = 1 PAUSE 500 '################################################# PORTB = 0 ######################## PORTB.1 = 1 '################################################# PAUSE 500 ######################## PORTB = 0 '################################################# PORTB.2 = 1 ######################## PAUSE 500 TESTB0S: PORTB = 0 DATAS = DATAR PORTB.3 = 1 PAUSE 500 PORTB = 0 PORTB.4 = 1 PAUSE 500 PORTB = 0 IF (PORTB.0 = 0) THEN GOTO B0LOWS ELSE GOTO B0HIGHS ENDIF PORTB.5 = 1 PAUSE 500 B0LOWS: PORTB = 0 IF (DATAS.0 = 0) THEN PORTB.6 = 1 GOTO HIGHB0S 98 ELSE GOTO LOWB0S ENDIF '################################################# ######################## TESTB2S: IF (PORTB.2 = 0) THEN B0HIGHS: IF (DATAS.0 = 0) THEN GOTO LOWB0S ELSE GOTO B2LOWS ELSE GOTO B2HIGHS ENDIF GOTO HIGHB0S ENDIF B2LOWS: IF (DATAS.2 = 0) THEN HIGHB0S: PORTB.0 = 1 GOTO TESTB1S LOWB0S: GOTO HIGHB2S ELSE GOTO LOWB2S ENDIF PORTB.0 = 0 GOTO TESTB1S B2HIGHS: '################################################# IF (DATAS.2 = 0) THEN ######################## TESTB1S: IF (PORTB.1 = 0) THEN GOTO B1LOWS GOTO LOWB2S ELSE GOTO HIGHB2S ENDIF ELSE GOTO B1HIGHS ENDIF HIGHB2S: PORTB.2 = 1 GOTO TESTB3S B1LOWS: LOWB2S: IF (DATAS.1 = 0) THEN PORTB.2 = 0 GOTO HIGHB1S ELSE GOTO LOWB1S ENDIF GOTO TESTB3S '################################################# ######################## TESTB3S: IF (PORTB.3 = 0) THEN B1HIGHS: IF (DATAS.1 = 0) THEN GOTO LOWB1S ELSE GOTO B3LOWS ELSE GOTO B3HIGHS ENDIF GOTO HIGHB1S ENDIF B3LOWS: IF (DATAS.3 = 0) THEN HIGHB1S: PORTB.1 = 1 GOTO TESTB2S LOWB1S: GOTO HIGHB3S ELSE GOTO LOWB3S ENDIF PORTB.1 = 0 GOTO TESTB2S B3HIGHS: IF (DATAS.3 = 0) THEN 99 GOTO LOWB3S ENDIF ELSE GOTO HIGHB3S ENDIF B5LOWS: IF (DATAS.5 = 0) THEN GOTO HIGHB5S HIGHB3S: PORTB.3 = 1 GOTO TESTB4S ELSE GOTO LOWB5S ENDIF LOWB3S: PORTB.3 = 0 B5HIGHS: GOTO TESTB4S IF (DATAS.5 = 0) THEN '################################################# ######################## TESTB4S: IF (PORTB.4 = 0) THEN GOTO LOWB5S ELSE GOTO HIGHB5S ENDIF GOTO B4LOWS ELSE GOTO B4HIGHS ENDIF HIGHB5S: PORTB.5 = 1 GOTO TESTB6S LOWB5S: B4LOWS: PORTB.5 = 0 IF (DATAS.4 = 0) THEN GOTO TESTB6S GOTO HIGHB4S ELSE GOTO LOWB4S '################################################# ######################## TESTB6S: ENDIF IF (PORTB.6 = 0) THEN B4HIGHS: ELSE GOTO B6LOWS IF (DATAS.4 = 0) THEN GOTO LOWB4S GOTO B6HIGHS ENDIF ELSE GOTO HIGHB4S ENDIF B6LOWS: IF (DATAS.6 = 0) THEN GOTO HIGHB6S HIGHB4S: PORTB.4 = 1 GOTO TESTB5S ELSE GOTO LOWB6S ENDIF LOWB4S: PORTB.4 = 0 B6HIGHS: GOTO TESTB5S IF (DATAS.6 = 0) THEN '################################################# ######################## TESTB5S: IF (PORTB.5 = 0) THEN GOTO LOWB6S ELSE GOTO HIGHB6S ENDIF GOTO B5LOWS ELSE GOTO B5HIGHS HIGHB6S: PORTB.6 = 1 100 GOTO TESTB7S LOWB6S: B0LOW: PORTB.6 = 0 IF (PORTD.0 = 0) THEN GOTO TESTB7S '################################################# ######################## TESTB7S: GOTO HIGHB0 ELSE GOTO LOWB0 ENDIF IF (PORTB.7 = 0) THEN GOTO B7LOWS ELSE GOTO B7HIGHS B0HIGH: IF (PORTD.0 = 0) THEN GOTO LOWB0 ENDIF ELSE B7LOWS: ENDIF GOTO HIGHB0 IF (DATAS.7 = 0) THEN GOTO HIGHB7S ELSE GOTO LOWB7S ENDIF HIGHB0: PORTB.0 = 1 GOTO TESTB1 LOWB0: PORTB.0 = 0 B7HIGHS: GOTO TESTB1 IF (DATAS.7 = 0) THEN '################################################# GOTO LOWB7S ELSE GOTO HIGHB7S ENDIF ######################## TESTB1: IF (PORTB.1 = 0) THEN GOTO B1LOW ELSE HIGHB7S: PORTB.7 = 1 GOTO B1HIGH ENDIF GOTO TESTB0 LOWB7S: B1LOW: PORTB.7 = 0 IF (PORTD.1 = 0) THEN GOTO TESTB0 '################################################# ######################## '################################################# GOTO HIGHB1 ELSE GOTO LOWB1 ENDIF ######################## '################################################# B1HIGH: ######################## IF (PORTD.1 = 0) THEN TESTB0: ELSE IF (PORTB.0 = 0) THEN ENDIF GOTO LOWB1 GOTO HIGHB1 GOTO B0LOW ELSE GOTO B0HIGH ENDIF HIGHB1: PORTB.1 = 1 GOTO TESTB2 101 LOWB1: PORTB.1 = 0 B3HIGH: GOTO TESTB2 IF (PORTD.3 = 0) THEN '################################################# ######################## TESTB2: IF (PORTB.2 = 0) THEN GOTO LOWB3 ELSE GOTO HIGHB3 ENDIF GOTO B2LOW ELSE GOTO B2HIGH ENDIF HIGHB3: PORTB.3 = 1 GOTO TESTB4 LOWB3: B2LOW: PORTB.3 = 0 IF (PORTD.2 = 0) THEN GOTO TESTB4 GOTO HIGHB2 ELSE GOTO LOWB2 ENDIF '################################################# ######################## TESTB4: IF (PORTB.4 = 0) THEN GOTO B4LOW B2HIGH: IF (PORTD.2 = 0) THEN GOTO LOWB2 ELSE GOTO B4HIGH ENDIF ELSE GOTO HIGHB2 B4LOW: ENDIF IF (PORTD.4 = 0) THEN HIGHB2: ELSE GOTO HIGHB4 PORTB.2 = 1 GOTO TESTB3 GOTO LOWB4 ENDIF LOWB2: PORTB.2 = 0 B4HIGH: GOTO TESTB3 IF (PORTD.4 = 0) THEN '################################################# ######################## TESTB3: IF (PORTB.3 = 0) THEN GOTO LOWB4 ELSE GOTO HIGHB4 ENDIF GOTO B3LOW ELSE GOTO B3HIGH ENDIF HIGHB4: PORTB.4 = 1 GOTO TESTB5 LOWB4: B3LOW: PORTB.4 = 0 IF (PORTD.3 = 0) THEN GOTO TESTB5 GOTO HIGHB3 ELSE GOTO LOWB3 ENDIF '################################################# ######################## TESTB5: IF (PORTB.5 = 0) THEN 102 GOTO B5LOW ELSE GOTO B5HIGH ENDIF HIGHB6: PORTB.6 = 1 GOTO TESTB7 LOWB6: B5LOW: PORTB.6 = 0 IF (PORTD.5 = 0) THEN GOTO TESTB7 GOTO HIGHB5 ELSE GOTO LOWB5 ENDIF '################################################# ######################## TESTB7: IF (PORTB.7 = 0) THEN GOTO B7LOW B5HIGH: IF (PORTD.5 = 0) THEN GOTO LOWB5 ELSE GOTO B7HIGH ENDIF ELSE GOTO HIGHB5 ENDIF B7LOW: IF (PORTD.7 = 0) THEN GOTO HIGHB7 HIGHB5: PORTB.5 = 1 GOTO TESTB6 ELSE GOTO LOWB7 ENDIF LOWB5: PORTB.5 = 0 B7HIGH: GOTO TESTB6 IF (PORTD.7 = 0) THEN '################################################# ######################## TESTB6: IF (PORTB.6 = 0) THEN GOTO LOWB7 ELSE GOTO HIGHB7 ENDIF GOTO B6LOW ELSE GOTO B6HIGH ENDIF HIGHB7: PORTB.7 = 1 GOTO CLEARBIT LOWB7: B6LOW: PORTB.7 = 0 IF (PORTD.6 = 0) THEN GOTO CLEARBIT GOTO HIGHB6 ELSE GOTO LOWB6 ENDIF '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&& CLEARBIT: IF (PORTD = 255) THEN GOTO START B6HIGH: IF (PORTD.6 = 0) THEN GOTO LOWB6 ELSE GOTO CLEARBIT ENDIF ELSE GOTO HIGHB6 ENDIF end 103 APPENDIX F Sources Code of Slave Controller B DEFINE OSC 10 '************************************************* DEFINE HSER_BAUD 9600 *************** DEFINE HSER_CLROERR 1 START: DEFINE HSER_RCSTA 90h DEFINE HSER_TXSTA 20h D1TEST: IF PORTB.4 = 1 AND PORTA.0 = 1 THEN A1 NAME VAR BYTE DATAT VAR BYTE A0: ‘Variable transmit data to Master IF PORTB.5 = 1 AND PORTA.1 = 1 THEN A0B1 DATAR VAR BYTE ‘Variable receive data from Master A0B0: DATAS VAR BYTE IF PORTB.6 = 1 AND PORTA.2 = 1 THEN A0B0C1 DATAD VAR BYTE ALARMR VAR BYTE A0B0C0: ‘Variable receive alarm status from Master IF PORTB.7 = 1 AND PORTA.3 = 1 THEN A0B0C0D1 ALARM VAR BYTE ‘Variable transmit alarm status to Master A0B0C0D0: ALARM = %00000000 D1 VAR BIT GOTO ALARMTEST D2 VAR BIT D3 VAR BIT A0B0C0D1: D4 VAR BIT ALARM = %00000001 GOTO ALARMTEST ADCON1 = 7 TRISA = 255 A0B0C1: TRISB = 0 IF PORTB.7 = 1 AND PORTA.3 = 1 THEN A0B0C1D1 TRISD = 255 TRISE = 0 A0B0C1D0: TRISC = %10100000 ALARM = %00000010 GOTO ALARMTEST PORTA = 255 PORTB = 0 A0B0C1D1: PORTD = 255 ALARM = %00000011 PORTE = 0 GOTO ALARMTEST PORTC = 0 A0B1: ALARMR = 0 IF PORTB.6 = 1 AND PORTA.2 = 1 THEN A0B1C1 ALARM = 0 A0B1C0: IF PORTB.7 = 1 AND PORTA.3 = 1 THEN A0B1C0D1 104 A1B1: A0B1C0D0:ALARM = %00000100 IF PORTB.6 = 1 AND PORTA.2 = 1 THEN A1B1C1 GOTO ALARMTEST A1B1C0: A0B1C0D1: IF PORTB.7 = 1 AND PORTA.3 = 1 THEN A1B1C0D1 ALARM = %00000101 GOTO ALARMTEST A1B1C0D0: ALARM = %00001100 A0B1C1: GOTO ALARMTEST IF PORTB.7 = 1 AND PORTA.3 = 1 THEN A0B1C1D1 A1B1C0D1: A0B1C1D0: ALARM = %00001101 ALARM = %00000110 GOTO ALARMTEST GOTO ALARMTEST A1B1C1: A0B1C1D1: IF PORTB.7 = 1 AND PORTA.3 = 1 THEN A1B1C1D1 ALARM = %00000111 GOTO ALARMTEST A1B1C1D0: '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ALARM = %00001110 A1: GOTO ALARMTEST IF PORTB.5 = 1 AND PORTA.1 = 1 THEN A1B1 A1B1C1D1: A1B0: ALARM = %00001111 IF PORTB.6 = 1 AND PORTA.2 = 1 THEN A1B0C1 GOTO ALARMTEST A1B0C0: ALARMTEST: IF PORTB.7 = 1 AND PORTA.3 = 1 THEN A1B0C0D1 A1B0C0D0: SERIALCOM: ALARM = %00001000 PORTC = ALARM GOTO ALARMTEST DATAT = PORTB hserin [WAIT("B"),STR DATAR\1,STR ALARMR\1] A1B0C0D1: hserout ["B",DATAT,ALARM] ALARM = %00001001 ''################################################ GOTO ALARMTEST #### ALARMRTEST: A1B0C1: IF PORTB.7 = 1 AND PORTA.3 = 1 THEN A1B0C1D1 IF ALARMR = 0 THEN GOTO TESTB0S ELSE A1B0C1D0: ALARM = %00001010 GOTO CUTOFF ENDIF GOTO ALARMTEST CUTOFF: A1B0C1D1: hserin [WAIT("B"),STR DATAR\1,STR ALARMR\1] ALARM = %00001011 GOTO ALARMRTEST GOTO ALARMTEST '################################################# ######################## 105 '################################################# ENDIF ######################## '################################################# TESTB1SL: ######################## IF (PORTB.1 = 0) THEN TESTB0S: ALARM = 0 DATAS = DATAR IF (PORTB.4 = 0) THEN GOTO B1LOWS ELSE GOTO B1HIGHS ENDIF GOTO TESTB0SL ELSE GOTO TESTB1S ENDIF B1LOWS: IF (DATAS.1 = 0) THEN GOTO HIGHB1S ELSE TESTB0SL: IF (PORTB.0 = 0) THEN GOTO LOWB1S ENDIF GOTO B0LOWS ELSE GOTO B0HIGHS ENDIF B1HIGHS: IF (DATAS.1 = 0) THEN GOTO LOWB1S ELSE B0LOWS: IF (DATAS.0 = 0) THEN GOTO HIGHB1S ENDIF GOTO HIGHB0S ELSE GOTO LOWB0S ENDIF HIGHB1S: PORTB.1 = 1 GOTO TESTB2S LOWB1S: B0HIGHS: PORTB.1 = 0 IF (DATAS.0 = 0) THEN GOTO TESTB2S GOTO LOWB0S ELSE GOTO HIGHB0S ENDIF '################################################# ######################## TESTB2S: IF (PORTB.6 = 0) THEN GOTO TESTB2SL HIGHB0S: PORTB.0 = 1 GOTO TESTB1S ELSE GOTO TESTB3S ENDIF LOWB0S: PORTB.0 = 0 TESTB2SL: GOTO TESTB1S IF (PORTB.2 = 0) THEN '################################################# ######################## TESTB1S: IF (PORTB.5 = 0) THEN GOTO B2LOWS ELSE GOTO B2HIGHS ENDIF GOTO TESTB1SL ELSE GOTO TESTB2S B2LOWS: IF (DATAS.2 = 0) THEN 106 GOTO HIGHB2S ELSE GOTO LOWB2S ENDIF HIGHB3S: PORTB.3 = 1 GOTO TESTB4S LOWB3S: B2HIGHS: PORTB.3 = 0 IF (DATAS.2 = 0) THEN GOTO TESTB4S GOTO LOWB2S ELSE GOTO HIGHB2S ENDIF '################################################# ######################## TESTB4S: IF (PORTB.0 = 0) THEN GOTO TESTB4SC HIGHB2S: PORTB.2 = 1 GOTO TESTB3S ELSE GOTO TESTB5S ENDIF LOWB2S: PORTB.2 = 0 TESTB4SC: GOTO TESTB3S IF (PORTB.4 = 0) THEN '################################################# ######################## TESTB3S: IF (PORTB.7 = 0) THEN GOTO B4LOWS ELSE GOTO B4HIGHS ENDIF GOTO TESTB3SL ELSE GOTO TESTB4S ENDIF B4LOWS: IF (DATAS.4 = 0) THEN GOTO HIGHB4S ELSE TESTB3SL: IF (PORTB.3 = 0) THEN GOTO LOWB4S ENDIF GOTO B3LOWS ELSE GOTO B3HIGHS ENDIF B4HIGHS: IF (DATAS.4 = 0) THEN GOTO LOWB4S ELSE B3LOWS: IF (DATAS.3 = 0) THEN GOTO HIGHB4S ENDIF GOTO HIGHB3S ELSE GOTO LOWB3S ENDIF HIGHB4S: PORTB.4 = 1 GOTO TESTB5S LOWB4S: B3HIGHS: PORTB.4 = 0 IF (DATAS.3 = 0) THEN GOTO TESTB5S GOTO LOWB3S ELSE GOTO HIGHB3S ENDIF '################################################# ######################## TESTB5S: IF (PORTB.1 = 0) THEN 107 GOTO TESTB5SC ELSE GOTO TESTB6S ENDIF B6LOWS: IF (DATAS.6 = 0) THEN GOTO HIGHB6S ELSE TESTB5SC: IF (PORTB.5 = 0) THEN GOTO LOWB6S ENDIF GOTO B5LOWS ELSE GOTO B5HIGHS ENDIF B6HIGHS: IF (DATAS.6 = 0) THEN GOTO LOWB6S ELSE B5LOWS: IF (DATAS.5 = 0) THEN GOTO HIGHB6S ENDIF GOTO HIGHB5S ELSE GOTO LOWB5S ENDIF HIGHB6S: PORTB.6 = 1 GOTO TESTB7S LOWB6S: B5HIGHS: PORTB.6 = 0 IF (DATAS.5 = 0) THEN GOTO TESTB7S GOTO LOWB5S ELSE GOTO HIGHB5S '################################################# ######################## TESTB7S: ENDIF IF (PORTB.3 = 0) THEN HIGHB5S: ELSE GOTO TESTB7SC PORTB.5 = 1 GOTO TESTB6S GOTO TESTB0 ENDIF LOWB5S: PORTB.5 = 0 TESTB7SC: GOTO TESTB6S IF (PORTB.7 = 0) THEN '################################################# ######################## TESTB6S: IF (PORTB.2 = 0) THEN GOTO B7LOWS ELSE GOTO B7HIGHS ENDIF GOTO TESTB6SC ELSE GOTO TESTB7S ENDIF B7LOWS: IF (DATAS.7 = 0) THEN GOTO HIGHB7S ELSE TESTB6SC: IF (PORTB.6 = 0) THEN GOTO LOWB7S ENDIF GOTO B6LOWS ELSE GOTO B6HIGHS ENDIF B7HIGHS: IF (DATAS.7 = 0) THEN GOTO LOWB7S 108 ELSE GOTO HIGHB7S ENDIF GOTO TESTB1 LOWB0: PORTB.0 = 0 GOTO TESTB1 HIGHB7S: '################################################# PORTB.7 = 1 ######################## GOTO TESTB0 TESTB1: LOWB7S: IF (PORTB.5 = 0) THEN PORTB.7 = 0 GOTO TESTB0 '################################################# ######################## GOTO TESTB1L ELSE GOTO TESTB2 ENDIF '################################################# ######################## TESTB1L: '################################################# IF (PORTB.1 = 0) THEN ######################## GOTO B1LOW ELSE TESTB0: IF (PORTB.4 = 0) THEN GOTO B1HIGH ENDIF GOTO TESTB0L ELSE GOTO TESTB1 ENDIF B1LOW: IF (PORTD.1 = 0) THEN GOTO HIGHB1 ELSE TESTB0L: IF (PORTB.0 = 0) THEN GOTO LOWB1 ENDIF GOTO B0LOW ELSE GOTO B0HIGH ENDIF B1HIGH: IF (PORTD.1 = 0) THEN GOTO LOWB1 ELSE B0LOW: IF (PORTD.0 = 0) THEN GOTO HIGHB1 ENDIF GOTO HIGHB0 ELSE GOTO LOWB0 ENDIF HIGHB1: PORTB.1 = 1 GOTO TESTB2 LOWB1: B0HIGH: PORTB.1 = 0 IF (PORTD.0 = 0) THEN GOTO TESTB2 GOTO LOWB0 ELSE GOTO HIGHB0 ENDIF '################################################# ######################## TESTB2: IF (PORTB.6 = 0) THEN GOTO TESTB2L HIGHB0: PORTB.0 = 1 ELSE GOTO TESTB3 109 ENDIF GOTO HIGHB3 ELSE TESTB2L: IF (PORTB.2 = 0) THEN GOTO LOWB3 ENDIF GOTO B2LOW ELSE GOTO B2HIGH ENDIF B3HIGH: IF (PORTD.3 = 0) THEN GOTO LOWB3 ELSE B2LOW: IF (PORTD.2 = 0) THEN GOTO HIGHB3 ENDIF GOTO HIGHB2 ELSE GOTO LOWB2 ENDIF HIGHB3: PORTB.3 = 1 GOTO TESTB4 LOWB3: B2HIGH: PORTB.3 = 0 IF (PORTD.2 = 0) THEN GOTO TESTB4 GOTO LOWB2 ELSE GOTO HIGHB2 ENDIF '################################################# ######################## TESTB4: IF (PORTB.0 = 0) THEN GOTO TESTB4C HIGHB2: PORTB.2 = 1 GOTO TESTB3 ELSE GOTO TESTB5 ENDIF LOWB2: PORTB.2 = 0 TESTB4C: GOTO TESTB3 IF (PORTB.4 = 0) THEN '################################################# ######################## TESTB3: IF (PORTB.7 = 0) THEN GOTO B4LOW ELSE GOTO B4HIGH ENDIF GOTO TESTB3L ELSE GOTO TESTB4 ENDIF B4LOW: IF (PORTD.4 = 0) THEN GOTO HIGHB4 ELSE TESTB3L: IF (PORTB.3 = 0) THEN GOTO LOWB4 ENDIF GOTO B3LOW ELSE GOTO B3HIGH ENDIF B4HIGH: IF (PORTD.4 = 0) THEN GOTO LOWB4 ELSE B3LOW: IF (PORTD.3 = 0) THEN GOTO HIGHB4 ENDIF 110 GOTO TESTB6C HIGHB4: PORTB.4 = 1 GOTO TESTB5 ELSE GOTO TESTB7 ENDIF LOWB4: PORTB.4 = 0 TESTB6C: GOTO TESTB5 IF (PORTB.6 = 0) THEN '################################################# ######################## TESTB5: IF (PORTB.1 = 0) THEN GOTO B6LOW ELSE GOTO B6HIGH ENDIF GOTO TESTB5C ELSE GOTO TESTB6 ENDIF B6LOW: IF (PORTD.6 = 0) THEN GOTO HIGHB6 ELSE TESTB5C: IF (PORTB.5 = 0) THEN GOTO LOWB6 ENDIF GOTO B5LOW ELSE GOTO B5HIGH ENDIF B6HIGH: IF (PORTD.6 = 0) THEN GOTO LOWB6 ELSE B5LOW: IF (PORTD.5 = 0) THEN GOTO HIGHB6 ENDIF GOTO HIGHB5 ELSE GOTO LOWB5 ENDIF HIGHB6: PORTB.6 = 1 GOTO TESTB7 LOWB6: B5HIGH: PORTB.6 = 0 IF (PORTD.5 = 0) THEN GOTO TESTB7 GOTO LOWB5 ELSE GOTO HIGHB5 ENDIF '################################################# ######################## TESTB7: IF (PORTB.3 = 0) THEN GOTO TESTB7C HIGHB5: PORTB.5 = 1 GOTO TESTB6 ELSE GOTO CLEARBIT ENDIF LOWB5: PORTB.5 = 0 TESTB7C: GOTO TESTB6 IF (PORTB.7 = 0) THEN '################################################# ######################## TESTB6: IF (PORTB.2 = 0) THEN GOTO B7LOW ELSE GOTO B7HIGH ENDIF 111 PORTB.7 = 1 B7LOW: GOTO CLEARBIT IF (PORTD.7 = 0) THEN LOWB7: GOTO HIGHB7 ELSE GOTO LOWB7 ENDIF PORTB.7 = 0 GOTO CLEARBIT '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&& CLEARBIT: B7HIGH: IF (PORTD.7 = 0) THEN GOTO LOWB7 ELSE GOTO HIGHB7 IF (PORTD = 255) THEN GOTO START ELSE GOTO CLEARBIT ENDIF ENDIF end HIGHB7: