ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 15.06.2025

Просмотров: 2088

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Chapter

10

Listing

10-2. Toggles a port bit and displays the result.

10

REM toggles P1.7 once per second

20

TIME=0

30

CLOCK 1

40

DO

50

ONTIME 1,100

60

WHILE 1=1

70

END

100

REM reset time

110

TIME=0

120

REM toggle Port 1, bit 7

130

PORT1=PORT1.XOR.80H

140

PRINT “Port 1, bit 7 = ”,(PORT1.AND.80H)/80H

150

RETI

But in reality, because of the way that BASIC-52 calculates time, small variations in XTAL usually do not effect the real-time clock. Although BASIC-52 will store a XTAL value as precise as 12000001, it uses the same time base for all XTAL values from 11963191 to 12039877. If your crystal frequency is within this range, small adjustments to XTAL won’t make the real-time clock more accurate. The value that controls the time base is stored at 4Ah in internal data memory. At 12 Mhz, it’s 64h. If you want to experiment, change the value of XTAL, then type PH0. DBY(4AH) to find out if the time base has changed.

A Watchdog Timekeeper

Dallas Semiconductor’s DS1286 Watchdog Timekeeper, shown in Figure 10-1, is another way to keep track of time. The chip is easy to use because it contains its own quartz-crystal timing reference, plus a lithium cell for backup power. Once you initialize the clock and calendar and start the oscillator, the clock keeps time for ten years or more, whether or not an external power source is present. You don’t have to reset the clock every time you power up.

The DS1286 can be especially useful in battery-powered systems. Since it continues to keep time when the main power supply is off, you can use its interrupt output to power circuitry at programmed times or intervals. For example, by adding circuits to control a power supply, the DS1286’s interrupt could trigger a data logger or other instrument to power up at a programmed time. After taking data or performing other operations, the instrument could power itself down until the next interrupt from the DS1286. The longer the time between readings, the greater the power savings.

174

The Microcontroller Idea Book


Clocks and Calendars

Figure 10-1. Pinout of the DS1286 Watchdog Timekeeper.

The DS1286 contains a series of registers that store time, date, alarm, and configuration information. You can read the current time and date from the DS1286 in hundredths of seconds, seconds, minutes, hours, day of the week, date of the month, and year. Months of different lengths and even leap years are handled automatically. Clock accuracy is better than ±1 minute per month at 25 degrees Celsius.

Figure 10-2 shows the pin connections for a DS1286 in a BASIC-52 system. To accommodate its crystal and power source, the DS1286 a 28-pin encapsulated DIP, just like the one used by Dallas’ NVRAMs.

The pinout and wiring are similar to that for static RAM. The chip’s access time is 150 nanoseconds, which is well within the 8052-BASIC’s timing requirements. The eight data lines (DQ0-DQ7) connect to the data bus. The chip has just six address inputs (A0-A5), which are all it needs to access its 64 bytes. The clock/calendar uses 14 bytes, and 50 bytes of nonvolatile RAM are available for any use. The chip is shown addressed at A000h, but you can use any unused chip-enable. The chip’s WE and OE inputs are driven by RDANY and

WRITE.

The DS1286 has two interrupt outputs, INTA and INTB. You can program one of these to toggle or pulse whenever the time and/or day match stored values. The other can generate an interrupt if the DS1286’s watchdog register isn’t accessed periodically. You can use this feature to automatically reset a system if a program crash causes the program to stop

The Microcontroller Idea Book

175

Chapter 10

Figure 10-2. Wiring diagram for the DS1286 Watchdog Timekeeper in an 8052-BASIC system.

accessing the watchdog register. When IPSW (register B, bit 6) is 1, the time-of-day interrupt is on INTA, and the watchdog interrupt is on INTB. When IPSW is 0, these are reversed, with the watchog on INTA and the time-of-day on INTB. The chip also has a 1024-Hz square-wave output.

Table 10-1 details the functions of the DS1286’s registers, which store time, date, configuration, and status information. To initialize the clock/calendar, you write the current time and date into registers 0-2, 4, 6, and 8 through A, then start the clock by clearing EOSC (bit 7 of register 9).

Time and date values are stored in binary-coded decimal (BCD) format. In BCD, a 4-bit nibble represents one decade, and nibbles greater than 9 (1001) are not allowed. Table 10-2 shows numbers expressed in decimal, BCD, and binary. Some values in the DS1286 don’t require a full 8 bits. For example, since the month can go no higher than 12, you need only 5 bits to store its value.

176

The Microcontroller Idea Book

Clocks and Calendars

Table

10-1.

Register functions for the DS1286 Watchdog Timekeeper

Register

Function

Bit 7

Bit 6

Bit 5

Bit 4

Bit 3

Bit 2

Bit 1

Bit 0

0

Clock

0.1 seconds

0.01 seconds

1

0

10 seconds

seconds

2

0

10 minutes

minutes

3

Alarm

MASK

10 minutes alarm

minutes alarm

4

Clock

0

10hr or

10hr

hours

12/24

AM/PM

5

Alarm

MASK

10hr or

10hr

hour alarm

12/24

AM/PM

6

Calendar

0

0

0

0

0

days

7

Alarm

MASK

0

0

0

0

day alarm

8

Calendar,

0

0

10 date

date

Oscillator

9

EOSC

ESQW

0

10 mo

months

A

10 years

years

B

Command

TE

IPSW

IBH/LO

PU/LVL

WAM

TDM

WAF

TDF

C

Watchdog

0.1 seconds

0.01 seconds

D

10 seconds

seconds

E-3F

User

free for any use

Time of Day Alarm Mask Bits

Minutes

Hours

Day

Alarm Frequency

1

1

1

Once per minute

0

1

1

When minutes match

0

0

1

When hours and minutes match

0

0

0

When hours, minutes, and days match

Symbol

Function

Symbol

Function

Enable Oscillator

Pulse/Level Triggered Interrupts

EOSC

PU/LVL

Enable Square Wave Out

WAM

Watchdog Alarm Mask

ESQW

Transfer Enable

TDM

Time-of-day Mask

TE

Interrupt Switch

WAF

Watchdog Alarm Flag

IPSW

Interrupt B High/Low Trigger

TDF

Time-of-day Flag

IBH/LO

The Microcontroller Idea Book

177


Chapter 10

To generate an interrupt at a specific time, you select an alarm frequency by setting or clearing three mask bits (bit 7 of registers 3, 5, and 7), and storing the desired alarm data in bits 0-6 of the same registers. Clearing a register’s mask bit means that the DS1286 will use values in that register to determine the alarm frequency. Setting a mask bit means that the DS1286 will ignore the information in the register. For example, to generate an interrupt at 3:15 daily, you would store the following values:

Register

Mask Byte

Alarm Data

3

0001 0101

15 minutes

5

0000 0011

3 hours

7

1XXX XXXX

days (X=don’t care)

Table 10-3. Decimal numbers and their equivalents in binary and binary-coded decimal. The values 0-9 are identical in BCD and binary.

Decimal

Binary-coded Decimal (BCD)

Binary

0

0000 0000

0000 0000

1

0000 0001

0000 0001

2

0000 0010

0000 0010

3

0000 0011

0000 0011

4

0000 0100

0000 0100

5

0000 0101

0000 0101

6

0000 0110

0000 0110

7

0000 0111

0000 0111

8

0000 1000

0000 1000

9

0000 1001

0000 1001

10

0001 0000

0000 1010

11

0001 0001

0000 1011

19

0001 1001

0001 0011

20

0010 0000

0001 0100

29

0010 1001

0001 1101

30

0011 0000

0001 1110

99

1001 1001

0110 0011

178

The Microcontroller Idea Book


Clocks and Calendars

