USB to RS232 adapter


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Location
    Toronto
    Posts
    109

    Default

    Ok, I got it to go from USB to the RS232 but I still cannot get the RS232 to USB to work. Here is the code I have so far.

    Code:
    DEFINE  OSC 48
    
    INCLUDE "modedefs.bas"
    
    Buffer   VAR BYTE[16]
    Cnt      VAR BYTE
    B0       VAR BYTE
    
    PIC_TX   VAR PORTC.6     ' Transmit Pin
    PIC_RX   VAR PORTC.7     ' Receive Pin
    
    ADCON1 = 15              ' Set all I/Os to Digital
    INTCON2.7 = 1            ' Disable PortB Pullups         
    CMCON = 7                ' Disable Comparators
    
    USBInit
    
    ' Main Program Loop
    MainLoop:
        USBService           ' Must service USB regularly
        cnt = 16
        USBIn 3, Buffer, Cnt, MainLoop
    
        SEROUT2 PIC_TX,84,[STR Buffer\Cnt]
        
    Get_RS232_Data:
        SERIN PIC_RX,T9600,100,Get_RS232_Data_Timeout,B0
        Buffer[0] = B0
        Cnt = 1
        GOTO OutLoop
        	
    Get_RS232_Data_Timeout:
        Buffer[0] = 0
        Cnt = 0
        
    OutLoop:
        USBService          ' Must service USB regularly
    	
        IF Cnt > 0 Then
            USBOut 3, Buffer, Cnt, OutLoop
        ENDIF
    
        GOTO Mainloop

  2. #2
    skimask's Avatar
    skimask Guest

    Default

    Using a MAX232 type chip in the circuit?
    Depending, you might have to use N9600 instead of T9600

  3. #3
    Join Date
    Jan 2006
    Location
    Toronto
    Posts
    109

    Default

    Yes, I am using the MAX232 between the PIC18F2550 and the RS232 port.

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

    Default

    At a quick glace, there's a couple problems. Maybe more, but haven't tested it yet.

    With the first USBIN, if there isn't any incoming data from USB, it just loops back to MainLoop. It never makes it to SERIN, unless it receives data from USB at the same time.

    You really should use HSERIN. The RS232 data may start while the program is sending or receiving USB data, in which case you would not be able to catch it with SERIN.

    Added: And the 100ms timeout on the SERIN will cause it to lose the USB connection, since it isn't servicing it anymore.

    DT

  5. #5
    Join Date
    Jan 2006
    Location
    Toronto
    Posts
    109

    Default

    Ok, I got it all working now. I took Darrel's advice and switched to using HSERIN and HSEROUT but added the use of hardware interrupts to capture the RS232 data.

    Here's the new code:

    Code:
    DEFINE    OSC 48
    DEFINE    HSER_RCSTA   90h
    DEFINE    HSER_TXSTA   20h
    DEFINE    HSER_BAUD    9600
    DEFINE    HSER_CLROERR 1
    
    INCLUDE "modedefs.bas"
    
    Buffer    VAR BYTE[16]
    Cnt       VAR BYTE
    B0        VAR BYTE
    PIC_TX    VAR PORTC.6     ' Transmit Pin
    PIC_RX    VAR PORTC.7     ' Receive Pin
    LED1A     VAR PORTB.0     ' 2 Color LED #1
    LED1B     VAR PORTB.1
    LED2A     VAR PORTB.2     ' 2 Color LED #2
    LED2B     VAR PORTB.3
    
    ADCON1 = 15               ' Set all I/Os to Digital
    INTCON2.7 = 1             ' Disable PortB Pullups         
    CMCON = 7                 ' Disable Comparators
    
    '-------------------------------------------------------------------------------
    ' Interrupt Settings
    '-------------------------------------------------------------------------------
    RCON.7 = 0                ' Disable Priority Interrupts, 1 to Enable
    
    ' Enable/Disable Interrupts
    INTCON.4 = 0              ' Disable INT0 (PORTB.0)
    INTCON3.3 = 0             ' Disable INT1 (PORTB.1)
    INTCON3.4 = 0             ' Disable INT2 (PORTB.2)
    INTCON.5 = 0              ' Disable TMR0 (Timmer 0)
    INTCON.6 = 1              ' Enable Peripheral Interrupts
    INTCON.7 = 1              ' Enable Global Interrupt Handler
    PIE1.4 = 0                ' Disable USART Transmit Interrupt
    PIE1.5 = 1                ' Enable USART Receive Interrupt
    PIE2.5 = 0                ' Disable USB Interrupt
    
    LOW LED1A                 ' Turn LED #1 Green
    HIGH LED1B
    LOW LED2A                 ' Turn LED #2 Off
    LOW LED2B
    
    PAUSE 500
    
    USBInit
    
    ON INTERRUPT GOTO Main_Interrupt_Handler
    ENABLE INTERRUPT
    
    ' Main Program Loop
    MainLoop:
        USBService            ' Must service USB regularly
        Cnt = 16
        USBIn 3, Buffer, Cnt, MainLoop
    
        HIGH LED2A
        PAUSE 25
    
        HSEROUT [STR Buffer\Cnt]
    
        LOW LED2A
        
        GOTO Mainloop
    	
    	
    '-------------------------------------------------------------------------------
    ' Interrupt Handler
    '-------------------------------------------------------------------------------
        DISABLE INTERRUPT     ' No Interrupts past this point
    Main_Interrupt_Handler:
    
        '---------------------------------------------------------------------------
        ' USART Transmit Interrupt
        '---------------------------------------------------------------------------
        IF PIR1.4 = 1 THEN
            PIR1.4 = 0
        ENDIF
        '---------------------------------------------------------------------------
    
        
        '---------------------------------------------------------------------------
        ' USART Receive Interrupt
        '---------------------------------------------------------------------------
        IF PIR1.5 = 1 THEN
            HIGH LED2B
            PAUSE 25
            
            HSERIN [B0]
            Buffer[0] = B0
            Cnt = 1
        
    OutLoop:
            USBService        ' Must service USB regularly
            USBOut 3, Buffer, Cnt, OutLoop
    
            LOW LED2B
    
            PIR1.5 = 0
        ENDIF
        '---------------------------------------------------------------------------
    
    IntDone:
        RESUME                ' Return to main program
        ENABLE INTERRUPT
    '-------------------------------------------------------------------------------
    
        END

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

    Default

    That was pretty quick!

    I've been playing with the program here and can say that the two PAUSE 25 statements, have to GO.

    Of course that messes up the LED's, but with the pauses in there, it only picks up every 7 or 8th character when sending continuous data.

    Without them, it works pretty good, but it's still a bit slow.
    Test data of about 1500 bytes takes nearly 3 times as long as it should. Around 1.2 sec.

    I think circular buffers would help speed it up, since it has to wait till it's finished sending the HSEROUT before in can receive more data from the USB. But I guess that depends on how fast you need it to be.

    DT

  7. #7
    Join Date
    Jan 2006
    Location
    Toronto
    Posts
    109

    Default

    Thanks Darrel. I have eliminated the LEDs all together because there is really no need for them. I was testing it by just using a terminal program on 2 computers so I never noticed the speed problem till now. How do I impliment the circular buffers you mentioned?

Similar Threads

  1. USB CDC Communications for Dummies!
    By Squibcakes in forum USB
    Replies: 104
    Last Post: - 15th January 2014, 13:43
  2. USB to rs232 emulator
    By Pic2008 in forum USB
    Replies: 0
    Last Post: - 5th March 2010, 15:54
  3. Simple USB Comms Problem
    By awmt102 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th January 2010, 20:17
  4. Bafo USB to serial adapter
    By ardhuru in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 29th July 2006, 18:48
  5. USB to Serial Adapter with 9.6v
    By serandre in forum Serial
    Replies: 3
    Last Post: - 10th May 2006, 20:16

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