|
|
Exercise 2 : Measurement of door opening time / Realization of alarm function | |||||
| ■ 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"
→Click here if you have some questions and comments. ←Back to the table of contents
Updated April 29, 2005 21:51
|
●Goal of Exercise 2As 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. ●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>
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 )
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: INCF instruction: Therefore, it is desirable to implement as described below for the INCF+Zero flag check. <Improvement example>
●Using each bit of timer as a timer postscalerEach 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
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 counterUsing 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 Example:Replacing the value H⇔L of bit4 in the W register XORLW B'00010000' <Implementation example of test5 module>
#Goto test5 from the interrupt routine goes without saying. You can confirm that LED#4 blinks for about 1.6 seconds when you execute. The LED blink is just like heartbeat.
●Experiment 2: Use of the new counter, counter1Using 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 incf counter1,0 ; Wreg <- counter1 + 1 bcf STATUS,RP1 ●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. <Outline of processing > ・The door is closed : RB7 (alarm) OFF ・The door is open : Division processing of timer <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: Point 2: Point 3: ●Practical exampleThe door is closed. → Open the door. → (10 seconds passed.)
→ Timeout and actuation of alarm
●Submission of Exercise 2
(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 (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> 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 considerationPoint 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?
|
|||||
|
wasaki@cs.shinshu-u.ac.jp |