; KeyScan.spt - Key scan function. ********** Start of test code *********************** *********** You can safely delete the test code once ***************** *********** you are familiar with the function ***************** OBLCD_Cls STScale 1 LaunchTask KPD_Start LaunchTask Test RunTasksForever Test: YieldTask GoSub KPD_GET_Key GoIfF Test OBLCD_SetCur 0,0 OBLCD_HexDispX GoTo Test ********** End of test code *************************** ;======= Key pad =================== ;Short name KPD_ ;Entry points: ; Start ;The task to launch ; GET_Key ;Get any result. ; ;Returns X = T if there is a key stroke ; ; Y=Key code ; ;A key is returned with X=T only once. ;Size: 207 bytes Flash. 3 bytes and 1 bit of RAM ;The code returned is a function of row and column, generated by a ;table in NVEM. ;A 4x4 keypad is connected to 4 outputs (rows) and 4 inputs (columns) ;Note that whether outputs are rows or columns is an arbitrary hardware design ;decision. We have made one choice in this program. "Unscrambling" is a matter ;of updating the translation table oKPD_Rows oEQU 0 ;Base address of row outputs iKPD_Cols iEQU 0 ;Base address of column inputs KPD_Row: defByte KPD_Col: defBYTE KPD_Temp: defBYTE sKPD_Flag: defSEM KPD_Start: KPD_Set0: LoadX 'F Push OutputM oKPD_Rows ;Turn on all row drives KPD_Set0a: MarkTime KPD_0: ;Wait for all inputs off for at least 50mS YieldTask InputFM iKPD_Cols LoadX 'F AndM GoIfNZ KPD_Set0a LoopIfTiming 5,KPD_0 KPD_Set1: KPD_1: ;Wait for any input YieldTask InputFM iKPD_Cols LoadX 'F AndM Push GoIfZ KPD_1 ;Have at least one input. Encode 4-bit pattern into a bit number GoSub KPD_Enc4 Push GoIfXEQ 'FF,KPD_Set0 ;Not an acceptable pattern Store KPD_Col KPD_2: ;Wait 20mS then check to see if input is the same Pause 5 ;Wait 20mS InputFM iKPD_Cols LoadX 'F AndM GoSub KPD_Enc4 Recall KPD_Col Compare GoIfNZ KPD_Set0 KPD_Set3: SetMem KPD_Row,%1000 SetMem KPD_Temp,0 KPD_3: Recall KPD_Row ;Bit pattern LoadX 'F OutputM oKPD_Rows YieldTask ;Allow a bit of time to settle (not critical) InputFM iKPD_Cols LoadX 'F AndM GoSub KPD_Enc4 Recall KPD_Col Compare GoIfNZ KPD_3a ;g/no input on this row Recall KPD_Temp Recall KPD_Row ORM Store KPD_Temp ;build pattern of row bit that turn on the colum KPD_3a Recall KPD_Row ;Bit pattern Push GoIfXEQ 1,KPD_Set4 ;Done all rows. Lets see if just one responded RORM Store KPD_Row GoTo KPD_3 ;Have identified a key. Temp is the row bit pattern. Col is the bit number KPD_Set4: Recall KPD_Temp ;Should have just one bit set GoSub KPD_Enc4 ;Convert to a bit number Push GoIfXEQ 255,KPD_Set0 ;255=more than 1 bit set ROLM ROLM Recall KPD_Col ORM ;Now have 4-bit row-column index NVSetPtr KPD_KeyTable GoSub KPD_TableLook Store KPD_Row SetS sKPD_Flag KPD_4: WaitForSF sKPD_Flag KBeep GoTo KPD_Set0 ;Encode 4-bit pattern into a 2-bit number. Return 'FF if >1 bit KPD_Enc4: NVSetPtr KPD_Enc4_table KPD_TableLook: LoadX 'F AndM ;Defensive!!! NVSetPage 0 NVPopRecNum NVSetRecLen 1 NVPushByte 0 Return KPD_GET_Key: Recall KPD_Row RecallS sKPD_Flag ClrS sKPD_Flag Return ******************************************************************************************* **** NVEM0 segment. YOU MUST MANUALLY CUT AND PASTE THIS ON THE *END* OF YOUR PROGRAM ***** **** As you edit make sure your program has only one NVEM0 directive, and that all ***** **** NVEM page zero stuff is after that directive ***** ******************************************************************************************* NVEM0 ******* Only one NVEM0 per program! ******* ;Table to translate from 4-bit pattern to bit number. ;Only patterns with 1 bit set get encoded. Anything else returns FF KPD_Enc4_table: ; 0 1 2 3 4 5 6 7 8 9 a b c d e f NV0Byte 'FF,'00,'01,'FF,'02,'FF,'FF,'FF,'03,'FF,'FF,'FF,'FF,'FF,'FF,'FF ;Table to translate from Row/Col to key code KPD_KeyTable: ; RC RC RC RC RC RC RC RC RC RC RC RC RC RC RC RC ; 00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33 NV0Byte '01,'02,'03,'80,'04,'05,'06,'81,'07,'08,'09,'82,'83,'00,'84,'85 ;TR0601 1 2 3 / 4 5 6 X 7 8 9 - C 0 . + ;Test table, produces a hex number where the first digit is Row and the second is Column ****** NV0Byte '00,'01,02,'03,'10,'11,'12,'13,'20,'21,'22,'23,'30,'31,'32,'33