Serin loop of death

Status
Not open for further replies.

cybersky

Member
Hi All

I have been building small pic networks using the rs232 interface, hardware (16f874) or software version (16f84). PROBLEM. If you use serin it will wait for the amount of bytes specified, forever. IS there a timeout available ?
 
Hi All

I have been building small pic networks using the rs232 interface, hardware (16f874) or software version (16f84). PROBLEM. If you use serin it will wait for the amount of bytes specified, forever. IS there a timeout available ?

You could consider using a timer interrupt, start it when you use the Serin command.
 
Write you own routine. I assume you can check the RXIF bit.

Mike.
 
so if i use the timer i could just chk for predefined timeout value then simply goto next code block? rxif is that for software or hardware only ? so if rxif bit is xx goto chkdata something to that effect ?
 
The bit PIR1.RXIF gets set when a byte is received. The received byte is in RCREG, reading this register clears RXIF. This is for hardware only.

Edit, how do you check a timer in a continuous loop?

Mike.
 
Last edited:
Pommie... Its a software rx tx so no interrupts or flags... The best thing is what Eric said Start a timer interrupt, if it times out stop the serin.
 
The OP mentioned both hardware and software. However, even software serial can be written to mimic hardware by using interrupts.

And, to repeat myself, how do you check for a timer timeout in a library routine? Plus, how do you stop a library routine from an interrupt?

Mike.
 
See what you mean.....Duh!!!..... sorry....shutting up now.
 
an example for 16F84 shown below
Code:
Define CONF_WORD = 0x3ff2

Dim x As Bit
Dim y As Byte

init:
AllDigital
x = 1

main:
Serout RB0, 9600, "Hello world!", CrLf
While x = 1
		Call serin_timeout(100)
Wend
End                                               
Proc serin_timeout(t As Byte)
	Dim serin_flag As Bit
	serin_flag = 0
	'enable prescaler for tmr0
	OPTION.PSA = 0
	'set tmr0 prescaler for 8
	OPTION.PS2 = 0
	OPTION.PS1 = 1
	OPTION.PS0 = 0
	'enable tmr0
	OPTION.T0CS = 0
	'clear tmr0
	TMR0 = 0
	While TMR0 < t
		If PORTB.1 = 0 Then
			Serin RB1, 9600, y
			Serout RB0, 9600, y, CrLf
			serin_flag = 1
		Endif
	Wend
	If serin_flag = 0 Then
		Serout RB0, 9600, "TIMEOUT", CrLf
	Endif
	'clear tmr0
	TMR0 = 0
	'disable TMR0
	OPTION.T0CS = 1
End Proc
 
hi all - thanks for the replies. too busy at work to even chk email let alone my fav forum. I will try code languer and give feedback asap. Thanks again
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…