;------------------------------------------------------------------------------- ; FILE: Serialkeypad ; AUTH: Ed's Projects ; DATE: 05/09/2016 ; DESC: Asynchronous and SPI Serial Keypad ;------------------------------------------------------------------------------- LIST P=16F690, R=DEC ; All numbers Decimal format unless otherwise stated. include ; Define configurations, registers, etc. __config _FCMEN_OFF & _IESO_OFF & _BOREN_ON & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDTE_OFF & _INTRC_OSC_NOCLKOUT ;------------------------------------------------------------------------------- ;----------------------------- Port Definitions ------------------------------ ;------------------------------------------------------------------------------- ; PortA - Output - RA0 - Pin 19 - Column 1 Keypad ; PortA - Output - RA1 - Pin 18 - Column 2 Keypad ; PortA - Output - RA2 - Pin 17 - Column 3 Keypad ; PortC - Output - RC0 - Pin 16 - Column 4 Keypad ; PortA - Input - RA3 - Pin 4 - Row 1 Keypad ; PortC - Input - RC5 - Pin 5 - Row 2 Keypad ; PortC - Input - RC4 - Pin 6 - Row 3 Keypad ; PortC - Input - RC3 - Pin 7 - Row 4 Keypad ; PortC - Input - RC1 - Pin 15 - Baud control 0 ; PortC - Input - RC2 - Pin 14 - Baud control 1 ; PortB - Input - RB4 - Pin 13 - SPI In ; PortB - Input - RB6 - Pin 11 - SPI Clock ; PortC - Input - RC6 - Pin 8 - SPI Chip Select ; PortC - Output - RC7 - Pin 9 - SPI Out ; PortB - Output - RB7 - Pin 10 - Asynchronous Serial Transmit ; PortB - Input - RB5 - Pin 12 - Asynchronous Serial Receive ; Baud - 00 = 9600 ; Baud - 01 = 19200 ; Baud - 10 = 57600 ; Baud - 11 = 115200 ;------------------------------------------------------------------------------- ;-------------------------------- Variables ---------------------------------- ;------------------------------------------------------------------------------- cblock h'20' temp TX ; Serial to transmit temp2 temp3 endc ;------------------------------------------------------------------------------- ;----------------------------- Define Symbols -------------------------------- ;------------------------------------------------------------------------------- #define R1 PORTA, 3 #define R2 PORTC, 5 #define R3 PORTC, 4 #define R4 PORTC, 3 #define C1 PORTA, 0 #define C2 PORTA, 1 #define C3 PORTA, 2 #define C4 PORTC, 0 #define BD0 PORTC, 1 #define BD1 PORTC, 2 ;------------------------------------------------------------------------------- ;------------------------------- Program Code -------------------------------- ;------------------------------------------------------------------------------- ORG 0 ;------------------------------------------------------------------------------- ;-------------------------- Initialisation of Ports -------------------------- ;------------------------------------------------------------------------------- Init bsf STATUS,RP0 bcf STATUS,RP1 ; Bank 1 Selected movlw b'00001000' movwf TRISA movlw b'11110000' movwf TRISB movlw b'01111110' movwf TRISC bcf STATUS,RP0 bsf STATUS,RP1 ; Bank 2 Selected clrf VRCON ; Set Vref to off clrf CM1CON0 ; Turn off comparator 1 clrf CM2CON0 ; Turn off comparator 2 clrf ANSEL ; Set analogue inputs to digital clrf ANSELH ; Set analogue inputs to digital bsf STATUS,RP0 ; Bank 3 Selected clrf SRCON ; Disable SR latch bcf STATUS,RP0 bcf STATUS,RP1 ; Bank 0 Selected clrf PORTA clrf PORTB clrf PORTC clrf TX ;------------------------------------------------------------------------------- ;--------------------- Asynchronous Serial Initialisation -------------------- ;------------------------------------------------------------------------------- bsf STATUS, RP0 ; Bank 1 selected bsf BAUDCTL, BRG16 bsf TXSTA, BRGH ; Set high baud rate clrf SPBRGH bcf STATUS, RP0 ; Bank 0 Selected btfsc BD1 goto baud1 btfsc BD0 goto baud3 movlw d'103' ; Baud 9600 goto baudset baud3 movlw d'51' ; Baud 19200 goto baudset baud1 btfsc BD0 goto baud2 movlw d'16' ; Baud 57600 goto baudset baud2 movlw d'8' ; Baud 115200 baudset bsf STATUS, RP0 ; Bank 1 selected movwf SPBRG bcf TXSTA, SYNC ; Set to asynchronous mode bsf TXSTA, TXEN ; Enable Transmit bcf STATUS, RP0 ; Bank 0 Selected bsf RCSTA, SPEN ; Enable Serial Port bsf RCSTA, CREN ; Turns on receive movf RCREG, w movf RCREG, w movf RCREG, w ;------------------------------------------------------------------------------- ;----------------------------- SPI Initialisation ---------------------------- ;------------------------------------------------------------------------------- movlw b'00100100' ; Serial port enable, slave mode movwf SSPCON ; Clock polarity idle high bsf STATUS, RP0 ; Bank 1 selected bcf SSPSTAT, SMP bsf SSPSTAT, CKE bcf STATUS, RP0 ; Bank 0 selected; movf SSPBUF, w movlw d'0' movwf SSPBUF ; Ensure contents are zero ;------------------------------------------------------------------------------- ;-------------------------------- Initialisaton ------------------------------ ;------------------------------------------------------------------------------- movlw d'255' movwf temp initloop goto $+1 goto $+1 goto $+1 goto $+1 decfsz temp, f goto initloop goto Keyscan ;------------------------------------------------------------------------------- ;------------------------------ Transmit Check ------------------------------- ;------------------------------------------------------------------------------- Transmit bsf STATUS, RP0 ; Bank 1 btfss TXSTA, TRMT goto $-1 bcf STATUS, RP0 ; Bank 0 movwf TXREG return ;------------------------------------------------------------------------------- ;---------------------------------- Keypad ----------------------------------- ;------------------------------------------------------------------------------- Keyscan call Check ; Check SPI buffer bcf C1 bcf C2 bcf C3 ; Check if a key has been pressed bcf C4 btfss R1 goto scanprocess btfss R2 goto scanprocess btfss R3 goto scanprocess btfss R4 goto scanprocess goto Keyscan Keynot call Check ; Check SPI buffer bcf C1 bcf C2 bcf C3 ; Check when key is depressed bcf C4 btfss R1 goto Keynot btfss R2 goto Keynot btfss R3 goto Keynot btfss R4 goto Keynot goto debounce debounce movlw d'20' ; 20ms debounce movwf temp3 loop movlw d'100' movwf temp2 loop2 goto $+1 ;2us goto $+1 ;2us goto $+1 ;2us nop ;1us decfsz temp2, f ;1us goto loop2 ;2us decfsz temp3, f goto loop ;Multiples of 1ms goto Keyscan scanprocess bcf C1 ; Check which key has been pressed bsf C2 bsf C3 bsf C4 movlw d'1' call Rscan bsf C1 movlw d'2' bcf C2 call Rscan bsf C2 movlw d'3' bcf C3 call Rscan bsf C3 movlw d'4' bcf C4 call Rscan bsf C4 goto Keynot Rscan btfss R1 ; Which row? goto R1set btfss R2 goto R2set btfss R3 goto R3set btfss R4 goto R4set return R1set addlw d'0' goto output R2set addlw d'4' goto output R3set addlw d'8' goto output R4set addlw d'12' goto output ;------------------------------------------------------------------------------- ;------------------------------- Keypad Output ------------------------------- ;------------------------------------------------------------------------------- output movwf temp call getchr ; Return character as ASCII movwf TX call Transmit ; Send Asynchronous return getchr movlw LOW table ; Get low 8 bits of table address addwf temp, w ; Check to see if a carry will occur movlw HIGH table ; Get high 5 bits of address btfsc STATUS, C ; Has page crossed? addlw 1 ; Yes then increment PCLATH - PCH movwf PCLATH ; Load high address into PCLATH - PCH movlw LOW table ; Get low 8 bits of table address again addwf temp, w ; Load original PCL offset value movwf PCL ; Jump into table table nop retlw "1" retlw "2" retlw "3" retlw "A" retlw "4" retlw "5" retlw "6" retlw "B" retlw "7" retlw "8" retlw "9" retlw "C" retlw "*" retlw "0" retlw "#" retlw "D" ;------------------------------------------------------------------------------- ;-------------------------------- Check buffer ------------------------------- ;------------------------------------------------------------------------------- Check bsf STATUS, RP0 ; Bank 1 selected btfss SSPSTAT, BF goto no ; Check if sent bcf STATUS, RP0 ; Bank 0 selected goto clearbuff no bcf STATUS, RP0 ; Bank 0 selected return clearbuff movf SSPBUF, w ; Clear buffer flag movf TX, w movwf SSPBUF clrf TX return end