機(jī)電之家資源網(wǎng)
單片機(jī)首頁|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
16C5X模擬串口
16C5X模擬串口
 更新時間:2008-7-26 17:09:01  點擊數(shù):4
【字體: 字體顏色

, ; *******************************************************************
;   Plug in Libraries for the by 12 architecture                    *
;   Written and Tested using MPASM V1.40                            *
;********************************************************************
;   Filename:        SWUART12.ASM                                *
;   Date:               05 November 1997                            *
;   File Version:       1.00                                        *
;********************************************************************
;   Functions:                                                      *
;                                                                   *
;            OpenUART                                    *
;            getcUART                                    *
;            putcUART                                    *
;********************************************************************
;   Revision History:                                               *
;      V1.00 - Beta release of Peripheral Libraries                 *
;********************************************************************
;   Notes:                                                          *
;     - Change these parameters as needed and reassemble the library *
;
        title "Software UART for by 12 Architecture.
    list p=pic16c5x
#include <p16c5x.inc>
;
#define UARTPORT PORTA
#define TXPIN  3        
#define RXPIN  2        
#define TRISMASK  0x04
;
MODE  equ     1       ; If ( X_MODE==1) Then transmit LSB first
Nbit  equ     1       ; if (X_Nbit==1) # of data bits ( Transmission ) 
;                              is 8 else 7
STOPBITS   equ  0     ; if Sbit2 = 0 then 1 Stop Bit else 2 Stop Bits
CLOCKSPEED equ  10
BAUDRATE   equ  9600
;
; *******************************************************************
;
    if UARTPORT < 5 || UARTPORT > 7
        messg "Invalid port value"
    endif
    if RXPIN > 7
        messg "Invalid rxpin value"
    endif

    if TXPIN > 7
        messg "Invalid txpin value"
    endif

; both Rx and Tx pins must be on the same port
portmask = ((0x01 << RXPIN) | TRISMASK) & ~(0x01 << TXPIN)
;
;
LSB     equ     0
MSB     equ     7

;*****************  Communication Parameters   **************************
;
    ifndef MODE
MODE  equ     1       ; If ( X_MODE==1) Then transmit LSB first
;                         if ( X_MODE==0) Then transmit MSB first ( CODEC like )
    endif
    ifndef Nbit
Nbit  equ     1       ; if (X_Nbit==1) # of data bits ( Transmission ) is 8 else 7
    endif
;
    ifndef STOPBITS
STOPBITS   equ     0       ; if Sbit2 = 0 then 1 Stop Bit else 2 Stop Bits
    endif
    ifndef CLOCKSPEED
        error "CLOCKSPEED not defined"
    endif
    ifndef BAUDRATE
        error "BAUDRATE not defined"
    endif
;
;
    if (CLOCKSPEED != 4 && CLOCKSPEED != 8 && CLOCKSPEED != 10 && CLOCKSPEED != 20)
        error "Clockspeed not supported.  Should be one of (4, 8, 10 or 20 Mhz)"
    endif
    if (BAUDRATE != 1200 && BAUDRATE != 2400 && BAUDRATE != 4800 && BAUDRATE != 9600)
        error "Baudrate not supported.  Baudrate should be one of (1200, 2400, 4800, 9600)"
    endif


    if CLOCKSPEED == 4 
        if BAUDRATE == 19200
                error "Baudrate and clock speed incompatable"
        endif
        if BAUDRATE == 9600
BAUD_1  equ     .34     ; 3+3X = CLKOUT/Baud (9600 baud, 4Mhz)
BAUD_4  equ     .42     ; 3+3X = 1.25*CLKOUT/Baud
BAUD_X  equ     .31     ; 11+3X = CLKOUT/Baud
BAUD_Y  equ     .32     ; 9 +3X = CLKOUT/Baud
        endif
        if BAUDRATE == 4800
BAUD_1  equ     .68     ; 3+3X = CLKOUT/Baud (9600 baud, 4Mhz)
BAUD_4  equ     .86     ; 3+3X = 1.25*CLKOUT/Baud
BAUD_X  equ     .66     ; 11+3X = CLKOUT/Baud
BAUD_Y  equ     .66     ; 9 +3X = CLKOUT/Baud
        endif
        if BAUDRATE == 2400
BAUD_1  equ     .138     ; 3+3X = CLKOUT/Baud (9600 baud, 4Mhz)
BAUD_4  equ     .173    ; 3+3X = 1.25*CLKOUT/Baud
BAUD_X  equ     .135    ; 11+3X = CLKOUT/Baud
BAUD_Y  equ     .136    ; 9 +3X = CLKOUT/Baud
        endif
        if BAUDRATE == 1200
                error "Baudrate and clock speed incompatable"
        endif
    endif
    if CLOCKSPEED == 8 
        if BAUDRATE == 19200
                error "Baudrate and clock speed incompatable"
        endif
        if BAUDRATE == 9600
