⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serio_sp.a51

📁 用于51系列单片机的串口程序源码
💻 A51
字号:
$nomod51	symbols	debug	nolist
$INCLUDE(\i\rg51fb.pdf)
$INCLUDE(charname.a51)
$list
;
;	CONSOLE I/O ROUTINES AND DRIVERS:
;	======= === ======== === =======
; S_INIT - Initializes Serial port.
;
; C_IN   - waits for character from serial port. Returns it in A.
; C_OUT  - sends character in A.
; C_STS  - console status. if char RXD, C=1, A=char, else C=0.
; STROUT - Sends in-line character string to console. String is terminated
;	by last character's MSB set.  Hence, can only handle 7 bit ASCII.
;
;********************************************
;
; Split Baud rate manually set
;  using Timer 2 for XMIT and Timer 1 for RCV.
;
;********************************************
;
CODE_SEG	segment	code
public	S_init,C_IN,C_OUT,STROUT
;public	baudload
;
	rseg	CODE_SEG
;
;BAUD rate table for Timer 1
;===========================
;Baud_Rate = Fosc/12 * (2^smod/32) / (256-TH1)
;	where 2^smod = 2 for smod=1, and 1 for smod=0
;	Fosc is oscillator frequency; and TH1 is timer 1 reload value.
;TH1 =  256 - (Fosc/12 * (2^smod/32) / Baud_Rate)
;
;Fosc =	12MHz	16MHz	20MHz	11.0592	14.7456	18.4320	smod
;Rate
;150	030H	--	--	040H	000H	-- 	0
;300	098H	075H	052H	0A0H	080H	060H	0
;600	0CCH	0BBH	0A9H	0D0H	0C0H	0B0H	0
;1200	0E6H	0DEH	0D5H	0E8H	0E0H	0D8H	0
;2400	0F3H	0EFH	0EAH	0F4H	0F0H	0ECH	0
;4800	*	*	0F5H	0FAH	0F8H	0F6H	0
;9600	--	--	*	0FDH	0FCH	0FBH	0
;19200	--	--	--	0FDH	0FCH	0FBH	1
;38400  --	--	--	--	0FEH	--	1
;76800	--	--	--	--	0FFH	--	1
;* These baud rates available by using the value above, and setting SMOD=1
Baud1Load	equ	0f6h	; 18.432MHz, 9600, 2x
;
;************************************************
;
;BAUD rate table for Timer 2
;===========================
;Baud_Rate = ( Fosc / 32 ) / (65536 - (RCAP) )
;	Where RCAP = RCAP2H,RCAP2L, taken as a 16 bit unsigned integer.
;RCAP = 65536 - ( Fosc / 32 ) / Baud_Rate
;
;Fosc =	12MHz	16MHz	20MHz	11.0592	14.7456	18.4320
;Rate
;110	-3409	-4545	-5682	-3142	-3994	-5236
;150	-2500	-3333	-4167	-2304	-3072	-3840
;300	-1250	-1667	-2083	-1152	-1536	-1920
;600	-625	-833	-1042	-576	-768	-960
;1200	-312	-417	-521	-288	-384	-480
;2400	-156	-208	-260	-144	-192	-240
;4800	-78	-104	-130	-72	-96	-120
;9600	-39	-52	-65	-36	-48	-60
;19200	---	-26	-33	-18	-24	-30
;38400  ---	-13	-16	-9	-12	-15
;57600	---	---	---	-6	-8	-10
;76800	---	---	---	---	-6	---
;115200 ---     ---	---	-3	-4	-5
;
Baud2Load	equ	-60	; 18.432 MHz, 9600
;
S_INIT:
	CLR	TR1
	MOV	SCON,#01011010B	;TI set indicates transmitter ready.
				; mode 1,REN
	MOV	T2CON,#00010000B; Initialize Timer 2 as Baud Rate generator
				; for transmit only
;	ANL	PCON,#not SMOD	; Set SMOD to single baud rate
	orl	PCON,#SMOD	; Set to double rate.
	mov	rcap2h,#High Baud2Load	; Set reload value
	mov	rcap2l,#low Baud2Load
	MOV	TMOD,#00100001B	;Timer 1 is set to 8 bit auto reload mode
	orl	PCON,#SMOD	; Set to double rate.
	mov	TH1,#baud1load
	setb	tr2		; start timer.
	setb	tr1
      	ret
;
;=======
;
C_IN:
;	Console character input routine.
;	Waits for next input from console device and returns with character
;	code in accumulator.
;
        JNB     RI,$            ;Wait until character received.
        MOV     A,SBUF          ;Read input character.
	CLR	RI		;Clear reception flag.
	ret			;
;
;=======
;
C_OUT:
;	Console character output routine.
;	Outputs character received in accumulator to console output device.
;
        JNB     TI,$            ;Wait until transmission completed.
        CLR     TI              ;Clear interrupt flag.
	MOV	SBUF,A		;Write out character.
	RET
;
;=======
;
;Console Status
;
; Returns C=0 if no character is ready
; if character ready, returns C=1 and character in A
; Note: Serial input status can be checked by RI bit, also.
;
C_STS:	MOV	C,RI
	JNC	CNTRET		;Poll whether character has been typed.
	CALL	C_IN
CNTRET:	RET
;
;=======
;
;STROUT
;	Copy in-line character string to console output device.
;	uses:	DPTR,ACC
;
STROUT:	POP	DPH		;get in-line string address from stack
	POP	DPL
STRO_1:	CLR	A
	MOVC	A,@A+DPTR	;Read next byte.
	INC	DPTR		;Bump pointer.
	JBC	ACC.7,STRO_2	;Escape after last character.
	CALL	C_OUT		;Output character.
	SJMP	STRO_1		;Loop until done.
;
STRO_2:	CALL	C_OUT		;Output character.
	CLR	A
	JMP	@A+DPTR		;Return to program.
;
	end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -