BS818A-2 Serial Interface Application Description BS818A-2 Serial Interface Application Description D/N: HA0348E Introduction This text shows how the BS818A-2 serial interface is used. Description Transfer Format (Data & Clock) Data Transfer Timing Data Format Start bit:When a key changes state, the Data pin outputs a low level to wake up the master, which can then read the key status. Bit 0 : Key1 sate – "0" = touched, "1" = not touched Bit 1 : Key2 sate – "0" = touched, "1" = not touched Bit 2 : Key3 sate – "0" = touched, "1" = not touched Bit 3 : Key4 sate – "0" = touched, "1" = not touched Bit 4 : Key5 sate – "0" = touched, "1" = not touched Bit 5 : Key6 sate – "0" = touched, "1" = not touched Bit 6 : Key7 sate – "0" = touched, "1" = not touched Bit 7 : Key8 sate – "0" = touched, "1" = not touched Bit 11~8 : Checksum – the total number of "0" is used to indicate how many keys have been touched. Bit 15~12 : Stop bits, always "1010B". 1 BS818A-2 Serial Interface Application Description Bit Function 15 ~ 12 11 ~ 8 7~0 Stop Bit Check Sum 1010 1000 1010 0111 Descriptions Key4 ~ Key1 State 0: Touched / 1: Nnot Touched 00000000 Check Sum = 1000, 8 keys are touched. 00000001, 00000010, 00000100, 00001000 Check Sum = 0111, 7 keys are touched. 00010000, 00100000, 01000000 or 10000000 00000011, 00000110, 00011000, 00110000 1010 0110 Check Sum = 0110, 6 keys are touched. 11000000 or 10000001 … 00000111, 00001110, 11100000, 10000011, 1010 0101 Check Sum = 0101, 5 keys is touched. 10000110, 10001100 or 10011000… 00001111, 00011110, 00111100, 01111000, Data Stream 1010 0100 Check Sum = 0100, 4 keys is touched. 11110000 or 10000111 … 00011111, 00111110, 01111100, 11111000 1010 0011 Check Sum = 0011, 3 keys are touched. 100011111 or 110001111… 00111111, 01111110, 11111100, 1010 0010 Check Sum = 0010, 2 keys are touched. 01111110, 01111101or 01111011… 11111110, 11111101, 11111011, 11110111 1010 0001 Check Sum = 0001, 1 key is touched. 11101111, 11011111, 10111111 or 01111111 1010 0000 11111111 Check Sum = 0000, No key is touched. Serial Data Stream Format for 8-key 2 BS818A-2 Serial Interface Application Description Program Flowchart START Initial Clock Pin & Data Pin as Input Set Clock Pin Low Delay 30us Set Clock Pin as Input Read Data Pin State Delay 30us N Get 16bits ? Y Error Function N Stop bit & Check Sum Correct ? Y END 3 BS818A-2 Serial Interface Application Description Program Example SET Clock_C SET Data_C CLR Receive_BufferH CLR Receive_BufferL MOV A,16 MOV Receive_Count, A Receive_Loop: CLR Clock CLR Clock_C MOV A,20 SDZ Acc JMP $-1 ;----Read bit SET Clock_C SET C SNZ Data CLR C RRC Receive_BufferH RRC Receive_BufferL MOV A,20 SDZ Acc JMP $-1 SDZ Receive_Count JMP Receive_loop ;set Clock pin as input ;set Data pin as input ;clear the Data Receive Buffer ; receive 16 bits of data ;Clock = low ;-----Delay ;delay 30us ;Clock = high ;read data through the data pin ; ; ;rotate data to the Receive Buffer ;rotate data to the Receive Buffer ;-----Delay ;delay 30us ;-----Count down Receive_Count ;Receive 16 bits of data? ;------Verify Data MOV XOR AND SNZ JMP A,Receive_BufferH A,10100000B A,11110000B Z Receive_error MOV A,Receive_bufferL MOV Data_Buffer,A CLR Key_pressed_Amount MOV RRC SNZ INC SDZ JMP A,8 Data_Buffer C Key_Pressed_Amount ACC $-4 MOV A Receive_buffer XOR A,Key_Press_Amount AND A,00001111B SZ Z JMP Receive_error RET Receive_error: ;check stop bit = 1010 ? ;--; ; ; ;----; ;total number of touched keys ; ; ; ; ;---- -Verify Check Sum ; ;data correct ;--do error function RET 4 BS818A-2 Serial Interface Application Description Additional Information Both the BS818A-2 Data pin and Clock pin have an integrated pull high resistor. When the touch key device detects that a touch key has been pressed, it will output a low level on the Data pin which can be used to wake up the master MCU. The touch key status data can be read by using a polling method or waiting for the Data pin to change to a low level. When a low clock signal is received, the key status data is prepared by the touch key device. When the clock signal changes to a high level, the master reads the touch key status data from the Data pin. Every 8 clock periods form a data group. After reading a data group, it is suggested to use the checksum bits to determine the data integrity. If there are any errors in reading the data, it is necessary to wait for about 6ms and then read the data again. 5