Top Banner
E.E. 395.3 Electrical Engineering Design Final Report Submitted to: Dr. Seok-Bum Ko Lecture Section: 2 Prepared by: Cory Anderson Jon Lovering Submission Date: Tuesday April 12 th , 2005
41

E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Apr 18, 2018

Download

Documents

hadan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

E.E. 395.3 Electrical Engineering Design

Final Report

Submitted to: Dr. Seok-Bum Ko

Lecture Section: 2

Prepared by: Cory Anderson Jon Lovering

Submission Date: Tuesday April 12th, 2005

Page 2: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

Table of Contents

MOTOROLA 68HC11 PROJECTS................................................................................. 1

Microcontroller tutorial ..............................................................................................................................................1 Pre-lab ......................................................................................................................................................................1 Objective...................................................................................................................................................................1 Procedure .................................................................................................................................................................1 Questions & Answers ..............................................................................................................................................6

Two example operations of the 68HC11 microcontroller ........................................................................................6 Pre-lab ......................................................................................................................................................................6 Objective...................................................................................................................................................................7 Procedure .................................................................................................................................................................7 Questions & Answers ..............................................................................................................................................9

An example C Cross-compiler for the 68HC11.........................................................................................................9 Pre-lab ......................................................................................................................................................................9 Objective...................................................................................................................................................................9 Procedure .................................................................................................................................................................9 Questions & Answers ............................................................................................................................................16

An example difference between the Intel 8051 and the Motorola 68HC11 microcontrollers .............................17 Pre-lab ....................................................................................................................................................................17 Objective.................................................................................................................................................................17 Procedure ...............................................................................................................................................................17 Questions & Answers ............................................................................................................................................18

FINAL PROJECT.......................................................................................................... 18

Introduction ...............................................................................................................................................................18

Implementation..........................................................................................................................................................18

Hardware Diagrams ..................................................................................................................................................22

Program......................................................................................................................................................................23

Conclusions ................................................................................................................................................................39

i

Page 3: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

1. Motorola 68HC11 Projects

1.1. Microcontroller tutorial

1.1.1. Pre-lab While no previous knowledge of Motorola’s 68HC11 microcontroller is necessary for this tutorial, some familiarity of its components would help a great deal in order to completely understand all aspects of this tutorial. The following components are required in order to complete this tutorial:

• Motorola 68HC11 microcontroller with accompanying serial cable • A Window’s based personal computer equipped with the AsmIDE software

The tutorial contains numerous screenshots of the very same steps to be followed as they would appear directly on the computer screen. This has been done to ensure the ease of this tutorial, so as to focus the objective of this exercise on learning more about the operation of the 68HC11, not on attempting to decipher instructions. 1.1.2. Objective After having completed this tutorial, the student will be able to interface the microcontroller with a computer, upload a program into the microcontroller, and realize the program’s execution. This tutorial will introduce the elementary operation of port B. In this example, port B will operate the on-board LEDs.

1.1.3. Procedure

1. Attach the appropriate end of the supplied serial cable to the 68HC11. Similarly,

attach the opposing end to the LPT1 port on the personal Computer. 2. Plug the supplied AC-DC converter into a nearby wall outlet, and attach the

opposing end barrel plug into the appropriate socket on the microcontroller.

3. Search for the AsmIDE software on the computer. Activate the software. The following two pictures show the AsmIDE icon on the Windows 98© and what the program looks like when it is opened.

1

Page 4: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

2

Page 5: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

4. At the top of the screen choose file → open.

5. Locate the file program1, highlight it, and click the ‘open’ button. A few lines of assembly code should be visible in the main window. The following two slides illustrate this step.

3

Page 6: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

The assembly code used for this section is as follows:

portb: equ $1004 ;alias the memory address of port b to symbole portb SPEED: equ $ffff ;alias the speed of the delay loop to SPEED Org $C000 ;set the start location of the program. loop: ldaa #$ff ;load accumulator a with ff staa portb ;place ff on port b jsr delay ;delay ldaa #$00 ;load accumulator a with 00 staa portb ;place 00 on port b jsr delay ;delay jmp loop ;repeat delay: ldx #SPEED ;load x with the speed symbol

dly: dex ;decrement x nop ;no opperation nop bne dly ;if x is not zero, loop again rts ;return

