Você está na página 1de 10

EMBEDDED SYSTEM DESIGN

2.2

System Software Design 2.2.1 System Software Flowchart

EMBEDDED SYSTEM DESIGN

Start Declaration of file register, user register, constants & variables. Clear data buffers for displaying & Writing data If input bit 0 of port A is cleared (pressed) Yes No Move character LOCK to chr0, chr0+1,chr0+2, chr03(user register) Display one character from chr0+chrcntr No If chrcntr = 0 Yes Scan key pressed from key group 1 (Upper bit of key pressed will replace lower key pressed) If key bit 7 of port b is cleared (pressed) Yes Move character OPEN to chr0, chr0+1,chr0+2, chr03(user register) Display one character from chr0+chrcntr 1 If chrcntr = 0 2

Turn off the magnetic lock for 5 seconds and then will be turn on back. No

EMBEDDED SYSTEM DESIGN

If key group 1 pressed? Yes Save key pressed to key buffer

Scan key pressed from key group 2 (Upper bit of key pressed will replace lower key pressed)

No

If key group 2 pressed? Yes Save key pressed to key buffer

If key pressed more than one? Yes Clear variable of keyprs (used for check whether the key pressed has been processed) No

No If total key pressed is 4 Yes If diset is equal to one (security mode) No Set port a as input If bit 0 of port a is cleared (pressed) Yes Update data of 4 characters to datbf1 (using indirect addressing)

No

If datbf1 equal to eedata? No Yes If datbf1+1 equal to eedata+1?

EMBEDDED SYSTEM DESIGN

Yes Move character OPEN to chr0, chr0+1,chr0+2, chr03(user register) Display one character from chr0+chrcntr No If chrcntr = 0 Yes Turn off the magnetic lock for 5 seconds and then will be turn on back. Move character err1 to chr0, chr0+1,chr0+2, chr03(user register) if first try Move character err1 to chr0, chr0+1,chr0+2, chr03(user register) if second try Move character err1 to chr0, chr0+1,chr0+2, chr03(user register) if third try Move character fail to chr0, chr0+1,chr0+2, chr03(user register) if more than three times try(security mode)

Display one character from chr0+chrcntr No If chrcntr = 0 Yes If more than three times enter password Yes Move character secu to chr0, chr0+1,chr0+2, chr03(user register) (SECURITY Mode) Display one character from chr0+chrcntr No If chrcntr = 0 Yes Disable set password and write admin password into eeprom data 1 Convert the number format to display format in seven segment display ( which data of datbf1, datbf1+1, datbf1+2 & datbf1+3 will be used to determine display format in seven segment display which saved in chr0, chr0+1, chr0+2 & chr0+3) 3

EMBEDDED SYSTEM DESIGN

Figure A:

General Program Flow of the Overall System

EMBEDDED SYSTEM DESIGN

2.2.2

Program Description

The declarations at the start of this program consist of three parts. Firstly, Special Function Registers are declared so as to use the peripheral feature of CPU including read and write in EEPROM data. Secondly, General Purpose Registers are declared to act as variable or counter of key-press, password-try, number-character, displaypassword and user register used as data container in indirect addressing and number of loops to be processed in a routine. Thirdly, Symbols are declared to acts as constant which this will not affect the value in the address in the memory map. The program begins by resetting a user flag which is used to indicate if a key was pressed. Then, the data buffers (Datbf1) which are used store password that entered by user from keypad are cleared. The program will be initializing with one Startup-Password in EEPROM memory which used to check the validity of password entered by user. The main section of code commences with the initialization of port B as output, and lines zero to three of port A also as output. CHRCNTR is then set to four, as this is the number of seven segment displays we are using. Besides, port a is initialize for a instant as input to check whether the bit 0 of port A is low.(key-switch has been turned). If the bit 0 of port A is low, this will make the program to display the character that entered by user. Otherwise, if bit 0 of port A is high, the program will display the status lock on seven segment display. Within MAIN1, the routines DISPLAY and DELAY are each called four times within a loop using the value in CHRCNTR as a counter. Following each execution of the DISPLAY and DELAY routines CHRCNTR is decremented and if not zero program control is transferred back to MAIN1. Thus each of the four displays are individually turned on with their correct data output to them in DISPLAY. DELAY ensures that each remains on long enough to be visible, however they are still each turned on and off so fast as to appear to be all lit at once. After process for display, the routine SCANKEY is called so as to scan the fifteen keys of the keypad. KEYCNTR is used within SCANKEY to count the number of keys found to be pressed. We only want to update our data based on the pressing of a single key, and so if more than one key was pressed we will ignore and try again for a valid key press. To check that only one key had been pressed KEYCNTR is Exclusive or operation with one. If the result of this exclusive or operation was found to be false then program flow is transferred to MAIN2 where the flag set after a new valid keypress is cleared. If only one key had been pressed, and that was found to be a new keypress, then the program continues by calling the routines UPDATE and NUM2CHIL The KEYPRS bit of FLAG is also set so that the program may again check against this next time. Then, the program eventually ends up at MAIN3 where control is passed back to MAIN so as to repeat the whole process once more.

EMBEDDED SYSTEM DESIGN

Indirect Addressing Indirect addressing is used within the routines of DISPLAY, NUM2CHR and UPDATE in the program. There are two important file registers which are firstly, IND used to access the contents of the memory location pointed to by the value in the FSR register and secondly, FSR to store the address of the memory location. Num-to-Char Routine The routine begins by copying DATBF1 (ODh) into RO, and CHRO (10h) into R1. The value three is also placed in R2 for use as a counter. In NUM2CHR1, RO is placed in the FSR register. The contents of DATBF1 are then read by way of INDF. As DATBF1 and the other two data buffers contain two numbers each, we need to separate the upper and lower nibbles of these registers. AND operation is performed between the data with OFh (00001111B) to first mask off the upper nibble. We are now left with the W register containing a number between zero and OR. Calling TABLE results in a return with Working register containing the corresponding seven segment display data. This is then stored temporarily in R3. R1 is copied into the FSR register so that CHRO may be accessed via INDF. This is used to place die seven segment display data held in R3 into CHRO. Then, the upper nibble of DAIBF1 that we are interested in this time we use 'SWAPF RTDFW which exchanges the upper and lower nibblcs of the data and stores the new byte in the W register. This byte is then ANDed with OFh to mask off the unwanted upper nibble. TABLE is then called so that the data returned maybe stored in CHRO+1. R1 was incremented from 10h for the location of CHRO to 11h for CHRO+1 and this is placed in the FSR register so that accessing INDF win access CHRO+1. The new display data (pattern) from the call to TABLE is now transferred from R3 to CHRO+1 by indirect addressing. Now RI is again incremented so as to now contain 12h for CHRO+2, and RO is also incremented to OEh for DATBF1 + 1. The above procedure is, then repeated so as to obtain the seven segment display data derived from the two nibbles of DATBF1+1 and store this in CH-RO+3 and CHRO+4. Finally R1 and RO are again incremented and the process repeated once more to obtain CHR0+5 and CHRO+6 from DATBF1 +2. Now, program control may return to the in main loops. Display Routine The routine DISPLAY, which is called from within MAIN1, outputs the desired data to the selected seven segment display. Each time DISPLAY is called, just one of the four displays is updated; which of the four is being determined by the value in CHRCNT. Then, the common anodes of the displays are turned off by setting line three of port A. The value in CHRCNTR is then decremented with the result placed in RO by way of the working register. The value of CHRCNTR which is still in working register is then

EMBEDDED SYSTEM DESIGN

carried out within a or operation with 00001000b and output on port A. This selects only the required display's common anodes. The data to be output to the cathodes of the displays is stored in the four locations 10h, 11h, 121h and 13h. CHRO was set to 10h in the program equates to mark the first of these locations. DISPLAY needs to take the contents of the one corresponding with the seven segment display it is currently outputting to. It does this using indirect addressing by adding the value in RO (derived from CHRCNTR) to CHRO. This value is then placed in FSR, the indirect address pointer. Thus, if CHRCNTR contains four, RO will have three as its contents. This will be added to the 1Oh in CHRO with the resulting 13h placed in the FSR register. Now, in reading the contents of address 00h, we are actually reading the contents of address 15h. That is, in reading INDF our program is actually reading the contents of the register whose address is held in FSR. The eight bits obtained from IND via indirect addressing it then output on port B to display desired character. Bit 3 of port A is cleared to enabled the display of character in seven segment display. Scankey Routine It starts with first clears the general purpose registers KEYBUF, KEYCNTR, and R2 and sets port B as input. RO is set to two to scan twice due to two key groups. By setting 110 (RA2, RA1, RA0) in Port A is then used to select the first group of eight keypads to be scanned. SCANKEY3 places eight in R1 and reads in from port B. This input is then placed in R3. A bit test on bit 7 of port B is performed to check whether the push button for Exit (exit from the building/house/room for those user has successful entered the valid password). If the bit 7 of port b is low, the bit 3 of port is set to disable the magnetic lock. In SCANKEY1, the contents of R3 are rotated tight one bit. This will be performed eight times using R1 as the counter. Then, whether a key his been pressed by the state of the carry flag after the rotation of R3 is determined. Each time R3 is rotated tight, R2 is incremented. It is from R2 that the value of the key pressed is obtained. For example, if keypad 3 is pressed on the keypad line 3 of PORTB will be low. This will be detected by checking the carry flag the fourth time R3 is rotated right. R2 contains three which corresponds to the key pressed and this is then placed in KEYBUF. After the first groups of eight keypads have all been tested, line zero of port A is set which allows for the second bank of switches to be checked. R2 contains nine and continues to be increment for each of the second keys pressed. If two keypads had been pressed at the same time, then the value first placed in KEYBUF would be replaced by the higher value derived from the higher of the two key values. However, KEYCNTR is incremented a second time. With it, the main routine of the program will be able to detect this and act accordingly by ignoring this data.

EMBEDDED SYSTEM DESIGN

Update Routine The routine UPDATE is called from MAIN after a new keypress was detected as a result of SCANKEY. UPDATE takes the value from KEYBUF and updates the two data buffers DATBF1 and DATBF1+1. DATBF1 (register ODh) and DATBF1+1 (register OEh) are used to hold the numbers which will be converted to the seven segment display data by the routine NUM2CHR, As these numbers are going to be between zero and D9 (hexadecimal), two may be stored within one byte. Thus DATBF1, DATBF1 +1 are used to hold four separate numbers between zero and OFh. When the program is first run these two registers are all cleared so the. Four numbers held by them will be all zeros. When the routine NUM2CHR is carried out, the seven segment display data corresponding to zero is derived from the two DATBF1 bytes if the keyswitch is not turned or the bit 0 of port A is not low. After a valid keypress has been detected from the routine SCANKEY, a number between zero and 0dh which corresponds to the keypads pressed is in the register named KEYBUR We now need to take this number and place it in the lowest nibble of DATBFl. However the number already held in the low nibble of DATBF1 needs to be moved to the high nibble of DATBF1. Also, the value already in the high nibble of DATBF1 now needs to be moved to the low nibble of DATBF1+1. This is done by making use of the rotate left through carry instruction. Then, the two nibbles within KEYBUF are exchanged using the SWAPF instruction. For example, if keypad 8 had been pressed so that KEYBUF contained 08h, KEYBUF will now contain 80h. Looking at this in binary it may be seen that KEYBUF contained 00001000b following the pressing of the keypads, but this then becomes 10000000b after SWAPF KEYBUF. Then place the 1000b held in KEYBUF into the lowest of the four nibbles stored in the three DATBF1 registers, and move these registers original contents along by four bits. When DATBF1 is rotated left one bit, besides its upper bit also being rotated out into the carry, the original carry is towed in to its lowest bit What has happened is that the one which was the upper bit of KEYBUF has now become the lowest bit of DA'MF1. ne upper bit of DATBF1 goes on to become the lower bit of DATBF1 + 1, and the upper bit of DATBF1+1 becomes the lower bit of DATBF1+2. The upper bit of DATBF1+2 is rotated out into the carry bit of STATUS but we are not interested in this and so leave it to be lost when the carry bit is changed by the second rotate left on KEYBUF. Then, R1 is used as a counter to ensure that the two registers DATBF1 and DATBF1+1 are each rotated one bit. RO, which was initialized as four upon commencement of UPDATE is used as a counter to ensure that the two DATBF1 registers along with KEYBUF have their contents rotated left four times. Thus, as a result, the nibbles in DATBF1 and DATBF1+1 are all rotated moved along one bit at a time, with the upper nibble of KEYBUF now in the lower nibble of DATBF1. The upper four bits that were in DATBF1+1 have all been lost. Then, the value of TOTLKY is test to determine whether it is sufficient numbers of password entered to be processed. If TOTLKY does equal four, then the comparison

EMBEDDED SYSTEM DESIGN

data in DATABF with the valid password in EEPROM memory (start with 0h) will be performed after this. Before that, there is a test to determine whether the availability of setting new password whereby if user has entered the error password more than two times, the features of setting new pass is disabled and only a specific password could unlock it. This is considered as security / PUK mode. This is performed by using the variable of DISET which if DISET equal to one, it will go into security / PUK mode and disable setting new password by skipping the test on bit 0 on port a. A test on line zero of port A is then carried out to establish whether or not the keyswitch is being turned. If it is activated, so that line zero of port A is low, then UPDATE4 is called where the ERPROM write is initiated. If keyswitch was not turned then the program continues by reading the contents already in the EEPROM memory so that the new code entered may be compared with it. First, the EEPROM memory address in EEADR is cleared because the password is stored at 0h address. Then, routine READ is called. READ simply activates the read by setting the RD bit 0 in EECON1. The value obtained from the EEDATA register and placed in die W register where it is exclusive or with DATBF1. In other words the two 4-bit numbers represented by two nibbles of DATBF1 and compared with the two corresponding 4-bit numbers which were placed in EEPROM when the code was stored. If a match was not found then the routine BACK is called. If the numbers matched then the next byte is obtained from EEPROM and compared with DATBF1 +1. . If a match was not found then the routine BACK is called. If these also matched then the correct code has been entered and so port A is made output in that the bit 3 in port A is set to disable the magnetic lock. A delay of 5 seconds is then called before the magnetic lock is again activated. BACK routine is called when incorrect of password entered by user. The numbers of error is count using the variable of TRYKEY. If value of TRYKEY is more than two times, this will lead the system go into security mode which the features setting new password is disabled by set the value of DISET. Besides, when any error of entering password, a error message will be displayed on the seven segment display. The error message will indicate the numbers of try of entering password. UPDATE5 is called when a new security code is entered and keyswitch is turned. This routine will store the new code in EEPROM memory. EEADR is first cleared and the value in DATBF1 placed into EEDATA. The routine WRITE is then called to activate the writing of this byte to EEPROM. T11= routine enables a write by setting the WREN bit in EECON1. It then activates the write by, in sequence, storing 55h 'in the EECON2 register, storing AAh in the EECON2register, and finally setting the WR bit of EECON1. At WRITE, the EEIF flag in EECON1 is repeatedly checked until it is found to be set thus indicating that the write operation has been completed. When complete, the EEIF flag is reset and the WREN bit in EECO-N11 cleared. The security code has now been placed in EEPROM memory.

Você também pode gostar