chuxue 发表于 2005-8-28 17:05:24

请教USART 实物不能通过???? 付汇编源码

请教USART 

实物仿真不能通过,接收到的都是乱码

不明白...

从机程序:

chushihua:   ;初始化; 设置波特率

    clr r2

        ldi temp,0

        ldi temp0,12

    out ubrrh,temp

        out UBRRL,temp0   ; 接收器与发送器使能

    ldi temp, (1<<mpcm)

    out ucsra,temp

        ldi temp, (1<<RXEN)|(1<<TXEN)|(1<<ucsz2)|(1<<rxcie)

        out UCSRB,temp

; 设置帧格式:9个数据位, 2 个停止位

        ldi temp, (1<<URSEL)|(1<<USBS)|(1<<UCSZ0)|(1<<UCSZ1)

        out UCSRC,temp

        ret

从机接收完成中断子程序:

intrxc:tst r2

       brne xuanzhong          

        nop

       rcall dujihao    ;取机号,存入R1内

       nop

       in temp,udr

           cp temp,r1

           brne rxcreti   ;与本机号相同?

           cbi ucsra,mpcm;清ucsra的mpcm位

           ldi temp,9

           mov r2,temp          

           out udr,r1   ;输出本机号

fandizi:nop

      sbis ucsra,txc

      rjmp fandizi

        sbi ucsra,txc

rxcreti:reti       

      

xuanzhong:cbi ucsrb,txcie                   

          push yh

          push yl

          clr yh

          mov yl,r2

          nop          

          in temp,udr

          st y+,temp

          mov r2,yl

          cpi yl,13

          brne rxcreti_1

          sbi ucsra,mpcm

          clr r2                

          nop      

               

rxcreti_1:pop yl

          pop yh       

          reti

主机传输子程序:

chuanshu:          ;传输开始         

         rcall chushihua

       nop

         rcall dujihao   ;读从机个数并存入R4内

         clr r1

outlp_next:

         sbi UCSRB,txb8   ;传输地址(txb8=1)      

         inc r1

outlp:   clr r2         

         out udr,r1      ;输出从机号,先从1号机开始

tslop:   rcall llop      ;短暂延时

         inc r2

         breq chuanshu_fnll   ;传输失败指示

         sbis ucsra,rxc    ;接收完成?   

         rjmp tslop

         nop

         in temp,udr         

         cp temp,r1          ;从机号相同?

         brne outlp

         cbi UCSRB,txb8    ;改为传输数据(txb8=0)   

       clr yh   

         ldi yl,9

txlop:    ld temp,y+

         out udr,temp

testl:   nop

         sbis ucsra,udre

         rjmp testl         

         cpi yl,13

         brne txlop

       rcall llop      

         cp r1,r4         ;R4内从机个数

         brlo outlp_next

         sbi porta,4

         ret

chuanshu_fnll:cp r1,r4         ;R4内从机个数

         brlo outlp_next

        sbi porta,5

         nop

         ret

主程序初始化:         

chushihua:   ;初始化

        ldi temp,0

        ldi temp0,12

      out ubrrh,temp

        out UBRRL,temp0

        ldi temp, (1<<RXEN)|(1<<TXEN)|(1<<ucsz2)|(1<<txb8)

        out UCSRB,temp

; 设置帧格式:9个数据位, 2 个停止位

        ldi temp, (1<<URSEL)|(1<<USBS)|(1<<UCSZ0)|(1<<UCSZ1)

        out UCSRC,temp

        ret



         

目的是传输的R9\R10\R11\R12的数据到从机相同地址。
-----此内容被chuxue于2005-08-30,16:21:13编辑过

free_ygd 发表于 2005-8-29 17:34:58

现在只会用C了,汇编不会了

chuxue 发表于 2005-8-29 22:09:47

楼上兄弟也是在笑话我了,小弟这是这样想呀,大家都是用C,这个程序搞定后一定要把C也给功下来。以后也好与各位大侠沟通,





从机接收地址后与自己机号比较,如是相同则清USARTA内的PMCP位,....小弟想明白此时从机是不是必须要跳出中断等待下次接收完成中断再读数据(也这样试了,就是收到的数据是0、1、0、1的,不知道为什么),小弟所写程序是在原地轮讯接收完成置位标志位(RXD)...为什么会不成功?????

chuxue 发表于 2005-8-30 17:36:10

自己顶一下



从新写了一下,就是不行,短短的百来行也就这么难.......



哪 位大侠能帮小弟一下就好了



多谢多谢

chuxue 发表于 2005-9-6 11:21:17

现在改为如下程序,也不对!!1

哪位高手路过看看哦,谢!



;从机程序,中断接收主机数据,存入R9、R10、R11、R12内

.include"m16def.inc"

.org $000

rjmp reset

.org $015

reti

.org $016

rjmp intrxc   ;接收完成中断入口

.org $029

nop

reti

.org $030

reset: ldi temp,0x04

       out sph,temp

       ldi temp,0x5f

       out spl,temp

       .............

       .............

       .............

       .............

rjmp reset





chushihua:   ;初始化

      clr r2          ;选中标志、数据指针

   ldi temp,0

   ldi temp0,12

      out ubrrh,temp   ; 设置波特率

    out UBRRL,temp0

; 接收器与发送器使能

      ldi temp, (1<<mpcm)   ;多机通信功能使能

      out ucsra,temp

    ldi temp, (1<<RXEN)|(1<<TXEN)|(1<<ucsz2)|(1<<rxcie)|(1<<txb8)

    out UCSRB,temp

; 设置帧格式:9个数据位, 2 个停止位

    ldi temp, (1<<URSEL)|(1<<USBS)|(1<<UCSZ0)|(1<<UCSZ1)

    out UCSRC,temp

    ret

   

intrxc:tst r2

       brne xuanzhong   ;选中了吗?

       push temp      

       in temp,sreg      

       push temp

    in temp,portc    ;此句目的是为了让显示消隐

    push temp

    clr temp

    out portc,temp

    out sreg,temp

    nop

    rcall dujihao      ;读本机号,并存入R1内

    nop

       in temp,udr

       cp temp,r1

    brne rxcreti

    cbi ucsra,mpcm   

    ldi temp,9

    mov r2,temp      

    out udr,r1

fandizi:nop

      sbis ucsra,txc

      rjmp fandizi

    sbi ucsra,txc

rxcreti:pop temp

      out portc,temp

      pop temp

      out sreg,temp

      pop temp

      reti         



      

xuanzhong:cbi ucsrb,rxcie   ;清屏蔽接收完成中断

          push temp         

          push yh

      push yl

      clr yh

      mov yl,r2

      nop      

          in temp,udr

          st y+,temp

      mov r2,yl

          cpi yl,13

          brne rxcreti_1         

      clr r2

      in temp,sreg

      push temp      

      in temp,portc

      push temp

      push temp0

      clr temp

      out portc,temp

      out sreg,temp

      nop

      rcall xieE_2    ;调用写EEROM

      nop

      nop

    pop temp0         

      pop temp

    out portc,temp   

    pop temp

    out sreg,temp

      sbi ucsra,mpcm

rxcreti_1:sbi ucsrb,rxcie                           

          pop yl

          pop yh         

      pop temp

      reti



         

         

         

;主机程序,轮循方试发送R9、R10、R11、R12内数据

.include"m8535def.inc"

.org $000

rjmp reset

ret

.def temp=r25

.def temp0=r16

.def temp1=r17

ret

.org $020

reset: ldi temp,0x02

       out sph,temp

       ldi temp,0x5f

       out spl,temp

       ..............

       ..............

       ..............

       ..............

       nop

       rcall dushu    ;扫描拔码,读取数据

       rcall chuanshu

       nop

       rjmp reset





chushihua:   ;初始化

   ldi temp,0

   ldi temp0,12

      out ubrrh,temp

    out UBRRL,temp0

    ldi temp, (1<<RXEN)|(1<<TXEN)|(1<<ucsz2)|(1<<txb8)

    out UCSRB,temp

; 设置帧格式:9个数据位, 2 个停止位

    ldi temp, (1<<URSEL)|(1<<USBS)|(1<<UCSZ0)|(1<<UCSZ1)

    out UCSRC,temp

    ret







               

chuanshu:          ;传输开始

         cbi portb,5

   cbi portb,4   ;清传输标志位         

         rcall chushihua

      nop

         rcall dujihao   ;读从机个数并存入R4内

         clr r1

outlp_next:

         sbi UCSRB,txb8   ;传输地址(txb8=1)      

         inc r1

outlp:   clr r2         

         out udr,r1      ;输出从机号,先从1号机开始

tslop:   rcall llop

         inc r2

         breq chuanshu_fnll   ;传输失败指示

         in temp,ucsra

         sbrs temp,7    ;接收完成?   

         rjmp tslop

         nop         

         in temp,udr         

         cp temp,r1

         brne outlp

ldi temp,(1<<RXEN)|(1<<TXEN)|(1<<ucsz2)|(0<<txb8)

         out UCSRB,temp    ;改为传输数据(txb8=0)   

         clr yh   

         ldi yl,9

txlop:   rcall aaz

         ld temp,y+

         out udr,temp

testl:   in temp,ucsra

         sbrs temp,5

         rjmp testl         

         cpi yl,13

         brne txlop

         rcall llop      

         cp r1,r4         ;R4内从机个数

         brlo outlp_next

         sbi portb,4      ;最后个从机传输完成指示

         ret

chuanshu_fnll:sbi portb,5   ;传输失误指示

         cp r1,r4         ;R4内从机个数

         brlo outlp_next               

         nop

         ret

         

               

无论主机设置有多少个从机,从机接收到的都是乱码!

如果主机设置从机个数有变化,则从机接收到的数据也会有变化,但没有规律。

如果主机设置从机个数没有变化,从机机号变化,则接收到的数据也会有变化;

   相同机号接收到的数据是相同的。





不知道错在什么地方,郁闷死了!失去了做事的信心!百思不得其解。

xuhailun 发表于 2005-9-13 08:31:01

收到乱码, 估计是波特率的问题吧.

contactor 发表于 2005-9-30 16:33:27

没看代码,只看了你接收时采用轮询的方式,因为接受标志位是只读的,并且它置高的意思是收到数据,并不是接受数据完成的意思
页: [1]
查看完整版本: 请教USART 实物不能通过???? 付汇编源码