PDA

View Full Version : HSERIN with/without interrupt



macinug
- 19th January 2009, 12:21
Hi,
I have my PIC reading from the PC (VB6 program). The code below makes the PIC wait, after switching on, for "XX-" from the PC and will then execute the line depending on the next character: "a","b" or "c". The start up tone is then heard. OK so far!
However I now wish to loop the program permanently, interrupting to execute the Select Case depending on the "XX-" "a", "b" or "c" character, then going back into the loop until the next "XX-x" is recieved.
My question is: can I use the timeout loop of the HSERIN as I have, if so where am I going wrong? Or do I need to use an interrupt? I have seen some stuff on PIR1.5, ON Interrupt etc but it looks out of my league.
Thanks wise ones!

@ DEVICE HS_OSC,LVP_OFF,WDT_OFF,PROTECT_OFF,BOD_OFF,PWRT_OF F
'declare variables
define OSC 20
DEFINE HSER_BAUD 19200 'serial
DEFINE HSER_RCSTA 90h 'serial
DEFINE HSER_TXSTA 20h 'serial
VBIn var byte 'for command from VB
MyBuzzer var portb.0 'labelling ports for clarity

trisb = %11000000 ' set port b 0-5 o/ps 6&7 i/ps

mybuzzer = 0 ' buzzer pin 21
VBIn = 0

hserin [wait("XX-"),VBIn] ' wait for XX and VBIn byte- WORKS
pause 10
select case vbin
case "a"
sound mybuzzer,[60,10]
case "b"
sound mybuzzer,[70,15]
case "c"
sound mybuzzer,[70,15,0,15,60,10]
end select

'Start-up beep
sound mybuzzer,[60,10,70,10,80,20]
mybuzzer = 0

'======= Following loop doesn't work ============================

loop:
vbin = 0 ' reset VBIn
pause 10
hserin 10,loop,[wait("XX-"),vbin] ' loops until XX- is read, then waits for rest

select case vbin
case "a"
sound mybuzzer,[60,10]
case "b"
sound mybuzzer,[60,10,70,10]
case "c"
sound mybuzzer,[60,10,0,15,70,10,0,15,80,10]
end select

goto loop
END

Jerson
- 19th January 2009, 12:53
Hi,
I have my PIC reading from the PC (VB6 program). ...... <snip>....
My question is: can I use the timeout loop of the HSERIN as I have, if so where am I going wrong? Or do I need to use an interrupt?

<snipped>


loop:
vbin = 0 ' reset VBIn
hserin 10,loop,[wait("XX-"),vbin] ' loops until XX- is read, then waits for rest
pause 10

select case vbin
case "a"
sound mybuzzer,[60,10]
case "b"
sound mybuzzer,[60,10,70,10]
case "c"
sound mybuzzer,[60,10,0,15,70,10,0,15,80,10]
end select

goto loop

END
You may like to try this change as suggested in red. It might work, but might cough a little at higher repetitions. You may actually want to do away with the pause 10 altogether.

You need to think interrupts only if your code is doing lots of things and you have the probability of losing data. If the receive register is not read soon enough, it would overflow due to the next character coming in. If you cannot read the register fast enough, you should consider using interrupts on UART receive.

Charles Linquis
- 19th January 2009, 13:23
You are probably better off reading PIR1.5 in your loop. This is a flag that is set whenever the USART has a character in the buffer. It is extremely fast and doesn't waste time if no characters are waiting in the buffer. Just Put a 'IF PIR1.5 THEN...' in your main loop. If it is set, then go to HSERIN and read the characters. Put a small timeout on the HSERIN so it jumps out if ALL the expected characters aren't read.
Of course, this technique generally works best if the USART isn't going to get flooded with a continuous stream of characters.

The PIR1.5 flag gets set whenever the first character comes in, but the buffer holds up to two characters, so you actually have two character "times" to get the buffer read. The HSERIN command clears PIR1.5 when the character(s) are read.

macinug
- 19th January 2009, 17:03
Thanks for the suggestions.
Using PIR1.5 I am getting some response but it only works once in the loop.
Below is my new simplified loop for testing.
I am sending the data from the PC myself manually so there is no barrage of data for it to deal with. You say the PIR1.5 is set to 0 after reading (HSERIN?) but is the buffer also cleared or is there an instruction for that?
I thought the buffer held more than 2 characters. . .
Basically all I want to do is for the PIC to read the buffer constantly, perform one of 3 routines depending on the character "a" "b" "c", then go back to the loop and start reading for another character.
Any advice appreciated. Thanks

loop:
vbin = 0 ' reset VBIn
pause 1000
mybuzzer = 0
if pir1.5 = 1 then ' suggestion from forum, character in buffer
hserin [vbin]
sound mybuzzer,[60,10]
hserout [vbin]
Endif
goto loop