BAUD_1  equ     .68     ; 3+3X = CLKOUT/Baud (9600 baud, 8Mhz)
BAUD_4  equ     .86     ; 3+3X = 1.25*CLKOUT/Baud
BAUD_X  equ     .66     ; 11+3X = CLKOUT/Baud
BAUD_Y  equ     .66     ; 9 +3X = CLKOUT/Baud
        endif
        if BAUDRATE == 4800
BAUD_1  equ     .137     ; 3+3X = CLKOUT/Baud (9600 baud, 8Mhz)
BAUD_4  equ     .173     ; 3+3X = 1.25*CLKOUT/Baud
BAUD_X  equ     .135     ; 11+3X = CLKOUT/Baud
BAUD_Y  equ     .136     ; 9 +3X = CLKOUT/Baud
        endif
        if BAUDRATE == 2400
                error "Baudrate and clock speed incompatable"
        endif
        if BAUDRATE == 1200
            error "Baudrate and clock speed incompatable"
        endif
    endif
    if CLOCKSPEED == 10
         if BAUDRATE == 19200
            error "Baudrate and clock speed incompatable"
           endif
        if BAUDRATE == 9600
BAUD_1  equ     .86     ; 3+3X = CLKOUT/Baud (9600 baud, 10Mhz)
BAUD_4  equ     .108     ; 3+3X = 1.25*CLKOUT/Baud
BAUD_X  equ     .83     ; 11+3X = CLKOUT/Baud
BAUD_Y  equ     .84     ; 9 +3X = CLKOUT/Baud
        endif
           if BAUDRATE == 4800
BAUD_1  equ     .173     ; 3+3X = CLKOUT/Baud (4800 baud, 10Mhz)
BAUD_4  equ     .216     ; 3+3X = 1.25*CLKOUT/Baud
BAUD_X  equ     .170     ; 11+3X = CLKOUT/Baud
BAUD_Y  equ     .171     ; 9 +3X = CLKOUT/Baud
        endif
           if BAUDRATE == 2400
            error "Baudrate and clock speed incompatable"
        endif
           if BAUDRATE == 1200
            error "Baudrate and clock speed incompatable"
        endif
    endif
    if CLOCKSPEED == 20
        if BAUDRATE == 19200
BAUD_1  equ     .86      ; 3+3X = CLKOUT/Baud (9600 baud, 10Mhz)
BAUD_4  equ     .108     ; 3+3X = 1.25*CLKOUT/Baud
BAUD_X  equ     .83     ; 11+3X = CLKOUT/Baud
BAUD_Y  equ     .84     ; 9 +3X = CLKOUT/Baud
                error "Baudrate and clock speed incompatable"
        endif
        if BAUDRATE == 9600
BAUD_1  equ     .173     ; 3+3X = CLKOUT/Baud (9600 baud, 10Mhz)
BAUD_4  equ     .216     ; 3+3X = 1.25*CLKOUT/Baud
BAUD_X  equ     .170     ; 11+3X = CLKOUT/Baud
BAUD_Y  equ     .171     ; 9 +3X = CLKOUT/Baud
        endif
        if BAUDRATE == 4800
                error "Baudrate and clock speed incompatable"
        endif
        if BAUDRATE == 2400
                error "Baudrate and clock speed incompatable"
        endif
        if BAUDRATE == 1200
                error "Baudrate and clock speed incompatable"
        endif
    endif

; Generic delay loop
Delay   macro
    local    loop
    MOVwf   DlyCnt
loop
    decfsz  DlyCnt,f
    goto    loop
    endm

; Specific delay loops... loads counter value then generic delay loop
Delay1  macro        ; 3+3X = CLKOUT/Baud
    MOVlw   BAUD_1
    Delay
    endm

Delay4  macro        ; 3+3X = 1.25*CLKOUT/Baud
    MOVlw   BAUD_4           
    Delay
    endm

DelayX  macro       ; 11+3X = CLKOUT/Baud
    MOVlw   BAUD_X
    Delay
    endm

DelayY  macro       ; 9 +3X = CLKOUT/Baud
    MOVlw   BAUD_Y
    Delay
    endm
;
;************************  Data RAM Assignments  **********************
;
        udata
RcvReg  res     1   ; Data received
XmtReg  res     1
        global  RcvReg
        global  XmtReg
        global  OpenUART
        global  putcUART
        global  getcUART

        udata_ovr
Count   res     1   ; Counter for #of Bits Transmitted
DlyCnt  res     1
;
        CODE
