RSS

PIC16C63串口通信程序

来源: 作者: 时间:2007-01-05 Tag: 点击:

;*******************************************
;MCU-PIC16C63AWDT=ONHS16MHZ
;本例将寄存器BUFFER1和BUFFER2中的数据串口发送出去,没有中断调用,单片机选用PIC16C63A
;*******************************************
LIST P=PIC16C63A
INCLUDE "P16C63A.INC"
ORG 0000H
GOTO MAIN
BUFFER1 EQU 20H
BUFFER2 EQU 21H

MAIN MOVLW 0X55
MOVWF BUFFER1
MOVLW 0X56
MOVWF BUFFER2
;发送过程
BSF STATUSRP0
MOVLW 25H
MOVWF SPBRG;设定波特率9600
CLRF TXSTA;SYNC=0-异步方式,没有第9位,BRGH=0低速
BCF STATUSRP0
MOVLW B'10010000'
MOVWF RCSTA;SPEN=1,串口使能
BSF TXSTATXEN;允许发送

MOVF BUFFER1W;发送第一个寄存器数据
MOVWF TXREG
again BTFSS TXSTATRMT;判断TSR为空则间断
GOTO again
MOVF BUFFER2W;发送第二个寄存器数据
MOVWF TXREG
again2 BTFSS TXSTATRMT;判断TSR为空则间断
GOTO again2
NOP
END

 

 

list      p=16c63           ; list directive to define processor
    #include <p16c63.inc>        ; 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&nbsp;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    STATUSw          ; move status register into W register
        bcf     STATUSRP0        ; 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     STATUSRP0        ; ensure file register bank set to 0
        movf    status_tempw     ; retrieve copy of STATUS register
        movwf    STATUS            ; restore pre-isr STATUS register contents
        swapf   w_tempf
        swapf   w_tempw          ; 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     TXSTABRGH        ; HIGH SPEED/ASYN/8BITS
                BSF     TXSTATXEN
        banksel    RCSTA
                CLRF    RCSTA  
                BSF     RCSTASPEN        ; SERIAL PORT ENABLE
                BSF     RCSTACREN        ; CONTINUOUS RECEIVE ENABLE
                                          ; 8BITS/DISABLE ADDEN
                banksel TXREG              movf    Buffer1W    
                movwf   TXREG
                call    TXPOLL
                movf    Buffer2W
        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    STATUSRP0
    btfss    PIR1RCIF
    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    STATUSRP0
TLOOP
    btfss    TXSTATRMT
    goto    TLOOP
    bcf    STATUSRP0
    return         
          
    END                       ; directive 'end of program'


1.部分资源来自网络,经ET电子归类整理,旨在服务电子爱好者并无商业目的,不保证正确性与完整性.
2.如果您觉得本站资源对您有用,请告知您的好友,用搜索引擎搜"ET电子"即可.


最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
合作站点