技术热线: 4007-888-234
设计开发

专注差异化嵌入式产品解决方案 给智能产品定制注入灵魂给予生命

开发工具

提供开发工具、应用测试 完善的开发代码案例库分享

技术支持

从全面的产品导入到强大技术支援服务 全程贴心伴随服务,创造无限潜能!

新闻中心

提供最新的单片机资讯,行业消息以及公司新闻动态

PIC16C63单片机串口通信程序一览

更新时间: 2019-03-21
阅读量:313

 list      p=16c63           ; list directive to define processor
    #include 
        ; processor specific variable definitions
       
        __CONFIG _BODEN_OFF&_CP_OFF&_WRT_ENABLE_ON&_PWRTE_ON&_WDT_OFF&_XT_OSC&_DEBUG_OFF&_CPD_OFF&_LVP_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.


;***** VARIABLE DEFINITIONS
w_temp        EQU     0x20        ; variable used for context saving 
status_temp   EQU     0x21        ; variable used for context saving
Buffer1          EQU     0x22
Buffer2          EQU     0x23
             
;**********************************************************************
        ORG     0x000             ; processor reset vector
            nop
                 clrf    PCLATH            ; ensure page bits are cleared
          goto    main              ; go to beginning of program


        ORG     0x004             ; interrupt vector location
        movwf   w_temp            ; save off current W register contents
        movf    STATUS,w          ; move status register into W register
        bcf     STATUS,RP0        ; ensure file register bank set to 0
        movwf    status_temp       ; save off contents of STATUS register

; isr code can go here or be located as a call subroutine elsewhere

        bcf     STATUS,RP0        ; ensure file register bank set to 0
        movf    status_temp,w     ; retrieve copy of STATUS register
        movwf    STATUS            ; restore pre-isr STATUS register contents
        swapf   w_temp,f
        swapf   w_temp,w          ; restore pre-isr W register contents
        retfie                    ; return from interrupt

                ORG 40H

main
                banksel    TRISC             ; MPLAB提供的宏,BANK选择
                MOVLW   0X80
                MOVWF   TRISC             ; TX/RX口输入输出配置
                banksel SPBRG
                MOVLW   D'25'             ; 十进制的25
                MOVWF   SPBRG             ; 9600 BPS/ 4MHZ
                banksel TXSTA
                CLRF    TXSTA
                BSF     TXSTA,BRGH        ; HIGH SPEED/ASYN/8BITS
                BSF     TXSTA,TXEN
        banksel    RCSTA
                CLRF    RCSTA  
                BSF     RCSTA,SPEN        ; SERIAL PORT ENABLE
                BSF     RCSTA,CREN        ; CONTINUOUS RECEIVE ENABLE
                                          ; 8BITS/DISABLE ADDEN
                banksel TXREG                
                movf    Buffer1,W    
                movwf   TXREG
                call    TXPOLL
                movf    Buffer2,W
        movwf   TXREG
        call    TXPOLL
        goto    $
            
;************************************************
;* RXPOLL - This function polls the USART       *
;* receive interrupt flag and waits until a     *
;* data byte is available in RCREG.             *
;************************************************
RXPOLL
    bcf    STATUS,RP0
    btfss    PIR1,RCIF
    goto    RXPOLL
    return

;************************************************
;* TXPOLL - This function polls the TRMT flag   *
;* and waits until the USART has finished       * 
;* shifting the data byte out of the transmit   *
;* shift register.                              *
;************************************************
TXPOLL
    bsf    STATUS,RP0
TLOOP
    btfss    TXSTA,TRMT
    goto    TLOOP
    bcf    STATUS,RP0
    return         
          
    END                       ; directive 'end of program'