;File: urt42cfg.a51
;Author: Eckart Hartmann Date:17/10/2003
;Development progress: Urt834.df
;
;UrtCfg==========Configure UART and baudrate.
;C Function prototype: char _UrtCfg(char cCfg, int iDiv);
;Description of Function: Configures UART and selects baudrate.
;User interface: One byte must be allocated in DATA segment for cUrtVar.
;		Into parameter1 put:
;			0 to disable UART.
;			1 to use T1 for baudrate.
;			2 to use T2 for baudrate.
 ; 3 to use fractional divider for baudrate.
;		To parameter1 add:
;			0 for no parity 1 stop bits.
;			4 for no parity 2 stop bits.
;			8 for odd parity 1 stop bit.
;			12 for even parity 1 stop bit.
 ; To parameter1 add desired CD bits times 16 (cCfg += CD<<4).
;		Put the reload value required for baudrate in parameter2.
;		Call _UrtCfg to set the above configuration.
;		Parameter1 is put in cUrtVar with MSB cleared.
;		Returns value of cUrtVar. 
;Robustness:	CD bits must be correct when communicating.
;		For T1 only low byte of parameter2 is used.
 ; For fractional divider low byte is T3FD high byte T3CON .
;		Program ADuCBr.exe is provided to determine parameters.
;Side effects: Overwrites a, cy, p and r1.
;		UART register SCON is changed.
;		If T1 is used TMOD, TCON, PCON.7 and TH1 are changed.
;		If T2 is used TMOD, T2CON, RACP2H and RCAP2L are changed.
;		If fractional divider is used T3FD and T3CON are changed.
;
NAME URTCFG
$NOMOD51
 $IC(..kei842.inc) ; Parameter passing registers for Keil .
 $IC(..kei842.dat) ; SFR definition for Keil .
;
extrn DATA (cUrtVar)
public _UrtCfg

?PR?_UrtCfg?URTCFG	SEGMENT CODE
	RSEG	?PR?_UrtCfg?URTCFG

_UrtCfg:
	mov	a,cP1l		;Clean parameter1.
	clr	ACC.7
	mov	cP1l,a
	mov	r1,#cUrtVar	;Save parameter1 to cUrtVar.
	mov	@r1,a
	anl	a,#3		;Check which timer to use.
	jnz	UrOn		;if(cP1l==0)
	mov	T3CON,#00h	; { stop T3 use.
	anl	T2CON,#0cfh	; Disable T2 baudrates.
	anl	SCON,#0efh	; Disable UART. }
UrOn:	cjne	a,#1h,UrQ2
	sjmp	UrIs1
UrQ2:	cjne	a,#2h,UrIs3
UrIs2:	mov	RCAP2H,#iP2hc
	mov	RCAP2L,#iP2lc
	mov	T3CON,#00h	;Stop T3 use.
	mov	T2CON,#034h	;T2 timer start for baudrates.
	sjmp	UrPty
UrIs3:	mov	T3FD,iP2lc	;Set fractional divider
	mov	T3CON,iP2Hc
	orl	T3CON,#080h	; and start.
	sjmp	UrPty
UrIs1:	orl	PCON,#080h	;Double baud.
	anl	TMOD,#0cfh
	orl	TMOD,#020h
	anl	T2CON,#0cfh	;Disable T2 baudrates.
	mov	T3CON,#00h	;Stop T3 use.
	mov	TH1,#iP2lc
	setb	TR1		;Start.
UrPty:	mov	a,cP1l		;Set
	anl	a,#0ch
	mov	SCON,#50h	; 8 bits +REN.
	jz	UrTI
	mov	SCON,#0d0h	; or 9bits +REN.
UrTI:	setb	TI		;Ready to transmit.
UrtCR:	mov	a,@r1		;Return cUrtVar. 
	mov	cP1l,a
	ret
;
;Function End==========================================================Function End
end
;
