• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Software Urat help required.

Thread starter #1
Hello Friends,

Need help with the below code.

Code:
Define SERIN_TIMEOUT_REG = INTCON
Define SERIN_TIMEOUT_BIT = TMR0IE

slave: 'check communication with slave
i = 0
slaveready = False
Serout PORTB.0, 9600, "A", CrLf
loop:
INTCON = %10100000
T0CON = %11100111
    Serin PORTB.1, 9600, serdata
    If serdata > 0 Then
        If serdata = "K" Then  'OK received
            slaveready = True
            INTCON = %00000000
            T0CON = %01100111
            Return
        Endif
    Endif
    
    i = i + 1
    If i = 10000 Then
        INTCON = %00000000
        T0CON = %01100111
        Return  'timeout
    Endif
Goto loop
    Return
The above code works well without the timeout, but i need to put timeout in there.
PIC18f2520 @20mhz

Any help suggestion shall be helpful.

Cheers
Mastero
 
#2
I used TMR1 as a timer to check for serial data (if alive) vs cancellling. However, the same logic can be used for the serial "interrupt" bit. The code below is only a segment of the code, as it was written for someone else and I'm only showing the section where I check the software serial input pin while waiting for a 50ms timeout. I have another example somewhere where I use a data input pin to stop the serin, but that method is rather common knowledge. This is just sections of code, not complete, but to show how I use a timer to check for traffic (and could be used to cancel things). No interrupts are used, just checking the flag status (use same flag to cancel SERIN in your case) Change timer to meet your timing requirements.
This code was for a lowly 12F675 chip. MUST use a crystal to get accurate baud rates.



Code:
'Use 8Mhz crystal
Define CLOCK_FREQUENCY = 8.0  '8 Mhz gives least error at 9600, 0.16%TX, 0.64%RX

.
.
.

Dim t1h As Byte  'Time high byte
Dim t1l As Byte  'Timer low byte
Dim tcon As Byte  'Timer prescalere and Set on

.
.
.

T1CON = 0  'make sure timer is off

.
.
.
.

'Set default loop of 50ms delay
t1h = 0x3c
t1l = 0xb0
tcon = 0x11

.
.
.


'Start 50ms timer

PIR1.TMR1IF = 0  'clear Timer1 flag
TMR1H = t1h  '50ms high value as defined at start
TMR1L = t1l  '50ms low value as defined at start
T1CON = tcon  'define 1:2 prescaler, enable timer1 to "on"
'In above, tcon variable happens to be 0x10 for prescaler, and 0x01 to start timer (TCON=0x11)
'for all times in the 40-60ms range. Other ranges may change tcon, hence it is now a variable defined
'in the initialization code near top. 80ms requires tcon=0x21

'Following loop is about 3.5 to 5us, faster than interrupt for RX checks
'due to fact interrupt routine saves registers, etc. before checking....
'Interrupt routine took about 9.5us, this is faster...

'NOTE:
'RCX is the serial input pin, and checked for change in polarity from idle.
'This value check is either 1 or 0, depending if the serial data is inverted or not
'What I do here is loop for the timer timeout (50ms), and if any data shows up (9600 baud)
'the system is fast enough to go do the serial read without missing anything.
' The 5uS delays in checking the bit is <0.5% error st 9600 baud (1mS/char)


lp:
If PIR1.TMR1IF = 1 Then Goto timeup  '50ms timer
If rcx = 0 Then Goto lp
Goto readfreq  'data coming in, loop immediately to start read. Next readbyte is 5.5uS later

timeup:
T1CON = 0  'disable timer
PIR1.TMR1IF = 0  'clear flag

.
.
.
 
#3
Looking at your first few line, you are using TMR0IE. Should if not be "TMR0IF"?
TMR0IE will always be set and never cleared if you enable that interrupt. No need for interrupts, just clear the timer flag and restart the timer, check the timer overflow flag.
 

Latest threads

EE World Online Articles

Loading

 
Top