6. At the top of the screen choose build → assemble. You will then see the some text scrolling at the bottom of the window in the ‘messages’ tab indicating that the asm file is assembling. The following two slides illustrate this step.

4

Page 7: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

7. Now click the terminal tab at the bottom of the screen. It is located directly beside the messages tab. You will see some text referring to the current version of the BUFFALO monitor. Click the mouse in the terminal window, and press enter. Once enter has been pressed you should see a ‘>’ symbol.

8. Now type “load t” after the ‘>’ symbol, and press the enter key.

5

Page 8: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

9. At the top of the screen choose build → download. When the download of the

program into the microcontroller is complete, you will see the word ‘done’ in the terminal.

10. Now type ‘g C000’ in the terminal after the ‘>’ symbol. You should now see the program in execution on the microcontroller.

1.1.1. Questions & Answers

1. What am I seeing on the microcontroller?

You are watching simple activation of all the outputs on port b, followed by the deactivation of the same outputs. This is easily verified by the repeated turning on and off of the LEDs.

1.2. Two example operations of the 68HC11 microcontroller

1.2.1. Pre-lab Just as the previous section, there is no formal pre-lab required for this section. As the following two examples demonstrate different components of the Motorola instruction set, some knowledge of the hardware and its operation would be an asset; but is not necessary.

6

Page 9: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

1.2.2. Objective

As described, these simple programs will demonstrate the operation of the some of the 68HC11’s instruction set. The first program will focuses on the logical shifting instructions inherent to the microcontroller, while the second program exercises its XOR instructions.

1.2.3. Procedure The procedure for this section is identical to that of section 1.1.3 with only two minor variations. Instead of working with program1, we will first execute program2a, followed by program2b. The assembly code for program2a is as follows: portb: equ $1004 ;alias the memory address of port b to symbole portb SPEED: equ $ffff ;alias the speed of the delay loop to SPEED start: org $C000 ;set the start location of the program. buildright: ldaa #$01 ;load accumulator a with 01 jsr display ;display ldaa #$03 ;load accumulator a with 03 (binary 11) jsr display ldaa #$07 ;load acca with 07 (binary 111) jsr display startloopleft: ldx #$0008 ;load x with 8 loopleft: lsla ;shift a left 1 place jsr display dex ;decrement x bne loopleft ;repeat 8 times buildleft: ldaa #$80 ;load acca with 80 (binary 1000 0000) jsr display ldaa #$C0 ;load acca with C0 (binary 1100 0000) jsr display ldaa #$E0 ;load acca with E0 (binary 1110 0000) jsr display startloopright: ldx #$0008 ;load x with 8 loopright: lsra ;shift a right 1 place jsr display dex ;decrement x bne loopright ;repeat 8 times jmp start end display: staa portb ;store a on portb jsr delay ;delay rts

7

Page 10: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

delay: pshx ldx #SPEED ;load x with the speed symbol dly: dex ;decrement x nop ;no opperation nop bne dly ;if x is not zero, loop again pulx rts ;return The assembly code for prgram2b is as follows: portb: equ $1004 ;alias the memory address of port b to symbole portb SPEED: equ $ffff ;alias the speed of the delay loop to SPEED

Org $C000 ;set the start location of the program. main: ldaa #$ff ;load accumulator a with ff ldx #$0003 ;load i with 3 staa portb ;place a on portb jsr delay ;delay loop1: lsla ;shift a left 1 place eora #$FF ;exor a with FF * 1111 1111 * 1111 1110 ^ 1111 1111 = 0000 0001 * 0000 0010 ^ 1111 1111 = 1111 1101 staa portb ;display jsr delay dex ;decrement x bne loop1 ;repeat 5 times

ldaa #$ff ;load accumulator a with ff ldx #$0003 staa portb ;place a on portb jsr delay ;delay loop2: lsra ;shift a right 1 place eora #$FF ;exor a with ff * 1111 1111 * 0111 1111 ^ 1111 1111 = 1000 0000 * 0100 0000 ^ 1111 1111 = 1011 1111 staa portb jsr delay dex ;decrement x bne loop2 ;repeat 5 times jmp main ;repeat delay: pshx ldx #SPEED ;load x with the speed symbol dly: dex ;decrement x nop ;no opperation nop

