;------------------------------------------------------------------------------- ; FILE: EEPROM ; AUTH: Ed's Projects ; DATE: 15/02/2017 ; DESC: Data taken from keypad, written to EEPROM, read from EEPROM, displayed on LCD ; ; PortD LCD data bus ; PortC, 3 enable, 2 RW, 1 RS ; PortB 7-4 keypad rows, 3-0 keypad columns ;------------------------------------------------------------------------------- LIST P=18F4520, R=DEC ; All numbers Decimal format unless otherwise stated. include ; Define configurations, registers, etc. CONFIG OSC = INTIO67 ; 8 MHz internal oscillator CONFIG FCMEN = OFF ; Failsafe clock monitor off CONFIG IESO = OFF ; Internal / External oscillator switchover off CONFIG PWRT = ON ; Power up timer on CONFIG BOREN = OFF ; Brown out reset off CONFIG WDT = OFF ; Watchdog timer off CONFIG MCLRE = OFF ; Master reset pin off CONFIG LPT1OSC = OFF ; Low power timer 1 off CONFIG PBADEN = OFF ; PortB 4 to 0 digital on reset CONFIG LVP = OFF ; Low voltage programming off CONFIG XINST = OFF ; Extended instruction set enabled ;------------------------------------------------------------------------------- ;-------------------------------- Variables ---------------------------------- ;------------------------------------------------------------------------------- cblock 0x000 b0_temp b0_temp2 b0_temp3 temp1 temp2 E_adr E_data tmpctr offset endc ;------------------------------------------------------------------------------- ;------------------------------ Define Symbols ------------------------------- ;------------------------------------------------------------------------------- #define LCDen LATC, 3, 1 #define LCDrw LATC, 2, 1 #define LCDrs LATC, 1, 1 #define Row1 PORTB, 7, 1 #define Row2 PORTB, 6, 1 #define Row3 PORTB, 5, 1 #define Row4 PORTB, 4, 1 #define Col1 LATB, 3, 1 #define Col2 LATB, 2, 1 #define Col3 LATB, 1, 1 #define Col4 LATB, 0, 1 ;------------------------------------------------------------------------------- ;------------------------------- Program Code -------------------------------- ;------------------------------------------------------------------------------- ORG 0x0 ; Reset Vector ORG 0x0008 ; High Priority Interrupt Vector ORG 0x0018 ; Low Priority Interrupt Vector ;------------------------------------------------------------------------------- ;-------------------------- Initialisation of Ports -------------------------- ;------------------------------------------------------------------------------- Init movlb d'15' ; Set BSR to bank 15 clrf ADCON0 ; Turn off analogue module movlw 00001111b movwf ADCON1 ; Configure all pins as digital movlw 00000111b movwf CMCON ; Turn off comparators clrf TRISA movlw 11110000b ; PortB 7-4 inputs, 3-0 outputs movwf TRISB clrf TRISC clrf TRISD ; Set all ports as outputs clrf TRISE movlw 01110000b movwf OSCCON ; 8MHz Internal oscillator movlw 01000000b movwf OSCTUNE ; Enable PLL ; 32MHz with PLL bsf EECON1, WREN, 1 ; Enable write mode EEPROM / FLASH ;------------------------------------------------------------------------------- ;------------------------------- Main Program -------------------------------- ;------------------------------------------------------------------------------- Main call LCDinit cat call Keyscan movwf E_data, 0 clrf E_adr, 0 call Ewrite clrf E_data, 0 clrf E_adr, 0 call Eread movf E_data, 0 call LCDsend goto cat ;------------------------------------------------------------------------------- ;---------------------------------- Keypad ----------------------------------- ;------------------------------------------------------------------------------- Keyscan bcf Col1 bcf Col2 bcf Col3 ; Check if a key has been pressed bcf Col4 btfss Row1 goto scanprocess btfss Row2 goto scanprocess btfss Row3 goto scanprocess btfsc Row4 goto Keyscan scanprocess bcf Col1 ; Check which key has been pressed bsf Col2 bsf Col3 bsf Col4 movlw d'1' call Rscan bsf Col1 movlw d'2' bcf Col2 call Rscan bsf Col2 movlw d'3' bcf Col3 call Rscan bsf Col3 movlw d'4' bcf Col4 call Rscan bsf Col4 goto Keyscan Rscan btfss Row1 ; Which row? goto R1set btfss Row2 goto R2set btfss Row3 goto R3set btfss Row4 goto R4set return R1set addlw d'0' goto clrct R2set addlw d'4' goto clrct R3set addlw d'8' goto clrct R4set addlw d'12' clrct movwf temp1, 0 decf STKPTR, f, 1 Keynot call d50ms ; 50ms debounce bcf Col1 bcf Col2 bcf Col3 ; Check when key is depressed bcf Col4 btfss Row1 goto Keynot btfss Row2 goto Keynot btfss Row3 goto Keynot btfss Row4 goto Keynot movlw LOW table ; take low table address addwf temp1, w, 0 ; add to temp1, acts as an offset movwf TBLPTRL, 1 ; Move low table pointer clrf TBLPTRH movlw HIGH table addwfc TBLPTRH, f, 1 ; add high table address and carry ; if there was one from last addwf clrf TBLPTRU movlw UPPER table ; do same for upper table address addwf TBLPTRU, f, 1 tblrd * ; Read the contents via TBLPTR movf TABLAT, w, 1 ; Move results into working register return table db "1", "1", "2", "3", "A", "4", "5", "6" ; our data table db "B", "7", "8", "9", "C", "*", "0", "#" db "D" ;------------------------------------------------------------------------------- ;----------------------------- LCD Initialisation ---------------------------- ;------------------------------------------------------------------------------- LCDinit call d50ms ;Function Set movlw 00111000b ;8-bit mode, 2 lines, 5x7 matrix call LCDinst ;Display on / off movlw 00001100b ;Display on, Cursor off, Blink off call LCDinst ;Entry Mode Set movlw 00000110b ;Increment, Display shifted call LCDinst ;Cursor or display shift movlw 00010000b ;Display Shift call LCDinst ;Clear Screen movlw 00000001b ; Clear screen call LCDinst return ;------------------------------------------------------------------------------- ;----------------------------- LCD Character Send ---------------------------- ;------------------------------------------------------------------------------- LCDsend movlb d'15' ; Ensure BSR is FSR banking bsf LCDrs ; Data mode bcf LCDrw ; Write mode call sndt ; Send data return ;------------------------------------------------------------------------------- ;---------------------------- LCD Instruction Send --------------------------- ;------------------------------------------------------------------------------- LCDinst movlb d'15' ; Ensure BSR is FSR banking bcf LCDrs ; Instruction mode bcf LCDrw ; Write mode call sndt ; Send data call d50ms return ;------------------------------------------------------------------------------- ;--------------------------------- Bus Send --------------------------------- ;------------------------------------------------------------------------------- sndt movwf LATD, 1 ; Place into LCD bus nop bsf LCDen nop nop nop bcf LCDen goto micro100 ;------------------------------------------------------------------------------- ;------------------------------ Delay Routines ------------------------------- ;------------------------------------------------------------------------------- delay movlw d'100' ; 125ns 1 second delay movwf b0_temp3, 0 ; 125ns nop ; 125ns label_3 movlw d'40' ; 125ns movwf b0_temp2, 0 ; 125ns nop ; 125ns nop ; 125ns label_1 movlw d'249' ; 125ns movwf b0_temp, 0 ; 125ns nop ; 125ns nop ; 125ns label_2 nop ; 125ns nop ; 125ns nop ; 125ns nop ; 125ns nop ; 125ns decfsz b0_temp, f, 0 ; 125ns goto label_2 ; 250ns (1us total inner loop) ; 125ns decfsz b0_temp2, f, 0 ; 125ns goto label_1 ; 250ns ; 125ns decfsz b0_temp3, f, 0 ; 125ns goto label_3 ; 250ns return d50ms movlw d'1' ; 50ms delay movwf b0_temp3, 0 goto label_3 micro100 movlw d'100' ; 100us delay movwf b0_temp, 0 label_4 nop ; 125ns nop ; 125ns nop ; 125ns nop ; 125ns nop ; 125ns decfsz b0_temp, f, 0 ; 125ns goto label_4 ; 250ns (1us total inner loop) return ;------------------------------------------------------------------------------- ;------------------------------ EEPROM Routines ------------------------------ ;------------------------------------------------------------------------------- Eread movff E_adr, EEADR ; Move address to pointer bsf EECON1, RD, 1 ; Start read movfF EEDATA, E_data ; Move read data to working file return Ewrite movff E_data, EEDATA ; Data to be written movff E_adr, EEADR ; Address pointer bcf EECON1, EEPGD, 1 ; Point to data mode bcf EECON1, CFGS, 1 ; Access EEPROM btfsc EECON1, WR, 1 ; Check to see if a write is still goto $-2 ; in progress movlw 0x55 movwf EECON2, 1 movlw 0xAA movwf EECON2, 1 ; Required sequence bsf EECON1, WR, 1 ; Start write return