Serin and Serout problem


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    13


    Did you find this post helpful? Yes | No

    Default Re: Serin and Serout problem

    Thanks Archangel, I changed the definitions as you said.

    On the other hand, there's a weird problem again: I send a serial bytes of "50" and "55" from 12F675 to 18F2550. 18F2550 is supposed to take the serial data input and send it to PC via USB.

    The transmitter code is as follows:

    Code:
    Include "modedefs.bas"  ' Include serial modes 
    
    DEFINE OSC 4
    
    CMCON = 7
    ANSEL = 0
    
    output GPIO.1
    SO var GPIO.1                ' Define serial out pin
    B0 Var byte
    
    mainloop:
       pause 1
       B0=50
       Serout SO,N9600,[B0] ' Send character
       pause 1
       B0=55
       Serout SO,N9600,[B0] ' Send character
       Goto mainloop        ' Forever
     
    End
    And on the 18F2550, just trying to read serial input and then send it via USBOUT command:

    Code:
    DEFINE    OSC 48Include "modedefs.bas"
    Buffer    VAR BYTE[1]
    Cnt       VAR BYTE
    counter   VAR WORD
    B0        VAR BYTE
    
    
    ADCON1 = 15               ' Set all I/Os to Digital      
    CMCON = 7                 ' Disable Comparators
    Cnt = 1
    
    
    input PORTA.0 
    SI var PORTA.0
    
    
    USBInit                   ' Initialize USART
    
    Loop2:
        Serin SI,N9600,B0
        Buffer(0)=B0  
    
    Loop1: 
        USBService        
        USBOut 3, Buffer, Cnt, loop1
    goto Loop2 ' Go and read serial input data again
    
    
    end
    The problem is: when I load the above code into 18F2550, PC doesn't see the virtual COM port. But when I change the code as follows, it works fine:

    Code:
    DEFINE    OSC 48Include "modedefs.bas"
    Buffer    VAR BYTE[1]
    Cnt       VAR BYTE
    counter   VAR WORD
    B0        VAR BYTE
    
    ADCON1 = 15               ' Set all I/Os to Digital      
    CMCON = 7                 ' Disable Comparators
    Cnt = 1
    
    input PORTA.0 
    SI var PORTA.0
    
    USBInit                   ' Initialize USART
    
    Loop2:
        Serin SI,N9600,B0
        Buffer(0)=B0  
    
    Loop1: 
        USBService        
        USBOut 3, Buffer, Cnt, loop1
    goto Loop1 ' Send the same received serial data again and again to the PC
    
    end
    The problem is: I need to read each serial input data and send it to PC via USB on 18F2550. Tried loads of combinations but nothing worked except the latter code above, which sends the data received only once at the start of the program.

    Any opinion to overcome this problem?

  2. #2
    Join Date
    Sep 2009
    Posts
    19


    Did you find this post helpful? Yes | No

    Default Re: Serin and Serout problem

    Hello.
    You have to service USB regularly (about 5ms max. between servicing) which the first 18F2550 program fails to do due to "goto Loop2" directing program to Serin. With Serin PIC just sits there waiting for serial data and can't do anything else (*1), like service USB, until the data arrives. And there are numeruos reasons for PICs not to get the data therefore just hanging on Serin.

    *1 - o.k., not entirely true, PICs can run comparators, timers, send/receive serial data, etc. in background, depending on device's features.


    You can try with Timeout parameter for Serin but I don't think it'll work as desired with such a short period as 5ms.
    I would rather suggest you to employ 18F2550's UART for serial data reception. That way you can service USB while receiving data in background.
    But keep in mind that UART size is 2 bytes so either make larger pause between data transmission so you have time to collect data from UART, send it to PC, and service USB; or use interrupts (mr. Darrel Taylor's Instant Interrupts are really great) in combination with (circular) buffer.
    Another thing of importance when dealing with UART is that you have to switch to true mode (T9600 instead of N9600; pull-ups instead of pull-downs; data-line idles high)


    Oh, one more thing - I see you're not using DEFINE OSCCAL in your 12F675 programs. I don't know if PBP3 (presuming that's your version) handles that differently than previous versions (PBP2.5 here) but please make sure and see about it in maual.

Members who have read this thread : 0

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