HSERIN with/without interrupt


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2008
    Location
    devon
    Posts
    15

    Default HSERIN with/without interrupt

    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

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947

    Default

    Quote Originally Posted by macinug View Post
    Hi,
    I have my PIC reading from the PC (VB6 program). ...... ....
    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?


    Code:
    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.

  3. #3
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107

    Default

    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.
    Charles Linquist

  4. #4
    Join Date
    Jul 2008
    Location
    devon
    Posts
    15

    Default

    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

Similar Threads

  1. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 03:35
  2. TMR0 interrupt and HSERIN
    By boban in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd September 2008, 12:48
  3. Help with Analog Interrupt
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th February 2008, 19:14
  4. Can a TMR interrupt stops HSERIN?
    By SergioRM in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 24th January 2006, 02:07
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 02:07

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts