18.11.2014 Views

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10<br />

(5) Save the result in the original location (register 9). Bits 5-7 are unchanged from the<br />

original, while bits 0-4 have been changed from 12 (December) to 6 (June).<br />

Listing 10-3 shows how to use the DS1286 in a BASIC-52 system. It’s a long program, but<br />

accomplishes a lot. If you don’t need the alarm or another function, you can shorten the<br />

program by editing out the code for it.<br />

<strong>The</strong> program begins with a menu that asks you select the desired function: set-up and<br />

initialize, display the time and date, or set the alarm. To initialize the clock/calendar, follow<br />

the prompts in the subroutine beginning at line 200, and enter the information requested.<br />

<strong>The</strong> program then uses the subroutine at line 3000 to convert the information to BCD, and<br />

stores the result in the appropriate register of the DS1286. When all of the information has<br />

been entered, line 470 starts the clock by bringing bit 7 of register 9 low.<br />

Line 1000 begins the subroutine to display the current time and date. Before reading from<br />

the DS1286, the program clears TE (transfer enable, register B, bit 7). This freezes the<br />

registers at their current values, and allows you to read the complete time and date<br />

information without errors.<br />

If you don’t freeze the registers, if one of them updates in the middle of a series of read<br />

operations, you could end up with an invalid time or date. For example, if you read the hour<br />

just before 10:00, and read the minutes just after 11:00, you will think that it is 10:00 when<br />

it is really 11:00. Freezing the registers ensures that you will read the value of all of the<br />

registers as they were when TE went low. Freezing the registers does not stop the clock,<br />

however. <strong>The</strong> DS1286 continues to keep track of the time, and when you bring TE high again,<br />

the chip updates the registers to the current time and date.<br />

After the program freezes the registers, it reads the values from the DS1286, uses a<br />

subroutine at line 3100 to convert them from BCD to decimal, and displays the results.<br />

Finally, the program sets TE to update the registers.<br />

A subroutine at line 2000 handles the third function of the program, setting the alarm. To<br />

use this routine, you must wire pin 1 of the DS1286 (INTA) to pin 13 of the 8052-BASIC<br />

(INT1). A menu asks you what type of alarm you would like, prompts you for additional<br />

information, and stores the appropriate values in the DS1286’s registers 3, 5, and 7.<br />

Line 2240 configures INTA as a low-going pulse. An endless loop at lines 2250-2270 then<br />

waits for an interrupt. On interrupt, the program displays the word ALARM and the current<br />

time and date. You could place any program code in the interrupt routine. For example, you<br />

could read sensor data, or write to a port to cause a stepping motor to increment.<br />

180 <strong>The</strong> <strong>Microcontroller</strong> <strong>Idea</strong> <strong>Book</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!