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.

If you want an alarm frequency other than daily, hourly, or on the minute, there are a couple<br />

of ways to achieve it. For an alarm every 10 minutes, you could generate an interrupt once<br />

per minute and ignore 9 out of 10 interrupts.<br />

If this seems wasteful, you can update the alarm minutes to the next desired value after each<br />

interrupt. Using the example of an interrupt every 10 minutes, you would set the mask bits<br />

for when minutes match (0-1-1), and start out by storing 0 in register 3, which will cause an<br />

interrupt to occur on the hour. When the interrupt occurs, you would add 10 to register 3 to<br />

schedule the next interrupt for 10 minutes after the hour. By continuing to add 10 to register<br />

3 after each interrupt, and returning to 0 on a count of 60, you end up with an interrupt every<br />

10 minutes.<br />

As Table 10-1 shows, many of the DS1286’s registers have multiple functions. In register<br />

9, bits 0-4 store the current month, bit 6 enables the square-wave output, bit 7 enables the<br />

clock, and bit 5 is always 0. For situations like this, you can create mask bytes and use<br />

BASIC-52’s logical operators to read and write to selected bits while ignoring other bits in<br />

a register. For example, assume that you want to store a month in bits 0-4 of the 1286’s<br />

register 9, without affecting the settings of bits 5-7.<br />

To do so, follow these steps:<br />

(1) Read the current value of the byte. In our example, with a current month of December,<br />

the clock enabled, and the square wave disabled, register 9 will hold these values:<br />

0101 0010<br />

(2) Create a mask byte by setting all bits to be masked, or unchanged, to 1, and clearing the<br />

other bits. To alter only the month’s value, bits 5-7 are masked:<br />

1110 0000<br />

(3) Logically AND the current value with the mask byte, with this result:<br />

0100 0000<br />

(4) Place the new month’s value in bits 0-4 of the byte. To change the month to June (6th<br />

month), logically OR the above byte with this:<br />

0000 0110<br />

which results in:<br />

0100 0110<br />

Clocks and Calendars<br />

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

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

Saved successfully!

Ooh no, something went wrong!