If you want an alarm frequency other than daily, hourly, or on the minute, there are a couple of ways to achieve it. For an alarm every 10 minutes, you could generate an interrupt once per minute and ignore 9 out of 10 interrupts.

If this seems wasteful, you can update the alarm minutes to the next desired value after each interrupt. Using the example of an interrupt every 10 minutes, you would set the mask bits for when minutes match (0-1-1), and start out by storing 0 in register 3, which will cause an interrupt to occur on the hour. When the interrupt occurs, you would add 10 to register 3 to schedule the next interrupt for 10 minutes after the hour. By continuing to add 10 to register 3 after each interrupt, and returning to 0 on a count of 60, you end up with an interrupt every 10 minutes.

As Table 10-1 shows, many of the DS1286’s registers have multiple functions. In register 9, bits 0-4 store the current month, bit 6 enables the square-wave output, bit 7 enables the clock, and bit 5 is always 0. For situations like this, you can create mask bytes and use BASIC-52’s logical operators to read and write to selected bits while ignoring other bits in a register. For example, assume that you want to store a month in bits 0-4 of the 1286’s register 9, without affecting the settings of bits 5-7.

To do so, follow these steps:

(1) Read the current value of the byte. In our example, with a current month of December, the clock enabled, and the square wave disabled, register 9 will hold these values:

0101 0010

(2) Create a mask byte by setting all bits to be masked, or unchanged, to 1, and clearing the other bits. To alter only the month’s value, bits 5-7 are masked:

1110 0000

(3) Logically AND the current value with the mask byte, with this result:

0100 0000

(4) Place the new month’s value in bits 0-4 of the byte. To change the month to June (6th month), logically OR the above byte with this:

0000 0110

which results in:

0100 0110

The Microcontroller Idea Book

179

Chapter 10

(5) Save the result in the original location (register 9). Bits 5-7 are unchanged from the original, while bits 0-4 have been changed from 12 (December) to 6 (June).

Listing 10-3 shows how to use the DS1286 in a BASIC-52 system. It’s a long program, but accomplishes a lot. If you don’t need the alarm or another function, you can shorten the program by editing out the code for it.

The program begins with a menu that asks you select the desired function: set-up and initialize, display the time and date, or set the alarm. To initialize the clock/calendar, follow the prompts in the subroutine beginning at line 200, and enter the information requested. The program then uses the subroutine at line 3000 to convert the information to BCD, and stores the result in the appropriate register of the DS1286. When all of the information has been entered, line 470 starts the clock by bringing bit 7 of register 9 low.

Line 1000 begins the subroutine to display the current time and date. Before reading from the DS1286, the program clears TE (transfer enable, register B, bit 7). This freezes the registers at their current values, and allows you to read the complete time and date information without errors.

If you don’t freeze the registers, if one of them updates in the middle of a series of read operations, you could end up with an invalid time or date. For example, if you read the hour just before 10:00, and read the minutes just after 11:00, you will think that it is 10:00 when it is really 11:00. Freezing the registers ensures that you will read the value of all of the registers as they were when TE went low. Freezing the registers does not stop the clock, however. The DS1286 continues to keep track of the time, and when you bring TE high again, the chip updates the registers to the current time and date.

After the program freezes the registers, it reads the values from the DS1286, uses a subroutine at line 3100 to convert them from BCD to decimal, and displays the results. Finally, the program sets TE to update the registers.

A subroutine at line 2000 handles the third function of the program, setting the alarm. To use this routine, you must wire pin 1 of the DS1286 (INTA) to pin 13 of the 8052-BASIC (INT1). A menu asks you what type of alarm you would like, prompts you for additional information, and stores the appropriate values in the DS1286’s registers 3, 5, and 7.

Line 2240 configures INTA as a low-going pulse. An endless loop at lines 2250-2270 then waits for an interrupt. On interrupt, the program displays the word ALARM and the current time and date. You could place any program code in the interrupt routine. For example, you could read sensor data, or write to a port to cause a stepping motor to increment.

180

The Microcontroller Idea Book