;Serielle Kommunikation mit Software-UART mit Baudratenmessung ;mögliche Baudrate bei 8MHz Osci: ca. 4800...19200 Baud .def Baud = r18 .def shift = r19 .def Bcnt = r20 .def BitTime = r21 Start: ;Master sendet $FF dh. nur Startbit ist Low sonst alles High BitMes: sbis PINB,Rx ;warten bis Idle (Rx=High) rjmp BitMes Bitm1: sbic PINB,Rx ;warten bis Startbit beginnt (Rx=LOW) rjmp Bitm1 clr BitTime Bitm2: inc BitTime ;zähle solange Startbit nop nop nop ;Mess-Loop dauert 10 Cycles nop ;genau wie BaudrateDelay-Loop bei SerIn und SerOut nop nop sbis PINB,Rx ;End of Startbit? rjmp Bitm2 dec BitTime ;BitTime enthält Startbitlänge Main: Loop: rcall SerIn ;Warten bis Command von Master empfangen ... ;AD Wandlung ... in shift,ADC ;ADC lesen rcall SerOut ;Wert senden rjmp Loop ; Serial Subroutines SerIn: sbis PINB,Rx ;wait until StopBit rjmp SerIn Seri2: sbic PINB,Rx ;wait until Startbit rjmp Seri2 ldi Bcnt,8 ;8 Databits mov Baud,BitTime lsr Baud ;1.5 BitTime add Baud,BitTime Seri3: rcall SerDel dec Baud brne Seri3 ;Baudrate-Delay (Loop=10 Cycles) mov Baud,BitTime ;1 BitTime lsr shift sbic PINB,Rx ;read Bit ori shift,$80 dec Bcnt brne Seri3 Seri4: sbis PINB,Rx ;wait until StopBit rjmp Seri4 ret ;Received Byte in shift SerOut: ldi Bcnt,11 ;8 Data +Start+2StopBit Byte to send in shift clc ;StartBit Sero1: mov Baud,BitTime ;1 BitTime sbi PORTB,Tx brcs Sero2 ;Carry to TXD cbi PORTB,Tx Sero2: rcall SerDel ;Baudrate-Delay (Loop=10 Cycles) dec Baud brne Sero2 sec ;StopBits ror shift ;next Bit in Carry dec Bcnt brne Sero1 ret SerDel: nop ;Delay 5 Cycles nop nop ret