Or, if you wanted to stick with PicBasic, you could try the usbcdc.bas example in the USB18 folder.
Or, if you wanted to stick with PicBasic, you could try the usbcdc.bas example in the USB18 folder.
DT
I have looked at booth but they are incomplete.
I have tried to capture data from the RS232 port on the pic and output it to the USB and also from the USB to the RS232 with no sucess.
All examples are just on how to emulate the CDC.
I basically need a USB to RS232 adapter like those you can buy in any store.
I have tons of these 18F2550 chips and would rather use them to convert my exsisting projects instead of going and buying some.
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
Using a MAX232 type chip in the circuit?
Depending, you might have to use N9600 instead of T9600
Yes, I am using the MAX232 between the PIC18F2550 and the RS232 port.
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
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
Bookmarks