LED Display by 8051
Short Description
Description : afsf...
Description
SIM UNIVERSITY SCHOOL OF SCIENCE AND TECHNOLOGY
APPLICATION OF MICROCONTROLLER IN LED MATRIX DISPLAY
STUDENT
: CHEW HANWEI (PI NO. E0806350) SUPERVISOR : DR. FUNG CHI FUNG PROJECT CODE : JAN2011/ENG/045
A project report submitted to SIM University in partial fulfilment of the requirements for the degree of Bachelor of Engineering November 2011
TABLE OF CONTENTS Page ABSTRACT
iv
ACKNOWLEDGEMENT
v
LISTS OF FIGURES
vi
CHAPTER ONE
7
INTRODUCTION
7
1.1
Project Objective
7
1.2
Overall Objective
7
1.3
Proposed Approach and Method
8
1.4
Project Management
9
CHAPTER TWO LITERATURE REVIEW
12 12
2.1
Introduction of LED
12
2.2
Applications of LED Matrix Display
13
2.3
Techniques of Driving LED Matrix Display
15
CHAPTER THREE HARDWARE SYSTEM IMPLEMENTATION
17 17
3.1
System Overall Structure Design
17
3.2
Atmel AT89S52 Control System
19
3.3
DS1307 Real-time Clock
20
3.4
LED Matrix Display Design
21
3.5
Circuit Design for 5V Power Source
22
3.6
AT89S52 In System Programming (ISP)
23
CHAPTER FOUR SOFTWARE SYSTEM IMPLEMENTATION
25 25
4.1
Software Design Structure
25
4.2
Codes for Dot Matrix Display Characters
26
4.3
DS1307 Interface With AT89S52
27
4.4
Interrupt Service Routine for Time Adjustment
29
4.5
Software Development Process
30
CHAPTER FIVE PROJECT INTEGRATION AND ANALYSIS
33 33
5.1
PCB Design and Component Assembly
33
5.2
Programming the AT89S52
36
ENG499 CAPSTONE PROJECT REPORT
ii
TABLE OF CONTENTS Page ABSTRACT
iv
ACKNOWLEDGEMENT
v
LISTS OF FIGURES
vi
CHAPTER ONE
7
INTRODUCTION
7
1.1
Project Objective
7
1.2
Overall Objective
7
1.3
Proposed Approach and Method
8
1.4
Project Management
9
CHAPTER TWO LITERATURE REVIEW
12 12
2.1
Introduction of LED
12
2.2
Applications of LED Matrix Display
13
2.3
Techniques of Driving LED Matrix Display
15
CHAPTER THREE HARDWARE SYSTEM IMPLEMENTATION
17 17
3.1
System Overall Structure Design
17
3.2
Atmel AT89S52 Control System
19
3.3
DS1307 Real-time Clock
20
3.4
LED Matrix Display Design
21
3.5
Circuit Design for 5V Power Source
22
3.6
AT89S52 In System Programming (ISP)
23
CHAPTER FOUR SOFTWARE SYSTEM IMPLEMENTATION
25 25
4.1
Software Design Structure
25
4.2
Codes for Dot Matrix Display Characters
26
4.3
DS1307 Interface With AT89S52
27
4.4
Interrupt Service Routine for Time Adjustment
29
4.5
Software Development Process
30
CHAPTER FIVE PROJECT INTEGRATION AND ANALYSIS
33 33
5.1
PCB Design and Component Assembly
33
5.2
Programming the AT89S52
36
ENG499 CAPSTONE PROJECT REPORT
ii
5.3
Prototype Under Test
CHAPTER SIX CONCLUSIONS AND FUTURE WORK
38 40 40
6.1
Conclusions
40
6.2
Recommendations
41
REFLECTION
42
REFERENCES
43
Appendixes
44
Appendix A – A – Program Source Code
44
Appendix A1: Serial Initialization
44
Appendix A2: DS1307 driver
44
Appendix A3: AT89S52 Main
48
Appendix B: Schematic Diagrams
54
Appendix B1: Schematic for LED Matrix Display
54
Appendix B2: Schematic for ISP Program
54
Appendix B3: Schematic for 5V Supply
55
Appendix C: Dot Matrix Characters
ENG499 CAPSTONE PROJECT REPORT
56
iii
ABSTRACT
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 environment.
This project introduces display design process about hardware and software based on Atmel AT89S52 single chip microcontroller. The system mainly involves the AT89S52 microcontroller, a LED matrix display and a real time clock (DS1307).
We use a simple external circuit to control the display screen, whose size is 8-pixel by 32 pixel. The display screen can display the size of four 5-pixel by 7-pixel dot matrix 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.
Based on the prototyping platform, an algorithm for translating register values from real 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 display.
ENG499 CAPSTONE PROJECT REPORT
iv
ACKNOWLEDGEMENT
I would like to present my appreciation to the following individuals for their untiring support and encouragement for the whole duration of my final year project. Without their help, this project would not be able to complete complete so smoothly and attain success.
Firstly, I would like to thank my project supervisor, Dr. Fung Chi Fung for his guidance and patience. I am grateful for his valuable feedback and suggestions that enable the project to progress smoothly as planned.
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 during my course of studies.
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
v
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
Figure 5.12: Digital Clock Display “21:50”
39
ENG499 CAPSTONE PROJECT REPORT
vi
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:
1.2
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.
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
7
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. ENG499 CAPSTONE PROJECT REPORT
8
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 displa y
ENG499 CAPSTONE PROJECT REPORT
9
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.
ENG499 CAPSTONE PROJECT REPORT
10
4 3 - 4 c e 2 4 1 4 0 4 9 - 3 v 8 o 3 7 3 6 3 5 - 3 t c 4 3 3 3 2 3 1 - 3 p 0 e 3 S 9 2 8 2 7 - 2 u 6 2 5 2 4 2 3 - 2 l 2 u J 2 1 2 0 2 9 - 1 n 8 u J 1 7 1 6 1 5 - 1 y a 4 1 3 1 2 1 1 - 1 r p 0 1 9
n , s o g n w t n i o t e o h p s o s h y s l a s r e a . w l b e l 4 e u c k s m o t r r a i t a T t e h n n o p C i y i t t t t y i t o a d n l a e d o r G d A p
k e e W n o i t a n i m a x E e e n o s v e
8 7
r a 6 5 4 3
b 2 e F
l a n u t a l c P A
1
s s
k e t e c j r w e i v e R e r u t a r e t i L . 1
n o i t i n i f e d t c e j o r P g n i d n a t s r e d n U 1 . 1
d n u o r g k c a B t c e j o r P n o h c r a e s e R 2 . 1
t r o p e R l a i t i n I t c e j o r P . 2
d n a r e l l o r t n o c o r c i M 1 5 0 n 8 g , y i a s e l d p s i g n d i x p y i y a r t t l a o t p s m o r i D p d E i L n x r n o t o n a a h l m c D r p E a s L e e z 1 s i . e l a R n 3 . i 3 f
r e l l o r t n o c o r c i M 1 5 0 8 2 . 3
y a l p s i d D E L f o g n i t s i s n n o g c i s e e p r y t e d e l o t l o p y o r r t t p n o f o t o c o r p n o r o c n i i t o a e t z d n i l e n a a m s n e e i F l p l u 1 . d m I o 4 . 4
e p y t o t o r p t n e m e l p m I 2 . 4
n o r k e c i l o l p c m e o c m i t C l g a n i e s r u y a g l n i p s i m d m o a t r r g e o l r l p o r r t e n l l o c y o r o t l r n c i s o i c m o x r c m i i r a r t M g 1 o . r 5 P . E 5 L
w e i v e R t c e j o r P . 6
t r o p e R l a n i F f o n o i t a r a p e r P . 7
n o i t a t n e s e r P l a r O d n a r e t s o P f o n o i t a r a p e r P . 8
Figure 1.1: Project Gantt Chart ENG499 CAPSTONE PROJECT REPORT
11
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.
ENG499 CAPSTONE PROJECT REPORT
12
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
13
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.
ENG499 CAPSTONE PROJECT REPORT
14
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
15
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.
In this project, a microcontroller is employed to interface the LED matrix display.
ENG499 CAPSTONE PROJECT REPORT
16
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 nonvolatile memory programmer. For the aforementioned reasons, AT89S52 is chosen as the controller of the entire system for the present application.
ENG499 CAPSTONE PROJECT REPORT
17
DS1307 Real Time Clock
AT89S52
Row Scanning Circuit
LED Matrix Display
Column Scanning Circuit
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.
ENG499 CAPSTONE PROJECT REPORT
18
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
19
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. ENG499 CAPSTONE PROJECT REPORT
20
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 the row signal data coming from Port2 of the microcontroller.
ENG499 CAPSTONE PROJECT REPORT
21
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.
Figure 3.10: 5V Power Supply
ENG499 CAPSTONE PROJECT REPORT
22
7805 is equipped with built-in protection circuitry against overheating and shortcircuits, 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
23
Figure 3.12: AT89S52 ISP Circuit
The 10-pin connector is then connected to the computer parallel port as shown.
Figure 3.13: ISP Download Cable
ENG499 CAPSTONE PROJECT REPORT
24
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.
Start
Initialize Serial
Setup Interrupts
Read DS1307 Infinite Loop Display Time
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.
ENG499 CAPSTONE PROJECT REPORT
25
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 deasserted. 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, // 0x44, 0x42, 0x7F, 0x40, 0x40, // 0x42, 0x61, 0x51, 0x49, 0x46, // 0x22, 0x41, 0x49, 0x49, 0x36, // 0x18, 0x14, 0x12, 0x7F, 0x10, // 0x27, 0x45, 0x45, 0x45, 0x39, // 0x3E, 0x49, 0x49, 0x49, 0x32, // 0x01, 0x71, 0x09, 0x05, 0x03, // 0x36, 0x49, 0x49, 0x49, 0x36, // 0x26, 0x49, 0x49, 0x49, 0x3E, //
{ 0 1 2 3 4 5 6 7 8 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. ENG499 CAPSTONE PROJECT REPORT
26
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 sbit SCL
= =
P1^1; P1^0;
// connect to SDA pin (Data) // 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
27
//------------------------------// 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); // *(buff+1)=ReadI2C(ACK); // *(buff+2)=ReadI2C(ACK); // *(buff+3)=ReadI2C(ACK); // *(buff+4)=ReadI2C(ACK); // *(buff+5)=ReadI2C(ACK); // *(buff+6)=ReadI2C(NO_ACK); Stop();
Second Minute hour Day date month // year
} //------------------------------// Write RTC //------------------------------void WriteRTC(unsigned char *buff) {
ENG499 CAPSTONE PROJECT REPORT
28
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 t he 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
29
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 #include #include #include #include
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. ENG499 CAPSTONE PROJECT REPORT
30
unsigned char code number[15][5] = 0x3E, 0x51, 0x49, 0x45, 0x3E, // 0x44, 0x42, 0x7F, 0x40, 0x40, // 0x42, 0x61, 0x51, 0x49, 0x46, // 0x22, 0x41, 0x49, 0x49, 0x36, // 0x18, 0x14, 0x12, 0x7F, 0x10, // 0x27, 0x45, 0x45, 0x45, 0x39, // 0x3E, 0x49, 0x49, 0x49, 0x32, // 0x01, 0x71, 0x09, 0x05, 0x03, // 0x36, 0x49, 0x49, 0x49, 0x36, // 0x26, 0x49, 0x49, 0x49, 0x3E, // 0x00, 0x00, 0x00, 0x00, 0x00};
{ 0 1 2 3 4 5 6 7 8 9
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>4; // h2=RTC_ARR[2]&0x0f; // m1=RTC_ARR[1]>>4; // m2=RTC_ARR[1]&0x0f; //
to read information from DS1307 and update the real-
// Read Get the Get the Get the Get the
DS1307 for tens digit ones digit tens digit ones digit
information for hour for hour for minutes for minutes
// Select first and second panels of the four LED panels P1_2=0; ENG499 CAPSTONE PROJECT REPORT
31
P1_3=0; // Display the hour digit(x,2,h1); digit(x,8,h2); // Display the colon if (blink8)*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
View more...
Comments