Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as ARM assembler by hggh ( 2 years ago )
; Define processor and configuration
    list        p=16f877a
    include     <p16f877a.inc>
    __CONFIG    _CP_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC

; Define constants
    #define     SBIT_TXEN   5
    #define     SBIT_SPEN   7
    #define     SBIT_CREN   4

; Define variables
    cblock 0x20
        ch
        i
    endc

; Define message
message:
    dt "Welcome to Pic Serial Comm, Type the char to be echoed: ", 0

; Initialize UART
UART_Init
    banksel TRISC
    movlw   0x80
    movwf   TRISC       ; Configure Rx pin as input and Tx as output

    banksel TXSTA
    bsf     TXSTA, SBIT_TXEN  ; Enable transmitter

    banksel RCSTA
    bsf     RCSTA, SBIT_SPEN  ; Enable Serial Port
    bsf     RCSTA, SBIT_CREN  ; Enable continuous receive

    ; Calculate SPBRG for 9600 baud rate at 20MHz
    movlw   d'31'
    movwf   SPBRG

    return

; Transmit a character
UART_TxChar
    movf    ch, W
    banksel TXIF
TxWait
    btfss   PIR1, TXIF
    goto    TxWait      ; Wait till the transmitter register becomes empty

    movf    ch, W
    movwf   TXREG       ; Load the char to be transmitted into transmit reg

    return

; Receive a character
UART_RxChar
RxWait
    btfss   PIR1, RCIF
    goto    RxWait      ; Wait till the data is received

    banksel RCREG
    movf    RCREG, W
    movwf   ch          ; Store the received data

    return

; Main program
main
    banksel TRISB
    clrf    TRISB
    clrf    PORTB

    call    UART_Init   ; Initialize UART

    ; Transmit predefined string
    movlw   low(message)
    movwf   FSR
TransmitLoop
    movf    INDF, W
    btfsc   STATUS, Z
    goto    MainLoop    ; End of string

    movwf   ch
    call    UART_TxChar

    incf    FSR, F
    goto    TransmitLoop

MainLoop
    ; Echo received characters
EchoLoop
    call    UART_RxChar
    call    UART_TxChar
    goto    EchoLoop

    end

 

Revise this Paste

Your Name: Code Language: