; Software Universal Asyncronous Receiver Transmitter ; I have seen many versions of this but ALL of them ; excluded waiting for the stop bit in the receiver. ; This caused framing errors. I noticed this when I got : FF's between all characters. It was seeing the high bit ; of the char as a start bit. I have a few baud rates ; worked out, they're primarily trial and error values. ; I haven't worked out an algorithm to compute the rate yet. ; The high byte of the rate MUST be at least 1 to exit ; the loop properly. I'm currently using this in my monitor ; at 9600 baud as the main I/O. The internal serial is used ; for midi info at 31250 baud. alone set 0 ; set to 1 if in monitor RPIN set P1.0 ;Receive on this pin TPIN set P1.1 ;Transmit on this pin d9600 equ 48 d4800 equ 101 d300 equ 1950 nfbit equ d9600 nhbit equ d9600/2 nqbit equ d9600/4 ; 1/4 ntbit equ nhbit + nqbit ; 3/4 tfhbit: mov r6,#low Ntbit mov r7,#high Ntbit or 1 ; at least 1 sjmp hofb hafbit: mov r6,#low Nhbit mov r7,#high Nhbit or 1 ; at least 1 sjmp hofb fulbit: mov r6,#low Nfbit ; 1 mov r7,#high Nfbit or 1 ; at least 1 hofb: djnz r6,hofb ; 2 djnz r7,hofb ; 2 ; if 1 then exit nobit: ret ; 2 ; ;Transmit character in A via TPIN line ; if alone outt: sndchr: endif putc: push 1 push 6 push 7 CLR TPIN ;Drop line for start bit call fulbit MOV R1,#8 ;Send 8 bits putc1: RRC A ;Move next bit into carry MOV TPIN,C ;Write next bit call fulbit djnz r1,putc1 SETB TPIN ;Set line high for stop bit rrc a call fulbit pop 7 pop 6 pop 1 RET ; receive a character if alone getchr: endif getc: push 1 push 6 push 7 getc1: JB RPIN,$ ; Wait for start bit getch2: call hafbit jb RPIN,getc1 ; false start mov r1,#8 getc3: call fulbit ; wait till middle of bit MOV C,RPIN ; Read bit RRC A ; Shift bit into ACC DJNZ R1,getc3 ; read all bits call tfhbit ; gobble stop bit pop 7 pop 6 pop 1 ret ; chkbrk: jb RPIN,nobrk ; start bit ? acall getc ; yes, get char cjne a,#3,nobrk ; break ? jmp warm ; yes nobrk: ret ; sorry to bother you