Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;***************************************************
- ;*** Lab 6 Nivå 1 - Leo Frössling - 20/02-18 ***
- ;***************************************************
- #include <p18F4520.inc>
- CONFIG OSC = HS
- CONFIG PWRT = OFF
- CONFIG WDT = OFF
- CONFIG PBADEN = OFF
- CONFIG LVP = OFF
- org 0x000000
- PORST GOTO MAIN
- org 0x000008
- INTERUPT BTFSC PIR1,RCIF ; if RX int...
- CALL ESRXISR, FAST ; ... read char
- RETFIE
- org 0x000020
- ;******************************************************************************
- ; *** COL toolbox 1.1 AC ******************************************************
- ; *** Needs: RA1,2,3,5,RE0,1,2,RC0 ********************************************
- ; *** RAM usage: 0x000 ********************************************************
- ;******************************************************************************
- IC8COL equ 0x000
- EC8INIT BCF TRISA, RA1
- BCF TRISA, RA2
- BCF TRISA, RA3
- BCF TRISA, RA5
- BCF TRISE, RE0
- BCF TRISE, RE1
- BCF TRISE, RE2
- BCF TRISC, RC0
- CLRF IC8COL
- CALL EC8FREE
- RETURN
- EC8COLQ MOVF IC8COL, W
- RETURN
- EC8NEXTQ INCF IC8COL, W
- BTFSC WREG, 3
- CLRF WREG
- RETURN
- EC8FREE BCF PORTA, RA1
- BCF PORTA, RA2
- BCF PORTA, RA3
- BCF PORTA, RA5
- BCF PORTE, RE0
- BCF PORTE, RE1
- BCF PORTE, RE2
- BCF PORTC, RC0
- RETURN
- EC8NEXT CALL EC8NEXTQ
- MOVWF IC8COL
- RLNCF WREG
- RLNCF WREG
- ADDWF PCL
- BSF PORTA, RA1
- RETURN
- BSF PORTA, RA2
- RETURN
- BSF PORTA, RA3
- RETURN
- BSF PORTA, RA5
- RETURN
- BSF PORTE, RE0
- RETURN
- BSF PORTE, RE1
- RETURN
- BSF PORTE, RE2
- RETURN
- BSF PORTC, RC0
- RETURN
- ;*** END COL toolbox***********************************************************
- ;****************************************************************
- ;*** 8*8 LED Matrix toolbox 1.0 AC ******************************
- ;*** Needs: FSR0 & PDx ******************************************
- ;*** RAM usage: 0x010..0x017 ************************************
- ;****************************************************************
- S88DISP equ 0x010 ; Shared 8 Byte LED buffer
- ;*** External E88INIT * Initialize HW/SW ***
- E88INIT CLRF TRISD
- LFSR 0,S88DISP ; FSR0 ptr to LED buffer.
- CLRF S88DISP+0
- CLRF S88DISP+1
- CLRF S88DISP+2
- CLRF S88DISP+3
- CLRF S88DISP+4
- CLRF S88DISP+5
- CLRF S88DISP+6
- CLRF S88DISP+7
- RETURN
- ;*** External E88VIEW ***
- E88VIEW MOVFF PLUSW0, PORTD
- RETURN
- ;*** END 8*8 LED Matrix *****************************************
- ;****************************************************************
- ;*** KEY toolbox 1.0 ********************************************
- ;*** Needs: RB0,2,4,5 *******************************************
- ;*** RAM usage: 0x003-0x005 *************************************
- ;*** Dependencies: COL toolbox **********************************
- ;****************************************************************
- IKTEMP equ 0x003
- IKPREV equ 0x004
- IKTIME equ 0x005
- EKINIT BSF TRISB, RB0
- BSF TRISB, RB2
- BSF TRISB, RB4
- BSF TRISB, RB5
- SETF IKPREV ; No key pressed
- CLRF IKTIME
- RETURN
- EKDOWNQ CALL EC8COLQ ; Every 8:th time...
- DECFSZ WREG
- GOTO LKDOWNQ
- DCFSNZ IKTIME ; ...dec repeat counter,
- INCF IKTIME ; but only to one!
- LKDOWNQ SETF WREG ; Key pressed? (Assume not!)
- BTFSS PORTB, RB0
- CLRF WREG
- BTFSS PORTB, RB2
- MOVLW 0x01
- BTFSS PORTB, RB4
- MOVLW 0x02
- BTFSS PORTB, RB5
- MOVLW 0x03
- BTFSC WREG, 7
- RETURN ; No! return!
- MOVWF IKTEMP ; Yes, Which...
- CALL EC8COLQ ; ...column...
- RLNCF WREG
- RLNCF WREG
- IORWF IKTEMP ; ...add row...
- MOVF IKPREV, W
- XORWF IKPREV, W ; ...same???...
- BNZ LKNEW
- MOVF IKTEMP, W ; ... yes, repeat?
- DECFSZ IKTIME
- SETF WREG ; ...no return 0xFF!
- RETURN
- LKNEW MOVF IKTEMP, W ; ...no! new key!!!
- MOVWF IKPREV ; remember it!
- CLRF IKTIME ; reset repeat cnt
- RETURN
- ;*** END KEY toolbox ********************************************
- ;******************************************************************************
- ; DELAY PACKAGE 10*80MS
- ; RAM USAGE: 0x07C - 0x07D
- ;
- ;******************************************************************************
- ELDSHORT EQU 0x07B
- ELDLONG EQU 0x07A
- ELDELONG EQU 0x079
- ED80MS CLRF ELDSHORT
- CLRF ELDLONG
- MOVLW 0x001
- MOVWF ELDELONG
- ELDLOOP DECFSZ ELDSHORT
- GOTO ELDLOOP
- DECFSZ ELDLONG
- GOTO ELDLOOP
- DECFSZ ELDELONG
- GOTO ELDLOOP
- RETURN
- ;END DELAY PACKAGE 80MS********************************************************
- ;******************************************************************************
- ; DELAY PACKAGE 10MS
- ; RAM USAGE: 0x07E - 0x07F
- ;
- ;******************************************************************************
- SDSHORT EQU 0x07E
- SDLONG EQU 0x07F
- ED10MS CLRF SDSHORT
- MOVLW 0x01
- MOVWF SDLONG
- SDLOOP DECFSZ SDSHORT
- GOTO SDLOOP
- DECFSZ SDLONG
- GOTO SDLOOP
- RETURN
- ;END DELAY PACKAGE 10MS********************************************************
- ;*************************************************
- ;*** Delay 2ms ***********************************
- ;*** RAM: 0x00..0x01 *****************************
- ;*************************************************
- ID2MSVAR1 equ 0x40
- ID2MSVAR2 equ 0x41
- ED2MSSTART
- MOVLW 0x07
- MOVWF ID2MSVAR1
- ID2MSLOOP1
- CLRF ID2MSVAR2
- ID2MSLOOP2
- DECFSZ ID2MSVAR2
- GOTO ID2MSLOOP2
- DECFSZ ID2MSVAR1
- GOTO ID2MSLOOP1
- RETURN
- ;***************************** END Delay 2ms ***
- ;*************************************************
- ;*** Delay 79ms **********************************
- ;*** RAM: 0x00..0x01 *****************************
- ;*************************************************
- ID79MSVAR1 equ 0x42
- ID79MSVAR2 equ 0x43
- ED79MSSTART
- CLRF ID79MSVAR1
- ID79MSLOOP1
- CLRF ID79MSVAR2
- ID79MSLOOP2
- DECFSZ ID79MSVAR2
- GOTO ID79MSLOOP2
- DECFSZ ID79MSVAR1
- GOTO ID79MSLOOP1
- RETURN
- ;***************************** END 79ms Delay ***
- ;************************************************************
- ;*** LCD toolbox 2.0 AC *************************************
- ;*** Need FSR1 (temp), PORTD, (short wr), RB1 EN & RB3 RS ***
- ;*** (RS = 1 DATA, RS = 0 INSTRUCTION )**********************
- ;*** 0xFF=\n, 0xFe=\cmd, 0xFD=%d ****************************
- ;*** RAM: 0x0F0 *********************************************
- ;*** Dependencies: - ****************************************
- ;************************************************************
- ILDCNT equ 0x0F0 ; Internal delay counter
- ELINIT SETF ILDCNT ; LCD I/O not busy!
- ELWRMSG INCFSZ ILDCNT ; Busy?
- RETURN ; ...yes, return
- SETF ILDCNT ; ...no: reset delay cnt!
- TBLRD*+ ; Getnext cmd/chr...
- MOVF TABLAT, W ; ... to WREG
- BTFSS STATUS, N ; CMD?
- GOTO PRNTCH ; ...no: Write char!
- INFSNZ WREG ; Case \0?
- GOTO LLNULL ; Yes, null ch!
- INFSNZ WREG ; Case \x?
- GOTO LLESCCH ; Yes, escape ch!
- ; Default: variable!
- LLINSVA TBLRD*+ ; Load variable adress...
- MOVFF TABLAT, FSR1L ; ... get adress...
- CLRF FSR1H ; ... into FSR1...
- MOVF INDF1, W ; ...get data through F1...
- ADDLW '0' ; ...make it an ascii digit!
- GOTO PRNTCH ; ...and display it!
- LLESCCH TBLRD*+ ; Next is the cmd...
- MOVLW 0x80 ; ... could take some time...
- MOVWF ILDCNT
- MOVF TABLAT, W ; ...Get the cmd...
- GOTO COMNWRT ; ... and transfer it!
- LLNULL DECF TBLPTRL ; Adjust tblptr...
- RETURN ; ...and return!
- ;************************************************
- ;*** LCD toolbox ********************************
- LCD_DATA equ PORTD
- LCD_CTRL equ PORTB
- RS equ RB3
- EN equ RB1
- ELCDINIT
- CLRF TRISD ; PORTD = Output
- BCF TRISB, RB1 ; PORTB = Output
- BCF TRISB, RB3
- BCF LCD_CTRL, EN ; Enable idle low
- MOVLW 0x38 ; init. LCD 2 lines, 5x7 matrix
- CALL COMNWRT ; call command subroutine
- CALL ED79MSSTART
- MOVLW 0x0C ; display on, cursor off
- CALL COMNWRT ; call command subroutine
- CALL ED2MSSTART
- MOVLW 0x01 ; clear LCD
- CALL COMNWRT ; call command subroutine
- CALL ED2MSSTART
- MOVLW 0x06 ; shift cursor right
- CALL COMNWRT ; call command subroutine
- CALL ED2MSSTART
- MOVLW 0x80 ; cursor at line 1, pos.
- CALL COMNWRT ; call command subroutine
- CALL ED2MSSTART
- RETURN
- ELCDCLEAR
- MOVLW 0x01 ; clear LCD
- CALL COMNWRT ; call command subroutine
- CALL ED2MSSTART
- RETURN
- ELCDPRNT
- LOADMSG
- TBLRD*+ ; Read ASCII from program memory
- MOVF TABLAT, W
- BZ EXIT
- CALL PRNTCH ; Print ASCII
- BRA LOADMSG
- EXIT RETURN
- COMNWRT ; send command to LCD
- MOVWF LCD_DATA ; copy wreg to LCD DATA pin
- BCF LCD_CTRL, RS ; RS = 0 for command
- BSF LCD_CTRL, EN ; E = 1 for high pulse
- CALL ED2MSSTART
- BCF LCD_CTRL, EN ; E = 0 for H-to-L pulse
- RETURN
- DATAWRT ; write data to LCD
- MOVWF LCD_DATA ; copy wreg to LCD DATA pin
- BSF LCD_CTRL, RS ; RS = 1 for data
- BSF LCD_CTRL, EN ; EN = 1 for high pulse
- CALL ED2MSSTART
- BCF LCD_CTRL, EN ; E = 0 for H-to-L pulse
- RETURN
- PRNTCH
- CALL DATAWRT ; call display subroutine
- CALL ED2MSSTART
- RETURN
- ;***************************** END LCD toolbox ***
- ;*************************************************
- ;*** Serial Toolbox 1.0 AC ***********************
- ;*************************************************
- TX equ RC6
- RX equ RC7
- ESRXISR MOVLW B'00010000' ; Shift back cursor
- CALL COMNWRT
- MOVF RCREG, W ; READ RX FIFO!
- CALL PRNTCH ; Print recieved char on LCD
- RETURN FAST
- ESINIT MOVLW B'00100000' ; Enable transmit and choose low baud rate
- MOVWF TXSTA ; write to reg
- MOVLW B'10010000' ; Enable recieve and serial port itself
- MOVWF RCSTA
- MOVLW D'15' ; 9600 bps (Fosc / (64 * Speed) - 1)
- MOVWF SPBRG ; write to reg
- BCF TRISC, TX ; Make TX pin of PORTC output
- BSF TRISC, RX ; Make RX pin of PORTC input
- BSF RCSTA, SPEN ; Enable the serial port
- BSF PIE1, RCIE ; Enable recice interrupt enable
- BSF INTCON, PEIE ; Enable peripheral interrupt enable
- RETURN
- ESWRITE BTFSS PIR1,TXIF ; WRITE, POLLED, BUSY?
- GOTO ESWRITE ; YES : CHECK AGAIN...
- MOVWF TXREG ; NO! : WRITE CHAR &
- RETURN ; RETURN!
- ;*** END Serial Toolbox **************************
- MAIN CALL EC8INIT
- CALL E88INIT
- CALL EKINIT
- CALL ELINIT
- CALL ELCDINIT
- CALL ESINIT
- LFSR 1,S88DISP
- BSF INTCON, GIE ; < Allow Global interrupt! >
- MOVLW low(LCDTEXT)
- MOVWF TBLPTRL ; Load in to TABLPTRL
- MOVLW high(LCDTEXT) ; Get high byte of code table
- MOVWF TBLPTRH ; Load in to TABLEPTRH ; Fetch data from TABLPTR
- CALL LOADMSG
- LOOP CLRF PORTD ; Clear PORTD
- CALL EC8NEXT ; Advance to and activate next column
- CALL EKDOWNQ ; Check if any key in column is pressed
- BTFSS WREG,7 ; Button pressed?
- CALL BTN_PRESS ; ... yes, call BTN_PRESS
- CALL EC8FREE ; Turn off all columns
- MOVF IKTEMP, W ; Move last pressed key to work
- CALL GET_NRCODE ; Look up 7seg code in table
- MOVWF PORTD ; Present code on PORTD
- BSF PORTA, RA1 ; Activate Column 0
- CALL ED10MS ; Let it glow for a while..
- BCF PORTA, RA1 ; Turn off Column 0
- GOTO LOOP
- BTN_PRESS CALL GET_ASCIICODE ; Get ASCII for button number
- CALL ESWRITE ; Send ASCII over serial port
- RETURN
- GET_ASCIICODE RLNCF WREG ; Multiplay WREG with 2
- MOVWF TBLPTRL ; Load in to TABLPTRL
- MOVLW high(ASCIICODES) ; Get high byte of code table
- MOVWF TBLPTRH ; Load in to TABLEPTRH
- TBLRD* ; Fetch data from TABLPTR
- MOVF TABLAT, W ; Load fetched data to work
- RETURN
- GET_NRCODE RLNCF WREG ; Multiplay WREG with 2
- MOVWF TBLPTRL ; Load in to TABLPTRL
- MOVLW high(NUMBERCODES) ; Get high byte of code table
- MOVWF TBLPTRH ; Load in to TABLEPTRH
- TBLRD* ; Fetch data from TABLPTR
- MOVF TABLAT, W ; Load fetched data to work
- RETURN
- NUMBERCODES org 0x500 ; 7seg TABLE
- DB B'00111111' ; 0
- DB B'00000110' ; 1
- DB B'01011011' ; 2
- DB B'01001111' ; 3
- DB B'01100110' ; 4
- DB B'01101101' ; 5
- DB B'01111100' ; 6
- DB B'00000111' ; 7
- DB B'01111111' ; 8
- DB B'01100111' ; 9
- DB B'01110111' ; A
- DB B'01111100' ; b
- DB B'00111001' ; C
- DB B'01011110' ; d
- DB B'01111001' ; E
- DB B'01110001' ; F
- ASCIICODES org 0x600 ; ASCII Table
- DB B'00110000' ; 0
- DB B'00110001' ; 1
- DB B'00110010' ; 2
- DB B'00110011' ; 3
- DB B'00110100' ; 4
- DB B'00110101' ; 5
- DB B'00110110' ; 6
- DB B'00110111' ; 7
- DB B'00111000' ; 8
- DB B'00111001' ; 9
- DB B'01000001' ; A
- DB B'01000010' ; b
- DB B'01000011' ; C
- DB B'01000100' ; d
- DB B'01000101' ; E
- DB B'01000110' ; F
- LCDTEXT org 0x700
- DB "Recieved: ", 0x00
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement