Problem with Serin and On Interrupt


Closed Thread
Results 1 to 9 of 9
  1. #1
    EvilGhozt's Avatar
    EvilGhozt Guest

    Unhappy Problem with Serin and On Interrupt

    Why doesnt this code work correct?
    Pic16f628 @ 4mhz

    DEFINE OSC 4
    on interrupt goto myint
    intcon = %10010000

    loop:
    pause 1
    goto loop

    disable
    myint:
    serin2 portb.1, 396, [wait("IB")]
    INTCON.1 = 0
    resume
    enable

    The problem is that when i send "IB" from the computer to the pic it hangs up in the Serin2 myint part and doesnt move along before it receive another "IB". It seems like it miss the first "IB" I sent, and then Serin waits for the computer to send "IB" again... Does the interrput command take to long time so it wont catch the first bit or something?

    I'm quite noob and my english sux so please explain it easy



    Edit: Sorry i duplicated the post, how do i delete it :P ?
    Last edited by EvilGhozt; - 11th October 2005 at 13:02.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Couple of things going on here. First, by using ON INTERRUPT the program can only be interrupted "In-between" PBP statements. With a PAUSE 1, and serial speed of 2400 baud (.4ms per bit), you can easily lose the first 3-bits before the interrupt even occurs.

    Second... Even IF you could get to "myint" faster, the start bit is already gone, because that's what would have triggered the interrupt to begin with.

    Since you are using a 16F628, it would be better to use the USART. But, if you don't want to do that ...

    Send 1 or 2 extra characters before the string you are waiting for to give it time to get to the serin2 statement. i.e. "xxIB"

    Also, serin2 mode 396 is 2400 baud, TRUE. Which means that the RS232 signal idles HIGH, so the first bit coming in will be a transition from HIGH to LOW. It may be better to change the INTEDG bit in the OPTION register to 0 so that it interrupts on the "Falling Edge". It defaults to 1. Saves one more "BIT" period.

    <br>
    Last edited by Darrel Taylor; - 11th October 2005 at 21:15.
    DT

  3. #3
    EvilGhozt's Avatar
    EvilGhozt Guest


    Did you find this post helpful? Yes | No

    Default

    Thx for the reply.

    But I think I'm going to use another method.
    By connecting RTS to B0/INT I can easy toggle RTS HIGH and then LOW, after that sending the rs232 data. This will alert the pic to listen for any incomming data on RX port...



    Now to next problem.

    Code:


    High led1
    Pause 100
    Low led1
    SerOut2 PORTB.2,396,["Start"]

    loop:
    i = i + 1
    IF i = 1000 Then High led2
    IF i = 2000 Then
    Low led2
    i = 0
    EndIF
    goto loop

    * There are some other code but I dont think its relevant in this matter..

    I resently detected that the led2 in main loop does not always start flashing in the beginning (happens about ~ once of ten) . It can take up to 5-6 sec before it starts flashing. Is there some problem with the SerOut2 command? or pic damage?
    Some other thing I was wondering, how often those a pic fail? I mean, can I run a loop like that (room temperature ~20C) in a month/year without a fail that cause a brake?

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    By connecting RTS to B0/INT I can easy toggle RTS HIGH and then LOW, after that sending the rs232 data. This will alert the pic to listen for any incomming data on RX port...
    WHY? Why not just use the USART and forget about the extra wiring. The USART will generate an interrupt to notify when data comes in. And, it won't miss a BIT.
    <br>
    DT

  5. #5
    EvilGhozt's Avatar
    EvilGhozt Guest


    Did you find this post helpful? Yes | No

    Default

    How should I write the code then?
    I would be grateful if you could paste some code or just help me with some hints how to do...

    Thx mate.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Using your original example, it might look like this ...
    Code:
    DEFINE OSC 4
    
    DEFINE HSER_RCSTA 90h 'Hser receive status init 
    DEFINE HSER_TXSTA 20h 'Hser transmit status init 
    DEFINE HSER_BAUD 2400 'Hser baud rate 
    
    INTCON.6 = 1          ' Enable Peripheral interrupts
    PIE1.5 = 1            ' Enable USART receive ints (RCIE)
    
    on interrupt goto myint
    
    loop:
        pause 1
    goto loop
    
    disable
    myint:
        Hserin [wait("IB")]
    resume
    enable
    HTH,
    &nbsp; Darrel
    Last edited by Darrel Taylor; - 11th October 2005 at 23:13.

  7. #7
    EvilGhozt's Avatar
    EvilGhozt Guest


    Did you find this post helpful? Yes | No

    Default

    Should i connect B0/INT to B1/RX? or does this detect interrupt on the RX port?

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    For a 16F628, the USART pins (RX and TX) are 7 and 8. Same as RB1 and RB2.
    <br>
    DT

  9. #9
    EvilGhozt's Avatar
    EvilGhozt Guest


    Did you find this post helpful? Yes | No

    Thumbs up

    Oh it works! Thanx _ALOT_ Darrel Taylor.
    This will make the program faster and a bit easier.

    That was all for today, time for bed. Good night...

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