《匠人手记》推荐网上购书渠道:
互动出版网(china-pub)购书入口   >>>
当当网(dangdang)购书入口   >>>
卓越亚马逊网 购书入口   >>>
淘宝网(taobao)购书入口   >>>
更多购书渠道……   >>> 

设为首页加入收藏联系匠人管理入口21IC首页21IC博客21IC社区侃单片机回复的贴参与的贴

天气预报
百宝日历
载入中...

百宝专栏

载入中...
最新货色

载入中...

粉丝评论

载入中...

载入中...



百宝信息

载入中...

百宝流量

(2006-07-01开始)


匠人手记

 匠人观点: 好记性不如烂笔头  
 黑色幽默:三鹿门——后世畅想

PIC:RS232通讯程序
程序匠人 发表于 2005-6-2 21:02:00  阅读全文 | 回复(0) | 引用通告 | 编辑

;*************************************************************************
 TITLE   "RS232 Communication Using PIC16C54"
;
;    Comments :
;               (1) Full Duplex
;               (2) Tested from 1200 to 9600 Baud( @ 8 Mhz )
;               (3) Tested from 1200 to 19200 Baud(@ 16 & 20 Mhz)
;
;       The User gets a total time as specified by the User Cycles
;       in the table ( or from equations ). The user routine has to
;       exactly use up this amount of time. After this time the User
;       routine has to give up the control to the Operating System.
;       If less than 52 uS is used, then the user should wait in a
;       delay loop, until exactly 52 uS.
;
;       Transmission :
;               Transmit Data is output on DX pin (Bit DX of Port_A).
;               In the user routine, the user should load the
;               data to be transmitted in the XmtReg and Set the
;               X_flag ( bsf FlagRX,X_flag ). This flag gets cleared
;               after the transmission.
;
;       Reception :
;               Data is received on pin DR ( bit DR of Port_A ).
;               The User should constantly check the "R_done" flag
;               to see if reception is over. If the reception is
;               in progress, R_flag is set to 1.
;               If the reception is over, "R_done" flag is set to 1.
;               The "R_done" flag gets reset to zero when a next start
;               bit is detected. So, the user should constantly check
;               the R_done flag, and if SET, then the received word
;               is in Register "RcvReg". This register gets cleared
;               when a new start bit is detected.
;
;    Program Memory :
;       Total Program Memory Locations Used ( except initialization
;       in "main" & User routine ) = 132 locations.
;
;    Data Memory :
;       Total Data memory locations (file registers used) = 6
;               2 File registers to hold Xmt Data & Rcv Data
;               1 File registers for Xmt/Rcv flag test bits
;               3 File registers for delay count & scratch pad
;
;    Stack :
;       Only one level of stack is used in the Operating System/RS232
;       routine. But this is freed as soon as the program returns to the
;       user routine.
;
;    RTCC :     Not Used
;    WDT  :     Not Used
;
 INCLUDE         "PICREG.H"
 INCLUDE         "RS232.H"
;
 ORG     0
;*************************************************************************
;
Delay   movlw   K0+1
 movwf   DlyCnt          ; Total Delay = 3K+6
redo    decfsz  DlyCnt,Same
 goto    redo
 retlw   0
;
Delay1  movwf   DlyCnt
redo_1  decfsz  DlyCnt,Same     ;
 goto    redo_1
 goto    User
;
Delay2  movwf   DlyCnt
redo_2  decfsz  DlyCnt,Same     ; Delay =   = 260 Cycles
 goto    redo_2
 goto    User_1
;
R_strt  btfsc   Port_A,DR       ; check for a Start Bit
 goto    ShellY          ; delay for 104/2 uS
 bcf     FlagRX,R_done   ; Reset Receive done flag
 bsf     FlagRX,R_flag   ; Set flag for Reception in Progress
 btfss   FlagRX,BitXsb
 bsf     FlagRX,A_flag   ; A_flag is for start bit detected in R_strt
 clrf    RcvReg          ; Clear all bits of RcvReg
 IF      R_Nbit
 movlw   8               ; 8 Data bits
 ELSE
 movlw   7               ; 7 data bits
 ENDIF
 movwf   Rcount
 goto    Shell           ; delay for 104+104/4
;
ShellY  btfss   FlagRX,BitXsb
 goto    Shell
 bsf     FlagRX,R_flag
 goto    Shell
;
R_next  bcf     STATUS,CARRY
 IF      R_MODE
 rrf     RcvReg,Same     ; to set if MSB first or LSB first
 ELSE
 rlf     RcvReg,Same
 ENDIF
 btfsc   Port_A,DR
 IF      R_MODE
   IF      R_Nbit
   bsf     RcvReg,MSB       ; Conditional Assembly
   ELSE
   bsf     RcvReg,MSB-1
   ENDIF
 ELSE
 bsf     RcvReg,LSB
 ENDIF
 decfsz  Rcount,Same
 goto    Shell
 bcf     FlagRX,R_flag
 bsf     FlagRX,S_flag
 bsf     FlagRX,R_done
 goto    Shell
