这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » UART I/O模拟UART,还有很多人想要,用多年了,贡献出来给大家!

共2条 1/1 1 跳转至

UART I/O模拟UART,还有很多人想要,用多年了,贡献出来给大家!

院士
2006-09-17 18:14:16     打赏
UART I/O模拟UART,还有很多人想要,用多年了,贡献出来给大家!



关键词: 模拟     还有     很多人     想要     多年     贡献出     来给     大家    

院士
2006-12-22 22:43:00     打赏
2楼
问 标准51单片机用,可供C语言调用
;/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
;@@@                          模拟串口通讯函数                               @@@
;@@@           晶振:11059200Hz    波特率2400 4800 9600 19200                @@@
;@@@    帧格式:11位数据,1位启起位,8位数据位,1位识别码,1位停止位         @@@
;@@@    发送一字节函数:_PutByte   入口参数:1字节数据,1字节识别码          @@@
;@@@    接收一字节函数:_GetByte  出口参数:1字节数据 ,1字节成功标志        @@@
;@@@                        (此函数只作主机用)                             @@@
;@@@    影响资源: r4,r5,r6,r7,a,b ,ucBaudrate 波特率选择                    @@@
;@@@    r5,r6,r7 entrance and exit parameter                                 @@@
;@@@    发送程序中,入口参数r5为0xff时是寻址,为0时是数据,R7为待送数据
;@@@    接收程序中,出口参数R6为0时检测不到起始位或停止位,数据在R7中。
; $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
public             _GetByte,_PutByte;

SimuSerialCodeSeg  segment   code

   mBaud           equ       2

bTransmit          bit       P1.3;
bReceive           bit       P1.2;       ;// (INT1);
bEnableSimu485     bit       P1.1;
;;//////////////////////////////////////////////////////////////
rseg               SimuSerialCodeSeg
_PutByte:          nop;
                   setb      bEnableSimu485;  ;; // 允许发送
                   clr       bTransmit;
                   mov       a,#mBaud;    ;; 9600bps
                   rl        a;               ;; 存贮在ucBaudrate单元
                   mov       dptr,#Blanch
                   jmp       @a+dptr;
Blanch:            ajmp      UAT2400
Blanch1:           ajmp      UAT4800
Blanch2:           ajmp      UAT9600
Blanch3:           ajmp      UAT19200
                   nop;
                   ret;



;; ////////////////////////////////////////////////////
                ;; //  416.67uS/bit
UAT2400:           nop;
                   mov       a,r5;
                   mov       c,acc.0;
                   mov       a,r7;
                   mov       r4,#9;
                   mov       r6,#183;
                   djnz      r6,$;
                   nop;
NextBit2400:       rrc       a;
                   mov       bTransmit,c;
                   mov       bTransmit,c;
                   mov       r6,#188;
                   djnz      r6,$;
                   djnz      r4,NextBit2400;
                   nop;
                   setb      bTransmit;        //  Stop bit
                   mov       r6,#190;
                   djnz      r6,$;
                   nop;
                   ret;


;; ///////////////////////////////////////////////////////////
               ;; //   208.33uS/bit
UAT4800:           nop;
                   mov       a,r5;
                   mov       c,acc.0;
                   mov       a,r7;
                   mov       r4,#9;
                   mov       r6,#85;
                   djnz      r6,$;
NextBit4800:       rrc       a;
                   mov       bTransmit,c;
                   mov       bTransmit,c;
                   mov       r6,#92;
                   djnz      r6,$;
                   djnz      r4,NextBit4800;
                   nop;
                   setb      bTransmit;        //  Stop bit
                   mov       r6,#94;
                   djnz      r6,$;
                   nop;
                   ret;

;;/////////////////////////////////////////////////////////////
               ;; // 104.167uS/bit
UAT9600:           nop;
                   mov       a,r5;
                   mov       c,acc.0;
                   mov       a,r7;
                   mov       r4,#9;
                   mov       r6,#37;
                   djnz      r6,$;
