LIST P=16F877, R=DEC #INCLUDE P16F877.INC CBLOCK 0X20 ENDC ; CBLOCK W_BAK, STATUS_BAK KYBUF ENDC ; ORG 0 NOP BANKSEL TRISB MOVLW 0 MOVWF TRISB MOVWF PORTD BANKSEL PORTB MOVLW H'FF' MOVWF PORTB ; ORG 4 PUSH: MOVWF W_BAK SWAPF STATUS,W CLRF STATUS MOVWF STATUS_BAK ; MOVF RCREG,W BCF PIR1,RCIF ; POP: SWAPF STATUS_BAK,W ; Restore STATUS MOVWF STATUS SWAPF W_BAK,F ; Restore WREG w/o SWAPF W_BAK,W ; affecting STATUS RETFIE ;---------------------------------------------------------------; NUM_TO_CHR_TBL: ANDLW H'0F' ADDWF PCL DT "0123456789ABCDEF" ; ASCII of numbers 0~F ;---------------------------------------------------------------; GOTO MAIN ; MAIN: BANKSEL ADCON1 ; ADCON1 is in bank1 MOVLW B'10001110' ; select right(1) justified (d7) ; 1 ch(AN0)/0 refs MOVWF ADCON1 ; BANKSEL ADCON0 MOVLW B'01000001' ; select Fosc/8(01), AN0(000) MOVWF ADCON0 ; a/d done(0), x(0), a/d ON(1) ; CALL DLY2_5mS ; wait for input charge BANKSEL SPBRG MOVLW 103 ; SPBRG value for 9600 BAUD @ 16MHZ MOVWF SPBRG ; CLRF TXSTA ; clear and setup TXSTA(TX9:0=8bit) BSF TXSTA,BRGH ; enable high BAUD RATE BSF TXSTA,TXEN ; enable serial transmission ; BANKSEL RCSTA CLRF RCSTA ; clear and setup RCSTA(RX:0=8bit) BSF RCSTA,CREN ; enable continuous reception BSF RCSTA,SPEN ; enable serial port ; BANKSEL PIE1 ; enable recption interrupt BSF PIE1,RCIE ; BANKSEL PIR1 BCF PIR1,RCIF ; clear reception flag BSF INTCON,PEIE ; enable peripheral interrupt BSF INTCON,GIE ; enable global interrupt ; ;***************************************; MAIN1: BSF ADCON0,GO ; start a/d converting CALL NUM_TO_CHR_TBL ; convert key data to ASCII MOVWF TXREG ; transmit data now! BCF PIR1,TXIF MAIN2: BTFSC ADCON0,GO ; a/d convertion finished? GOTO MAIN2 ; no ; BANKSEL ADRESL ; select bank1 MOVF ADRESL,W ; read a/d result ( low byte ) ; GOTO MAIN1 ;===============================================================; ; #INCLUDE ; ; ; END