;
;       Reception Done
;
X_strt  bcf     Port_A,DX       ; Send Start Bit
 IF      X_Nbit
 movlw   8
 ELSE
 movlw   7
 ENDIF
 movwf   Xcount
 IF      X_MODE
 ELSE
   IF    X_Nbit
   ELSE
   rlf   XmtReg,Same
   ENDIF
 ENDIF
 goto    X_SB
;
X_next  bcf     STATUS,CARRY
 IF      X_MODE
 rrf     XmtReg,Same     ; Conditional Assembly
 ELSE                    ; to set if MSB first or LSB first
 rlf     XmtReg,Same
 ENDIF
 btfsc   STATUS,CARRY
 bsf     Port_A,DX
 btfss   STATUS,CARRY
 bcf     Port_A,DX
 decf    Xcount,Same
 goto    X_Data
;
X_SB_1  bcf     FlagRX,X_flag   ; Xmt flag = 0 -- transmission over
 movlw   9
 movwf   Xcount
 bsf     Port_A,DX       ; Send Stop Bit
 goto    X_Stop
;
X_SB_2  bsf     Port_A,DX
 bcf     FlagRX,S_bit
 goto    X_Stop
;
;   End of Transmission
;
R0_X0   btfss   FlagRX,S_flag
 goto    User
 bcf     FlagRX,S_flag
 call    Delay
 movlw   K7+1
 goto    Delay1
;
R1_X0
 call    Delay
 movlw   K1+1            ; delay for 1st bit is 104+104/4
 movwf   DlyCnt
 IF      R_Nbit
 movlw   8               ; 8 Data bits
 ELSE
 movlw   7               ; 7 data bits
 ENDIF
 xorwf   Rcount,W
 btfsc   STATUS,Z_bit
 goto    redo_1
 movlw   K2+1
 goto    Delay1
;
R1_X1                           ; same as R0_X1
R0_X1   movlw   9
 subwf   Xcount,W
 btfsc   STATUS,Z_bit
 goto    X_strt
 movf    Xcount,Same     ; to check if All data bits Xmted
 btfss   STATUS,Z_bit
 goto    X_next
 IF      SB2
   btfsc FlagRX,S_bit
   goto  X_SB_2
   bsf   FlagRX,S_bit
   goto  X_SB_1
 ELSE
   goto    X_SB_1
 ENDIF
;
;
X_SB    goto    cycle4
cycle4  goto    X_Data
;
X_Data  btfsc   FlagRX,A_flag
 goto    SbDly
 btfsc   FlagRX,BitXsb
 goto    ABC
 call    Delay
 movlw   K3+1
 goto    Delay2
;
SbDly   bcf     FlagRX,A_flag
 call    Delay
 movlw   K4+1
 goto    Delay2
;
ABC     bcf     FlagRX,BitXsb
 call    Delay
 goto    User_1
;
X_Stop
 btfsc   FlagRX,A_flag
 goto    SbDly
 btfsc   FlagRX,BitXsb
 goto    ABC
 call    Delay
 movlw   K5+1
 goto    Delay2
;
User_1  btfsc   FlagRX,R_flag
 goto    Sync_1          ; Reception already in progress
 btfsc   FlagRX,S_flag
 goto    Sync_3
 btfsc   Port_A,DR       ; check for a Start Bit
 goto    Sync_2          ; No Start Bit - goto User routine
 bcf     FlagRX,R_done   ; Reset Receive done flag
 bcf     FlagRX,R_flag
 bsf     FlagRX,BitXsb   ; Set flag for Reception in Progress
 clrf    RcvReg          ; Clear all bits of RcvReg
 IF      R_Nbit
 movlw   8               ; 8 Data bits
 ELSE
 movlw   7               ; 7 data bits
 ENDIF
 movwf   Rcount
 goto    User
;
Sync_3  bcf     FlagRX,S_flag
 movlw   K6+1
 goto    Delay1
;
Sync_1
Sync_2  goto    User
;
;*************************************************************************
;
Shell   btfsc   FlagRX,R_flag
 goto    Chek_X
 btfsc   FlagRX,X_flag
 goto    R0_X1
 goto    R0_X0            ; Case for R0_X0
Chek_X  btfsc   FlagRX,X_flag
 goto    R1_X1
 goto    R1_X0
;
;
;*************************************************************************
;       Operating System
;  The User routine after time = B/2, should branch Here
;
Op_Sys  btfss   FlagRX,R_flag
 goto    R_strt
 goto    R_next
;
;*************************************************************************
;
main    movlw   0EH             ; Bit 0 of Port A is Output
 tris    Port_A          ; Set Port_A.0 as output ( DX )