8

Page 11: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

bne dly ;if x is not zero, loop again pulx rts ;return

1.2.4. Questions & Answers

1. Will I be able to tell the difference from the initial program and the two example programs?

Yes. All three programs are distinct in that each of their outputs will be quite noticeably different.

1.3. An example C Cross-compiler for the 68HC11

1.3.1. Pre-lab This section of the tutorial requires that the personal computer be equipped with the EmbeddedGNU software for the Motorola 68HC11. This program will allow the user to simply write C code, rather than assemble code, so as to realize a desired function on the microcontroller.

1.3.2. Objective This section will show that programs can also be realized on the Motorola 68HC11 by using the higher level ‘C’ programming language. While there are numerous advantages to programming with assembly code, such as time critical loops and interrupts; programming in a higher level language, for the most part, can take achieve the same results in a much shorter period of time. This section will show that the assembly program found in section 1.1.3 can be reproduced in entirety by authoring C code.

1.3.3. Procedure

1. Search for the EmbeddedGNU software on the computer. Activate the software. The following slide will illustrate the resulting window.

9

Page 12: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

2. At the top of the screen choose file → open project or file.

10

Page 13: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

3. Locate the file Project1, highlight it, click the ‘open’ button. The following two slides illustrate this step.

11

Page 14: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

4. You will now be looking at program code written in the C language. This program will execute the identical task as demonstrated in section 1.1. At the top of the screen choose build → make.

The code used in this section is as follows: #include <stdio.h>

#include <string.h> int main(void); void delay(void); #define SPEED 0xffff #define PORTB *(unsigned char volatile *)(0x1004) /* i/o port b */ int main() {

//loop forever while (1)

{ PORTB = 0xff; //put ff on portb delay(); //delay PORTB = 0x00; //put 00 on port b delay(); //delay }

return 0; /* not used */ } void delay() {

unsigned short int i; for (i = 0; i < SPEED; i++) {

/* nothing */ }

}

12

Page 15: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

5. A window will pop up showing that the files are compiling.

6. A second window will be shown once the make has been completed.

13

Page 16: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

7. Click ‘Ok’ on the above window to continue. You will then arrive at the following slide.

8. At the top of the screen choose build → download.

14

Page 17: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

9. At the top of the screen choose options → project options. A small window will

open.

10. Now click ‘Edit Profile.’ A second window will open.

15

Page 18: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

11. Take note of the number in the ‘text’ field. This is number represents the start location of the program. In this case the number is ‘9000.’

12. Now click ‘OK’ to close the window. Then click ‘OK’ again to close the previously opened window.

13. Now click the ‘Terminal’ tab at the bottom of the screen.

14. Then type ‘g 9000’ (where 9000 is the number found in step 11). The

microcontroller will now be running the C program. It should be noted that the operation carried out by the unit should be in every way identical to the observed output of section 1.1.3.

1.3.4. Questions & Answers

1. Are programming in assembly code and C code the only options?

They are the most common options, but you are only limited by the chosen cross-compiler. If you are able to find 68HC11 compatible programs that are able to compile and link other programming languages, you would have the freedom to choose the language of your choice.

2. Is there only one available C cross-compiler for the 68HC11?

No, there are other choices. This program, however, seemed an excellent choice for this tutorial as it possessed all of the required functions, and was free of charge.

16

Page 19: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

1.4. An example difference between the Intel 8051 and the Motorola 68HC11 microcontrollers

1.4.1. Pre-lab For this portion of the tutorial it is important to understand a specific hardware difference between Intel’s 8051 and Motorola’s 68HC11 microcontrollers. The Motorola unit is equipped with two 8-bit registers that can be treated as one 16-bit register. The Intel unit, however, cannot combine the use of its registers in any way, limiting it to a total resolution of 8 bits.

1.4.2. Objective It has been clearly stated in this section’s Pre-lab that the 8051 simply cannot work with 16-bit values, and the following procedure will show this statement to be true. This experiment will simply place a 16-bit value into the 68HC11’s stack. We will then pop two 8-bit values off of the stack, and find that we will have our original 16-bit value. The Intel system, however, simply does not accept the code, and will not load it. This is due to its inability to properly deal with 16-bit values.

1.4.3. Procedure

