SIM UNIVERSITY
SIM UNIVERSITY
SCHOOL OF SCIENCE AND TECHNOLOGY
SCHOOL OF SCIENCE AND TECHNOLOGY
APPLICATION OF MICROCONTROLLER IN
APPLICATION OF MICROCONTROLLER IN
LED MATRIX DISPLAY
LED MATRIX DISPLAY
STUDENT
STUDENT
:
: CHEW
CHEW HANWEI
HANWEI
(PI NO. E0806350)
(PI NO. E0806350)
SUPERVISOR
SUPERVISOR
:
: DR.
DR. FUNG
FUNG CHI
CHI FUNG
FUNG
PROJECT
PROJECT CODE
CODE :
: JAN2011/ENG/045
JAN2011/ENG/045
A project report submitted to SIM University
A project report submitted to SIM University
in partial fulfilment of the requirements for the degree of
in partial fulfilment of the requirements for the degree of
Bachelor of Engineering
Bachelor of Engineering
November 2011
November 2011
TABLE OF CONTENTS
TABLE OF CONTENTS
Page Page ABSTRACT iv ABSTRACT iv ACKNOWLEDGEMENT v ACKNOWLEDGEMENT v LISTSLISTS OF OF FIGURES FIGURES vivi
CHAPTER
CHAPTER ONE ONE 77
INTRODUCTION 7
INTRODUCTION 7
1.1
1.1 Project Project Objective Objective 77
1.2
1.2 Overall Overall Objective Objective 77
1.3
1.3 Proposed Proposed Approach Approach and and Method Method 88
1.4
1.4 Project Project Management Management 99
CHAPTER
CHAPTER TWO TWO 1212
LITERATURE
LITERATURE REVIEW REVIEW 1212
2.1
2.1 Introduction Introduction of of LED LED 1212
2.2
2.2 Applications Applications of of LED LED Matrix Matrix Display Display 1313
2.3
2.3 Techniques Techniques of of Driving Driving LED LED Matrix Matrix Display Display 1515 CHAPTER
CHAPTER THREE THREE 1717
HARDWARE
HARDWARE SYSTEM SYSTEM IMPLEMENTATION IMPLEMENTATION 1717
3.1
3.1 System System Overall Overall Structure Structure Design Design 1717
3.2
3.2 Atmel Atmel AT89S52 AT89S52 Control Control System System 1919
3.3
3.3 DS1307 DS1307 Real-time Real-time Clock Clock 2020
3.4
3.4 LED LED Matrix Matrix Display Display Design Design 2121
3.5
3.5 Circuit Circuit Design Design for for 5V 5V Power Power Source Source 2222
3.6
3.6 AT89S52 AT89S52 In In System System Programming Programming (ISP) (ISP) 2323 CHAPTER
CHAPTER FOUR FOUR 2525
SOFTWARE
SOFTWARE SYSTEM SYSTEM IMPLEMENTATION IMPLEMENTATION 2525
4.1
4.1 Software Software Design Design Structure Structure 2525
4.2
4.2 Codes Codes for for Dot Dot Matrix Matrix Display Display Characters Characters 2626
4.3
4.3 DS1307 DS1307 Interface Interface With With AT89S52 AT89S52 2727
4.4
4.4 Interrupt Interrupt Service Service Routine Routine for for Time Time Adjustment Adjustment 2929
4.5
4.5 Software Software Development Development Process Process 3030
CHAPTER
CHAPTER FIVE FIVE 3333
PROJECT
PROJECT INTEGRATION INTEGRATION AND AND ANALYSIS ANALYSIS 3333
5.1
5.1 PCB PCB Design Design and and Component Component Assembly Assembly 3333
5.2
ENG499 CAPSTONE PROJECT REPORT
ENG499 CAPSTONE PROJECT REPORT iiii
TABLE OF CONTENTS
TABLE OF CONTENTS
Page Page ABSTRACT iv ABSTRACT iv ACKNOWLEDGEMENT v ACKNOWLEDGEMENT v LISTSLISTS OF OF FIGURES FIGURES vivi
CHAPTER
CHAPTER ONE ONE 77
INTRODUCTION 7
INTRODUCTION 7
1.1
1.1 Project Project Objective Objective 77
1.2
1.2 Overall Overall Objective Objective 77
1.3
1.3 Proposed Proposed Approach Approach and and Method Method 88
1.4
1.4 Project Project Management Management 99
CHAPTER
CHAPTER TWO TWO 1212
LITERATURE
LITERATURE REVIEW REVIEW 1212
2.1
2.1 Introduction Introduction of of LED LED 1212
2.2
2.2 Applications Applications of of LED LED Matrix Matrix Display Display 1313
2.3
2.3 Techniques Techniques of of Driving Driving LED LED Matrix Matrix Display Display 1515 CHAPTER
CHAPTER THREE THREE 1717
HARDWARE
HARDWARE SYSTEM SYSTEM IMPLEMENTATION IMPLEMENTATION 1717
3.1
3.1 System System Overall Overall Structure Structure Design Design 1717
3.2
3.2 Atmel Atmel AT89S52 AT89S52 Control Control System System 1919
3.3
3.3 DS1307 DS1307 Real-time Real-time Clock Clock 2020
3.4
3.4 LED LED Matrix Matrix Display Display Design Design 2121
3.5
3.5 Circuit Circuit Design Design for for 5V 5V Power Power Source Source 2222
3.6
3.6 AT89S52 AT89S52 In In System System Programming Programming (ISP) (ISP) 2323 CHAPTER
CHAPTER FOUR FOUR 2525
SOFTWARE
SOFTWARE SYSTEM SYSTEM IMPLEMENTATION IMPLEMENTATION 2525
4.1
4.1 Software Software Design Design Structure Structure 2525
4.2
4.2 Codes Codes for for Dot Dot Matrix Matrix Display Display Characters Characters 2626
4.3
4.3 DS1307 DS1307 Interface Interface With With AT89S52 AT89S52 2727
4.4
4.4 Interrupt Interrupt Service Service Routine Routine for for Time Time Adjustment Adjustment 2929
4.5
4.5 Software Software Development Development Process Process 3030
CHAPTER
CHAPTER FIVE FIVE 3333
PROJECT
PROJECT INTEGRATION INTEGRATION AND AND ANALYSIS ANALYSIS 3333
5.1
5.1 PCB PCB Design Design and and Component Component Assembly Assembly 3333
5.2
5.3
5.3 Prototype Prototype Under Under Test Test 3838
CHAPTER
CHAPTER SIX SIX 4040
CONCLUSIONS
CONCLUSIONS AND AND FUTURE FUTURE WORK WORK 4040
6.1 6.1 Conclusions Conclusions 4040 6.2 6.2 Recommendations Recommendations 4141 REFLECTION 42 REFLECTION 42 REFERENCES 43 REFERENCES 43 Appendixes 44 Appendixes 44 Appendix A
Appendix A – – Program Program Source Source Code Code 4444
Appendix
Appendix A1: A1: Serial Serial Initialization Initialization 4444
Appendix
Appendix A2: A2: DS1307 DS1307 driver driver 4444
Appendix
Appendix A3: A3: AT89S52 AT89S52 Main Main 4848
Appendix
Appendix B: B: Schematic Schematic Diagrams Diagrams 5454
Appendix
Appendix B1: B1: Schematic Schematic for for LED LED Matrix Matrix Display Display 5454 Appendix
Appendix B2: B2: Schematic Schematic for for ISP ISP Program Program 5454
Appendix
Appendix B3: B3: Schematic Schematic for for 5V 5V Supply Supply 5555
Appendix
ENG499 CAPSTONE PROJECT REPORT
ENG499 CAPSTONE PROJECT REPORT iviv
ABSTRACT ABSTRACT
LED matrix display has become an important symbol of the city lighting, modernization LED matrix display has become an important symbol of the city lighting, modernization and information society with continuous improvement and beautification of people's living and information society with continuous improvement and beautification of people's living environment.
environment.
This project introduces display design process about hardware and software
This project introduces display design process about hardware and software based on Atmelbased on Atmel AT89S52 single chip microcontroller. The system mainly involves the AT89S52 AT89S52 single chip microcontroller. The system mainly involves the AT89S52 microcontroller, a LED matrix display and a real
microcontroller, a LED matrix display and a real time clock (DS1307).time clock (DS1307).
We use a simple external circuit to control the display screen, whose size is 8-pixel by We use a simple external circuit to control the display screen, whose size is 8-pixel by 32- pixel.
pixel. The The display display screen screen can can display display the the size size of of four four 5-pixel 5-pixel by by 7-pixel 7-pixel dot dot matrixmatrix characters by dynamically displaying a 4-digits real time clock. This display screen has characters by dynamically displaying a 4-digits real time clock. This display screen has advantages of compact in size, fewer hardware components and simpler circuit structure. advantages of compact in size, fewer hardware components and simpler circuit structure.
Based on the prototyping platform, an algorithm for translating register values from real Based on the prototyping platform, an algorithm for translating register values from real time clock to appropriate displayed patterns is implemented. Through production of time clock to appropriate displayed patterns is implemented. Through production of hardware and testing of software, we achieved the desired effect of a LED digital clock hardware and testing of software, we achieved the desired effect of a LED digital clock display.
ACKNOWLEDGEMENT ACKNOWLEDGEMENT
I would like to present my appreciation to the
I would like to present my appreciation to the following individuals for their untiring supportfollowing individuals for their untiring support and encouragement for the whole duration of my final year project. Without their help, this and encouragement for the whole duration of my final year project. Without their help, this project would not be able to comp
project would not be able to complete so smoothly and attain success.lete so smoothly and attain success.
Firstly, I would like to thank my project supervisor, Dr. Fung Chi Fung for his guidance and Firstly, I would like to thank my project supervisor, Dr. Fung Chi Fung for his guidance and patience.
patience. I I am am grateful grateful for for his his valuable valuable feedback feedback and and suggestions suggestions that that enable enable the the project project toto progress smoothly as planned.
progress smoothly as planned.
Secondly, I would like to thank the management and my colleagues of Panasonic Secondly, I would like to thank the management and my colleagues of Panasonic Semiconductor Singapore. With their kind understanding, I am able to concentrate and focus Semiconductor Singapore. With their kind understanding, I am able to concentrate and focus during my course of studies.
during my course of studies.
Last but not least, I would like to thank my family and friends for being so supportive and Last but not least, I would like to thank my family and friends for being so supportive and kept me going to finally complete this project and achieve success.
ENG499 CAPSTONE PROJECT REPORT vi
LISTS OF FIGURES
Figure 1.1: Project Gantt Chart 11
Figure 2.1: Structure of a LED 12
Figure 2.2: 8*8 LED matrix 13
Figure 2.3: “Bus Stopping” 13
Figure 2.4: ERP gantry 14
Figure 2.5: Common-Anode LED matrix 15
Figure 2.6: Example of a 5x7 font characters within 8x8 LED matrixes 16
Figure 3.1: ATMEL AT89S52 Microcontroller 17
Figure 3.2: Overview System Design 18
Figure 3.3: Clock Circuit for AT89S52 19
Figure 3.4: Reset Circuit for AT89S52 19
Figure 3.5: AT89S52 External Interrupts 20
Figure 3.6: DS1307 Real Time Clock 20
Figure 3.7: Four 8x8 LED Matrix Panels 21
Figure 3.8: UDN2981 LED Driver 21
Figure 3.9: Two 74HC154 for Columns Date 22
Figure 3.10: 5V Power Supply 22
Figure 3.11: AT89S52 ISP Timing Diagram 23
Figure 3.12: AT89S52 ISP Circuit 24
Figure 3.13: ISP Download Cable 24
Figure 4.1: Program Flowchart 25
Figure 4.2: Code for Dot Matrix Character 26
Figure 4.3: DS1307 I2C Timing Diagram 27
Figure 5.1: Components Layout for Prototype 33
Figure 5.2: Wiring for Prototype 33
Figure 5.3: 5V Fixed Voltage Regulator 34
Figure 5.4: Two 74HC154 for Columns Data 34
Figure 5.5: DS1307 and 3V Backup Battery 35
Figure 5.6: ISP Circuit 35
Figure 5.7: AT89S52 Control System 36
Figure 5.8: Atmel ISP Flash Programmer 37
Figure 5.9: Prototype Powered Up 38
Figure 5.10: Initialization of LED Matrix Display 38
Figure 5.11: Push Switches for Hour and Minutes Adjustment 39
CHAPTER ONE INTRODUCTION
1.1 Project Objective
This project aims at studying the hardware structure of LED matrix display system based on microcontroller control. As a case study, we will focus on prototyping a LED matrix display panel for displaying real time clock information. It involves interfacing a microcontroller to a group of array LED modules and a real-time clock. Based on the prototyping platform, an algorithm for translating register values from real-time clock to appropriate displayed patterns is needed to be implemented. Possible extension of interfacing the prototyping system to PC via a communication port could be considered.
The objectives highlighted for this project are as follows:
To implement a LED matrix display panel.
To interface a microcontroller to the LED matrix display.
To program the microcontroller using C complier.
To display real-time clock on the LED matrix display.
1.2 Overall Objective
Nowadays, LED matrix displays are used to display information on machines, lifts, public transports like buses, MRT and many other devices requiring a simple display device of limited resolution. The display consists of an array of LEDs arranged in a rectangular configuration such that by selective switching of LEDs, text or graphics are then displayed.
ENG499 CAPSTONE PROJECT REPORT 8
Through studying the existing techniques of driving LED matrix display, this project aims to simplify the hard wiring of the circuit yet not compromising the flexibility of the controller, and most importantly acquiring the skill for implementing a small scale LED matrix display.
1.3 Proposed Approach and Method
In this project, an LED matrix will be implemented. This LED matrix display will be interfaced by programming an 8051 microcontroller and driving the LED matrix.
The project scope will be divided into different phases in order to achieve the project objectives.
Phase 1 – Background Information research Phase 2 – Preparation of initial report – TMA01
Phase 3 – Research on applications, commonly used techniques for driving LED display panels, LED matrix display, 8051 Microcontroller, inter-device communication protocols
Phase 4 – Implementation of prototype consisting of LED display panel and microcontroller
Phase 5 – Program microcontroller to display real-time clock on LED matrix display Phase 6 – Project review
Phase 7 – Preparation of final report
Phase 8 – Preparation of oral presentation
In adopting the above approach, the project could be expected to develop in a more structured and systematic way and this will ensure that the project will be able to progress smoothly.
Phases 4 and 5 are marked as the major milestone of this project.
In phase 4, the prototype must be implemented with care to deliver the functionality of the LED matrix display. Time will be allocated wisely in case of troubleshooting error arises.
In phase 5, programming will be carried out for the 8051 microcontroller. Basic control of the LED matrix display should be carrying out to ensure functionality of the circuit. Finally, a real-time clock will be display on the LED matrix display.
1.4 Project Management
The tasks for each project phase are listed for reference. The following are the detail tasks descriptions required for each project phase.
Phase 1 (Literature Review):
Understanding project definitions Research on project background
Phase 2 (Preparation of Project Proposal):
Setting the objective for the project
Planning the schedule for the project‟s progress Capstone Project Proposal submission to MyUniSIM
Phase 3 (Research on LED matrix display, 8051 Microcontroller):
Understanding the arrangement of LEDs in matrix display
Research on applications of LED matrix display in everyday life Understand the existing techniques use to drive LED matrix displa y
Phase 4 (Implementation of prototype consisting of LED display panel, microcontroller):
Come up plan for the prototype design
Implement the prototype consisting of the necessary components
Phase 5 (Program microcontroller to display real-time clock on LED matrix display):
Program the Microcontroller to drive the LED matrix display Display real time clock on the LED matrix display
ENG499 CAPSTONE PROJECT REPORT 10
Phase 6 (Project Review):
Going through the things learnt from this project Discuss on area for improvement
Phase 7 (Preparation of final report):
Finalizing the contents for Project Report Capstone Project Report submission to SRL
Phase 8 (Preparation of oral presentation):
Preparation of the Poster for oral presentation Oral Presentation for Project
In this project, a Gantt chart is utilised as the main tool for project management. Gantt charts illustrate the start and finish dates for the tasks of a project in bar chart format. The Gantt chart is created to monitor project progress with reference to the planned schedule. It provides a general guideline on the dateline for each individual task. The amount of time allocated depends on the scope of implementation. More time are allocated to tasks that require in-depth study and labour intensive.
Figure 1.1: Project Gantt Chart e e k 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 r j c t s s P l a n A c t u a l 6 . P r o j e c t R e v i e w 7 . P r e p a r a t i o n o f F i n a l R e p o r t 8 . P r e p a r a t i o n o f P o s t e r a n d O r a l P r e s e n t a t i o n e v s o n e e E x a m i n a t i o n W e e k 4 . I m p l e m e n t a t i o n o f p r o t o t y p e c o n s i s t i n g o f L E D d i s p l a y o d u l e s a n d i c r o c o n t r o l l e r 4 . 1 F i n a l i z e o n p r o t o t y p e d e s i g n 4 . 2 I m p l e m e n t p r o t o t y p e 5 . P r o g r a m m i c r o c o n t r o l l e r t o d i s p l a y r e a l t - i m e c l o c k o n L E t r i x i s l y 5 . 1 M i c r o c o n t r o l l e r p r o g r a m m i n g u s i n g C c o m p l i e r 1 . 1 U n d e r s t a n d i n g P r o j e c t d e f i n i t i o n 1 . 2 R e s e a r c h o n P r o j e c t B a c k g r o u n d 2 . P r o j e c t I n i t i a l R e p o r t 3 . R e s e a r c h o n L E D m a t r i x d i s p l a y , 8 0 5 1 M i c r o c o n t r o l l e r a n d f i n a l i z e s p l a n o n p r o t o t y p i n g d e s i g n 3 . 1 L E D m a t r i x d i s p l a y 3 . 2 8 0 5 1 M i c r o c o n t r o l l e r e c - 1 . L i t e r a t u r e R e v i e w F e b - a r - p r - a y - J u n - J u l - u - S e p - c t - o v - G a n t t C h a r t c l e a r l y s h o w s d e l a y i n T a s k 4 . A d d i t i o n t i m e w a s s p e n t o n p r o t o t y p e t r o u b l e s h o o t i n g ,
ENG499 CAPSTONE PROJECT REPORT 12
CHAPTER TWO
LITERATURE REVIEW
2.1 Introduction of LED
A light-emitting diode (LED) is a semiconductor light source which we see in our daily lives. LEDs are used in many electronic devices such as lamps, traffic lights and display with limited resolution. When a LED is turned on, electrons recombine with the holes (positive carriers), these moving electrons thus release energy. The released energy is emitted in a form of light photons, creating the visible light that we can see. The energy level will determine the light frequency and hence the colour of the light.
Figure 2.1: Structure of a LED
LED has several advantages over the conventional incandescent bulb. Due to the fact that they do not have a filament that will burnt out, they last much longer. LED also generates little heat as most of the energy is going directly to generating light resulting higher energy efficiency.
2.2 Applications of LED Matrix Display
Figure 2.2: 8*8 LED matrix
When an array of LEDs is arranged in a rectangular configuration, a display panel is formed. Text or graphic can be displayed on the LED matrix display. With the mechanical robustness and long lifetime, LED matrix display is becoming widely adopted in a wide range of practical applications.
Figure 2.3: “Bus Stopping”
A commonly seen example in our daily life is the “Bus Stopping” display. When commuter presses the button, the display will lit up and alert others that bus will be stopping at the next stop.
ENG499 CAPSTONE PROJECT REPORT 14
Figure 2.4: ERP gantry
Another example shown in Figure 2.4 is the ERP gantry. By using a large LED matrix display, it clearly shows the time display and charges for passing the gantry within the stated timeframe.
2.3 Techniques of Driving LED Matrix Display
LEDs are arranged in a rectangular arra y which allows the use of multiplexing. By repeatedly turning on and off a LED at a sufficiently high speed, human eyes will not be able to detect that the LED is off as the eyes remember the light source for approximation of 40ms which is equivalent to 25Hz. This is known as the persistence of vision theory. And hence multiplexing reduces the number of driving
signals required to control a LED matrix display.
There are typically 2 types of LED matrix connections, common anode and common cathode. The LEDs are arranged in such a way that either all the cathodes or anodes are connected together.
Figure 2.5: Common-Anode LED matrix
In Figure 2.5 shows a circuit diagram of a common-anode matrix. From the circuit, the LEDs‟ anode is connected together in each row. With an 8x8 LED module, the panel totals up to 64 LEDs. Only 16 pins are needed to be wired-up in order to achieve full control of all the 64 LED‟s. As the scale of the panel being expanded, an additional 8 control pins are required for every 8x8 LED module being included.
A possible alternative solution is to use LED drivers that are commercially available in the market. One example is the MAX6952 from MAXIM. A single MAX6952 is
ENG499 CAPSTONE PROJECT REPORT 16
capable of driving an LED matrix up to 14 cathodes by 10 anodes. 104 ASCII fonts are built-in on chip. This allows textual information to be displayed at reduced complexity in control software.
Figure 2.6: Example of a 5x7 font characters within 8x8 LED matrixes
Due to the built-in font set, the programming involved in controlling alphanumeric display, as shown in Figure 2.6, becomes very easy. Nevertheless, this approach unavoidably limits the capability of controlling each LED individually and thereby reducing the flexibility of the display pattern.
CHAPTER THREE
HARDWARE SYSTEM IMPLEMENTATION
3.1 System Overall Structure Design
Microcontroller models were selected according to the target, function, reliability, cost, accuracy and speed of the control system. According to the actual situation of the subject, the choice of microcontroller is largely based on two primary concerns: easy-to-use and low-cost.
Due to the popularity in using 8051 amongst local higher institutions in Singapore, readily accessible related resources are widely available. The 89S series, introduced by Atmel in 2003, features in flexibility in programming, high performance and low
cost and low power.
Figure 3.1: ATMEL AT89S52 Microcontroller
AT89S52 is an 8-bit microcontroller with 8Kbytes of in-system programmable Flash memory. The device is manufactured using Atmel‟s high -density non-volatile memory technology. It is fully compatible with the industry-standard 8051 instruction set. The on-chip Flash allows the program memory to be reprogrammed either by in-system; chip on board firmware downloading or by a conventional non-volatile memory programmer. For the aforementioned reasons, AT89S52 is chosen as the controller of the entire system for the present application.
ENG499 CAPSTONE PROJECT REPORT 18
Figure 3.2: Overview System Design
By combining a versatile 8-bit CPU with in-system programmability on a monolithic chip, the Atmel AT89S52 is a powerful microcontroller which provides a highly-flexible and cost-effective solution to many embedded control applications. The system is implemented by a circuitry which consists of AT89S52 chip, DS1307 real time clock, column scanning circuit, row scanning circuit and the four 8 x 8 LED dot matrix panels.
The display unit is composed of the four 8 x 8 LED dot matrix modules, a UDN2981 and two 74HC154. Row data signal is driven by the UDN2981; the UDN2981 data are from the P2 port of the microcontroller AT89S52.
The column scanning signal of each character was driven by the two 74HC154, using a 74LS138 as an address decoding logic for chip selection. The input signal of the 74HC154 and 74LS138 was given by the P0.0~P0.3 and P1.2~1.3 of the AT89S52 respectively.
DS1307 Real Time Clock
AT89S52 LED Matrix
Display
Column Scanning Circuit Row Scanning
3.2 Atmel AT89S52 Control System
To realize the functionality of the AT89S52 chip, it is made up a few must have circuitry. They consist of the clock circuit, reset circuit and the interrupt circuit.
Figure 3.3: Clock Circuit for AT89S52
The clock circuit of AT89S52 composes of pin 18, 19 (XTALI and XTAL2), connected to a 12MHz crystal X1, capacitor C1 and C2, and uses on-chip oscillator mode.
Figure 3.4: Reset Circuit for AT89S52
Reset circuit uses a simple power-on reset circuit, and mainly consist of resistor R1, capacitor C3, connected to the AT89S52's reset input pin.
ENG499 CAPSTONE PROJECT REPORT 20
Figure 3.5: AT89S52 External Interrupts
Push button switches are connected to the AT89S52‟s external interrupt pin. With the switches connected to ground, the interrupts are programmed to be triggered during the signal falling edge. These switches will be programmed to facilitate the settings of clock‟s hour and minute.
3.3 DS1307 Real-time Clock
The DS1307 serial real-time clock (RTC) is a low-power; full binary-coded decimal (BCD) clock/calendar. Address and data are transferred serially through an I2C, bidirectional bus. The SDA and SCL pin are connected to P1.1 and P1.0 of the AT89S52 respectively. The clock/calendar provides a full set of real-time information about seconds, minutes, hours, day, date, month, and year.
Figure 3.6: DS1307 Real Time Clock
The clock operates in either 24-hour or 12-hour format with an AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup 3V supply. Time-keeping operation continues while the part operates from the backup supply.
3.4 LED Matrix Display Design
The display screen is made up of four 8 x 8 led matrix modules, resulting an 8 x 32 pixels display in 2-dimension. The size of each character is 5 x 7 in our design. With
the display holding up to four characters one at a time, it allows a four digit hour and minute clock to be displayed simultaneously.
Figure 3.7: Four 8x8 LED Matrix Panels
The eight rows of the four LED matrix modules are connected to the Port2 of the microcontroller via a UDN2981 current driver. Port2 supplies the row data signal of the required character to be displayed.
Figure 3.8: UDN2981 LED Driver
The 32 columns of LEDs are connected to the outputs of the two 74HC154 chips. The 74LS138 is used to decode the address for the selection between the two 74HC154 chips. Its inputs are given from the microcontroller port pins P1.2~1.3 and its outputs are connected to the enable pins of the two 74HC154 chips. When either one of the 74HC154 chips is asserted, it then gets the input from microcontroller port pins P0.0~P0.3 thus enabling a single column to be turned on with reference to
ENG499 CAPSTONE PROJECT REPORT 22
Figure 3.9: Two 74HC154 for Columns Date
3.5 Circuit Design for 5V Power Source
In this project, the circuit is powered by a 5 volt supply. To obtain a regulated 5 volt supply, a 7805 fixed voltage regulator, powered by a common 9 volt adapter is used.
7805 is equipped with built-in protection circuitry against overheating and short-circuits, making it quite robust. This protection feature provides protection not only for the component itself, but also for the rest of the circuits.
3.6 AT89S52 In System Programming (ISP)
According to the specification of the AT89S52, ISP is performed using 4 lines. Physically data are transferred through 2 lines only, as in the case of I2C interface. Data is shifted in serially on bit-by-bit basis though the MOSI line, with a clock cycle between each bit and the next on the SCK line. MISO line is used for reading as well as code verification; it is only used to output the code from the FLASH memory of the microcontroller.
Figure 3.11: AT89S52 ISP Timing Diagram
The RST pin, which is used to reset the device, is also used to enable the 3 pins (MOSI,MISO and SCK) to be used for ISP simply by setting RST to HIGH (5V), otherwise if RST is low (0V), the program will start running and those three pins, are used normally as P1.5, P1.6 and P1.7
ENG499 CAPSTONE PROJECT REPORT 24
Figure 3.12: AT89S52 ISP Circuit
The 10-pin connector is then connected to the computer parallel port as shown.
CHAPTER FOUR
SOFTWARE SYSTEM IMPLEMENTATION
4.1 Software Design Structure
Due to the maturity in compiler support, C language is commonly used often a preferred choice of language to program microcontroller. Programming a microcontroller using C often results in considerable saving in programming effort and much reduced development cycle in design.
Figure 4.1: Program Flowchart
The entire software design mainly composes of display routine and real time clock control routine. The characters to be displayed on the LED matrix modules and other data for transmission control and display functions were achieved by dynamic scanning. Real-time communication between DS1307 and the microcontroller ensures the information being displayed is always up-to-date.
Initialize Serial Start Setup Interrupts Display Time Read DS1307 Infinite Loop
ENG499 CAPSTONE PROJECT REPORT 26
4.2 Codes for Dot Matrix Display Characters
In the present design, the characters code is obtained by column scanning method. Each character is composed of 5 x 7 pixels.
R1 0 1 1 1 0 R2 1 0 0 0 1 R3 1 0 0 1 1 R4 1 0 1 0 1 R5 1 1 0 0 1 R6 1 0 0 0 1 R7 0 1 1 1 0 R8 0 0 0 0 0 C1 C2 C3 C4 C5
Figure 4.2: Code for Dot Matrix Character
In the first column for character „0‟, R2~R7 are asserted while the rest are de-asserted. That is, in binary 00111110, and converts to hexadecimal as 3Eh. With the codes for each column, the program turns on the respective LEDs column by column with a refresh rate faster than 25Hz.
As human vision only remembers a light source for approximately 40ms, the program scans all columns within the time frame. This theory is also known as persistence of vision.
It can be seen from this principle, no matter what font or image display, we can use this method to analyze the scan code and display on the LED matrix display.
unsigned char code number[15][5] = { 0x3E, 0x51, 0x49, 0x45, 0x3E, // 0 0x44, 0x42, 0x7F, 0x40, 0x40, // 1 0x42, 0x61, 0x51, 0x49, 0x46, // 2 0x22, 0x41, 0x49, 0x49, 0x36, // 3 0x18, 0x14, 0x12, 0x7F, 0x10, // 4 0x27, 0x45, 0x45, 0x45, 0x39, // 5 0x3E, 0x49, 0x49, 0x49, 0x32, // 6 0x01, 0x71, 0x09, 0x05, 0x03, // 7 0x36, 0x49, 0x49, 0x49, 0x36, // 8 0x26, 0x49, 0x49, 0x49, 0x3E, // 9
With reference to the application of a real time clock in this project, a lookup table of the codes for characters 0 to 9 is created. This will allow the program to get the necessary code when updating the clock display.
4.3 DS1307 Interface With AT89S52
The AT89S52 microcontroller and DS1307 are linked via a serial communication interface. In order to implement the serial communication functions between the two devices, we need to initialise the serial port.
//---// Initialize serial port
//---void InitSerial(void)
{
SCON = 0x52; // setup serial port control
TMOD = 0x20; // hardware (9600 BAUD @11.05592MHZ) TH1 = 0xFD; // TH1
TR1 = 1; // Timer 1 on }
After which the SDA and SCL pin of the DS1307 are connected to pins of the AT89S52 as defined in the program.
sbit SDA = P1^1; // connect to SDA pin (Data) sbit SCL = P1^0; // connect to SCL pin (Clock)
To start and stop the data transfer, the state of SDA line need to switch from high to low and low to high respectively, while keeping the SCL line high.
Figure 4.3: DS1307 I2C Timing Diagram
Referring to the timing diagram in Figure 4.3, data transfer is initiated after a Start condition. The information is transferred byte – wise and each receiver acknowledges with a ninth bit. To end the transfer of data, the program needs to terminate the I2C communication with a Stop condition. The functions for the Start and Stop of the I2C communications are as follows.
ENG499 CAPSTONE PROJECT REPORT 28 //---// start I2C //---void Start(void) { SDA = 1; SCL = 1; _nop_();_nop_(); SDA = 0; _nop_();_nop_(); SCL = 0; _nop_();_nop_(); } //---// stop I2C //---void Stop(void) { SDA = 0; _nop_();_nop_(); SCL = 1; _nop_();_nop_(); SDA = 1; }
During the period of data transfer, the AT89S52 will read the real time clock information or write data for the adjustment of time when necessary.
//---// Read RTC (all real time)
//---void ReadRTC(unsigned char * buff) { Start(); WriteI2C(0xD0); WriteI2C(0x00); Start(); WriteI2C(0xD1); *(buff+0)=ReadI2C(ACK); // Second *(buff+1)=ReadI2C(ACK); // Minute *(buff+2)=ReadI2C(ACK); // hour *(buff+3)=ReadI2C(ACK); // Day *(buff+4)=ReadI2C(ACK); // date *(buff+5)=ReadI2C(ACK); // month *(buff+6)=ReadI2C(NO_ACK); // year Stop(); } //---// Write RTC //---void WriteRTC(unsigned char *buff) {
Start(); WriteI2C(0xD0); WriteI2C(0x00); WriteI2C(*(buff+0)); WriteI2C(*(buff+1)); WriteI2C(*(buff+2)); WriteI2C(*(buff+3)); WriteI2C(*(buff+4)); WriteI2C(*(buff+5)); WriteI2C(*(buff+6)); Stop(); }
4.4 Interrupt Service Routine for Time Adjustment
For the adjustment of the time information in the DS1307, two external interrupts of the AT89S52 are connected to ground through push switches. With reference to the hard wiring of the switch connections, the interrupts are set to be triggered at falling edge.
setup_interrupts () // Function to setup the External interrupt {
EA = 1; // global interrupts enable EX0 = 1; // enable external interrupt 0 EX1 = 1; // enable external interrupt 1
IT0 = 1; // make ext. interrupt 0 edge triggered IT1 = 1; // make ext. interrupt 1 edge trigerred }
With the setting up of the external interrupt, the program enters the interrupt service routine when either switch is pressed. When interrupt 0 is triggered, the program increments the hour and write the updated time into the DS1307.
void EXO_int0 (void) interrupt 0 // Interrupt Routine for Hour Increment { ReadRTC(&RTC_ARR[0]); if(RTC_ARR[2] > 0x22) { RTC_ARR[2] = 0x00; WriteRTC(&RTC_ARR[0]); }
else if((RTC_ARR[2] & 0x0f) > 0x08) {
RTC_ARR[2] = (RTC_ARR[2] + 0x10) & 0xf0; WriteRTC(&RTC_ARR[0]); } else { RTC_ARR[2] = RTC_ARR[2] + 0x01; WriteRTC(&RTC_ARR[0]); } delayms(10);
ENG499 CAPSTONE PROJECT REPORT 30
The function basically increases the ones digit of the hour. When the hour reaches 9 or 19 (in 24-hour format), it sets the ones digit to 0 and carry over the 1 to the tens digit. Until the hour reaches 23 (in 24-hour format), double digits are reset.
The program performs in a similar manner for the settings of minutes when interrupt 2 is triggered.
void EX1_int2 (void) interrupt 2 // Interrupt Routine for Minute Increment { ReadRTC(&RTC_ARR[0]); if(RTC_ARR[1] > 0x58) { RTC_ARR[1] = 0x00; WriteRTC(&RTC_ARR[0]); } else if((RTC_ARR[1]&0x0f) > 0x08) {
RTC_ARR[1] = (RTC_ARR[1] + 0x10) & 0xf0; WriteRTC(&RTC_ARR[0]); } else { RTC_ARR[1] = RTC_ARR[1] + 0x01; WriteRTC(&RTC_ARR[0]); } delayms(10); }
4.5 Software Development Process
All C programs have a common organization scheme, this organization scheme are followed in order to construct the application smoothly.
In the first part, headers file are included in the source code. These headers contain functions that are shared across different programs.
#include <REGX52.h> #include <stdio.h> #include <math.h> #include <ds1307.h> #include <serial.h>
Next we declare the global variables. Since global variables are able to be referenced across the entire program, the rows and columns data for the LED matrix display and the data information from the DS1307 are declared.
unsigned char code number[15][5] = { 0x3E, 0x51, 0x49, 0x45, 0x3E, // 0 0x44, 0x42, 0x7F, 0x40, 0x40, // 1 0x42, 0x61, 0x51, 0x49, 0x46, // 2 0x22, 0x41, 0x49, 0x49, 0x36, // 3 0x18, 0x14, 0x12, 0x7F, 0x10, // 4 0x27, 0x45, 0x45, 0x45, 0x39, // 5 0x3E, 0x49, 0x49, 0x49, 0x32, // 6 0x01, 0x71, 0x09, 0x05, 0x03, // 7 0x36, 0x49, 0x49, 0x49, 0x36, // 8 0x26, 0x49, 0x49, 0x49, 0x3E, // 9 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char code dis_y[] =
{0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0x fe,0xff,0x0f,0x1f,0x2f,0x3f,0x4f,0x5f,0x6f,0x7f,0x8f,0x9f,0xaf,0xbf,0xcf, 0xdf,0xef,0xff};
unsigned char RTC_ARR[7]; // Buffer for second,minute,...,year
After declaration of global variables, functions that are called by the main program are written. Sub-programs that are triggered by external interrupts are written too.
Lastly, the main software program consists of initialization, and an infinite loop for retrieving the real time clock information from DS1307 and updating the LED matrix display.
main() {
unsigned char x,y,m1,m2,h1,h2,blink;
InitSerial(); // Initialize serial port setup_interrupts (); // Setup of interrupts
for(x=0;x<16;x++) //Testing of LED display {
ledtest(); ledtest2(); }
ledtest3();
while(1) // Program starts to read information from DS1307 and update the real-time clock display
{
ReadRTC(&RTC_ARR[0]); // Read DS1307 for information h1=RTC_ARR[2]>>4; // Get the tens digit for hour h2=RTC_ARR[2]&0x0f; // Get the ones digit for hour m1=RTC_ARR[1]>>4; // Get the tens digit for minutes m2=RTC_ARR[1]&0x0f; // Get the ones digit for minutes // Select first and second panels of the four LED panels
ENG499 CAPSTONE PROJECT REPORT 32
P1_3=0;
// Display the hour digit(x,2,h1); digit(x,8,h2);
// Display the colon if (blink<50) { colon(x,y); } else if (blink<100) { } else { blink=0; } blink++;
// Select the third and fourth panels of the four LED panels P1_2=1;
P1_3=0;
// Display the minutes digit(x,3,m1);
digit(x,9,m2); }
CHAPTER FIVE
PROJECT INTEGRATION AND ANALYSIS
5.1 PCB Design and Component Assembly
Taking into account that the number of components used in this design is little, it achieves the aim of creating a simplified circuit that could be wired manually. This eliminates the need of fabricating a PCB and allows changes to be made to the circuit with ease.
Figure 5.1: Components Layout for Prototype
With reference to the hardware design from chapter three, the different components are soldered to place. The wires are then carefully soldered, trying to minimise any error which may lead to malfunctioning of the prototype.
ENG499 CAPSTONE PROJECT REPORT 34
The 7805 fixed voltage regulator is the main power source for most components. A 9V adapter is connected to the 7805 through the power jack to enable the 7805 to supply a stable 5V.
Figure 5.3: 5V Fixed Voltage Regulator
The LED matrix display consists of four 8 x 8 dot matrix modules. The eight rows are connected to the UDN2981, while the thirty-two columns are connected to two 74HC154.
The DS1307 is connected to a 32 kHz crystal and two pull-up resistors for the I2C bus. The DS1307 is also connected to a 3V backup. The 3V Lithium button battery ensures the time keeping function continues running when off accidental power failure occurs.
Figure 5.5: DS1307 and 3V Backup Battery
The 10 pin connector together with the 74HCT154 line buffer are used to program the AT89S52 microcontroller when connected to the PC through the ISP cable.
ENG499 CAPSTONE PROJECT REPORT 36
Lastly, the AT89S52 microcontroller control system. The few essential circuits include the 11 MHz oscillator, reset, and the external interrupts.
Figure 5.7: AT89S52 Control System
After completion of soldering all components and wires, the prototype was put into test. All contact points were checked for short and cold joints. This ensures all electrical contacts are securely connected. With the prototyping hardware in place, it was ready to be loaded with the compiled executable firmware in HEX format.
5.2 Programming the AT89S52
With the success of writing and compiling the C program explained in chapter four, a HEX file corresponding to the codes is generated. This HEX file is a binary format executable code being run on the AT89S52 target after downloading.
To transfer the HEX file to the AT89S52 microcontroller, an ISP cable is connected to the PC parallel port. After which a 5V supply is provided to the microcontroller. Ensuring that the connections for the microcontroller are functioning and able to operate correctly, the HEX file will be transferred to the microcontroller through an ISP Flash Programmer.
Figure 5.8: Atmel ISP Flash Programmer
By launching the ISP Flash Programmer, it requires user to browse for the HEX file through the Open File button. The Write button initiates the transfer of the HEX file from PC to the microcontroller.
As soon as transfer is complete, the program starts and the LED matrix display is turned on.
ENG499 CAPSTONE PROJECT REPORT 38
5.3 Prototype Under Test
To power up the prototype, a 9V adaptor is connected. The program undergoes an initialization stage. In the initialisation stage, the LED matrix display displays a few self-testing patterns, this ensure that all four LED matrix modules are properly functioning prior to displaying real time information.
Figure 5.9: Prototype Powered Up
Following the initialization process, the program enters an infinite loop to display real time clock on the LED matrix display in a scanning fashion. Due to the limitation in display area, only hour and minutes digits are displayed in the present design.
As backup battery is installed for the DS1307, the time keeping function can be maintained even when the prototyping board is powered down. But if there is a need for time adjustment, user can still adjust the hour and minute digits through the dedicated push button switches being connected on interrupt pins.
Figure 5.11: Push Switches for Hour and Minutes Adjustment
The first switch controls the hour. To simplify the time setting procedure of the prototyping board, each push button switch triggers the increment of current hour by 1. When it reaches 23 hour, the next increment returns the hour to 00. The same principle applies to the setting of minute, with a maximum count at 59 before being
reset to 00.
Figure 5.12: Digital Clock Display “21:50”
With the capability of displaying real-time clock on the LED matrix display and time adjustment, the prototyping circuit is well proven to be fully functioning.
ENG499 CAPSTONE PROJECT REPORT 40
CHAPTER SIX
CONCLUSIONS AND FUTURE WORK
6.1 Conclusions
The main objective of this project is to develop a prototype, where real time clock is being displayed on LED matrix display modules. The idea is successfully implemented on a prototyping platform using an Atmel AT89S52 microcontroller together with interfacing discrete logic components to drive the LED matrix display modules.
The development of this project is split into two major tasks, the hardware and software development. The hardware mainly includes the circuit construction by wiring up the AT89S52 microcontroller, DS1307 real time clock and four LED dot matrix modules. Together with a few other components, the chosen hardware is soldered and wired manually on the PCB.
Although this project manages to design a simple circuit by cutting down the number of components, extra labour effort is still needed to be put in the soldering. The circuit has gone through quite numerous several rounds of iterative checks in order to eliminate short, poor contacts and wrong wiring. This process can be considered the most labour intensive as all the work need to be done manually.
The second task is the software development. Having chosen the AT89S52 microcontroller, programming becomes a lot easier as 8051 series is practically a de-facto industry standard which receives tremendous interest both academically and commercially. Much information on 8051 is available hence the program is developed with much reduced difficulty.
By combining the two major tasks described above, we ended up with a fully functioning prototype equipped with the ability to adjust and display the real time clock on an array of LED display modules.
6.2 Recommendations
The aim of this project involves an in-depth study of the system architecture for controlling an array of LED display modules in a cost-effective manner, hence designing a system of driving LED matrix display using a microcontroller. A chosen low-cost microcontroller is proven to be a viable potential option which offers a high degree of flexibility with minimal hardware construction and programming effort. Therefore the design of the system was kept as simple and straight forward, allowing this report with reference value of both theory and practical.
Keeping in mind that designing a simple circuit does not mean limiting its flexibility, as long as the microcontroller I/O interface is expanded, and increase the number of LED matrix panels and related chips, one can design a larger screen and unleash the full potential of the LED matrix display. Based on the success of the present implementation, any large scale display can be easily constructed by
cascading similar logic functions to form a bigger system.
By using the DS1307 real time clock, information including the day and date are readily available for use. This information can be displayed together with the digital clock on a bigger screen.
The 8051 series microcontroller is so widely use in the market, it is compatible with many peripheral chips and devices can be build around it. The area of applications need not be limited to time display but expand into display of information like temperature, commercial advertisements, real-time traffic information, public announcement etc.
As the core control unit of the system, that is AT89S52 has relatively low operating frequency, displaying video on LED panel may require the support of more advanced processors running at a higher clock rate.
ENG499 CAPSTONE PROJECT REPORT 42
REFLECTION
This project experience is considered one of the most challenging in my years of study. The process of completing this project is never similar to others that I have done. Firstly, being an individual project, it requires a lot of effort to ensure smooth progress. Secondly, as a part time student, I must admit to the fact that I have lesser time to complete my project.
This is where factors like time constraint and limitation of resources come into play.
The project objectives are reviewed carefully, ensuring that realistic targets are set to be achieved. As I happen to take the PMJ300 project management concurrently with this project. I picked up many tips in managing my project. Keeping in mind the importance of project management, I try to follow closely to the project plan.
As expected, things do not always goes as planned. I started facing issues with my prototype design. The design of the circuit was made simple with little components, but the prototype just did not work properly. Although I have both the hardware and software ready, they just do not seem to cooperate. I was unable to flash sample program into the microcontroller. To make things worse, I was not able to determine the issue as it involves so many parameters. It could be the hardware, ISP programmer, cables or wirings.
Troubleshooting begins and that is where I start lagging behind. I try to focus on troubleshooting the hardware as I believe I messed up the circuit. I manually check the circuit for short. As I could not find any short circuit, I try touching up the soldering for the whole board, removing chances of any poor contacts. The process feels like I am making a new board. Thankfully, I was able to transfer the program to the microcontroller after all the effort. With the prototype functioning, I begin focus on the programming the microcontroller. I succeeded in programming the microcontroller and display the digital clock on the LED matrix display. But having lost time in troubleshooting the board, I had to compromise developing additional features for the prototype.
This project allows me to gain technical knowledge and skills in project management. Having successfully develop the end product is rewarding, but the valuable experience gained through the process of this project is priceless.
REFERENCES
[1] Light Emitting Diode, Retrieved on Feb 2011, from World Wide Web: http://en.wikipedia.org/wiki/Light-emitting_diode
[2] Maxim Application note 1033, Retrieved on Feb 2011, from World Wide Web: http://www.maxim-ic.com/app-notes/index.mvp/id/1033
[3] How Light Emitting Diodes Work, Retrieved on Feb 2011, from World Wide Web: http://www.howstuffworks.com/led.htm
[4] LED Matrix Computer Controlled, Retrieved on Feb 2011, from World Wide Web: http://www.jbprojects.net/projects/led
[5] 8051 tutorial, Retrieved on May 2011, from World Wide Web: http://www.ikalogic.com/tut_8051_1.php
[6] ATMEL ISP Programmer, Retrieved on Jun 2011, from World Wide Web: http://www.sixca.com/eng/articles/at89s_isp/index.html
[7] Frederick M. Cady, Microcontrollers and Microcomputers Principles of Software and Hardware Engineering, Oxford University Press, Inc., 1997
[8] Raj Kamal, Embedded Systems (2008), Architecture, Programming and Design, 2nd ED, Tata McGraw Hill
ENG499 CAPSTONE PROJECT REPORT 44
Appendixes
Appendix A – Program Source Code
Appendix A1: Serial Initialization
//---// Serial port driver
// KEIL C51 v7.5
//---#include<REGX52.h>
//---// Initialize serial port
//---void InitSerial(void)
{
SCON = 0x52; // setup serial port control
TMOD = 0x20; // hardware (9600 BAUD @11.05592MHZ) TH1 = 0xFD; // TH1
TR1 = 1; // Timer 1 on }
Appendix A2: DS1307 driver
//---// DS1307 driver //---#include<REGX52.h> #include<intrins.h> #define ACK 1 #define NO_ACK 0 #define SLAVE 0xD0 #define WRITE 0x00 #define READ 0x01 #define ERR_ACK 0x01 unsigned char i;
const unsigned char * DayStr[7] = {{"Sun"},
{"Mon"}, {"Tue"}, {"Wen"}, {"The"}, {"Fri"}, {"Sat"}};
const unsigned char * MonthStr[12] ={{"Jan"},
{"Feb"}, {"Mar"}, {"Apr"}, {"May"}, {"Jun"}, {"Jul"}, {"Aug"}, {"Sep"},
{"Oct"}, {"Nov"}, {"Dec"}};
sbit SDA = P1^1; // connect to SDA pin (Data) sbit SCL = P1^0; // connect to SCL pin (Clock)
//---// Convert BCD 1 byte to HEX 1 byte
//---unsigned char BCD2HEX(//---unsigned int bcd) { unsigned char temp; temp=((bcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f); return temp; } //---// start I2C //---void Start(void) { SDA = 1; SCL = 1; _nop_();_nop_(); SDA = 0; _nop_();_nop_(); SCL = 0; _nop_();_nop_(); } //---// stop I2C //---void Stop(void) { SDA = 0; _nop_();_nop_(); SCL = 1; _nop_();_nop_(); SDA = 1; } //---// Write I2C //---void WriteI2C(unsigned char Data) { for (i=0;i<8;i++) {
SDA = (Data & 0x80) ? 1:0; SCL=1;SCL=0; Data<<=1; } SCL = 1; _nop_();_nop_(); SCL = 0;
ENG499 CAPSTONE PROJECT REPORT 46
}
//---// Read I2C
//---unsigned char ReadI2C(bit ACK_Bit) { unsigned char Data=0; SDA = 1; for (i=0;i<8;i++) { SCL = 1; Data<<= 1;
Data = (Data | SDA); SCL = 0;
_nop_(); }
if (ACK_Bit == 1) SDA = 0; // Send ACK else
SDA = 1; // Send NO ACK _nop_();_nop_(); SCL = 1; _nop_();_nop_(); SCL = 0; return Data; } //---// Read 1 byte form I2C
//---unsigned char ReadBYTE(//---unsigned char Addr) { unsigned char Data; Start(); WriteI2C(0xD0); WriteI2C(Addr); Start(); WriteI2C(0xD1); Data = ReadI2C(NO_ACK); Stop(); return(Data); } //---// Write 1 byte to I2C
//---void WriteBYTE(unsigned char Addr,unsigned char Data) { Start(); WriteI2C(0xD0); WriteI2C(Addr); WriteI2C(Data); Stop(); }
//---// Read RTC (all real time)
//---void ReadRTC(unsigned char * buff) { Start(); WriteI2C(0xD0); WriteI2C(0x00); Start(); WriteI2C(0xD1); *(buff+0)=ReadI2C(ACK); // Second *(buff+1)=ReadI2C(ACK); // Minute *(buff+2)=ReadI2C(ACK); // hour *(buff+3)=ReadI2C(ACK); // Day *(buff+4)=ReadI2C(ACK); // date *(buff+5)=ReadI2C(ACK); // month *(buff+6)=ReadI2C(NO_ACK); // year Stop(); } //---// Write RTC //---void WriteRTC(unsigned char *buff) { Start(); WriteI2C(0xD0); WriteI2C(0x00); WriteI2C(*(buff+0)); WriteI2C(*(buff+1)); WriteI2C(*(buff+2)); WriteI2C(*(buff+3)); WriteI2C(*(buff+4)); WriteI2C(*(buff+5)); WriteI2C(*(buff+6)); Stop(); } //---// Convert date (BCD) to string of Day // 1=Sunday
// 2=Monday // And so on
//---char * Int2Day(unsigned char day) {
return DayStr[day-1]; }
//---// Convert month (BCD) to string of Month // 0x01=January // 0x02=February // ... // 0x12 = December // And so on //---char * Int2Month(unsigned char month) {
ENG499 CAPSTONE PROJECT REPORT 48
}
Appendix A3: AT89S52 Main
//---// AT89s52 Main // KEIL C51 v7.5 //---#include <REGX52.h> #include <stdio.h> #include <math.h> #include <ds1307.h> #include <serial.h>
unsigned char code number[15][5] = { 0x3E, 0x51, 0x49, 0x45, 0x3E, // 0 0x44, 0x42, 0x7F, 0x40, 0x40, // 1 0x42, 0x61, 0x51, 0x49, 0x46, // 2 0x22, 0x41, 0x49, 0x49, 0x36, // 3 0x18, 0x14, 0x12, 0x7F, 0x10, // 4 0x27, 0x45, 0x45, 0x45, 0x39, // 5 0x3E, 0x49, 0x49, 0x49, 0x32, // 6 0x01, 0x71, 0x09, 0x05, 0x03, // 7 0x36, 0x49, 0x49, 0x49, 0x36, // 8 0x26, 0x49, 0x49, 0x49, 0x3E, // 9 0x00, 0x00, 0x00, 0x00, 0x36, // dot1 0x36, 0x00, 0x00, 0x00, 0x00, // dot2 0x55, 0xAA, 0x55, 0xAA, 0x55, // checker 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // all on 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char code dis_y[] =
{0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,0x0f ,0x1f,0x2f,0x3f,0x4f,0x5f,0x6f,0x7f,0x8f,0x9f,0xaf,0xbf,0xcf,0xdf,0xef,0xff};
unsigned char RTC_ARR[7]; // Buffer for second,minute,...,year setup_interrupts () // Function to setup the External interrupt {
EA = 1; // global interrupts enable EX0 = 1; // enable external interrupt 0 EX1 = 1; // enable external interrupt 1
IT0 = 1; // make ext. interrupt 0 edge triggered IT1 = 1; // make ext. interrupt 1 edge trigerred }
void delayms(unsigned int count) // Delay Function { unsigned int i; while(count) { i = 120; while(i>0) i--; count--; } }
void EXO_int0 (void) interrupt 0 // Interrupt Routine for Hour Increment {
if(RTC_ARR[2] > 0x22) {
RTC_ARR[2] = 0x00; WriteRTC(&RTC_ARR[0]); }
else if((RTC_ARR[2] & 0x0f) > 0x08) {
RTC_ARR[2] = (RTC_ARR[2] + 0x10) & 0xf0; WriteRTC(&RTC_ARR[0]); } else { RTC_ARR[2] = RTC_ARR[2] + 0x01; WriteRTC(&RTC_ARR[0]); } delayms(10); }
void EX1_int2 (void) interrupt 2 // Interrupt Routine for Minute Increment { ReadRTC(&RTC_ARR[0]); if(RTC_ARR[1] > 0x58) { RTC_ARR[1] = 0x00; WriteRTC(&RTC_ARR[0]); } else if((RTC_ARR[1]&0x0f) > 0x08) {
RTC_ARR[1] = (RTC_ARR[1] + 0x10) & 0xf0; WriteRTC(&RTC_ARR[0]); } else { RTC_ARR[1] = RTC_ARR[1] + 0x01; WriteRTC(&RTC_ARR[0]); } delayms(10); }
void digit(int x,y,z) // Function to display the character at specific location of screen { for(x=0;x<5;x++) { P0=dis_y[y]; P2=number[z][x]; y++; delayms(1); } }
void colon(int x,y) // Function to display the colon to seperate the hour n minutes {
P1_2=0; P1_3=0; y=15;
ENG499 CAPSTONE PROJECT REPORT 50 { P0=dis_y[y]; P2=0x36; y++; delayms(1); } P1_2=1; P1_3=0; y=0; for(x=0;x<1;x++) { P0=dis_y[y]; P2=0x36; y++; delayms(1); } }
void ledtest() // Testing of LEDs { unsigned int y,x=10; while(x>0) { P1_2=0; P1_3=0; y=0; for(y=0;y<16;y++) { P0=dis_y[y]; if (y%2==0) {P2=0x55;} else {P2=0xAA;} delayms(1); } P1_2=1; P1_3=0; y=0; for(y=0;y<16;y++) { P0=dis_y[y]; if (y%2==0) {P2=0x55;} else {P2=0xAA;} delayms(1); } x--; } }
void ledtest2() //Testing of LEDs {
unsigned int y,x=10; while(x>0)
{ P1_2=0; P1_3=0; y=0; for(y=0;y<16;y++) { P0=dis_y[y]; if (y%2==0) {P2=0xAA;} else {P2=0x55;} delayms(1); } P1_2=1; P1_3=0; y=0; for(y=0;y<16;y++) { P0=dis_y[y]; if (y%2==0) {P2=0xAA;} else {P2=0x55;} delayms(1); } x--; } }
void ledtest3() //Testing of LEDs { unsigned int y,x=60; while(x>0) { P1_2=0; P1_3=0; y=0; for(y=0;y<16;y++) { P0=dis_y[y]; P2=0xFF; delayms(1); } P1_2=1; P1_3=0; y=0; for(y=0;y<16;y++) { P0=dis_y[y]; P2=0xFF; delayms(1); } x--; } }
ENG499 CAPSTONE PROJECT REPORT 52
main() {
unsigned char x,y,m1,m2,h1,h2,blink;
InitSerial(); // Initialize serial port setup_interrupts (); // Setup of interrupts
for(x=0;x<16;x++) //Testing of LED display {
ledtest(); ledtest2(); }
ledtest3();
ReadRTC(&RTC_ARR[0]); // Read information from DS1307 /*
RTC_ARR[0] = RTC_ARR[0] & 0x7F; // enable oscillator (bit 7=0) RTC_ARR[1] = 0x15; // minute = 15
RTC_ARR[2] = 0x09; // hour = 09 ,24-hour mode(bit 6=0) RTC_ARR[3] = 0x02; // Day = 1 or sunday
RTC_ARR[4] = 0x06; // Date = 30 RTC_ARR[5] = 0x09; // month = Sept RTC_ARR[6] = 0x11; // year = 11 or 2011 WriteRTC(&RTC_ARR[0]); // Set RTC
*/
while(1) // Program starts to read information from DS1307 and update the real-time clock display
{
ReadRTC(&RTC_ARR[0]); // Read DS1307
h1=RTC_ARR[2]>>4; // Get first hour digit h2=RTC_ARR[2]&0x0f; // Get second hour digit m1=RTC_ARR[1]>>4; // Get first minute digit m2=RTC_ARR[1]&0x0f; // Get second minute digit // Select first and second panels of the four LED panels P1_2=0;
P1_3=0;
// Update the hour digit(x,2,h1); digit(x,8,h2);
// Display the colon if (blink<50) { colon(x,y); } else if (blink<100) { } else { blink=0; } blink++;
// Select the third and fourth panels of the four LED panels P1_2=1;
P1_3=0;
// Update the minutes digit(x,3,m1);
digit(x,9,m2); }
ENG499 CAPSTONE PROJECT REPORT 54
Appendix B: Schematic Diagrams
Appendix B1: Schematic for LED Matrix Display