Skip to main content
Knowledgebase
Home
Renesas Electronics Europe - Knowledgebase

How to obtain a program that construct UART using 8 bit timer H and port?

Latest Updated:02/01/2009

Question:

I would like to obtain a simple program which construct UART using 8-bit timer H and a the general port.

Answer:

These are examples of UART which used timer H and a the general port.
The setting condition is follows.
(1) Communication method: Asynchronous mode
(2) Communication speed: 9600 bps
(3) Data length: 8 bit
(4) Parity: none
(5) Stop. bit: 1 bit
(6) LSB first
The communication shall be done by a half-duplex methodology.
We show 78K0S/KY+ as an example have.

  • (TxD) : P40
  • (RxD) : P21/INTP0

1. Transmission
The communication format of transmitting data "55H" in shown below.



The transmission of a piece of data output START bit "0", bit0 to bit7 of transmitting data and also "1" of STOP bit with internal time "T".

1-1. The transmission operation
The shows the transmission operation of 1 byte data, but it requires the following initialization.
[1] Output "1" with TxD of P40
[2] Set 8-bit timer H1

  • Set interval timer
  • Interval time = 104 µs (9600bps)

[3] Set interrupt function of 8-bit timer H1

  • At first, set interrupt function prohibit.

[4] Set transmitting data in A register.
[5] Set transmitting count in B register   B=09H (9, including STOP bit)
[6] Set transmitting end flag in C register

  • intial value amount = 00H


(1) The start of transmission.


(2) Data transmission (interrupt routine of 8-bit timer H1)
Output bit0 to bit7 of a data and STOP bit.


2. Receiving
The communication format of tran receiving data "55H" in shown below.



UART receiving operation is started, to detect START bit "0" at first.
The falling of terminal P21/INTP0 is recognized as the start signal of STRAT bit.
Following Time "T", the terminal P21/INTP0 is sampled, and START bit "0" must be confirmed.
(START bit confirmation)
Secondly, the terminal P21/INTP0 is sampled at each Time "T" and f set D0 to D7 as a data bit.
Finally, the terminal P21/INTP1 is sampled, and after STOP bit is "1" is confirmed ,then the data receiving operation is ended.
If, STOP bit is "0", it is regarded as framing error.


(1) Receiving opration
The flowchart of Receiving with 1 data is shown below, however it nees initialization at first.
[1] (RxD) is P21, and use the interrupt function of INTP0. (For detection of START bit)
[2] Enable the interrupt function execution of INTP0.
[3] Set 8-bit timer H1

  • Set it is Interval timer
  • Interval time = 52 µs (1/2 of 9600bps)

[4] Sampling of data bit is carries out by interrupt function execution of 8-bit timer H1 at the timing of 52 µs × 2 of the "t".
[5] Set interrupt function execution 8-bit timer H1

  • Initial value must be prohibit of interrupt function execution.

[6] Receiving data must be set to A register
[7] Receiving bit count must be set to B register B=08H (data length: 8-bit)
[8] Receiving end flag must be set to RAM area    Initial value: 00H
[9] error flag must be set to RAM area    Initial value: 00H
[10] Interrupt count must be set to RAM area    Initial value: 00H
[11] START bit detection flag must be set to RAM area    Initial value: 00H

(2) Start receiving


(3) START bit detection (INTP0 Interrupt routine)


(4) Receiving data (8-bit timer H1 interrupt routine)


3. Transmit program list

 
;*************************************
;UART USE TIMERH1      Transmit
;*************************************
DWORK DSEG AT 0FE80H
END_FLAG: DS 1
TX_DATA: DS 1
;
 CSEG
@VECT CSEG AT 0
 DW START
 ORG 000CH
 DW INTTMH1
;
OPT_SET CSEG AT 80H
 DB 94H  ;clk set
 DB 0FFH
;
 CSEG
PPP CSEG AT 200H
START:
 MOV WDTM,#77h ;WDT OFF
 DI
 MOVW AX,#0FEFFH
 MOVW SP,AX
CLK_INZ:
 MOV PCC,#00H ;CPU CLOCK=fx
 MOV PPCC,#00H
 MOV LSRCM,#00H ;LOW SPEED CLOCK=OFF
 MOV OSTS,#00H ;CLOCK START TIME=1024usec
;
PORT_INZ:
 MOV P4,#01H
 MOV PM4,#00H ;PORT MODE = OUT
TM_H_INZ:
 MOV TMHMD1,#10H ;CLK=2MHz INTERVAL MODE
 MOV CMP01,#0D0H ;T=104usec
 SET1 TMMKH1   ;MASK
 SET1 TMIFH1  ;IF FLAG=0
 CLR1 PMK1  ;INTP1 ENABLE
;
 EI
;********************************
MAIN:
 MOV END_FLAG,#00H ;END FLAG=00H
 MOV TX_DATA,#33H ;TX DATA
TX:
 MOV A,TX_DATA ;TX DATA SET
 MOV B,#0AH  ;TX BIT NUMBER
 CLR1 TMMKH1  ;INT MASK OFF
 CLR1 P4.0  ;START BIT OUT
 SET1 TMHE1  ;TMH1 START