Again, following the same procedure in section 1.1.3, we will load the 68HC11 with program4. Its assembly code is as follows: portb: equ $1004 ;alias the memory address of port b to symbol portb SPEED: equ $ffff ;alias the speed of the delay loop to SPEED

Org $C000 ;set the start location of the program. loop: ldaa #$00 staa portb ldy #$1881 ;load x with AABB pshy ;push x to the stack pula ;pull a (one byte of x) from the stack and put on portb staa portb jsr delay pula ;pull a (the other byto of x) from the stack and put on portb staa portb jsr delay jmp loop end delay: pshx ldx #SPEED ;load x with the speed symbol dly: dex ;decrement x

17

Page 20: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

nop ;no opperation nop bne dly ;if x is not zero, loop again pulx rts ;return Make note of the results found on port b (the LEDs). You can now attempt to run the run the very same program in the 8051, but you will find that the program will not even load. It cannot handle 16-bit values and therefore states that there is an error in the code.

1.4.4. Questions & Answers

1. Does this mean that the 68HC11 cannot handle 8-bit numbers?

No; not at all. The 68HC11 is fully capable of handling any value less than a 16-bit resolution.

2. Final Project

2.1. Introduction

Our final project involved the design and realization of a fully functional 12/24 hour clock. While at first the problem definition called for the clock solely, the design requirements changed after meeting with the customer. We included other features such as backlighting, alarm, and current date display.

We were able to come up with a successful design in the time allotted. Our results were as expected: a fully functioning unit, in line with the required specifications.

2.2. Implementation

Our first step was to come up with a very basic block diagram of the entire system. We concluded that there were only a few major blocks that would comprise our entire design. They were the inputs (buttons), outputs (liquid crystal display), and the Intel 8051 microcontroller itself. We decided to split the block diagram for the controller unit into two separate blocks: the controller itself, and its internal clock. Because our design was going to weigh heavily on the clock, we felt it would be easier to consider a separate entity. The following pages will show various black diagrams that were vital in designing the final code that was implemented to realize this design. Attempting to write the code without these basic diagrams would have resulted in certain failure. After having visualized the problem this way, writing the code was much easier, and was organized in a fashion that made sense.

18

Page 21: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

After establishing a central block diagram outlining the system we wanted to create, we then began to create block diagrams for each component in the system block diagram. We began with the main inputs, the buttons:

It was now easier to understand what function we wanted each button to execute. We continued by drawing a block diagrams as to how the microcontroller would execute the setting of each function:

19

Page 22: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

As this function was extremely complicated, it could be fit into this document in its entirety without being severely compressed. As such, it may be difficult to read. After this difficult diagram had been, the next step was to ensure that the interrupts were set-up properly to both keep time, and to take care of a button press:

20

Page 23: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

All that remained was to accurately map out how all of the appropriate information would be displayed to the LCD screen:

And also to ensure that the time was continually updated:

21

Page 24: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

After we had mapped out how our design was going to function, we were able to use these block diagrams to organize and formulate a C language program to realize the design. We enjoyed successful results.

2.3. Hardware Diagrams

Besides the microcontroller itself, we did employ the use secondary components such as a backlit liquid crystal display, and external flip-flops to act as debouncers for the input buttons:

The flip-flops are shown on the far left of the schematic, while the display is shown on the far right of the diagram. We were also able to refine our design so that a much smaller and more easily produced product could be mass manufactured:

22

Page 25: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

This final design can easily be fed into computer controlled robots, and production of this product could begin.

2.4. Program

The following is the complete code written in the C language to run the main part of our program:

#define MICROCONTROLLER_AT89S53 #include <mcs51reg.h> #include <stdio.h> //#define DEBUG //Initial condition settings #define TICKSTOSEC 100 #define SSEC 00 #define SMIN 30

23

Page 26: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

