原创 串行通信接口典型应用举例

2010-3-22 20:48 1756 1 1 分类: PCB
串行通信接口典型应用举例

SCI_FLAG                   .usect         ".data0",1                                     ;SCI标志寄存器<?xml:namespace prefix = o />


TXD_PTR                   .usect         ".data0",8                                     ;发送的数据存放区


RXD_PTR                   .usect         ".data0",8                                     ;接收到的数据存放区


                            .include           "F2407REGS.H"            ;引用头部文件


.def               _c_int0


;(1)建立中断向量表


                            .sect         ".vectors"                                        ;定义主向量段


RSVECT             B    _c_int0                                            ;PM 0        复位向量                         1


INT1                          B    GISR1                                               ;PM 2         中断优先级1                   4


INT2                          B    PHANTOM                                   ;PM 4        中断优先级2                  5


INT3                          B    PHANTOM                                   ;PM 6        中断优先级3                  6


INT4                          B    PHANTOM                                   ;PM 8        中断优先级4                  7


INT5                          B    PHANTOM                                   ;PM A        中断优先级5                  8


INT6                          B    PHANTOM                                   ;PM C        中断优先级6                  9


RESERVED      B    PHANTOM                                   ;PM E        (保留位)                      10


SW_INT8        B    PHANTOM                                   ;PM 10    用户定义软件中断            —


                                             …


SW_INT31       B    PHANTOM                                   ;PM 3E    用户定义软件中断         —


;中断子向量入口定义pvecs


                            .sect         ".pvecs"                                          ;定义子向量段


PVECTORS        B         PHANTOM                                     ;保留向量地址偏移量0000h


                            B         PHANTOM                                     ;保留向量地址偏移量0001h


                                             …


                            B         PHANTOM                                     ;保留向量地址偏移量0005h


                            B         SCI_RX_ISR                                     ;保留向量地址偏移量0006h SCI接收中断


                            B         PHANTOM                                     ;保留向量地址偏移量0007h


                                             …


                            B         PHANTOM                                     ;保留向量地址偏移量0041h


 


;(2)主程序:


        .text


_c_int0    


SETC          INTM


               CLRC          SXM


               CLRC          OVM


CLRC          CNF


LDP                   #0E0H


SPLK                  #81FEH,SCSR1                                     ;CLKIN=6 M,CLKOUT=24 M


SPLK                  #0E8h,WDCR                                   ;关看门狗


                   CALL         SCI_INIT                                       ;调串口初始化程序


                   LDP           #5H


                   SPLK                   #00H,SCI_FLAG                            ;清发送软件标志位


TXD_LOOP:  


                   LDP           #5H


                   BIT            SCI_FLAG,BIT0


                   BCND           TXD_DATA,TC                            ;SCI_FLAG.0=1则发送数据


                   B                WAIT


TXD_DATA:                                                                   ;发送N个数据程序


                   MAR                   *,AR0


                   LAR          AR0,#TXD_PTR


                   ADRK         #N+1                                                        ;发送N个数据


                   MAR                   *,AR4


                   LACC         *+                                                   ;读数据


                   CMPR         00


                   BCND         TXD_DATA_END,TC                 ;判数据发送完?


                   MAR                   *,AR2


                   SACL         *,AR4                                          ;数据送SCITXBUF寄存器


XMIT_RDY:  


                   LDP           #DP_PF1


                   BIT            SCICTL2,BIT7


                   BCND         XMIT_RDY,NTC                         ;判发送器是否空?


                   B                TXD_DATA


TXD_DATA_END:


                   MAR                   *, AR4


                   LAR          AR4,#TXD_PTR                           ;恢复发送的数据指针


                   LDP             #5H


                   SPLK                   #00H,SCI_FLAG                            ;清发送软件标志位


WAIT:    NOP


                   B                TXD_LOOP


(3)串行通信初始化程序:


SCI_INIT:


                   LDP                    #0E1H


                   LACL         MCRA


                   OR             #03H


                   SACL         MCRA                                           ;配置串行口引脚为特殊功能:TXD、RXD


                   LDP                   #DP_PF1  


                   SPLK          #000FH,SCICCR                     ;地址位唤醒模式,8位数据,


;1位停止位,无奇偶校验


SPLK          #0007H,SCICTL1                             ;接收、发送、内部时钟使能 ,SLEEP=1


               SPLK          #0002H,SCICTL2                    ;接收中断使能


               SPLK          #0001H,SCIHBAUD


               SPLK          #0038H,SCILBAUD                          ;波特率为9600


               SPLK          #0027H,SCICTL1                    ;串口初始化完成


               LAR          AR1,#SCIRXBUF                         ;接收缓冲寄存器地址


               LAR          AR2,#SCITXBUF                         ;发送缓冲寄存器地址


               LAR          AR3,#RXD_PTR                           ;接收的数据指针


LAR          AR4,#TXD_PTR                            ;发送的数据指针


LDP          #0


                   SPLK                  #0001h,IMR                                     ;允许INT1中断


                   SPLK                  #0FFFFh,IFR                               ;清所有中断标志


                   CLRC         INTM                                             ;开总中断


                   RET


(4)中断程序


GISR1:                                                                              ;优先级INT1中断人口


                   ;保护现场


                   LDP           #0E0H


                   LACC         PIVR,1                                         ;读取外设中断向量寄存器(PIVR),并左移一位


                   ADD                  #PVECTORS                                     ;加上外设中断人口地址


                   BACC                                                                 ;跳到相应的中断服务子程序


SCI_RX_ISR:                                                                   ;接收中断服务程序


                   LDP           #DP_PF1


                   MAR                   *,AR1        


                   BIT            SCICTL1,BIT2


                   BCND         RXD_DATA,NTC                         ;SLEEP=0,接收数据


                   LACC         *                                                      ;读入地址


                   SUB           #0AAH


                   BCND         TXD_FLAG,EQ                            ;接收到的地址为AAH,则发送数据


                   SUB           #0001H


                   BCND         SCI_RX_END,NEQ                      ;地址错误,退出中断


                   SPLK                  #0033H,SCICTL1                     ;地址为ABH,则清SLEEP=0


;表示准备接收数据


                   B                SCI_RX_END


TXD_FLAG:                                                                    ;置发送标志


                   LDP           #5H


SPLK                  #01H,SCI_FLAG


                   B                SCI_RX_END


RXD_DATA:                                                                   ;接收N个数据


                   LACC         *,AR3                                          ;读入数据


                   SACL         *+,AR0                                        ;保存数据


                   LAR          AR0,#RXD_PTR


                   ADRK         #N


                   MAR                   *,AR3


                   CMPR         00


                   BCND         SCI_RX_END,NTC                      ;判是否接收完


                   LAR          AR3,#RX_PTR                              ;恢复接收的数据指针


                   SPLK                  #0037H,SCICTL1                     ;置SLEEP=1表示准备接收地址


SCI_RX_END:                                                                           ;中断返回


                   ;恢复现场


                   CLRC         INTM                                             ;开总中断,因为一进中断就


;自动关闭总中断


                   RET


(5)假中断程序


PHANTOM      


KICK_DOG                                                         ;复位看门狗


                   RET


                   END

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
1
关闭 站长推荐上一条 /3 下一条