NextBit9600:       rrc       a;
                   mov       bTransmit,c;
                   mov       bTransmit,c;
                   mov       r6,#44;
                   djnz      r6,$;
                   djnz      r4,NextBit9600;
                   nop;
                   setb      bTransmit;        //  Stop bit
                   mov       r6,#46;
                   djnz      r6,$;
                   nop;
                   ret;



;;/////////////////////////////////////////////////////////////
               ;; // 52.1uS/bit
UAT19200:          nop;
                   mov       a,r5;
                   mov       c,acc.0;
                   mov       a,r7;
                   mov       r4,#9;
                   mov       r6,#13;
                   djnz      r6,$;
NextBit19200:      rrc       a;
                   mov       bTransmit,c;
                   mov       bTransmit,c;
                   mov       r6,#20;
                   djnz      r6,$;
                   djnz      r4,NextBit19200;
                   nop;
                   setb      bTransmit;        //  Stop bit
                   mov       r6,#22;
                   djnz      r6,$;
                   nop;
                   ret;



        
   mBaudR2400      equ       46    ;;// 电平状态采样点数,一半以上为高电平,则为1
   mBaudR4800      equ       22
   mBaudR9600      equ       10
   mBaudR19200     equ       4
   bWatchDog       bit       P1.4;
;;///////////////////////////////////////////////////////////////
;;///////////////////////////////////////////////////////////////

_GetByte:          clr       bEnableSimu485;  ;; // 允许接收
                   clr       bEnableSimu485;  ;; // 允许接收
                   mov       B,#1
                   mov       a,r7;
                   jnz       FindAddressWait;
                   mov       B,#3;            // 不是等待地址应答,延时久些
FindAddressWait:   nop;
                   mov       a,#0
                   setb      bWatchDog;        ;;// 写完客户机时,等待时间可能很长
                   clr       bWatchDog;        ;;// 先清零看门狗
                   setb      bWatchDog;
WaitStartBit:      nop;
                   jnb       bReceive,StartBit; ;;;
                   jnb       bReceive,StartBit; ;; // 等待起始位
                   clr       bEnableSimu485;    ;; // 允许接收
                   jnb       bReceive,StartBit;
                   jnb       bReceive,StartBit;
                   inc       a;
                   jnz       WaitStartBit;
                   nop;
                   djnz      B,WaitStartBit;
                   setb      bEnableSimu485;
                   clr       C;
                   mov       r7,#0h;
                   ret;
StartBit:          nop;
                   mov       a,#mBaud;
                   rl        a;
                   mov       dptr,#BlanchR
                   jmp       @a+dptr;
BlanchR:           ajmp      UAR2400
BlanchR1:          ajmp      UAR4800
BlanchR2:          ajmp      UAR9600
BlanchR3:          ajmp      UAR19200
                   nop;
                   ret;

;; ////////////////////////////////////////////////////
                ;; //  416.67uS/bit
UAR2400:           nop;
                   clr       bEnableSimu485;
                   nop;
                   mov       r6,#180;   ;; 120
                   djnz      r6,$;      ;; 起始位延时
                   clr       a;
                   mov       r4,#8
                   mov       r5,#0;
                   mov       r7,#0;
                   mov       r6,#mBaudR2400
NextBitR2400:      nop;                 ;; // 判断一次需8.8uS;
                   jnb       bReceive,LowLevel1;
                   inc       r5;
                   ajmp      JugeNextBit1
LowLevel1:         inc       r7;
                   nop;
                   nop;
JugeNextBit1:      djnz      r6,NextBitR2400;
                   mov       r6,#mBaudR2400;
                   rr        a;
                   nop;
                   nop;
                   cjne      r5,#mBaudR2400/2,JugeLevel1
