;Dick Cappels' project pages sap@cappels.org - you should be able to copy and paste this text into an assembler file.
   ;©2002 Richard Cappels All Rights Reserved. Republication is unrestriced provided proper attribution
;is given and notice of publicaton is sent to projects@cappels.org
;HOME ; ; DS send and receive a byte. ; Two I/O pins are used, one for data/signaling and one only for signaling. ; The pins are bidirectional with weak pullups (or optionally, exernal pullups) ; Protocol is byte based via the rxtxbyte register. ; A 9th bit is transferred through the carry bit. When a 1, data is being transferred. ; When the 9th bit is a 0, an instruction is being transferred. ;.include "tn12def.inc" .include "2313def.inc"; REMEMBER TO INITIALIZE THE STACK POINTER .def rxtxbyte = r2 ;byte to be sent .def temp = r16 ;scratch .def delayc = r17 ;counter used for timing delay .def bitcount= r18 ;counter for number of bits transfered .equ dataline= 0 ;B0 Data line .equ attline = 1 ;B1 Attention line .equ dout = 2 ;B2 Data output pin for test purposes .equ setltime= $01 ;How long to wait for data and I/O lines to settle .ORG $0000 ldi temp,low(ramend) out spl,temp ;set the 8 bit stack pointer to the top of ram FOR AT90S2313 ldi temp,$04 out DDRB,temp ldi temp,$03 ;Set B0 and B1 weak pullups high. out PORTB,temp cbi PORTB,dout rcall shortdelay main: ldi temp,$00 mov rxtxbyte,temp clc rcall SendByte rjmp main ;//////////SEND A BYTE ; Send carry bit as first bit, then send rxtxbyte bit-by-bit. rxtxbyte destroyed. SendByte: ldi bitcount,$09 S1: sbis PINB,attline ;Wait for Attention line to go high =--SEND A BIT rjmp S1 sbis PINB,dataline ;! This instruction not in flow chart and not extensively tested rjmp S1 ;but is included because it covers a theoretical risk. sbi PORTB,dataline ;Put data on data line cbi DDRB,dataline brcs R1 cbi PORTB,dataline sbi DDRB,dataline R1: cbi PORTB,attline ;Set Attention line low sbi DDRB, attline rcall shortdelay ;Wait a short time so other chip can see Attnetion line is low cbi DDRB,attline ;Release Attention line for a peak sbi PORTB,attline rcall shortdelay ;Short delay to allow settling of lines sbic PINB,attline ;If Attention line isn't low, go back and put it low again, else continue rjmp R1 brcs szero ;Invert data line sbi PORTB,dataline cbi DDRB,dataline rjmp R3 szero: cbi PORTB,dataline sbi DDRB,dataline R3: sbis PINB,attline ;Wait for Attention line to go high rjmp R3 cbi DDRB,dataline ;Let data line float sbi PORTB,dataline ;-finished sending a bit rol rxtxbyte ;Shift rxtxbyte through carry dec bitcount brne S1 ;Continue until all bits sent ret ReceiveByte: ;Get a byte into rxtxbyte, and "start bit" into carry bit. ldi bitcount,$09 W3: ;Get a bit from the input into the carry sbic PINB,attline ;Wait for Attention line to go low rjmp w3 clc ;Latch dataline into carry bit sbis PINB,dataline rjmp NotaOne sec cbi PORTB,attline ;ACK by pulling attention line low sbi DDRB, attline W1: ;Wait for data line to go low sbic PINB,dataline rjmp W1 rjmp RelesaseAttLineAndGo NotaOne: cbi PORTB,attline ;ACK by pulling attention line low sbi DDRB, attline W2: ;Wait for data line to go high sbis PINB,dataline rjmp W2 RelesaseAttLineAndGo: ;Release attention line cbi DDRB, attline sbi PORTB,attline ;DONE RECEIVING BIT rol rxtxbyte ;Shift rxtxbyte through carry dec bitcount brne W3 ;Continue until all bits sent ret shortdelay: ldi delayc,setltime D1: dec delayc brne D1 ret ;http://projects.cappels.org/ ;HOME