#define SHOUR 18 #define SDAY 03 #define SMONTH 4 #define SYEAR 2005 #define SALHOUR 00 #define SALMIN 00 #define SAL 0 #define SFUNC 1 #define SHRMINSEC 1 #define S_1224 0 #define SENTMIN 10 #define TIMER0_TICKS 10000 //these are bit flags set by interrupts volatile bit update_next; volatile bit set_mode; //the defines set the symbols for the input switches #define set_S P1_0 #define display_S P1_1 #define _1224_S P1_2 #define func_S P1_3 #define FFCLOCK P1_7 #define ALARM_RING P1_6 //All of the internal values volatile unsigned char hour; volatile unsigned char minute; volatile unsigned char second; volatile unsigned char day; volatile unsigned char month; volatile int year; volatile unsigned char alarm_hour; volatile unsigned char alarm_minute; volatile bit alarm_on; //sentinel is a special variable used for various counting things volatile unsigned char sentinel; //various internal settings. volatile bit hrminsec; volatile bit _1224; volatile unsigned char func; // 30 days has september, april, june, and noverember //jan, feb, march, april, may, june, july, august, septemember, october, //november, decemeber char max_days[] = {00, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //symbolistic constants #define TIME 1

24

Page 27: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

#define ALARM 2 #define DATE 3 #define TRUE 1 #define FALSE 0 /* This interrupt is called when ever there is an positive negative going edge on EX0. */ void E0interrupt(void) interrupt 0 using 0 { sentinel = 0; //When a button is and the alarm is running, stop it if (ALARM_RING == 0x1) { alarm_on = FALSE; ALARM_RING = 0x0; } //if the button is the display button, change the display type if (display_S) { hrminsec = !hrminsec; } //if the button is the 12/24 button change the 12/24 mode if (_1224_S) { _1224 = !_1224; } //if the button is the function button, change to the next function. //Special case: the next function is > DATE (beyond last function type) then //loop back if (func_S) { if(++func > DATE) { func = TIME; } } //if the button is the set mode button, then assert set mode if (set_S) { set_mode = TRUE; } return; } /*

25

Page 28: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

