$RB(0) $RB(1) $NOMOD51 INCLUDE(87C752.pdf) $INCLUDE(common.inc) ; Put Character routine CLIBC SEGMENT CODE RSEG CLIBC PUBLIC INTCHAR ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; INTCHAR ; Timing dependent ASSuME 5 cycles average to get here. ; best case is 38 clock ticks worst case is 86 clock ticks. ; *(R1) contains character read. ; R5 gets trashed ; ; At 9600 baud, 104.16667 usec/bit. at 5.24288MHz 1 cycle = 2.28882usec. ; number of cycles per bit is 104.16667 / 2.28882 in this case or, ; 46 (45.5111) cycles per bit. ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; INTCHAR: ;5 cycles (average) to get here. CLR EA ; 1 cycle Turn off interrupts JB SCTS,BAILGETC ; 2 cycles if this is high it isn't for us. PUSH ACC ; 2 cycles PUSH PSW ; 2 cycles PUSH R06 ; 2 cycles PUSH R07 ; 2 cycles ;read first (one) bit. Load a with number of samples left on start bit ;make sure at least 50% of the time the signal is (low) or bail ;16 cycles to get here. MOV R6,#9 ; 1 cycle; 8 bits in, 1 stop bit. MOV R7,#((46-19)/6) ; 1 cycle MOV A,#(((46-19)/6)/2) ; 1 cycle ;19 cycles to get here + 6 cycles each loop. SAMPLE: MOV C,SRD ; 1 cycle SUBB A,#0 ; 1 cycles JB ACC.7,RETGETC ; 2 cycles who cares it's noise. DJNZ R7,SAMPLE ; 2 cycles go for another sample. ;Up to 5 cycles off of the target here, in the case of 9600 baud we are 3 cycles ;too fast here. MOV A,#0 ; 1 cycle reset character repository. ; R7 will contain the time necessary to match 9600 baud. GNEXTBIT: MOV R5,A ; 1 cycle save result of rrc or start. MOV R7,#(46-8)/4 ; 1 cycle MOV A,#(((46-8)/4)/2) ; 1 cycle ; should be basecycles HERE! ; loops = (baseloops-8)/4 NEXTSAMPLE: MOV C,SRD ; 1 cycle SUBB A,#0 ; 1 cycle DJNZ R7,NEXTSAMPLE ; 2 cycles go for another sample. MOV C,ACC.7 ; 1 cycles MOV A,R5 ; 1 cycle RRC A ; 1 cycle DJNZ R6,GNEXTBIT ; 2 cycles ; Check stop bit, should be a 0 JNB ACC.7,RETGETC ; Bailout, the stop bit is wrong. ;Should check the parity, but don't understand it yet. ; should be 10*basecycles HERE! ; Result is put in *R1 MOV A,R5 ; Get 8 bits in. MOV @R1,A INC R1 CJNE R1,#IBUFEND,RETGETC MOV R1,#IBUFSTART RETGETC: POP R07 POP R06 POP PSW POP ACC BAILGETC: SETB EA RETI $EJECT END ;