JugeLevel1:        mov       r5,#0;
                   mov       r7,#0;;
                   nop;
                   jc        ItIsLow1;
                   orl       a,#80h;
                   ajmp      ShiftAcc1;
ItIsLow1:          nop;
                   nop;
                   anl       a,#7fh;
ShiftAcc1:
                   djnz      r4,NextBitR2400; ;; 未完,下一位二进制
                   nop;
                   mov       r7,a;
                   setb      C;;
                   mov       r6,#100;
                   djnz      r6,$;
                   push      ACC;
JugeAgain1:        nop;
                   jnb       bReceive,JugeStopBit1;
                   nop;
                   nop;
                   setb      bEnableSimu485;
                   setb      bTransmit;
                   pop       ACC;
                   ret;
JugeStopBit1:      nop;
                   inc       a;
                   jnz       JugeAgain1;
                   clr       C;          ;; // No stop bit,error,
                   setb      bEnableSimu485;
                   setb      bTransmit;
                   pop       ACC;
                   ret;

;; ////////////////////////////////////////////////////
                ;; //  208.3uS/bit
UAR4800:           nop;
                   clr       bEnableSimu485;
                   nop;
                   mov       r6,#84;   ;;
                   djnz      r6,$;      ;; 起始位延时
                   clr       a;
                   mov       r4,#8
                   mov       r5,#0;
                   mov       r7,#0;
                   mov       r6,#mBaudR4800
NextBitR4800:      nop;                 ;; // 判断一次需8.8uS;
                   jnb       bReceive,LowLevel2;
                   inc       r5;
                   ajmp      JugeNextBit2
LowLevel2:         inc       r7;
                   nop;
                   nop;
JugeNextBit2:      djnz      r6,NextBitR4800;
                   mov       r6,#mBaudR4800;
                   rr        a;
                   nop;
                   nop;
                   cjne      r5,#mBaudR4800/2,JugeLevel2
JugeLevel2:        mov       r5,#0;
                   mov       r7,#0;;
                   nop;
                   jc        ItIsLow2;
                   orl       a,#80h;
                   ajmp      ShiftAcc2;
ItIsLow2:          nop;
                   nop;
                   anl       a,#7fh;
ShiftAcc2:         djnz      r4,NextBitR4800; ;; 未完,下一位二进制
                   nop;
                   mov       r7,a;
                   setb      C;
                   mov       r6,#50;
                   djnz      r6,$;
                   push      ACC;
JugeAgain2:        nop;
                   jnb       bReceive,JugeStopBit2;
                   nop;
                   nop;
                   setb      bEnableSimu485;
                   setb      bTransmit;
                   pop       ACC;
                   ret;
JugeStopBit2:      nop;
                   inc       a;
                   jnz       JugeAgain2;
                   clr       C;          ;;// No stop bit,error
                   setb      bEnableSimu485;
                   setb      bTransmit;
                   pop       ACC;
                   ret;



;; ////////////////////////////////////////////////////
                ;; //  104.2uS/bit
UAR9600:           nop;
                   clr       bEnableSimu485;
                   nop;
                   mov       r6,#36;   ;;
                   djnz      r6,$;      ;; 起始位延时
                   clr       a;
                   mov       r4,#8
                   mov       r5,#0;
                   mov       r7,#0;
                   mov       r6,#mBaudR9600
NextBitR9600:      nop;                 ;; // 判断一次需8.8uS;
                   jnb       bReceive,LowLevel3;
                   inc       r5;
                   ajmp      JugeNextBit3
LowLevel3:         inc       r7;
                   nop;
                   nop;
JugeNextBit3:      djnz      r6,NextBitR9600;
                   mov       r6,#mBaudR9600;
                   rr        a;
                   nop;
                   nop;
                   cjne      r5,#mBaudR9600/2,JugeLevel3
JugeLevel3:        mov       r5,#0;
                   mov       r7,#0;;
                   nop;
                   jc        ItIsLow3;
                   orl       a,#80h;
                   ajmp      ShiftAcc3;
ItIsLow3:          nop;
                   nop;
                   anl       a,#7fh;
ShiftAcc3:
                   djnz      r4,NextBitR9600; ;; 未完,下一位二进制
                   nop;
                   mov       r7,a;
                   setb      C;
                   mov       r6,#25;
                   djnz      r6,$;
                   push      ACC;
JugeAgain3:        nop;
                   jnb       bReceive,JugeStopBit3;
                   nop;
                   nop;
                   setb      bEnableSimu485;
                   setb      bTransmit;
                   pop       ACC;
                   ret;
JugeStopBit3:      nop;
                   inc       a;
                   jnz       JugeAgain3;
                   clr       C;          ;;// No stop bit,error
                   setb      bEnableSimu485;
                   setb      bTransmit;
                   pop       ACC;
                   ret;

;; ////////////////////////////////////////////////////
                ;; //  52.1uS/bit
UAR19200:          nop;
                   clr       bEnableSimu485;
                   nop;
                   mov       r6,#12;   ;;
                   djnz      r6,$;      ;; 起始位延时
                   clr       a;
                   mov       r4,#8
                   mov       r5,#0;
                   mov       r7,#0;
                   mov       r6,#mBaudR19200
NextBitR19200:     nop;                 ;; // 判断一次需8.8uS;
                   jnb       bReceive,LowLevel4;
                   inc       r5;
                   ajmp      JugeNextBit4
LowLevel4:         inc       r7;
                   nop;
                   nop;
JugeNextBit4:      djnz      r6,NextBitR19200;
                   mov       r6,#mBaudR19200;
                   rr        a;
                   nop;
                   nop;
                   cjne      r5,#mBaudR19200/2,JugeLevel4
JugeLevel4:        mov       r5,#0;
                   mov       r7,#0;;
                   nop;
                   jc        ItIsLow4;
                   orl       a,#80h;
                   ajmp      ShiftAcc4;
ItIsLow4:          nop;
                   nop;
                   anl       a,#7fh;
ShiftAcc4:
                   djnz      r4,NextBitR19200; ;; 未完,下一位二进制
                   nop;
                   mov       r7,a;
                   setb      C;
                   mov       r6,#10;
                   djnz      r6,$;
                   push      ACC;
JugeAgain4:        nop;
                   jnb       bReceive,JugeStopBit4;
                   nop;
                   nop;
                   setb      bEnableSimu485;
                   setb      bTransmit;
                   pop       ACC;
                   ret;
JugeStopBit4:      nop;
                   inc       a;
                   jnz       JugeAgain4;
                   clr       C;        ;;// No stop bit,error
                   setb      bEnableSimu485;
                   setb      bTransmit;
                   pop       ACC;
                   ret;

                ;;////////////////////
                   end;
1: 支持。虽然我不用51。 2: 用过3年了早就不用了,占用资源太多,而且数据量大的时候有一定的出错率,现在全用550\552\554来扩展,要做就做稳定的东西 3: 不錯.鼓勵一下. 4: 要求不高的场合用,很爽呀,便宜!!!   做主机用的效率也不低呀。
   其实有些人写串口程序不用中断,而用查询,效率跟我的模拟串口一样!!! 5: 软串口也应该鼓励用中断容易融入程序结构中。 6: 顶一下吧! 7: 不错,顶一下. 8: 软串口用中断?发送?接收?用定时器中断??   软串口用定时器中断的效率很低,也许低过查询
   最好不要用软串口做从机使用,
   不得已要做从机时,接收脚可用中断引脚 9: 对呀,收发都用中断接收占用一个外中断和定时器中断,发送只占用定时器中断。如果要全双工,占用一个中断+2个定时器中断,哈哈,有些破费啊。 10: 同意4行8字LCD意见 11: 顶一下!! 12: 用软件delay,效率太低。应该只用一个定时器中断。

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]