This function deals with an overflow of timer 0. This function has three responsibilities: 1) determine if the display needs updating, and assert the flag to do so 2) determine if the time needs updating, and assert the flag to do so 3) toggle to slow clk (used for hardware debounce) */ void t0overflow (void) interrupt 1 using 0 { static char wait_time = 0; //This embedded ASM resets the timer. This structure is very robust, and //can handle an interrupt being late. _asm clr TR0 mov A, TL0 add A, #(-(TIMER0_TICKS - 7)) & 0xFF mov TL0, A mov A, TH0 addc A, #((-(TIMER0_TICKS - 7)) >> 8) & 0xFF mov TH0, A setb TR0 _endasm; //Run the slow clk //FFCLOCK = 1; //FFCLOCK = 0; FFCLOCK = !FFCLOCK; if(++wait_time >= TICKSTOSEC) { wait_time = 0; update_next = TRUE; } } /*max day is used to correctly handle the days of the months (especially February on leap years)*/ char max_day(void) { if (month == 2) { if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { return 29; } else { return 28; } } return max_days[month]; }

26

Page 29: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

/*Update time has two functions: 1) reset mode if the inactive time is greater than 5 seconds 2) increment the time (and date)*/ void update_time (void) { //increment the sentinel and if it is greater than 5, return to TIME mode, //and deactivate set mode. sentinel++; if (sentinel >= SENTMIN) { sentinel = 0; func = TIME; set_mode = FALSE; } //This if block cascades, if ++(blank) > (blanks max) then update the next //largest value after (blank) if (++second >= 60) { second = 0; if (++minute >= 60) { minute = 0; if (++hour >= 24) { hour = 0; if (++day > max_day()) { day = 1; if (++month > 12) { month = 1; year++; } } } } } update_next = FALSE; return; } /* display has only one function, display correctly the right information. There is a hierarchy of cases: FUNCTION 12/24 Hour/Min/Sec Mode */ void display(bit mode) { char hour12 = 0;

27

Page 30: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

bit pm = 0; #ifdef DEBUG func = SFUNC; #endif //if the function is TIME if (func == TIME) { //12/24? if (_1224) { /* this block determines 12 hour time. if the hour is greater than 12, the mod of the hour is placed in hour12. */ hour12 = hour; pm = 0; if (hour12 > 12) { hour12 = hour % 12; pm = 1; } //Special case: in tw if (hour12 == 0) hour12 = 12; if (hrminsec) { //print the time: mode is handled by a trinarry statement in the //printf, as is am/pm. printf("%sTIME\f%02i:%02i:%02i %s\n", (mode ? "SET " : ""), hour12, minute, second, (pm ? "pm" : "am")); } else { //print, no seconds printf("%sTIME\f%02i:%02i %s\n", (mode ? "SET " : ""), hour12, minute, (pm ? "pm" : "am")); } } else { if (hrminsec) { //print 24 hour, /w seconds printf("%sTIME\f%02i:%02i:%02i\n", (mode ? "SET " : ""), hour, minute, second); } else { //print 24 hour, /wo seconds printf("%sTIME\f%02i:%02i\n", (mode ? "SET " : ""), hour, minute); } } }

28

Page 31: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

//Function is alarm. All code like time. //Special case: alarm on / off handled by triarry statement. if (func == ALARM) { if (_1224) { hour12 = alarm_hour; pm = 0; if (alarm_hour > 12) { hour12 = alarm_hour % 12; pm = 1; } if (hour12 == 0) hour12 = 12; printf("%sALARM\f%02i:%02i %s %s\n", (mode ? "SET " : ""), hour12, alarm_minute, (pm ? "pm" : "am"), (alarm_on ? "on" : "off")); } else { printf("%sALARM\f%02i:%02i %s\n", (mode ? "SET " : ""), alarm_hour, alarm_minute, (alarm_on ? "on" : "off")); } } //function is date if (func == DATE) { //print the date printf("%sDATE\f%02i/%02i/%04i\n", (mode ? "SET " : ""), day, month, year); } return; } /* Set the time, alarm, or date */ void setmode (void) { char set_sent = 0; /* This loop ensures that the set switch is held for 2 seconds Time continues to count */ printf("SET\n"); while (set_sent < 2) { while (!update_next && set_S) { } update_time(); if (set_S) {

29

Page 32: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

set_sent++; } else { return; } } //turn off interrupts, for great justice EX0 = 0; //Ensure the set_S is released prior to entering the set loop printf("Release Set\n"); while (set_S) { if (update_next) { update_time(); } } //set just for insurance. Technically guaranteed at this point. set_mode = TRUE; sentinel = 0; //while we're still in set mode: this loop will break if the user turns off //set mode (holds Set_S for 2 seconds) or the sentinel turns of set mode while (set_mode) { //if the current function is time if (func == TIME) { //while we are still in set_mode, and the set_s isn't being pressed while (set_mode && !set_S) { //if display_S is pressed, then reset the sentinel and update //the hour. if (display_S) { sentinel = 0; if (++hour >= 24) { hour = 0; } display(1); //ensure that display_S is released, (8)time keeps ticking, //ticking, ticking, into the future (8) while (display_S) { if (update_next) { update_time(); display(1); }

30

Page 33: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

} } //if the 12/23 switch is pressed, then reset the sentinel, and //increment the minute if (_1224_S) { sentinel = 0; if (++minute >= 60) { minute = 0; } display(1); //ensure that 12/24 is release. Again with the time thing while (_1224_S) { if (update_next) { update_time(); display(1); } } } //pesky time if (update_next) { update_time(); display(1); } } //if we're done, then 0 second. second = 0; } //if the function is alarm, we handle it just like time, but change the //variable names. //Special case: alarm on / off is toggled by func_S if (func == ALARM) { while (set_mode && !set_S) { if (display_S) { sentinel = 0; if (++alarm_hour >= 24) { alarm_hour = 0; } display(1); while (display_S) { if (update_next) { update_time(); display(1);

31

Page 34: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

} } } if (_1224_S) { sentinel = 0; if (++alarm_minute >= 60) { alarm_minute = 0; } display(1); while (_1224_S) { if (update_next) { update_time(); display(1); } } } if (func_S) { sentinel = 0; alarm_on = !alarm_on; display(1); while (func_S) { if (update_next) { update_time(); display(1); } } } if (update_next) { update_time(); display(1); } } } //if the function is date. //switches do different dances, but the logic is the same. //display_S : days //12/24: months //func_S: years **years will not advance beyond 2030 too bad** if (func == DATE) { while (set_mode && !set_S) { if (display_S) {

32

Page 35: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

