Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Bitbanging code help for 8051!!

Status
Not open for further replies.

nerdspot

Junior Member level 2
Junior Member level 2
Joined
Oct 28, 2006
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,444
mov r0,#bittim

I got a bit banging code online from **broken link removed**, sorta modified it and used it for my AT89C51. I'm using a 3.5795Mhz clock and a 300 baud transfer rate. Can some one help me out with a valid bit banging code? It sends the number 9 and receives it on another 8051 and displays the result on port0 on 8 LED's. now the problem is, when i connect it, all the LEDs light up or sometimes, they light up randomly. Please help..
 

Your code :
Code:
;Rx at another AT89C51
SEND EQU P1.0 ;Transmit on this pin
RECV EQU P1.1 ;Receive on this pin
; The serial baud rate is determined by the processor crystal, and
; this constant which is calculated as: (((crystal/baud)/12) - 5) / 2
BITTIM EQU 494 ;(((3579545/300)/12) - 5) / 2
;
getc:
loop: 
  JB    RECV,loop     ; Wait for start bit
  MOV   R0,#BITTIM/2  ;Wait 1/2 bit-time
loop1: 
  DJNZ  R0,loop1      ; To sample in middle
  JB    RECV,getc     ;Insure valid
  MOV   R1,#8         ;Read 8 bits
getc1: 
  MOV   R0,#BITTIM    ;Wait full bit-time
loop2: 
  DJNZ  R0,loop2      ; For DATA bit
  MOV   C,RECV        ;Read bit
  RRC   A             ;Shift it into ACC
  DJNZ  R1,getc1      ;read 8 bits
  MOV   P0,A
  
  END

After excute MOV P0,A 8051 is loss control, we don't know what it'll doing, so you see :
all the LEDs light up or sometimes, they light up randomly.

Add SJMP $ after MOV P0,A
 

    nerdspot

    Points: 2
    Helpful Answer Positive Rating
then how do i display any data? Do i loop the txd and rxd instructions? is my modification of the bitbanging code correct?

Added after 6 minutes:

so, with $, it'll loop back to the first address.. right? but i have to do that to the transmitter too or there wont be any data received, correct?
 

SJMP $ is equivalent to :
Code:
Here:
     SJMP Here
it is the way to "stop" 8051

If you want repeat the whole process replace SJMP $ with SJMP getc, and add SJMP putc at the other program
 

    nerdspot

    Points: 2
    Helpful Answer Positive Rating
Cool.. Thx man.. will try it out tomorrow and post.. Do i have to put SJMP $ for the transmitting too?
 

Do i have to put SJMP $ for the transmitting too?
It depend on you, whether you want stop the processor (use SJMP $) or you want repeat the process (use SJMP getc and SJMP putc).
 

    nerdspot

    Points: 2
    Helpful Answer Positive Rating
When i run the code(with the modified code) in both the transmitting and recieving uc, no LEDs glow.
 

Transmitter:
Code:
;Tx from one AT89C51
SEND EQU P1.0 ;Transmit on this pin
RECV EQU P1.1 ;Receive on this pin
; The serial baud rate is determined by the processor crystal, and
; this constant which is calculated as: (((crystal/baud)/12) - 5) / 2
BITTIM EQU 494 ;(((3579545/300)/12) - 5) / 2
;
; Transmit character in A via TXD line

putc:
      MOV   A,9
      CLR   SEND           ;Drop line for start bit
      ACALL bittime        ;start bit
      MOV   R1,#8          ;Send 8 bits
putc1:
      RRC   A              ;Move next bit into carry
      MOV   SEND,C         ;Write next bit
      ACALL bittime        ; data bits
      DJNZ  R1,putc1       ;write 8 bits
      SETB  SEND           ;Set line high
      ACALL bittime        ;stop bit
      SJMP  putc

bittime:
      MOV   R0,#BITTIM     ;Wait full bit-time
      DJNZ  R0,$
      RET

      END

Receiver:
Code:
;Rx at another AT89C51
SEND EQU P1.0 ;Transmit on this pin
RECV EQU P1.1 ;Receive on this pin
; The serial baud rate is determined by the processor crystal, and
; this constant which is calculated as: (((crystal/baud)/12) - 5) / 2
BITTIM EQU 494 ;(((3579545/300)/12) - 5) / 2
;
getc:
       JB    RECV,$             ;Wait for start bit
       MOV   R0,#BITTIM/2       ;Wait 1/2 bit-time
       DJNZ  R0,$               ;To sample in middle
       JB    RECV,getc          ;invalid startbit
;
       MOV   R1,#8              ;Read 8 bits
getc1:
       MOV   R0,#BITTIM         ;Wait full bit-time
       DJNZ  R0,$               ;For DATA bit
       MOV   C,RECV             ;Read bit
       RRC   A                  ;Shift it into ACC
       DJNZ  R1,getc1           ;read 8 bits
;
       MOV   P0,A
       SJMP  getc

       END

I don't see any error on your code. Try again!

Re-check the circuit connection, don't forget P1.0 of Transmitter connect to P1.1
of Receiver
Turn on Receiver supply first, after that turn on transmitter
 

    nerdspot

    Points: 2
    Helpful Answer Positive Rating
Hi,

budhy said:
Turn on Receiver supply first, after that turn on transmitter

absolutly correct, some months back i wasted many hours in a project due to this problem.



thanks
sawaak
 

    nerdspot

    Points: 2
    Helpful Answer Positive Rating
Is
Code:
MOV   A,9
the correct way of moving the number 9 to the acc? Does it have to be
Code:
MOV   A,#9
 

Yes, it have to be :
Code:
        MOV   A,#9

but it doesn't make receiver can't receive any thing (P0 display nothing, no LEDs glow), except content of memory #9 = 0 or memory #9 = 0xFF (depend on your LED circuit)
 

    nerdspot

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top