;
;*****************************************************************
;    Function Name:    OpenUART                                 *
;    Return Value:    none                                     *
;    Parameters:    None (parameters set at assembly time)   *
;    RAM Usage:    0 bytes                                  *
;    ROM Usage:    4 words                                  *
;    Description:    Sets the TRIS register for the UARTPORT  *
;            such that the RX_PIN is an input, the    *
;                       TX_PIN is an output, and the rest of the *
;                       bits are as specified by TRISMASK.  Also *
;                       sets the TX_PIN high (idle condition).   *   
;*****************************************************************
OpenUART
    bsf    UARTPORT,TXPIN    ; Start it off high
    MOVlw    portmask
    tris    UARTPORT        ; Set PWM pin for output
        retlw   0

;
;*****************************************************************
;    Function Name:    getcUART                                 *
;    Return Value:    returns a status in the carry bit (STATUS.C) *
;                       if a character was recieved, carry =1.   *
;                       if no character recieved, carry = 0      *
;                       received character returned in RcvReg    *
;    Parameters:    None (parameters set at assembly time)   *
;    RAM Usage:    0                                     *
;    ROM Usage:    depends                                  *
;    Description:    looks for a start bit on the RX_PIN.  If *
;            none, it clears the carry bit and returns. *
;                       if there is a carry bit, it reads in the *
;                       rest of the byte, saves it in RcvReg,    *
;                       and sets the carry bit.                  *   
;*****************************************************************
getcUART
    clrf   RcvReg           ; Clear RcvReg
    bcf    STATUS,C        ; Clear the carry bit (assume nothing rx'd)
    btfsc   UARTPORT,RXPIN    ; check for a Start Bit
    
    retlw    0        ; no char received.. return null.
    Delay4                  ; delay for 1

    IF      Nbit
    MOVlw   8               ; 8 Data bits
    ELSE
    MOVlw   7               ; 7 data bits
    ENDIF
;
    MOVwf   Count
R_next  bcf     STATUS,C
    IF      MODE
    rrf     RcvReg,f     ; to set if MSB first or LSB first
    ELSE
    rlf     RcvReg,f
    ENDIF
    btfsc   UARTPORT, RXPIN    
;
    IF      MODE
      IF      Nbit
      bsf     RcvReg,MSB       ; Conditional Assembly
      ELSE
      bsf     RcvReg,MSB-1
      ENDIF
    ELSE
    bsf     RcvReg,LSB
    ENDIF
;
    DelayY
    decfsz  Count,f
    goto    R_next
    bsf    STATUS,C
    retlw    0x01
;
;
;*****************************************************************
;    Function Name:    putcUART                                 *
;    Return Value:    none                                     *
;    Parameters:    byte to send, in the W register          *
;    RAM Usage:    0                                     *
;    ROM Usage:                                             *
;    Description:    Sends the contents of the W register out *
;            the TX_PIN.                              *
;*****************************************************************
;
putcUART
    MOVwf    XmtReg
    IF      Nbit
    MOVlw   8
    ELSE
    MOVlw   7
    ENDIF
    MOVwf   Count
;
    IF      MODE
    ELSE
      IF    Nbit
      ELSE
      rlf   XmtReg,f
      ENDIF
    ENDIF
;
    bcf     UARTPORT, TXPIN       ; Send Start Bit
    Delay1          ; 52 uS (19200 baud)
X_next  bcf     STATUS,C
;
    IF      MODE
    rrf     XmtReg,f     ; Conditional Assembly
    ELSE                    ; to set if MSB first or LSB first
    rlf     XmtReg,f
    ENDIF
;
    btfsc   STATUS,C
    bsf     UARTPORT,TXPIN
    btfss   STATUS,C
    bcf     UARTPORT, TXPIN
    DelayX
    decfsz  Count,f
    goto    X_next
    bsf     UARTPORT, TXPIN       ; Send Stop Bit
    Delay1
;
    IF      STOPBITS
    bsf     UARTPORT,TXPIN
    Delay1
    ENDIF
;
    retlw    0x01        ; return to calling program
    end

 

  • 上一篇: 單片機(jī)和FIFO的接口和操作
  • 下一篇: 16CF73振蕩激勵不夠
  • 發(fā)表評論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會員助手 | 免費鏈接

    點擊這里給我發(fā)消息66821730(技術(shù)支持)點擊這里給我發(fā)消息66821730(廣告投放) 點擊這里給我發(fā)消息41031197(編輯) 點擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實性、準(zhǔn)確性和合法性。
    機(jī)電之家對此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請聯(lián)系機(jī)電之家,機(jī)電之家將及時作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會
    中國行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營許可證:浙B2-20080178-1