sentinel = 0; if (++day > max_day()) { day = 1; } display(1); while (display_S) { if (update_next) { update_time(); display(1); } } } if (_1224_S) { sentinel = 0; if (++month > 12) { month = 1; } display(1); while (_1224_S) { if (update_next) { update_time(); display(1); } } } if (func_S) { sentinel = 0; if (++year > 2030) { year = 2005; } display(1); while (func_S) { if (update_next) { update_time(); display(1); } } } if (update_next) { update_time();

33

Page 36: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

display(1); } } } /* This loop ensures that the set switch is held for 2 seconds */ printf("SET EXIT\n"); set_sent = 0; while (set_sent < 2) { while (!update_next && set_S); update_time(); if (set_S) { set_sent++; } else { break; } } if (set_sent >= 2) { set_mode = FALSE; } } //turn back on interrupts EX0 = 1; display(0); return; } /* The heart: set up all of the functions, and then wait. */ void main(void) { //set the timer up, and start it TL0 = TIMER0_TICKS & 0xFF; TH0 = (TIMER0_TICKS >> 8) & 0xFF; TMOD = 0x01; TR0 = 1; EA = 1; //Enable interrupts IT0 = 1; //Make EX0 edge triggered EX0 = 1; //Enable X0 (External interrupt) ET0 = 1; //Enable TO (Timer interrupt) //Force Alarm ring (the pin) low ALARM_RING = 0x0;

34

Page 37: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

//Set the function to the initialization function func = SFUNC; //Set time to the initialization values second = SSEC; minute = SMIN; hour = SHOUR; day = SDAY; month = SMONTH; year = SYEAR; //set the alarm to the initialization values alarm_hour = SALHOUR; alarm_minute = SALMIN; alarm_on = SAL; //set the display thingies to initialization values hrminsec = SHRMINSEC; _1224 = S_1224; //No time or display updates (yet) update_next = FALSE; for(;;) { //if the time needs updating: do it, and update the display if (update_next == TRUE) { update_time(); display(0); } //set mode? do it if (set_mode == TRUE) { setmode(); } //if the alarm is on, then should it go off? if (alarm_on == TRUE) { if (alarm_hour == hour) { if(alarm_minute == minute) { ALARM_RING = 0xF; } } } } } //Done

35

Page 38: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

There was also additional code needed for the proper operation of the LCD on the 8051: #include <8051.h> #include "lcd.h" #define LCD_ENABLE P3_7 #define LCD_RS P0_0 /* 0=Command 1=Data */ #define LCD_RWBAR P0_1 /* 0=Write 1 = Read */ #define LCD_PORT_BUSY_FLAG P0_7 #define LCD_DATA_PORT P0 #define LCD_DATA_MASK 0xF0 #define LCD_DATA_SHIFT 4 #pragma CALLEE-SAVES LCD_Delay, LCD_Wait /* * Delay specified number of 100 usec intervals * Assumes a 12 MHz clock....and a smart compiler! */ static void LCD_Delay (volatile unsigned char husec) { do { volatile unsigned char i = 50; do { } while (--i); } while (--husec); } static void LCD_Wait (void) { bit bf; LCD_RS = 0; LCD_RWBAR = 1; LCD_DATA_PORT |= LCD_DATA_MASK; do { LCD_ENABLE = 1; bf = LCD_PORT_BUSY_FLAG; LCD_ENABLE = 0; LCD_ENABLE = 1; LCD_ENABLE = 0; } while (bf); } /* * Send a data character to the LCD */ void LCD_SendData (char c) {

36

Page 39: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

LCD_Wait (); LCD_RS = 1; LCD_RWBAR = 0; LCD_DATA_PORT &= ~LCD_DATA_MASK; LCD_DATA_PORT |= c & LCD_DATA_MASK; LCD_ENABLE = 1; LCD_ENABLE = 0; LCD_DATA_PORT &= ~LCD_DATA_MASK; LCD_DATA_PORT |= (c << LCD_DATA_SHIFT) & LCD_DATA_MASK; LCD_ENABLE = 1; LCD_ENABLE = 0; } /* * Send a command byte to the LCD */ void LCD_SendCmd (char cmd) { LCD_Wait (); LCD_RS = 0; LCD_RWBAR = 0; LCD_DATA_PORT &= ~LCD_DATA_MASK; LCD_DATA_PORT |= cmd & LCD_DATA_MASK; LCD_ENABLE = 1; LCD_ENABLE = 0; LCD_DATA_PORT &= ~LCD_DATA_MASK; LCD_DATA_PORT |= (cmd << LCD_DATA_SHIFT) & LCD_DATA_MASK; LCD_ENABLE = 1; LCD_ENABLE = 0; } /* * Initialize LCD for 4-bit data bus */ void LCD_Init (void) { LCD_DATA_PORT &= ~LCD_DATA_MASK; LCD_DATA_PORT |= (0x3 << LCD_DATA_SHIFT); LCD_RS = 0; LCD_RWBAR = 0; LCD_ENABLE = 0; LCD_Delay (150); LCD_ENABLE = 1; LCD_ENABLE = 0; LCD_Delay (41); LCD_ENABLE = 1; LCD_ENABLE = 0; LCD_Delay (1); LCD_ENABLE = 1; LCD_ENABLE = 0; LCD_Delay (41);

37

Page 40: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

LCD_DATA_PORT &= ~LCD_DATA_MASK; LCD_DATA_PORT |= (0x2 << LCD_DATA_SHIFT); LCD_ENABLE = 1; LCD_ENABLE = 0; LCD_SendCmd (0x28); /* 4-bit, 2-line, 5x8 */ LCD_SendCmd (0x08); /* Display off, cursor off, blink off */ LCD_SendCmd (0x01); /* Clear display*/ LCD_SendCmd (0x02); /* Home Cursor */ LCD_SendCmd (0x06); /* Increment cursor, no shift */ LCD_SendCmd (0x0C); /* Display on, cursor off, blink off */ /* * I don't know why the following loop is necessary, but without * the loop (even with just the LCD_Delay) the first character * displayed is gibberish. */ {unsigned int i; for(i = 0 ; i < 1 ; i++) LCD_Delay (200);} } To accompany this C file is its header file: #pragma CALLEE-SAVES LCD_Init, LCD_Putchar, LCD_SendData, LCD_SendCmd void LCD_Init (void); void LCD_Putchar (char c); void LCD_SendData (char c); void LCD_SendCmd (char cmd); And lastly, another C file was needed to take care of character placement: #include "lcd.h" /* * Special values for `character position' */ #define POS_INIT 255 #define POS_END 254 #define POS_NEWLINE 253 /* * Supply putchar if user code has not done so. */ void putchar (char c) { static unsigned char pos = POS_INIT; if (pos == POS_INIT) { LCD_Init (); pos = POS_END; } if (c == '\r') {

38

Page 41: E.E. 395.3 Electrical Engineering Designhomepage.usask.ca/~jal423/EE395Design/EE395FinalReport.pdf · FINAL PROJECT ... Final Report 1. Motorola 68HC11 Projects ... The procedure

Final Report

pos = 0; } else if (c == '\n') { pos = POS_END; } else if (c == '\f') { pos = POS_NEWLINE; } else { if (pos == POS_END) { LCD_SendCmd (0x1); pos = 0; } else if (pos == POS_NEWLINE) { LCD_SendCmd (0xC0); pos = 16; } if (pos < 16) LCD_SendCmd (0x80 + pos); else if (pos < 32) LCD_SendCmd (0x80 + 0x40 + pos - 16); LCD_SendData (c); pos++; } }

2.5. Conclusions

While the idea of designing a clock seemed rather elementary at first, the final design proved to be quite challenging. Much more programming was needed than originally had been anticipated, and conversely, less hardware than anticipated. We had difficulty, in particular, with ensuring that our clock was able to keep time within given parameters. For our prototype design, we had decided that a time variation of ±5 minutes per 24 hour period would be considered a successful design. We found, however, that our design was losing close to 5 minutes in less than 10 hours. After going over the code several times, very minor glitches were found, and repaired. The design is now accurate to within 7 seconds per 24 hour period – a dramatic improvement. There are some significant design flaws, however. The back-lighting that we chose for this design would not be feasible for mass-production. We found that when the back-light is at its maximum intensity, it drew far too much current to be considered an efficient product. This however, is the only major change that we would consider making to this product, as the remainder functioned well within our expectations. Additionally, we would also like to have utilized the 68HC11 for this design, but only discovered a good C cross-compiler for the device after much of the design was complete for the 8051. The Motorola device had many useful attributes for our design.

39