;                                 & Port_A.1 is input ( DR )
 bsf     Port_A,DX
 movlw   9
 movwf   Xcount          ; If Xcount == 9, Then send start bit
 clrf    FlagRX          ; Clear All flag bits.
 IF      SB2
   bsf     FlagRX,S_bit    ; Set Xmt Stop bit flag(2 Stop Bits)
 ELSE
   bcf     FlagRX,S_bit    ; Clear Xmt Stop bit flag
 ENDIF
 movlw   1FH               ; Prescaler = 4
 OPTION                    ; Set RTCC increment on internal Clock
 goto    Op_Sys
;
;*************************************************************************
;
;       *******************  User Routine  ***************
;  The User routine should use up time exactly = User time as given
;  in the Constants Table ( or by Equations for constants ).
;  At 9600, this 86 Clock Cycles. RTCC timer is used here to count
;  upto 86 cycles ( From 128-86 To 0 ) by examining Bit 7 of RTCC.
;
K_user  equ     .128+.6-.86
;
User    movlw   K_user
 movwf   RTCC
 btfsc   FlagRX,R_done
 goto    ErrChk
SetXmt  btfsc   FlagRX,X_flag
 goto    Op
 movlw   41H
 movwf   XmtReg
 bsf     FlagRX,X_flag   ; Enable Xmission
 goto    Op
;
ErrChk
 movlw   "Z"
 xorwf   RcvReg,W
 btfsc   STATUS,Z_bit
 goto    SetXmt
error   goto    error           ; Received word is not "Z"
;
Op      btfss   RTCC,MSB        ; Test for RTCC bit 7
 goto    Op              ; If Set, Then RTCC has incremented
Oflow   goto    Op_Sys          ; to 128.
;
; ***********************************************************
;
 ORG     PIC54
 goto    main

 END

看《匠人手记》,与匠人同行!北航出版,正在热卖!

发表评论:
载入中...

芯片专题

器件专题

软件专题

硬件专题

综合专题

项目专题

原创专题

器件检测
LCD LED
按键 触摸键
E2PROM
电池 电机
电阻 电容 电感

指令系统
软件算法
编程规范
滤波算法
串行通讯

PCB设计
I2C PWM
红外遥控
充电技术
中断 ADC 

匠人手记
匠人夜话
网络心路
一周热点串烧
从零开始玩PIC
DIY旋转时钟

广告5号位 [投放]


学习板、开发板、编程器、下载器、仿真器(查看详情……)

广告3号位 [投放]

站内搜索


站外搜索


百度  google
mp3  歌词 
图片  FLASH 
知道  文档
新闻  词典 
地图  mp3 
软件  天网 
雅虎  爱问 
搜狗  讯雷 
网讯  华军 
天空 

21IC器件搜索
百宝箱分站
  • 《匠人的百宝箱》21IC站
  • 《匠人的百宝箱》21IC笔记团队
  • 《匠人手记》21IC书友会
  • 《匠人的百宝箱》MCUBLOG站
  • 《匠人的百宝箱》MCUBLOG笔记团队
  • 《匠人的百宝箱》EDN站
  • 《匠人手记》EDN书友会
  • 《匠人的百宝箱》与非网站
  • 《匠人的百宝箱》新浪站
  • 《匠人的百宝箱》百度站
  • 《匠人的百宝箱》网易126站
  • 《匠人的百宝箱》网易163站
  • 《匠人的百宝箱》互动出版网站
  • 广告4号位 [投放]

     
     

    匠人原创

    往日酷贴

     
     
     

    大千八卦

    友情连接

    新浪新闻:
    新浪财经:
    AK58新闻:
    新浪股票:
    新浪股票:
    证券之星:

     [更多酷站连接]

     

     

    [欢迎交换连接]

    [百宝箱之与非门分舵]

    [电脑圈圈的家当]

    [IC921的博客]

    [柔月阁]

    [八楼的呼吸]

    [hotpower 的水潭]

    [xwj的文君阁]

    [所长的BLOG]

    [阿摆手记]

    [电子伙伴]

    [unaided的笔记]

    [小飞的笔记]

    [单片机开发联盟]

    [网址之家]

    [好东西网址大全]

    [美萍中文精选]

    [数字电视之家]

    [SMARTCODE电子书斋]

    [软件开发之窗]

    [Armoric]

    [我爱研发网]

    [infernal的笔记]

    [雄鹰的空中加油站]

    [SunK]

    [逍遥电子]

    [ningpanda的博客]

    [C-Design]

    [一网见天下]

    [海边淘沙]

    [嵌入式365]

    [水牛的仓库]

    [股剩是怎样炼成的]

    [PIC论坛]

    [ICC AVR开发网]

    [中国高校自动化网]

     

     

     

    MCU博客-中国电子工程师博客网 

    大学生电子网 

     

     

     

     

     

    !!! 《匠人的百宝箱》 !!!