PICNIC

Exercise 2 : Measurement of door opening time / Realization of alarm function

■ Exercise 2 

1. Goal of Exercise 2

2. About the timer counter for socket timeout check

3. Using each bit of timer as a timer postscaler

4. Experiment 1: Use of the timer counter

5. Experiment 2: Use of the new counter, counter1

6. Experiment 3: Actuation of alarm after the timeout of "the state of the door being open"

7. Practical example

8. Submission of Exercise 2

9. Point of consideration

■Report submission system

 

→Click here if you have some questions and comments.

←Back to the table of contents

Updated April 29, 2005 21:51

 

●Goal of Exercise 2

As Exercise 2, submit the exercise of "Measurement of door opening time / Realization of alarm function". →Click here for the submission of exercise.

The basic implementation policy is as follows.

・Using the timer counter as a postscaler of the TMR1 interrupt.
・Checking the state of opening and closing of the door at interval of the TMR1 timer interrupt (approx. 13.1msec).
・Setting of the timeout time.
・Actuation of alarm after the timeout of "the state of the door being open".

●About the timer counter for socket timeout check

(1) Overview

The timer counter (BANK#1 register) for the socket timeout check increases by 1 immediately after the int_tmr2 interrupt routine is called at interval of the TMR1 interrupt (13.1msec).

<Implementation around the interrupt routine, int_tmr2 label>

int_tmr2	; Socket timeout check
bsf STATUS,RP0 ; BANK#1
MOVLW 1
ADDWF timer,1 ; timer++1
btfsc STATUS,C ; Rollover check
GOTO dec_tm ; Timer management for socket
; --> Back to dec_tm9.
dec_tm9 ; Back to this label after the callout of dec_tm
CLRF STATUS ;

Because the timer counter is 8 bits, it rolls over after the 256 counts and calls out dec_tm. The amount of time for that is as follows: 13.107msec x 256count = 3355.392msec ( = approx. 3.36 sec )


(2) Improvement of the increment part

The timer just adds one without using Wreg. So "INCF + Zero flag check" might be better because it can cut one word more than "MOVLW + ADDWF + Carry flag check".

Use appropriately because the difference between ADD(SUB) arithmetic operation and INC(DEC) arithmetic operation is the change of STATUS flag after operation. (C, DC and Z change in ADD arithmetic operation. Only Z changes in INC arithmetic operation.)

Refer to the following data sheet instructions for details.

ADDWF instruction:
PIC16F87x MIDRANGE Reference Manual page531
microchip/33023a.pdf#page=531

INCF instruction:
PIC16F87x MIDRANGE Reference Manual page546
microchip/33023a.pdf#page=546

Therefore, it is desirable to implement as described below for the INCF+Zero flag check.

<Improvement example>

int_tmr2	; Socket timeout check
bsf STATUS,RP0 ; BANK#1
INCF timer,1 ; timer++1
btfsc STATUS,Z ; Rollover check
GOTO dec_tm ; Timer management for socket
; --> Back to dec_tm9.
dec_tm9 ; Back to this label after the callout of dec_tm
CLRF STATUS ;

 

●Using each bit of timer as a timer postscaler

Each bit (bit0 to bit7) of the timer counter can be seen as a postscaler which divides the interval (approx. 13.1msec) of the TMR1 interrupt into periods from 1:2 to 1:256.

Table 1: Period of each bit of timer

----------------------------------------
bit |Division ratio| Period (msec)
----------------------------------------
bit0 | 1:2 | 26.2
bit1 | 1:4 | 52.4
bit2 | 1:8 | 104.8
bit3 | 1:16 | 209.7
bit4 | 1:32 | 419.4
bit5 | 1:64 | 838.8
bit6 | 1:128 | 1677.6
bit7 | 1:256 | 3355.3
----------------------------------------

The period of the top bit (bit7) is about 3.36 sec. It takes a little too long. The period of bit5 is about 0.48 sec. It is appropriate for the measurement of door opening time.

●Experiment 1: Use of the timer counter

Using the divider ratio (1:64) of bit5 of the timer counter after callout instead of the test4 module created in Excersize 1, turn the light of RB4(LED#4) on and off approximately every 0.84 seconds and check the operation of a postscaler.

The easiest method of using the conditional branch instruction of the PIC microcomputer is to use a bit mask with the AND command and if the result is all 0, the Z flag will be set and checking this flag will be enough.

Therefore, if you want to branch operations based on the timer:bit5 frequency ratio (1:64), perform a check to see if bit5~0 is all 0 and then implement a way of changing the output value of RB4 (H⇔L).

<Method of replacing the value H⇔L of a specified bit>

Use the XOR operator command. As with the bitmask, using
・value of bits to be changed 1
・value of bits to be preserved 0
allows for H⇔L value changes in one convenient XOR operation.

Example:Replacing the value H⇔L of bit4 in the W register

   XORLW B'00010000'

<Implementation example of test5 module>


test5
; Entry point from the interrupt routine

; Wreg <- timer
; bit mask [0011.1111]
; bit5--0 all zero?
 → test5_09

; Change the output state of RB4
; Read PORTB→regW
; replace value of bit4 H⇔L
; regW→Output to PORTB

test5_09
; Return to the interrupt routine

#Goto test5 from the interrupt routine goes without saying.

You can confirm that LED#4 blinks for about 1.6 seconds when you execute.
(How? →Hint: It takes about one minute to count the "blinks" to 36.)

The LED blink is just like heartbeat.

 

●Experiment 2: Use of the new counter, counter1

Using the new counter, counter1 (BANK#3) instead of diverting the timer counter, perform the same operation as the test5 module. Of course, the counter1 itself have to increment.

(Changes)

bsf STATUS,RP1
bsf STATUS,RP0 ; BANK#3 register

incf counter1,0 ; Wreg <- counter1 + 1
movwf counter1 ; counter1 <- Wreg

bcf STATUS,RP1
bcf STATUS,RP0 ; BANK#0 register

●Experiment 3: Actuation of alarm after the timeout of "the state of the door being open"

Using the new counter, counter2, create the test6 module for the measurement of "door opening time", the setting of timeout time and the notification processing.

Counter2 is counted by the period timer of "heartbeat" created in Experiment 1 and 2.

Refer to the test2 module created Exercise 0/ Experiment 4 for the conditional branch processing by the state of door.

<Specification>

Actuate the alarm if the state of "the door being open" has continued over a given time.
・Set the timeout time to about 10 seconds.
・The alarm stops when the door closes.

<Outline of processing >

・The door is closed : RB7 (alarm) OFF
         Set counter2 to zero

・The door is open : Division processing of timer
         Does counter2 exceed a certain number of counts?
         →If it exceeds : RB7 (alarm) ON
         →If it does not exceed : Add one to counter2.

<Example of measurement of timeout time>

When the door is open, the counter2 counter will be incremented in about 0.84 seconds the TMR1 interrupt interval for the 1:64 frequency ratio.

For example, to meet the requirement for "actuating the alarm after about 10 seconds after the door opens",

the number of counts to timeout is 10 / 0.84 = 11.90…. Because counter2 starts the count from 0, "the threshold value" for timeout is 12 in the end.

Point 1:
Because counter2 is an 8-bit counter and can count from 0 to 255, the maximum value of the timeout time is 0.84sec x 255count =214.2sec (about 3 and a half minutes). If you want to use a longer timeout period, get a frequency ratio from a larger bit (e.g., bit7) of the timer (or counter1).

Point 2:
If you want to raise the precision of the timeout time, get the frequency ratio from a smaller bit and use a larger number for the timeout value.

Point 3:
When debugging the program, it is very advantageous if the value of counter2 can be monitored. Refer to "Display of the counter for the number of times of opening and closing the door to the web control screen (put_count module)" implemented in Exercise 1.

●Practical example

The door is closed. → Open the door. → (10 seconds passed.) → Timeout and actuation of alarm
→Stop the alarm when the door closes. → (Back to the start.)


●Submission of Exercise 2

【Exercise 1】(Experiment 3)
Create the test6 module for the measurement of "door opening time", the setting of timeout time and the notification processing. Set the timeout time to approximately 10 seconds. Using the changes of the state of door sensor, the web control screen and the counter value, confirm to work as required.

(1) Create the test6 module for the realization of actuation of alarm after the timeout of "the state of the door being open".

(2) Using the changes of the state of door sensor, the web control screen and the counter value, confirm to work as required.

(3) Change the version number of the assembled source file to
 ver1.2.0.7.
And change arbitrarily the footer of the web control screen to your student ID number and so on.

(4) Save the assembled source file under the name of v1207e2.asm. (English one byte characters/ Lower-case alphabets/ Do not use one-byte katakana and double-byte characters as file name.)

(5) From the following report submission system, upload v1207e2.asm. We will mark your report after receiving it.

<How to submit>
For the program you created, submit the assembled source file (the extension is not *.hex but *.asm) by using the following report system (with the file upload function).

We will judge submission or completion after checking the operation of the file you submitted. You have to resubmit if there are any defects in the operation. You can finish if the operation is OK.

Click here to submit the report of Exercise 2 (Exercise number: picnic-exp02).

(6) You have to "wait our judgement". Send an e-mail to here if you do not receive the result of judgement for a long time.

(7) If you received the judgement of submission, correct the defects indicated, change the filename to the name of v1206e2a.asm and so on and submit it.

(8) Without (7) (that is, the judgement of completion), your report was accepted and you can finish.

 

●Point of consideration

Point of consideration on this time's specification and implementation is as follows. You do not have to answer it as a report, but make sure to arrange your thoughts before you finish the program and submit your report.

 

[1] When assuming the range such as a few seconds to a few hours, the disadvantage occurs in the postscaler by the 8-bit counter of this implementation. What should be improved to meet such a requirement?

[2] The internal timer itself has very high precision (equivalent to a quartz clock) because it uses a frequency ratio of an external crystal oscillator. It would be easy to modify the system to work not as a door opening alarm, but as a 24 hour timer for turning on/off a relay at fixed intervals. However, if we used frequency ratios 1:2^n of the clock (20MHz) as we do now, we will not have a counter in units of seconds, minutes, hours. If we calculate each of these values and use them in variables counter1,2,3 how should we handle the frequency ratios of the system?

[3] The counter2 counter should be set the default value to 0 somewhere after reset. Where should that be implemented exactly?

Shinshu University Graduate School on the Internet

wasaki@cs.shinshu-u.ac.jp
Copyright(c) 2005 Katsumi Wasaki. All rights reserved.