PICNIC

Exercise 1 : Count of the number of times of opening the door / Realization of display function

■ Exercise 1 

1. Goal of Exercise 1

2. Creation of the "counter" variable for the count of the number of times

3. TMR1 internal timer / Interrupt handling routine

4. State of opening and closing of the door (RB0) → Output to LED#4 (RB4) with positive logic

5. Count module of the number of times of opening the door: test4

6. Display of the counter to the web control screen

7. Practical example

8. Submission of Exercise 1

9. Point of consideration

■Report submission system

 

→Click here if you have some questions and comments.

←Back to the table of contents

Updated May 4, 2005 20:40

 

●Goal of Exercise 1

As Exercise 1, submit the exercise of "Count of the number of times of opening the door / Realization of display function". →Click here for the submission of exercise.

The basic implementation policy is as follows.

・Creating a variable (8bit) for the count of the number of times.
・Checking the state of opening and closing of the door at interval of the TMR1 timer interrupt (approx. 13.1msec).
・The state of opening and closing of the door (RB0) → Output to LED#4 (RB4) with positive logic.
・Perceiving the state transition from the closing to opening of door, then adding 1 to the counter.
・Displaying the counter to the web control screen (Replacing the display of packet number).

●Creation of the "counter" variable for the count of the number of times (2003/10/12 updated)

Create the "counter" variable for the count of the number of times to the BANK#3 register.

<Implementation example>

;-------------------------------------------------------------
; Experimental memory area BANK#3 [RP1:RP0]=[1:1] 1B0h -- 1EFh
;-------------------------------------------------------------
; PAGE 3
counter EQU 1E0H ; Counter#0
counter1 EQU 1E1H ; Counter#1
counter2 EQU 1E2H ; Counter#2
counter3 EQU 1E3H ; Counter#3

Create 4 counters together with the future in mind.
Access and refer to the BANK#3 register after switching the bank by setting the STATUS register, RP1 and RP2, to 1.(If you don't know what it means, refer to page 14 of the manual.)


※2003/10/12 Changes: Initially, 4 bytes from 1B0 were gotten as the counter variable. However, it was found that a value of some kind was entered in the general register near 1B0 somewhere when frequently switching RB5 from the web control screen or mounting continuous attacks from the web browser. Because the variable is not actually defined in this area by EQU, the original v12.asm general register operation (RP0 and RP1) might have a bug (BANK#3 instead of BANK#1 is changed) or PIC16F877 itself might have a problem. To avoid these problems, we changed to get 4 bytes from 1E0 as the counter variable. Because BANK#3 remains the same anyway, the following description is the same as before. In addition, the counter variable should be initialized at the end of the start module.

●TMR1 internal timer / Interrupt handling routine

Reminders:In Exercise 0, we added four steps for the call processing of the test module immediately after the main0 label and confirmed the operation. (Refer to this part of Exercise 0.)

However, from Exercise 1 on, because the module to be implemented is called out by jumping from the timer interrupt routine , make sure to comment out or delete the callout processing which was added immediately after the main0 label. (2005/5/3 added)


Click here to see the current description related to the internal timer/ interrupt handling routine in PIC16F877+PICNIC firmware,v12.asm.


When calling to the outside from within the interrupt routine, PC stack overflows somewhere and a malfunction occurs.

It is virtually difficult to increase the number of codes within the interrupt routine (The program memory, PAGE#0, is full).

From the interrupt routine, it is easy to PCLATH to an external module which resides in program memory PAGE#3 and go back to the return tag in the interrupt routine using a goto jump, so we will use this method.


Callout of the interrupt routines
<Implementation example>

int_tmr1        ; Timeout check of DHCP request completion
bcf PIR1,TMR1IF
BSF STATUS,RP0
MOVF dhcp_done,0
andlw 7FH
btfss STATUS,Z
DECF dhcp_done,1
bcf STATUS,RP0
;;++++++++++++++++++++++++++++++++++++++
movlw HIGH (test4) ; test4 >> 8 Get the PAGE position
movwf PCLATH ; Set the PAGE control register
goto test4 ; Jump to the test4 module
int_tmr9 ; →Back to here from test4
clrf PCLATH ; Set the PAGE control register
;;++++++++++++++++++++++++++++++++++++++
int_tmr2 ; Socket timeout check

The above example is executed in the following order from the interrupt handling routine;

int_tmr1→jump to test4→(count processing)→back to int_tmr9→int_tmr2

●State of opening and closing of the door (RB0) -> Output to LED#4 (RB4) with positive logic

Refer to Experiment 4 (test2 module) in the tutorial (Execise 0).

【Experiment 4】RB0 (door sensor)→Output to RB4 (LED#4); Creation of the test2 module

 

●Count module of the number of times of opening the door: test4

It should be called out at 13.1-msec intervals from the interrupt handling routine.
For the basic implementation, it is only necessary to base Experiment 4 (test2 module) in the tutorial.

Because of the callout from the interrupt routine, it is dangerous to save the STATUS and W register's values by wk2 and wk1 as the tutorial (Exercise 0) described.

・Do not conserve the STATUS value.
・For the temporary save of the W register's value, divert the tmp variable for the LCD display (probably OK).


When basing the test2 module created before, the outline of processing of timing in adding one to the counter variable is as follows:

・Is the door open? (Check of RB0)
・Check "the previous state of the door", that is, the bit of RB4.
・If the door was closed in the previous state,
 the door changed from the opening state to the closing state in this timing.
Add one to the counter.

In the test4 module
<Implementation form>

;----------------------------------------------------------------------
; test4module [ver0.6]
;----------------------------------------------------------------------
org 1E00h
test4
; Entry point from the interrupt routine
clrF STATUS ; Clear the STATUS register.
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
................
※Implement as described above.
State of opening and closing of the door (RB0) →LED#4 (RB4)
Detection of the change from the opening state to the closing state of door Add one to the counter.
................
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Return to the interrupt routine
movlw HIGH (int_tmr9) ; int_tmr9 >> 8 Get the PAGE position
movwf PCLATH ; Set the PAGE control register
goto int_tmr9 ; Jump to int_tmr9
;----------------------------------------------------------------------

●Display of the counter to the web control screen

Display the value (8bit) of the counter variable counted to the web control screen.
The place to display it is Status:Replace the display of packet number.
(Divert the control code '$9'.)

Change the callout from the control code '$9' in ctrl_code1.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<Before changing>
goto put_send_packet ; 9:The number of transmitted packets/p>

<After changing>
goto put_counter ; 9:The display of experimental counter [ver0.6]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


The processing of the put_counter module for the display of experimental counter is as follows:

<Example of the processing procedure>

put_counter     ; For the display of experimental counter
Switch to BANK#3
Wreg <-- counter
Switch to BANK#0
val <-- Wreg
Callout of the put_decimal module (8-bit decimal number/ the argument is val.)

●Practical example

Immediately after power-on → After the door is opened and closed once → .... → Ten times

Depending on the state of the door, LED#4(RB4) is lighted or unlighted.

●Submission of Exercise 1

【Exercise 1】
Create each module for the realization of the function of counting the number of times of opening the door and the function of displaying it. Using the changes of the state of door sensor, the web control screen and the counter value, confirm to work as required.

(1) Create each module for the realization of the function of counting the number of times of opening the door and the function of displaying it.

(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.6.
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 v1206e1.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 v1206e1.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 1 (Exercise number: picnic-exp01).

(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 v1206e1a.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] Because the 8-bit counter is used, the number of times returns to 0 because the variable overflows when the door is opened and closed 256 times. What should be done to avoid that?

[2] If a 16-bit counter is used as the number of times of opening and closing the door, the variable will not overflow in actual use. Which part should be changed to do that?

[3] If a door switch with extremely long chattering time (about three times as long as TMR1 timer interrupt interval) is entered, there is a possibility that the count-up is mistaken more than once in a single operation of opening and closing. What should be improved to avoid that?

[4] The 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.