;
LOOP:
 BF END_FLAG.0,$LOOP
 MOV END_FLAG,#00H
;
LOOP1:
 BR $LOOP1
;
;*********************************
INTTMH1:
 DEC B
 BZ $END_RET
 BT A.0,$OUT_1
 CLR1 P4.0  ;P40=0
 BR $DATA_SHIFT
OUT_1:
 SET1 P4.0  ;P40=1
DATA_SHIFT:
 SET1 CY
 RORC A,1
 RETI
;
END_RET:
 MOV END_FLAG,#0FFH
 CLR1 TMHE1  ;TMH STOP
 SET1 TMMKH1  ;MASK
 CLR1 TMIFH1
 RETI
;
 END


4. Receve program list

;*************************************
;UART USE TIMERH1    Receve
;*************************************
DWORK	DSEG	AT 0FE80H
END_FLAG:	DS	1
START_BIT:	DS	1
ERR_FLAG:	DS	1
INT_CNT:	DS	1
RX_DATA:	DS	1
;
	CSEG
@VECT	CSEG	AT	0
	DW	START
	ORG	0008H
	DW	INTP0
	ORG	000CH
	DW	INTTMH1
;
OPT_SET CSEG AT 80H
	DB	9CH		;clk set
	DB	0FFH
;
	CSEG
PPP	CSEG	AT	200H
START:
	MOV	WDTM,#77h	;WDT OFF
	DI
	MOVW	AX,#0FEFFH
	MOVW	SP,AX
CLK_INZ:
	MOV	PCC,#00H	;CPU CLOCK=fx
	MOV	PPCC,#00H
	MOV	LSRCM,#00H	;LOW SPEED CLOCK=OFF
	MOV	OSTS,#00H	;CLOCK START TIME=1024usec
;
PORT_INZ:
	MOV	PM2,#0FFH	;PORT MODE = IN
	MOV	PMC2,#00H	;P21=PORT MODE
	MOV	P4,#00H
	MOV	PM4,#00H
;
INTP0_INZ:
	CLR1	PIF0		;INTP0 IF=0
	SET1	PMK0		;INT DISABLE
	MOV	INTM0,#00H	;INTP0 LOW 
	SET1	PU2.1		;P21 PULL UP
;
TM_H_INZ:
	MOV	TMHMD1,#10H	;CLK=2MHz INTERVAL MODE
	MOV	CMP01,#68H	;T=52usec
	SET1	TMMKH1  	;MASK
	SET1	TMIFH1		;IF FLAG=0
;
	EI
;********************************
MAIN:
	CLR1	PIF0
	CLR1	PMK0		;INTP0 ENABLE
MAIN1:
	MOV	END_FLAG,#00H
	MOV	INT_CNT,#00H
	MOV	START_BIT,#00H
	MOV	ERR_FLAG,#00H
	MOV	RX_DATA,#00H
;
RX:
	NOP
	BF	END_FLAG.0,$RX		;END RX FLAG = FFH?
	BT	ERR_FLAG.0,$FLAG_CLR
	MOV	RX_DATA,A
;
FLAG_CLR:
	MOV	END_FLAG,#00H
	MOV	ERR_FLAG,#00H
;	
LOOP:
	BR	$LOOP
;**********************************
INTP0:
	SET1	TMHE1		;TMH1 START
	CLR1	TMIFH1	
	CLR1	TMMKH1
	SET1	PMK0		;INP0 MASK
	MOV	B,#08H		;DATA 8 BIT
	MOV	INT_CNT,#00H	;INT COUNT
	MOV	A,#00H
;
	RETI
;**********************************
INTTMH1:
	BT	START_BIT.0,$RECV_DATA
	BT	P2.1,$END_ERR
	MOV	START_BIT,#01H		;START BIT DETECTION
	BR	$R_RET
;
RECV_DATA:
	INC	INT_CNT			;INTTMH1 COUNT
	BT	INT_CNT.0,$R_RET
;
	INC	B
	DEC	B			;B=0 CHECK
	BZ	$STOP_BIT_CHK
;
	ROR	A,1			;A RIGHT SHIFT
	BT	P2.1,$R_BIT1
	CLR1	A.7			;R_BIT SET=0
	BR	$R_DATA_SHIFT
R_BIT1:
	SET1	A.7			;R_BIT SET=1
R_DATA_SHIFT:
;	ROR	A,1			;RECV DATA SHIFT
;
	DEC	B			;RECV BIT COUNT
	BR	$R_RET
;
STOP_BIT_CHK:
	BT	P2.1,$END_RET
;
END_ERR:
	MOV	ERR_FLAG,#0FFH		;ERR RX FLAG SET
;
END_RET:
	MOV	END_FLAG,#0FFH
	MOV	START_BIT,#00H
	CLR1	TMHE1			;TMH STOP
	SET1	TMMKH1			;MASK
	CLR1	TMIFH1
	CLR1	PIF0
	CLR1	PMK0			;INP0 INT ENABLE
R_RET:
	RETI
;
	END

(2009/02)

Suitable Products
78K0S/Kx1+